Compare naive and tz-aware Values

Let's look at the types of timestamps and how we can use them.

We'll cover the following

Try it yourself

Try executing the code below to see the result.

Press + to interact
import pandas as pd
s1 = pd.to_datetime([
'2020-01-01T00:00:00+00:00',
'2020-02-02T00:00:00+00:00',
'2020-03-03T00:00:00+00:00',
])
s2 = pd.Series([
pd.Timestamp(2020, 1, 1),
pd.Timestamp(2020, 2, 2),
pd.Timestamp(2020, 3, 3),
])
print(s1 == s2)
pd.to_datetime(df['timestamp'], unit='s')

Explanation

In pandas and Python, there is one Timestamp or datetime type. However, that one type is divided into these two subtypes:

  1. naive
  2. tz-aware

The naive type doesn’t have time zone information associated with it, while the tz-aware type does.

We can’t compare naive and tz-aware values. Doing so will result in an exception, like the one in this teaser:

In [1]: t = pd.Timestamp(2020, 5, 23)
In [2]: t
Out[2]: Timestamp('2020-05-23 00:00:00')
In [3]: ut = t.tz_localize('UTC')
In [4]: ut
Out[4]: Timestamp('2020-05-23 00:00:00+0000', tz='UTC')
In [5]: ut == t
...
TypeError: Cannot compare tz-naive and tz-aware timestamps

We must work with tz-aware timestamps to convert from one time zone to another.

In [6]: t.tz_convert('US/Pacific')
...
TypeError: Cannot convert tz-naive Timestamp, use tz_localize to localize
In [7]: ut.tz_convert('US/Pacific')
Out[7]: Timestamp('2020-05-22 17:00:00-0700', tz='US/Pacific')

Time zone database

Python versions before 3.8 don’t come with a time zone database. Pandas time zone database depends on the included pytz package, which periodically updates. Python 3.9 introduced a built-in zoneinfo module that is included in later versions as well

Solution

Press + to interact
import pandas as pd
s1 = pd.to_datetime('2020-01-01T00:00:00+00:00')
s2 = pd.Timestamp(2020, 1, 1)
s2 = s2.to_pydatetime()
print(s1 == s2)

Get hands-on with 1300+ tech skills courses.