我已读取json文件并将其存储在数据框中。
执行时
df1.dtypes
我获得存储日期的字段是unplug_hourTime对象
如果我使用以下方法显示最短记录:
df1.unplug_hourTime.head(1)
我获得:
0 {'$date': '2017-04-01T01:00:00.000+0200'}
Name: unplug_hourTime, dtype: object
我不确定如何从此结构中提取日期
我想有一个存储01/04/2017的变量
我也尝试过:
df1['Dia'] = pd.to_datetime(df1.unplug_hourTime)
但检索到错误:
TypeError: <class 'dict'> is not convertible to datetime
答案 0 :(得分:1)
您似乎需要:
df1['Dia'] = pd.to_datetime(df1.unplug_hourTime.apply(lambda x: x['$date'])).dt.date
答案 1 :(得分:0)
您需要将dict
扩展为如下所示的列:
df = df.join(pd.DataFrame(df.pop("unplug_hourTime").tolist()))
这会将其添加到您的DataFrame中:
$date
0 2017-04-01T01:00:00.000+0200
然后将所得系列作为日期时间:
df['Dia'] = pd.to_datetime(df['$date'])