在 pandas.Series 中将时间戳转换为 datetime.datetime
- 作者: ---0---带泪的鱼---0---
- 来源: 51数据库
- 2023-01-03
问题描述
我有熊猫系列,其中索引是整数列表(时间戳),我如何将它们转换为 datetime.datetime(带时区)比以下原始转换更有效?
I have pandas Series where index is a list of integer (timestamp), how can I convert them to datetime.datetime (with timezone) more efficient than below raw conversion?
pd.Series(data=s.values, index=map(lambda x:datetime.datetime.fromtimestamp(x,tz=utc), s.index))
推荐答案
In [49]: s = Series(range(10))
使用to_datetime,你可以提供一个单位来选择整数的含义.
Using to_datetime, you can supply a unit to select what the meaning of the integers.
In [50]: pd.to_datetime(s,unit='s') Out[50]: 0 1970-01-01 00:00:00 1 1970-01-01 00:00:01 2 1970-01-01 00:00:02 3 1970-01-01 00:00:03 4 1970-01-01 00:00:04 5 1970-01-01 00:00:05 6 1970-01-01 00:00:06 7 1970-01-01 00:00:07 8 1970-01-01 00:00:08 9 1970-01-01 00:00:09 dtype: datetime64[ns] In [51]: pd.to_datetime(s,unit='ms') Out[51]: 0 1970-01-01 00:00:00 1 1970-01-01 00:00:00.001000 2 1970-01-01 00:00:00.002000 3 1970-01-01 00:00:00.003000 4 1970-01-01 00:00:00.004000 5 1970-01-01 00:00:00.005000 6 1970-01-01 00:00:00.006000 7 1970-01-01 00:00:00.007000 8 1970-01-01 00:00:00.008000 9 1970-01-01 00:00:00.009000 dtype: datetime64[ns] In [52]: pd.to_datetime(s,unit='D') Out[52]: 0 1970-01-01 1 1970-01-02 2 1970-01-03 3 1970-01-04 4 1970-01-05 5 1970-01-06 6 1970-01-07 7 1970-01-08 8 1970-01-09 9 1970-01-10 dtype: datetime64[ns]
创建一个系列就很简单了
Creating a Series is then straightforward
In [54]: Series(s.values,index=pd.to_datetime(s,unit='s')) Out[54]: 1970-01-01 00:00:00 0 1970-01-01 00:00:01 1 1970-01-01 00:00:02 2 1970-01-01 00:00:03 3 1970-01-01 00:00:04 4 1970-01-01 00:00:05 5 1970-01-01 00:00:06 6 1970-01-01 00:00:07 7 1970-01-01 00:00:08 8 1970-01-01 00:00:09 9 dtype: int64
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
