pandas 1.4.2

ParametersRaisesReturns
tz_localize(self: 'NDFrameT', tz, axis=0, level=None, copy: 'bool_t' = True, ambiguous='raise', nonexistent: 'str' = 'raise') -> 'NDFrameT'

This operation localizes the Index. To localize the values in a timezone-naive Series, use Series.dt.tz_localize .

Parameters

tz : str or tzinfo
axis : the axis to localize
level : int, str, default None

If axis ia a MultiIndex, localize a specific level. Otherwise must be None.

copy : bool, default True

Also make a copy of the underlying data.

ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise'

When clocks moved backward due to DST, ambiguous times may arise. For example in Central European Time (UTC+01), when going from 03:00 DST to 02:00 non-DST, 02:30:00 local time occurs both at 00:30:00 UTC and at 01:30:00 UTC. In such a situation, the :None:None:`ambiguous` parameter dictates how ambiguous times should be handled.

  • 'infer' will attempt to infer fall dst-transition hours based on order

  • bool-ndarray where True signifies a DST time, False designates a non-DST time (note that this flag is only applicable for ambiguous times)

  • 'NaT' will return NaT where there are ambiguous times

  • 'raise' will raise an AmbiguousTimeError if there are ambiguous times.

nonexistent : str, default 'raise'

A nonexistent time does not exist in a particular timezone where clocks moved forward due to DST. Valid values are:

  • 'shift_forward' will shift the nonexistent time forward to the closest existing time

  • 'shift_backward' will shift the nonexistent time backward to the closest existing time

  • 'NaT' will return NaT where there are nonexistent times

  • timedelta objects will shift nonexistent times by the timedelta

  • 'raise' will raise an NonExistentTimeError if there are nonexistent times.

Raises

TypeError

If the TimeSeries is tz-aware and tz is not None.

Returns

Series or DataFrame

Same type as the input.

Localize tz-naive index of a Series or DataFrame to target time zone.

Examples

Localize local times:

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series([1],
...  index=pd.DatetimeIndex(['2018-09-15 01:30:00']))
... s.tz_localize('CET') 2018-09-15 01:30:00+02:00 1 dtype: int64

Be careful with DST changes. When there is sequential data, pandas can infer the DST time:

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series(range(7),
...  index=pd.DatetimeIndex(['2018-10-28 01:30:00',
...  '2018-10-28 02:00:00',
...  '2018-10-28 02:30:00',
...  '2018-10-28 02:00:00',
...  '2018-10-28 02:30:00',
...  '2018-10-28 03:00:00',
...  '2018-10-28 03:30:00']))
... s.tz_localize('CET', ambiguous='infer') 2018-10-28 01:30:00+02:00 0 2018-10-28 02:00:00+02:00 1 2018-10-28 02:30:00+02:00 2 2018-10-28 02:00:00+01:00 3 2018-10-28 02:30:00+01:00 4 2018-10-28 03:00:00+01:00 5 2018-10-28 03:30:00+01:00 6 dtype: int64

In some cases, inferring the DST is impossible. In such cases, you can pass an ndarray to the ambiguous parameter to set the DST explicitly

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series(range(3),
...  index=pd.DatetimeIndex(['2018-10-28 01:20:00',
...  '2018-10-28 02:36:00',
...  '2018-10-28 03:46:00']))
... s.tz_localize('CET', ambiguous=np.array([True, True, False])) 2018-10-28 01:20:00+02:00 0 2018-10-28 02:36:00+02:00 1 2018-10-28 03:46:00+01:00 2 dtype: int64

If the DST transition causes nonexistent times, you can shift these dates forward or backward with a timedelta object or :None:None:`'shift_forward'` or :None:None:`'shift_backward'`.

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series(range(2),
...  index=pd.DatetimeIndex(['2015-03-29 02:30:00',
...  '2015-03-29 03:30:00']))
... s.tz_localize('Europe/Warsaw', nonexistent='shift_forward') 2015-03-29 03:00:00+02:00 0 2015-03-29 03:30:00+02:00 1 dtype: int64
This example is valid syntax, but we were not able to check execution
>>> s.tz_localize('Europe/Warsaw', nonexistent='shift_backward')
2015-03-29 01:59:59.999999999+01:00    0
2015-03-29 03:30:00+02:00              1
dtype: int64
This example is valid syntax, but we were not able to check execution
>>> s.tz_localize('Europe/Warsaw', nonexistent=pd.Timedelta('1H'))
2015-03-29 03:30:00+02:00    0
2015-03-29 03:30:00+02:00    1
dtype: int64
See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/generic.py#9804
type: <class 'function'>
Commit: