pandas 如何把时间转成index_Pandas 时间类型转换

Pandas 中文教程修订中,欢迎加微信 sinbam 提供建议、纠错、催更。查看更新日志

由于时间格式样式比较多,很多情况下 Padnas 并不能自动识别为时间类型,所以我们在处理前的数据清洗过程中,需要专门对数据进行时间类型转换。

astype 转换

astype 是最简单的时间转换方法,它只能针对相对标准的时间格式,如:

s = pd.Series([‘2016-01-31’, ‘2016-02-29’, ‘2016-03-31’])

s.astype(‘datetime64[ns]’)

”’

0 2016-01-31

1 2016-02-29

2 2016-03-31

dtype: datetime64[ns]

”’

如果需要指定时区:

s = pd.Series([‘2016-01-31’, ‘2016-02-29’, ‘2016-03-31’])

s.astype(‘datetime64[ns, US/Eastern]’)

”’

0 2016-01-31 00:00:00-05:00

1 2016-02-29 00:00:00-05:00

2 2016-03-31 00:00:00-04:00

dtype: datetime64[ns, US/Eastern]

”’

北京时间是 Asia/Shanghai

转为时间

Original: https://blog.csdn.net/weixin_35774446/article/details/112866913
Author: Shepherd Young
Title: pandas 如何把时间转成index_Pandas 时间类型转换

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/753218/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球