你可以使用。
df['timestamp'] = pd.to_datetime(df['timestamp'].str.replace('\+.*', ''))
df.info()
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Resource 5 non-null object
1 timestamp 5 non-null datetime64[ns]
df['timestamp'] = pd.to_datetime(df['timestamp'])
df.info()
如果没有替换,你会返回一个datetime64[ns, pytz.FixedOffset(120)]
的日期类型。
df['timestamp'] = pd.to_datetime(df['timestamp'])
df.info()
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Resource 5 non-null object
1 timestamp 5 non-null datetime64[ns, pytz.FixedOffset(120)]
你也可以使用。
df['timestamp'] = pd.to_datetime(df['timestamp']).dt.tz_convert(None)
从这个答案来看。https://stackoverflow.com/a/59919896/6366770
Per the docs,在2019年1月25日发布的0.24.0
中,pandas
处理datetime
偏移的方式有了变化。
*Previous behavior:
In [2]: pd.to_datetime("2015-11-18 15:30:00+05:30")
Out[2]: Timestamp('2015-11-18 10:00:00')
In [3]: pd.Timestamp("2015-11-18 15:30:00+05:30")
Out[3]: Timestamp('2015-11-18 15:30:00+0530', tz='pytz.FixedOffset(330)')
# Different UTC offsets would automatically convert the datetimes to UTC (without a UTC timezone)
In [4]: pd.to_datetime(["2015-11-18 15:30:00+05:30", "2015-11-18 16:30:00+06:30"])
Out[4]: DatetimeIndex(['2015-11-18 10:00:00', '2015-11-18 10:00:00'], dtype='datetime64[ns]', freq=None)
New behavior:
In [56]: pd.to_datetime("2015-11-18 15:30:00+05:30")
Out[56]: Timestamp('2015-11-18 15:30:00+0530', tz='pytz.FixedOffset(330)')
In [57]: pd.Timestamp("2015-11-18 15:30:00+05:30")
Out[57]: Timestamp('2015-11-18 15:30:00+0530', tz='pytz.FixedOffset(330)')*