RobC
RobC

Reputation: 1415

Pandas.to_datetime() fails only on column from Data Frame

I apologize for the title to this, but couldn't figure out a way to phrase it better.

When I try to run

pd.to_datetime(df['TimeStamp'], format='%Y-%m-%d %H:%M:%S.%f')

I receive the following exception:

time data '2019-01-06 15:34:19.000' doesn't match format specified

This time is that contained in the first row of my data frame. It sure seemed to be in the specified format, so I tested it by manually copy-pasting a few rows from the TimeStamp column directly into the Series constructor:

tempSeries = pd.Series(['2019-01-06 15:34:19.023', '2019-01-06 15:34:19.000'])

When I call pd.to_datetime(tempSeries, format='%Y-%m-%d %H:%M:%S.%f') , things works fine.

I should be able to pass the series in as in my first example, correct? Can anyone make a suggestion as to what might be going on?

EDIT:

As requested, here is df['TimeStamp].head().to_dict():

{0: "'2019-01-06 15:36:19.000'",
 1: "'2019-01-06 15:37:19.000'",
 2: "'2019-01-06 15:38:19.000'",
 3: "'2019-01-06 15:39:19.000'",
 4: "'2019-01-06 15:40:19.000'"}

Upvotes: 1

Views: 124

Answers (1)

harpan
harpan

Reputation: 8631

You can see that your strings are single and double quoted.

You need to change the format to

format="'%Y-%m-%d %H:%M:%S.%f'"

Or

Use

pd.to_datetime(df['Timestamp'].str.strip("'")) #cs95

Upvotes: 1

Related Questions