Bera
Bera

Reputation: 1949

Wrong year pd.to_datetime

I have a dataframe with dates stored like objects in the format of 2020-01-01 00:00:00+01:00

I want to extract year as a new column:

tp['year'] = pd.to_datetime(tp.datefield).dt.year, utc=True)

(without utc=True i get: ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True)

But for dates like 2020-01-01 00:00:00+01:00 I get year 2019 when it should be 2020. What am I doing wrong?

Upvotes: 0

Views: 331

Answers (1)

jezrael
jezrael

Reputation: 863226

There is nothing wrong, if convert to utc get:

tp['date'] = pd.to_datetime(tp.datefield, utc=True)
print (tp)
                   datefield                      date
0  2020-01-01 00:00:00+01:00 2019-12-31 23:00:00+00:00

So year is 2019.

Btw, for me working it correct, pandas 1.1.1:

tp['year'] = pd.to_datetime(tp.datefield).dt.year
print (tp)

                   datefield  year
0  2020-01-01 00:00:00+01:00  2020

Upvotes: 2

Related Questions