如何将UTC时间戳字符串转换为熊猫日期时间?

MT4*_*467 1 python datetime date dataframe pandas

我有一个带有日期格式列的熊猫数据框,如下所示:

PublishDateTime= 2018-08-31 12:00:00-UTC
Run Code Online (Sandbox Code Playgroud)

我使用 panda to_gbq() 函数将数据转储到 bigquery 表中。在转储数据之前,我确保列的格式与表方案匹配。发布日期是 bigquery 表中的时间戳。如何实现类似于:

df['PublishDateTime'] = df['PublishDateTime'].astype('?????')
Run Code Online (Sandbox Code Playgroud)

我试过了,datetime[ns]但没有用!

cs9*_*s95 6

此处转换的主要挑战是日期时间的格式。format="%Y-%m-%d %H:%M:%S-%Z"作为参数传递给pd.to_datetime并将您的列转换为datetime.

df['PublishDateTime'] = pd.to_datetime(df['PublishDateTime'], 
                                       format='%Y-%m-%d %H:%M:%S-%Z', 
                                       errors='coerce')
Run Code Online (Sandbox Code Playgroud)