[英]Convert datetime column with multiple timezones to epoch timestamp
I have a datetime column with multiple timezones that I want to convert to epoch timestamp:我有一个包含多个时区的日期时间列,我想将其转换为纪元时间戳:
pd.DataFrame(['2020-09-29 09:30:00-04:00', '2020-09-29 10:30:00-04:00', '2020-09-29 11:00:00-05:00'], columns = ['datetime'])
The desired results look like:所需的结果如下所示:
datetime timestamp
0 2020-09-29 09:30:00-04:00 1601386200
1 2020-09-29 10:30:00-04:00 1601389800
2 2020-09-29 11:00:00-05:00 1601395200
(note 2020-09-29 11:00:00
has a -05:00
tz) (注2020-09-29 11:00:00
有-05:00
tz)
I tried:我试过:
df['timestamp'] = pd.to_datetime(df['datetime']).astype(int) // 10 ** 9
It works well when datetime
has only one timezone info but throws a int() argument must be a string, a bytes-like object or a number, not 'datetime.datetime'
error when the timezone info is different and I'm not sure why Pandas cannot recognize this.当datetime
只有一个时区信息但抛出一个int() argument must be a string, a bytes-like object or a number, not 'datetime.datetime'
错误时,当时区信息不同并且我不确定时,它运行良好为什么熊猫无法识别这一点。
I assume people may ask similar questions but couldn't find the exact solution.我想人们可能会问类似的问题,但找不到确切的解决方案。 Any ideas?有任何想法吗? Thanks!谢谢!
You have mixed time zone, so you want to pass utc=True
to to_datetime
:您有混合时区,因此您想将utc=True
传递给to_datetime
:
pd.to_datetime(df['datetime'], utc=True).astype(int) // 10 ** 9
Output:输出:
0 1601386200
1 1601389800
2 1601395200
Name: datetime, dtype: int64
Using pd.Timestamp
:使用pd.Timestamp
:
df['timestamp'] = df['datetime'].apply(lambda x: int(pd.Timestamp(x).timestamp()))
df
datetime timestamp
0 2020-09-29 09:30:00-04:00 1601386200
1 2020-09-29 10:30:00-04:00 1601389800
2 2020-09-29 11:00:00-05:00 1601395200
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.