简体   繁体   English

如何将UTC时间戳字符串转换为pandas datetime?

[英]How to convert UTC timestamp string to pandas datetime?

I have a panda dataframe with a column with date format as below: 我有一个熊猫数据框,其中的日期格式如下:

PublishDateTime= 2018-08-31 12:00:00-UTC

I used panda to_gbq() function to dump data into a bigquery table. 我使用panda to_gbq()函数将数据转储到bigquery表中。 Before dumping data, I make sure that the format of columns match with table scheme. 在转储数据之前,我确保列的格式与表格方案匹配。 publishedate is timestamp in bigquery table. publishedate是bigquery表中的时间戳。 How can achieve something similar to: 如何实现类似的东西:

df['PublishDateTime'] = df['PublishDateTime'].astype('?????')

I tried datetime[ns] but that didn't work! 我试过datetime[ns]但是没有用!

The main challenge in the conversion here is the format of your datetime. 转换的主要挑战是日期时间的格式。 Pass format="%Y-%m-%d %H:%M:%S-%Z" as an argument to pd.to_datetime and convert your column to datetime . 传递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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM