Search⌘ K
AI Features

Compare naive and tz-aware Values

Explore the differences between naive and timezone-aware timestamps in pandas. Learn why comparing these two types raises exceptions, how to localize naive timestamps, and convert tz-aware timestamps between time zones. This lesson helps you handle datetime data effectively and avoid common pitfalls when working with pandas timestamps.

We'll cover the following...

Try it yourself

Try executing the code below to see the result.

Python 3.8
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
...