Home > OS >  Converting to datetime - ParserError: Unknown string format: 2022-02-17 7
Converting to datetime - ParserError: Unknown string format: 2022-02-17 7

Time:02-20

I have a pandas dataframe with some string values that have the hour of a date in one-digit format if the hour is smaller than 10, like this:

2022-02-17 7

I now want to get this strings to datetime format but when applying

df['datetime'] = pd.to_datetime(df['datetime'], infer_datetime_format=True)

I get a ParserError:

ParserError: Unknown string format: 2022-02-17 7

How can I solve this?

CodePudding user response:

Use:

df = pd.DataFrame({'datetime':['2022-02-17 7']})
df['datetime'] = df['datetime'].str.replace(' (\d{1})', ' 0\\1')
df['datetime'] = pd.to_datetime(df['datetime'], format='%Y-%m-%d %H')

The result:

enter image description here

A more general case which can handle two-digits too:

df = pd.DataFrame({'datetime':['2022-02-17 17', '2022-02-17 7']})
df['datetime'] = df['datetime'].str.replace('\s(\d)$', ' 0\\1')

The result:

enter image description here

  • Related