Puzzle 8 Once Upon a Time

Pandas Brain Teasers — by Miki Tebeka (16 / 34)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 A Delicious Div Sum | TOC | A Hefty Bonus 👉

times.py

​ ​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.

images/hline.png

This code will raise a TypeError.

images/hline.png

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
​…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.