简体   繁体   English

将 Python 数据框列转换为日期格式

[英]Converting Python dataframe column to date format

I have a column in a dataframe that I want to convert to a date.我在数据框中有一列要转换为日期。 The values of the column are either DDMONYYY or DD Month YYYY 00:00:00.000 GMT .该列的值为DDMONYYYDD Month YYYY 00:00:00.000 GMT For example, one row in the dataframe could have the value 31DEC2002 and the next row could have 31 December 2015 00:00:00.000 GMT .例如,数据31DEC2002的一行可能具有值31DEC2002 ,下一行可能具有31 December 2015 00:00:00.000 GMT I think this is why I get an error when trying to convert the column to a date using pd.to_datetime or datetime.strptime to convert.我认为这就是为什么我在尝试使用 pd.to_datetime 或 datetime.strptime 将列转换为日期时出现错误的原因。

Anyone got any ideas?有人有任何想法吗? I'd be very grateful for any help/pointers.我将非常感谢任何帮助/指示。

For me working to_datetime with utc=True for converting all values to UTC and errors='coerce' for convert not parseable values to NaT (missing datetime):对我to_datetime使用utc=True使用to_datetime将所有值转换为UTC而使用errors='coerce'将不可解析的值NaTNaT (缺少日期时间):

df = pd.DataFrame({'date':['31DEC2002','31 December 2015 00:00:00.000 GMT','.']})

df['date'] = pd.to_datetime(df['date'], utc=True, errors='coerce')
print (df)
                       date
0 2002-12-31 00:00:00+00:00
1 2015-12-31 00:00:00+00:00
2                       NaT

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

相关问题 Python:将秒数转换为数据帧列中的日期时间格式 - Python: Converting a seconds to a datetime format in a dataframe column 将数据帧日期列转换为 Python 中的时间戳 - Converting dataframe date column to timestamp in Python 将不同的日期格式转换为数据框列中的单一格式 - Converting different date formats to a single format in a dataframe column 将动态日期列转换为 pyspark 数据框中的其他格式 - Converting a dynamic date column to other format in pyspark dataframe 将SQL时间戳列转换为Python数据框的日期格式列 - Convert SQL timestamp column to date format column of Python dataframe 在Python的数据框列中将两种日期格式转换为一种 - Converting two date formats to one in a dataframe column in Python 在 python 的数据表框中将字符串列转换为日期格式 - Converting string column to date format in datatable frame in python 将 python dataframe 转换为 json 格式 - Converting python dataframe to json format 如何将python中的数据框列对象转换为日期时间格式 - How to convert a dataframe column object in python to date time format 如何为python数据框中的整个列创建日期格式? - How do I make a date format for an entire column in python dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM