我想从下面的时间戳中删除+00:00
。我使用下面的代码从时间戳中删除了T和Z,但数据类型仍然是datetime64[ns, UTC]
,理想情况下我想将其转换为datetime64[ns]
df['Timestamp_column'].dt.tz_localize(None)
转换前的时间戳_列:
2020-07-10T14:12:39.000Z
输出:
2020-07-10 14:12:39+00:00
您可以从tz_convert
到None
:
import pandas as pd
df = pd.DataFrame({'Timestamp_column': ['2020-07-10T14:12:39.000Z']})
df['Timestamp_column'] = pd.to_datetime(df['Timestamp_column']).dt.tz_convert(None)
# df['Timestamp_column']
# 0 2020-07-10 14:12:39
# Name: Timestamp_column, dtype: datetime64[ns]
In [50]: df
Out[50]:
date
0 2020-07-10T14:12:39.000Z
In [51]: df.dtypes
Out[51]:
date object
dtype: object
In [52]: df["new_date"] = pd.to_datetime(df["date"], format="%Y-%m-%dT%H:%M:%S.%fZ")
In [53]: df
Out[53]:
date new_date
0 2020-07-10T14:12:39.000Z 2020-07-10 14:12:39
In [54]: df.dtypes
Out[54]:
date object
new_date datetime64[ns]
dtype: object