Convert tz-aware Datetime Array/Index from one time zone to another. Parameters ---------- tz : str, zoneinfo.ZoneInfo, pytz.timezone, dateutil.tz.tzfile, datetime.tzinfo or None Time zone for time. Corresponding timestamps would be converted
(self, tz)
| 326 | return Index(arr, name=self.name, dtype=arr.dtype, copy=False) |
| 327 | |
| 328 | def tz_convert(self, tz) -> Self: |
| 329 | """ |
| 330 | Convert tz-aware Datetime Array/Index from one time zone to another. |
| 331 | |
| 332 | Parameters |
| 333 | ---------- |
| 334 | tz : str, zoneinfo.ZoneInfo, pytz.timezone, dateutil.tz.tzfile, datetime.tzinfo or None |
| 335 | Time zone for time. Corresponding timestamps would be converted |
| 336 | to this time zone of the Datetime Array/Index. A `tz` of None will |
| 337 | convert to UTC and remove the timezone information. |
| 338 | |
| 339 | Returns |
| 340 | ------- |
| 341 | Array or Index |
| 342 | Datetme Array/Index with target `tz`. |
| 343 | |
| 344 | Raises |
| 345 | ------ |
| 346 | TypeError |
| 347 | If Datetime Array/Index is tz-naive. |
| 348 | |
| 349 | See Also |
| 350 | -------- |
| 351 | DatetimeIndex.tz : A timezone that has a variable offset from UTC. |
| 352 | DatetimeIndex.tz_localize : Localize tz-naive DatetimeIndex to a |
| 353 | given time zone, or remove timezone from a tz-aware DatetimeIndex. |
| 354 | |
| 355 | Examples |
| 356 | -------- |
| 357 | With the `tz` parameter, we can change the DatetimeIndex |
| 358 | to other time zones: |
| 359 | |
| 360 | >>> dti = pd.date_range( |
| 361 | ... start="2014-08-01 09:00", freq="h", periods=3, tz="Europe/Berlin" |
| 362 | ... ) |
| 363 | |
| 364 | >>> dti |
| 365 | DatetimeIndex(['2014-08-01 09:00:00+02:00', |
| 366 | '2014-08-01 10:00:00+02:00', |
| 367 | '2014-08-01 11:00:00+02:00'], |
| 368 | dtype='datetime64[us, Europe/Berlin]', freq='h') |
| 369 | |
| 370 | >>> dti.tz_convert("US/Central") |
| 371 | DatetimeIndex(['2014-08-01 02:00:00-05:00', |
| 372 | '2014-08-01 03:00:00-05:00', |
| 373 | '2014-08-01 04:00:00-05:00'], |
| 374 | dtype='datetime64[us, US/Central]', freq='h') |
| 375 | |
| 376 | With the ``tz=None``, we can remove the timezone (after converting |
| 377 | to UTC if necessary): |
| 378 | |
| 379 | >>> dti = pd.date_range( |
| 380 | ... start="2014-08-01 09:00", freq="h", periods=3, tz="Europe/Berlin" |
| 381 | ... ) |
| 382 | |
| 383 | >>> dti |
| 384 | DatetimeIndex(['2014-08-01 09:00:00+02:00', |
| 385 | '2014-08-01 10:00:00+02:00', |