Puzzle 8 Once Upon a Time
Pandas Brain Teasers — by Miki Tebeka (16 / 34)
Published in
3 min readMar 23, 2022
👈 A Delicious Div Sum | TOC | A Hefty Bonus 👉
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)
Guess the Output
IMPORTANT
Try to guess what the output is before moving to the next page.
This code will raise a TypeError.
In Pandas (and Python) there is one Timestamp (or datetime) type. However, it is divided into two subtypes: naive and tz-aware. The naive type doesn’t have time-zone information associated with it, while the tz-aware type does.
You cannot compare naive and tz-aware values:
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
…