Pandas DataFrame | tz_convert method
Start your free 7-days trial now!
Pandas DataFrame.tz_convert(~) method converts the timezone of the row or column index.
Parameters
1. tzlink | string or tzinfo object
The timezone to convert into.
2. axis | int or string | optional
Whether to convert row labels or column labels:
Axis | Description |
|---|---|
| Row labels are converted |
| Column labels are converted |
By default, axis=0.
3. level | int or string | optional
The level to target. This is only relevant if the source DataFrame is multi-index. By default, level=None.
4. copy | boolean | optional
If
True, then a new DataFrame is returned. Modifying this DataFrame will not mutate the source DataFrame, and vice versa.If
False, then no new DataFrame is created - modifying the returned DataFrame will mutate the source DataFrame, and vice versa.
By default, copy=True.
Return Value
A DataFrame with the date-index's timezone converted.
Examples
Basic usage
Consider the following DataFrame:
df
A2020-12-25 00:00:00+09:00 42020-12-26 00:00:00+09:00 5
Here, the index is of type datetime64, and the timezone is set to Asia/Tokyo.
To convert the timezone into Europe/Paris:
result = df.tz_convert(tz="Europe/Paris") # axis=0result
A2020-12-24 16:00:00+01:00 42020-12-25 16:00:00+01:00 5
Converting column labels
Consider the following DataFrame:
Here, the column label is of type datetime64, and the timezone is set in Asia/Tokyo.
To convert the timezone into Europe/Paris:
df.tz_convert(tz="Europe/Paris", axis=1)
2020-12-24 16:00:00+01:000 41 5