0

I have a df column with two columns, df.info() yields the following:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 200 entries, 0 to 199
Data columns (total 2 columns):
date          200 non-null object
all_events    200 non-null int64
dtypes: int64(1), object(1)
memory usage: 3.2+ KB

Question:

How can convert the df['date'] into a datetime object?

Relevant Research:

What I have tried:

  • df['date'] = pd.to_datetime(df['date'], infer_datetime_format=True)
  • df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d %H:%M:%S')
  • df['date'] = pd.to_datetime(df['date'].str.strip(), format='%Y-%m-%d %H:%M:%S')

Problem:

whatever I have I tried, I always get the following message:

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

3kstc
  • 1,871
  • 3
  • 29
  • 53

1 Answers1

0

If all entries in your date column had form like yyyy-mm-dd hh:mm:ss then pd.to_datetime(df['date']) would be enough (this function is "clever" enough to discover the proper format).

Apparently you have some "exceptional" values, with different formatting, which can not be converted to datetime.

Note that one of parameters of to_datetime is errors with default value of raise, meaning "raise an exception in case of any error".

So try e.g. tu run pd.to_datetime(df['date'], errors='coerce') to convert such "exceptional" values to NaT.

Then check which output rows contain such value and take a look at corresponding rows in the source DataFrame (or e.g. in a file which you read this DataFrame from). Most likely you will find what is wrong in your input and decide how to correct these errors.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41