13

I have df like this:

df['Time']
2017-08-06 11:00:00+00:00
2017-08-08 15:00:00+00:00
2017-08-10 04:00:00+00:00
2017-08-12 23:00:00+00:00
2017-08-08 15:00:00+00:00

I want to slice it with condition

mask1=df['Time'] > datetime.datetime.strptime('2017-08-12', "%Y-%m-%d")

I get an error like

can't compare offset-naive and offset-aware datetimes

Somehow I have to convert df['Time'] to offset-naive.
Please help me fix this.

1 Answer 1

19

It seems you need:

df['Time'] = df['Time'].dt.tz_localize(None)

Or:

df['Time'] = df['Time'].dt.tz_convert(None)

Or:

df['Time'] = df['Time'].astype('datetime64[ns]')

See also tz aware dtypes.

6
  • I had tried 3rd option . It converted date-time in 2017-08-12T23:00:00.000000000 format. Then I tried in top two options but output df remains in same format. How to convert in simple format 2017-08-12 23:00:00. I am printing o/p to csv file. When i print on o/p terminal , format is as exacted. Commented Sep 19, 2017 at 8:41
  • Hmmm, it seems using oldier version of pandas. So maybe help df['Time'] = pd.to_datetime(df['Time'].astype('datetime64[ns]'))
    – jezrael
    Commented Sep 19, 2017 at 8:42
  • Tried with this option too. it's same o/p. I am using >>> pd.__version__ '0.16.2' Commented Sep 19, 2017 at 8:49
  • Then it is bug. Last version is 0.20.3 - there is no error.
    – jezrael
    Commented Sep 19, 2017 at 8:51
  • yes . all three options do not work in expected format. Will check with version you mentioned. thanks Commented Sep 19, 2017 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.