Preparation
Before any data manipulation can occur, one (1) new library will require installation.
- The Pandas library enables access to/from a DataFrame.
To install this library, navigate to an IDE terminal. At the command prompt ($
), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($
). Your terminal prompt may be different.
π‘ Note: The pytz
comes packaged with pandas and does not require installation. However, this library is needed for the tz_ localize()
and tz_convert()
methods to work.
$ pip install pandas
Hit the <Enter>
key on the keyboard to start the installation process.
If the installation was successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required library.
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.
import pandas as pd import pytz
DataFrame tz_localize()
The tz_localize()
method localizes a time zone’s native index of a DataFrame/Series to a targeted time zone.
To view a list of available time zones, click here or navigate to an IDE and run the following code:
$ print(pytz.all_timezones)
The syntax for this method is as follows:
DataFrame.tz_localize(tz, axis=0, level=None, copy=True, ambiguous='raise', nonexistent='raise')
Parameter | Description |
---|---|
tz | The parameter is a string of a valid time zone. Ex: 'America/Phoenix' . |
axis | If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
level | A datetime level in a MultiIndex scenario to use for resampling. |
copy | If True , this parameter makes a copy. |
ambiguous | If the clock moves backward (counterclockwise), an error may occur. The available options are: – 'infer' : infer DST-transition hours (based on order). – Boolean-array: True reflects as a DST. False , set as a non-DST time. – 'NaT' : returns this value if ambiguous times occur. – The default is 'raise' . |
nonexistent | If the clock moves forward (clockwise), an error may occur. The available options are: – 'shift-forward' : moves the blank/invalid time forward to the nearest non-empty time. – 'shift-backward' : moves the blank/empty time backward to the nearest non-empty time. – 'NaT' : returns this value if ambiguous times occur. – timedelta : shift empty times by the timedelta. – The default is 'raise' . |
For this example, the time localizes to Berlin, Germany.
tz_1 = pd.date_range('2022-12-25 09:00', periods=3) print(tz_1) tz_2 = tz_1.tz_localize(tz='Europe/Berlin') print(tz_2)
- Line [1] creates a date range based on a start date and a 3-day duration. The output saves to
tz_1
. - Line [2] outputs
tz_1
to the terminal. - Line [3] localizes the time zone to Berlin. The output saves to
tz_2
. - Line [4] outputs
tz_2
to the terminal.
Output
tz_1
DatetimeIndex(['2022-12-25 09:00:00', '2022-12-26 09:00:00', '2022-12-27 09:00:00'], dtype='datetime64[ns]', freq='D')
tz_2
DatetimeIndex(['2022-12-25 09:00:00+01:00', '2022-12-26 09:00:00+01:00', '2022-12-27 09:00:00+01:00'], dtype='datetime64[ns, Europe/Berlin]', freq=None)
π‘Β Note: The localized time zone displays as Europe/Berlin.
More Pandas DataFrame Methods
Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:
Also, check out the full cheat sheet overview of all Pandas DataFrame methods.