Skip to content
Advertisement

Remove part of a string from pd.to_datetime() unconverted values

I tried to convert a column of dates to datetime using pd.to_datetime(df, format='%Y-%m-%d_%H-%M-%S') but I received the error ValueError: unconverted data remains: .1

I ran:

JavaScript

to identify the problem. 119/1037808 dates in the date column have an extra “.1” at the end of them. Other than the “.1”, the dates are fine. How can I remove the “.1” from the end of those dates only and then convert the column values to datetime?

Here is an example dataframe that recreates the issue:

JavaScript

I have tried:

JavaScript

and

JavaScript

but these did not remove the “.1”. The final result should look like this:

JavaScript

Advertisement

Answer

You can use pandas.Series.replace to get rid of the extra dot/number :

JavaScript

# Output :

JavaScript

If you don’t want a datetime format, use just data["date"].replace(r".d+", "", regex=True)

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement