如何在Pandas DataFrame中将int转换为日期时间

时间:2019-05-26 06:29:04

标签: python pandas datetime

我有一个具有以下结构的pandas DataFrame:

enter image description here

其中time属性/列表示某个时间点,从该时间点开始对特定动物进行测量,以animal_id属性表示。度量是分别由属性xy表示的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]

我该如何指定两件事:

  1. 开始日期,而不是1970-01-01说2019-05-10
  2. 在上面的输出中指定连续两次之间的差异,以表示分钟之间的差异

谢谢!

1 个答案:

答案 0 :(得分:2)

使用to_timedelta设置分钟时间增量,并添加Timestamp,因为使用参数originunit的分钟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