Python pandas 通过 dt 访问器有效地将日期时间转换为时间戳

新手上路,请多包涵

我有一个包含几(数亿)行的 DataFrame。我想有效地将日期时间转换为时间戳。我该怎么做?

我的样本 df

 df = pd.DataFrame(index=pd.DatetimeIndex(start=dt.datetime(2016,1,1,0,0,1),
    end=dt.datetime(2016,1,2,0,0,1), freq='H'))\
    .reset_index().rename(columns={'index':'datetime'})

看起来像:

              datetime
0 2016-01-01 00:00:01
1 2016-01-01 01:00:01
2 2016-01-01 02:00:01
3 2016-01-01 03:00:01
4 2016-01-01 04:00:01

现在,我使用 .apply() 将日期时间按值转换为时间戳,但如果我有几(数百)百万行,则需要很长时间(几个小时):

 df['ts'] = df[['datetime']].apply(lambda x: x[0].timestamp(), axis=1).astype(int)

输出:

              datetime          ts
0 2016-01-01 00:00:01  1451602801
1 2016-01-01 01:00:01  1451606401
2 2016-01-01 02:00:01  1451610001
3 2016-01-01 03:00:01  1451613601
4 2016-01-01 04:00:01  1451617201

上面的结果就是我想要的。

如果我尝试使用 .dt 访问器 pandas.Series 然后我收到错误消息:

 df['ts'] = df['datetime'].dt.timestamp

AttributeError: ‘DatetimeProperties’ 对象没有属性 ‘timestamp’

如果我尝试创建例如。使用 .dt 访问器的日期时间的日期部分然后它比使用 .apply()

 df['date'] = df['datetime'].dt.date

输出:

              datetime          ts        date
0 2016-01-01 00:00:01  1451602801  2016-01-01
1 2016-01-01 01:00:01  1451606401  2016-01-01
2 2016-01-01 02:00:01  1451610001  2016-01-01
3 2016-01-01 03:00:01  1451613601  2016-01-01
4 2016-01-01 04:00:01  1451617201  2016-01-01

我想要与时间戳类似的东西……

但我不太了解官方文档:它谈到“ 转换为时间戳”,但我在那里看不到任何时间戳;它只是谈论转换为日期时间 pd.to_datetime() 但不是时间戳……

pandas.Timestamp 构造函数也不起作用(返回以下错误):

 df['ts2'] = pd.Timestamp(df['datetime'])

类型错误:无法将输入转换为时间戳

pandas.Series.to_timestamp 也与我想要的完全不同:

 df['ts3'] = df['datetime'].to_timestamp

输出:

              datetime          ts                                                ts3
0 2016-01-01 00:00:01  1451602801  <bound method Series.to_timestamp of 0    2016...
1 2016-01-01 01:00:01  1451606401  <bound method Series.to_timestamp of 0    2016...
2 2016-01-01 02:00:01  1451610001  <bound method Series.to_timestamp of 0    2016...
3 2016-01-01 03:00:01  1451613601  <bound method Series.to_timestamp of 0    2016...
4 2016-01-01 04:00:01  1451617201  <bound method Series.to_timestamp of 0    2016...

原文由 ragesz 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 526
2 个回答

I think you need convert first to numpy array by values and cast to int64 - output is in ns , so need divide by 10 ** 9

 df['ts'] = df.datetime.values.astype(np.int64) // 10 ** 9
print (df)
              datetime          ts
0  2016-01-01 00:00:01  1451606401
1  2016-01-01 01:00:01  1451610001
2  2016-01-01 02:00:01  1451613601
3  2016-01-01 03:00:01  1451617201
4  2016-01-01 04:00:01  1451620801
5  2016-01-01 05:00:01  1451624401
6  2016-01-01 06:00:01  1451628001
7  2016-01-01 07:00:01  1451631601
8  2016-01-01 08:00:01  1451635201
9  2016-01-01 09:00:01  1451638801
10 2016-01-01 10:00:01  1451642401
11 2016-01-01 11:00:01  1451646001
12 2016-01-01 12:00:01  1451649601
13 2016-01-01 13:00:01  1451653201
14 2016-01-01 14:00:01  1451656801
15 2016-01-01 15:00:01  1451660401
16 2016-01-01 16:00:01  1451664001
17 2016-01-01 17:00:01  1451667601
18 2016-01-01 18:00:01  1451671201
19 2016-01-01 19:00:01  1451674801
20 2016-01-01 20:00:01  1451678401
21 2016-01-01 21:00:01  1451682001
22 2016-01-01 22:00:01  1451685601
23 2016-01-01 23:00:01  1451689201
24 2016-01-02 00:00:01  1451692801

to_timestamp 用于从句点转换 为日期时间索引

原文由 jezrael 发布,翻译遵循 CC BY-SA 3.0 许可协议

我认为您不应该使用 apply,只需 astype 就可以了:

 df['ts'] = df.datetime.astype('int64') // 10**9

原文由 Mithril 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题