📜  Python| Pandas Timestamp.tzinfo(1)

📅  最后修改于: 2023-12-03 15:19:16.220000             🧑  作者: Mango

Python | Pandas Timestamp.tzinfo

Pandas is a powerful Python library for data analysis and manipulation. One of the data types provided by Pandas is Timestamp, which represents a single timestamp or date/time. Timestamp objects have a tzinfo attribute that stores information about the timezone of the timestamp.

Working with Timestamp.tzinfo

In Pandas, Timestamp objects can be created using the pd.Timestamp() function. By default, the resulting Timestamp is timezone-naive, meaning it does not have any timezone information associated with it.

import pandas as pd

# create a timestamp without timezone info
timestamp_naive = pd.Timestamp('2021-12-31 23:59:59')
print(timestamp_naive)

Output:

2021-12-31 23:59:59

To create a Timestamp with timezone info, you can provide the timezone as an argument to the pd.Timestamp() function. The timezone can be specified using a string with the timezone name, or a pytz timezone object.

import pytz

# create a timestamp with timezone info
timezone = pytz.timezone('Europe/London')
timestamp_aware = pd.Timestamp('2021-12-31 23:59:59', tz=timezone)
print(timestamp_aware)

Output:

2021-12-31 23:59:59+00:00

Note that the Timestamp object now includes a +00:00 at the end, indicating the timezone.

You can access the tzinfo attribute of a Timestamp object by calling its tz method. This returns a pytz timezone object that represents the timezone of the Timestamp.

print(timestamp_aware.tzinfo)

Output:

Europe/London
Conclusion

The Timestamp.tzinfo attribute in Pandas allows you to work with timestamps that include timezone information. This is important when working with data from different timezones, or when performing operations across timezones. By default, Timestamp objects are timezone-naive, but you can create timezone-aware Timestamps using the tz argument in pd.Timestamp(). The tzinfo attribute of a Timestamp can be accessed using the tz method, which returns a pytz timezone object representing the timezone of the Timestamp.