我有一个具有以下结构的pandas DataFrame:
其中time
属性/列表示某个时间点,从该时间点开始对特定动物进行测量,以animal_id
属性表示。度量是分别由属性x
和y
表示的x和y坐标。
我想将time
从int转换为datetime格式。但是当我执行以下操作时:
data['time'] = pd.to_datetime(data['time'])
输出:
data['time'][:10]
是:
0 1970-01-01 00:00:00.000000001
1 1970-01-01 00:00:00.000000001
2 1970-01-01 00:00:00.000000001
3 1970-01-01 00:00:00.000000001
4 1970-01-01 00:00:00.000000001
5 1970-01-01 00:00:00.000000002
6 1970-01-01 00:00:00.000000002
7 1970-01-01 00:00:00.000000002
8 1970-01-01 00:00:00.000000002
9 1970-01-01 00:00:00.000000002
Name: time, dtype: datetime64[ns]
我该如何指定两件事:
谢谢!
答案 0 :(得分:2)
使用to_timedelta
设置分钟时间增量,并添加Timestamp
,因为使用参数origin
和unit
的分钟to_datetime
不会实现:
data = pd.DataFrame({'time':[1,1,2,2,3,4]})
data['time0'] = pd.to_timedelta(data['time'], unit='Min') + pd.Timestamp('2019-05-10')
data['time1'] = pd.to_datetime(data['time'], origin='2019-05-10', unit='s')
data['time2'] = pd.to_datetime(data['time'], origin='2019-05-10', unit='d')
print (data)
time time0 time1 time2
0 1 2019-05-10 00:01:00 2019-05-10 00:00:01 2019-05-11
1 1 2019-05-10 00:01:00 2019-05-10 00:00:01 2019-05-11
2 2 2019-05-10 00:02:00 2019-05-10 00:00:02 2019-05-12
3 2 2019-05-10 00:02:00 2019-05-10 00:00:02 2019-05-12
4 3 2019-05-10 00:03:00 2019-05-10 00:00:03 2019-05-13
5 4 2019-05-10 00:04:00 2019-05-10 00:00:04 2019-05-14
因为:
data['time0'] = pd.to_datetime(data['time'], origin='2019-05-10', unit='Min')
ValueError:无法转换单位Min