这是我的数据框:
dt_object Lng
1 2020-01-01 00:00:00 1.57423
2 2020-01-01 01:00:00 1.57444
3 2020-01-01 02:00:00 1.57465
4 2020-01-01 03:00:00 1.57486
5 2020-01-01 04:00:00 1.57506
dt_object用python经典的datetime函数编写。 但是
print (df.columns)
给我:
Index([['dt_object','Lng'],dtype ='object'),
所以它们都是对象类型。
我尝试:
df_candles['dt_object'] = df_candles['dt_object'].astype('datetime64')
但这没有帮助。
它仍然是对象。 如何将该列转换为熊猫“ datetime64”?
答案 0 :(得分:1)
为此,您可以使用to_datetime函数对其进行转换
df_candles['dt_object'] = pd.to_datetime(df_candles['dt_object'])
请注意,如果需要,还可以将字符串对象的格式指定为
df_candles['dt_object'] = pd.to_datetime(df_candles['dt_object'], format='%Y-%m-%d %H:%M:%S')
还要检查 https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
答案 1 :(得分:1)
尝试:
df_candles['dt_object'] = pd.to_datetime(df_candles['dt_object'])
当您尝试检查列类型时,请使用:
df.dtypes
代替:
df.columns