Checking pandas Timestamp for timezone string

Question:

In the code below I want to acquire the timezone of a pandas Timestamp supplied to a function:

import pandas as pd
import pytz

timestamp = pd.Timestamp("2022-05-01", tz='Europe/Paris')
print(timestamp.tzinfo)

This prints:

Europe/Paris

However, I would like to check whether the timezone matches the timezone that I want and return a boolean value, such as:

timestamp.tzinfo == 'Europe/Paris'

But this returns False. Is there anyone here who knows a clever of checking the timezone of a timezone aware pandas Timestamp?

Asked By: brokkoo

||

Answers:

Checking the type

type(timestamp.tzinfo)

pytz.tzfile.Europe/Paris

We can see this is not a string, but some type of pytz object.

However if we look closer we can see there is a .zone which does return a string and is probably what you are looking for.

timestamp.tzinfo.zone == 'Europe/Paris'

True
Answered By: Chris
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.