Code Sample, a copy-pastable example if possible
# Your code here
df = pd.DataFrame()
df["time"] = pd.date_range(start="2017-01-01", end="2018-01-01", freq="D", tz="utc")
df["now"] = pd.datetime.utcnow()
df["now"] - df["time"] #raises ValueError: Incompatible tz's on datetime subtraction ops
Problem description
Calling df["now"].dt.tz
returns None
, meaning that pd.datetime.utcnow()
has not been explicitly localized to utc. Since pd.datetime.now()
is tz-naive, it is useful to have a similar function which returns the current time with timezone localization for operating on tz-aware datetimes.
Expected Output
df["now"].dt.tz
returns <UTC>
Output of pd.show_versions()
Comment From: jreback
you need to localize. despite its name datetime.utcnow()
does not return a tz-aware value. this is a standard library problem where there is no concrete impementation of timezones.
In [4]: df.dtypes
Out[4]:
time datetime64[ns, UTC]
now datetime64[ns]
dtype: object
```
In [10]: df['now'].dt.tz_localize('UTC')
Out[10]:
0 2018-02-14 10:59:27.808468+00:00
1 2018-02-14 10:59:27.808468+00:00
2 2018-02-14 10:59:27.808468+00:00
3 2018-02-14 10:59:27.808468+00:00
4 2018-02-14 10:59:27.808468+00:00
5 2018-02-14 10:59:27.808468+00:00
...
360 2018-02-14 10:59:27.808468+00:00
361 2018-02-14 10:59:27.808468+00:00
362 2018-02-14 10:59:27.808468+00:00
363 2018-02-14 10:59:27.808468+00:00
364 2018-02-14 10:59:27.808468+00:00
365 2018-02-14 10:59:27.808468+00:00
Name: now, Length: 366, dtype: datetime64[ns, UTC]
````