简体   繁体   English

将数据框中的列转换为日期时间

[英]Converting column in Dataframe to datetime

I've been trying to use the to_datetime function to convert values in my column to datetime:我一直在尝试使用 to_datetime 函数将列中的值转换为日期时间:

df['date'] = pd.to_datetime(df['date'],errors='coerce',format='%Y-%m-%d %H:%M:%S %z %Z') 

After that, I received only NaT values.之后,我只收到了 NaT 值。

Example: Value Format in Column: '1979-01-01 00:00:00 +0000 UTC'示例:列中的值格式:'1979-01-01 00:00:00 +0000 UTC'

I think you can't parse utc offset (+0000) and timeszone information at the sime time.我认为您无法同时解析 utc 偏移量(+0000)和时区信息。

You might want to remove the UTC at the end and only parse the offset.您可能希望在末尾删除 UTC 并仅解析偏移量。

df['date'] = df.date.str[:-4]
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d %H:%M:%S %z') 

Pandas can't manage both %z and %Z as you can see here . Pandas 不能同时管理%z%Z正如你在这里看到的。 Note that Python's strptime can handle this, but doesn't deal with %Z .请注意,Python 的strptime可以处理这个问题,但不处理%Z

In your case you might want to just peel off the last bit with ser.str and consider opening a feature request.在您的情况下,您可能只想使用ser.str剥离最后一点并考虑打开功能请求。

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

相关问题 将pandas dataframe中的对象列转换为datetime - Converting object column in pandas dataframe to datetime Python:将秒数转换为数据帧列中的日期时间格式 - Python: Converting a seconds to a datetime format in a dataframe column 将dataframe列从对象转换为date而不是datetime - converting dataframe column from object to date not datetime 将 dataframe 列从 Pandas 时间戳转换为日期时间(或 datetime.date) - Converting dataframe column from Pandas Timestamp to datetime (or datetime.date) 根据数据帧的列将具有 UTC 时区的日期时间列转换为另一个时区 - Converting a datetime column with UTC timezone to another TimeZone based on a column of the Dataframe 如何解决使用to_datetime转换数据帧中的时间列的错误? - How to fix an error converting a time column in a dataframe with the to_datetime? 将数据框转换为熊猫中的日期时间 - Converting dataframe to datetime in pandas 将日期时间数据的数据框列转换为 DD/MM/YYYY 字符串数据 - Converting dataframe column of datetime data to DD/MM/YYYY string data 将原始日期时间列转换为新时区 Pandas Dataframe - Converting a naive datetime column to a new timezone Pandas Dataframe 将 MM:SS 格式的 Pandas Dataframe 列 object 转换为日期时间类型? - Converting Pandas Dataframe column object in MM:SS format to Datetime type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM