日期时间输出的差异
Discrepancy in datetime output
我有一堆日期如下:
df['Execution Date']
Out[123]:
214 20180420
215 20180420
256 20180423
258 20180424
262 20180425
273 20180508
274 20180510
275 20180510
278 20180511
281 20180511
284 20180511
287 20180511
290 20180511
293 20180511
296 20180511
333 20180516
我执行df['Execution Date'] = df['Execution Date'].apply(lambda x: pd.to_datetime(str(x), format='%Y%m%d'))
当我检查我的控制台时,重新格式化似乎是正确的,但在我的 Spyder 变量资源管理器窗格中,我在每个 YYYY-MM-DD
之后都有不必要的 00:00:00
。
这会影响可读性。有什么解决方法吗?
使用df['Execution Date'] = df['Execution Date'].apply(lambda x: datetime.strptime(x,'%Y%m%d'))
如下
d = {'Execution Date' : ['20180420','20180420', '20180423']}
df = pd.DataFrame(d)
df['Execution Date'] = df['Execution Date'].apply(lambda x: datetime.strptime(x,'%Y%m%d'))
print(df)
结果是
Execution Date
0 2018-04-20
1 2018-04-20
2 2018-04-23
[Finished in 1.2s]
如果'Execution Date'的类型是int,则按如下方式转换为string。
d = {'Execution Date' : [20180420,20180420, 20180423]}
df = pd.DataFrame(d)
df['Execution Date'] = df['Execution Date'].apply(lambda x: datetime.strptime(str(x),'%Y%m%d'))
print(df)
我有一堆日期如下:
df['Execution Date']
Out[123]:
214 20180420
215 20180420
256 20180423
258 20180424
262 20180425
273 20180508
274 20180510
275 20180510
278 20180511
281 20180511
284 20180511
287 20180511
290 20180511
293 20180511
296 20180511
333 20180516
我执行df['Execution Date'] = df['Execution Date'].apply(lambda x: pd.to_datetime(str(x), format='%Y%m%d'))
当我检查我的控制台时,重新格式化似乎是正确的,但在我的 Spyder 变量资源管理器窗格中,我在每个 YYYY-MM-DD
之后都有不必要的 00:00:00
。
这会影响可读性。有什么解决方法吗?
使用df['Execution Date'] = df['Execution Date'].apply(lambda x: datetime.strptime(x,'%Y%m%d'))
如下
d = {'Execution Date' : ['20180420','20180420', '20180423']}
df = pd.DataFrame(d)
df['Execution Date'] = df['Execution Date'].apply(lambda x: datetime.strptime(x,'%Y%m%d'))
print(df)
结果是
Execution Date
0 2018-04-20
1 2018-04-20
2 2018-04-23
[Finished in 1.2s]
如果'Execution Date'的类型是int,则按如下方式转换为string。
d = {'Execution Date' : [20180420,20180420, 20180423]}
df = pd.DataFrame(d)
df['Execution Date'] = df['Execution Date'].apply(lambda x: datetime.strptime(str(x),'%Y%m%d'))
print(df)