Home > Enterprise >  Checking pandas Timestamp for timezone string
Checking pandas Timestamp for timezone string

Time:08-19

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?

CodePudding user response:

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
  • Related