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

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

问题描述:

我在熊猫数据框中有一个对象列,其格式为dd/mm/yyyy,我想使用to_datetime进行转换.

I have an object column in a pandas dataframe in the format dd/mm/yyyy, that I want to convert with to_datetime.

我尝试使用以下方法将其转换为日期时间:

I tried to convert it to datetime using the below:

df['Time stamp'] = pd.to_datetime(df['Time stamp'], format= '%d/%m/%Y')

我收到以下错误:

TypeError: Unrecognized value type: <class 'str'>
ValueError: unconverted data remains:  

这是否意味着某处有空白行,我已经检查了原始的csv,但看不到它.

Does this mean that there is a blank row somewhere, I have checked the original csv and I cannot see one.

这意味着您有多余的空间.尽管pd.to_datetime通常在不指定任何格式的情况下很好地解析日期,但是当您实际指定格式时,它必须完全匹配.

It means you have an extra space. Though pd.to_datetime is very good at parsing dates normally without any format specified, when you actually specify a format, it has to match EXACTLY.

您可以通过添加.str.strip()在转换之前删除多余的空格来解决您的问题.

You can likely solve your issue by adding .str.strip() to remove the extra whitespace before converting.

import pandas as pd
df['Time stamp'] = pd.to_datetime(df['Time stamp'].str.strip(), format='%d/%m/%Y')

或者,您可以利用dayfirst=True参数来利用其解析各种格式的日期的功能

Alternatively, you can take advantage of its ability to parse various formats of dates by using the dayfirst=True argument

df['Time stamp'] = pd.to_datetime(df['Time stamp'], dayfirst=True)


示例:

import pandas as pd
df = pd.DataFrame({'Time stamp': ['01/02/1988', '01/02/1988 ']})

pd.to_datetime(df['Time stamp'], format= '%d/%m/%Y')

ValueError:仍保留未转换的数据:

ValueError: unconverted data remains:

pd.to_datetime(df['Time stamp'].str.strip(), format='%d/%m/%Y')
#0   1988-02-01
#1   1988-02-01
#Name: Time stamp, dtype: datetime64[ns]

pd.to_datetime(df['Time stamp'], dayfirst=True)
#0   1988-02-01
#1   1988-02-01
#Name: Time stamp, dtype: datetime64[ns]