Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Checking pandas Timestamp for timezone string

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading