Skip to content

Commit 6126ad6

Browse files
IHackPyjorisvandenbossche
authored andcommitted
DOC: Updated docstring DatetimeIndex.tz_localize (#20050)
1 parent 12de69b commit 6126ad6

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

pandas/core/indexes/datetimes.py

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,16 +2022,21 @@ def tz_convert(self, tz):
20222022
mapping={True: 'infer', False: 'raise'})
20232023
def tz_localize(self, tz, ambiguous='raise', errors='raise'):
20242024
"""
2025-
Localize tz-naive DatetimeIndex to given time zone (using
2026-
pytz/dateutil), or remove timezone from tz-aware DatetimeIndex
2025+
Localize tz-naive DatetimeIndex to tz-aware DatetimeIndex.
2026+
2027+
This method takes a time zone (tz) naive DatetimeIndex object and
2028+
makes this time zone aware. It does not move the time to another
2029+
time zone.
2030+
Time zone localization helps to switch from time zone aware to time
2031+
zone unaware objects.
20272032
20282033
Parameters
20292034
----------
20302035
tz : string, pytz.timezone, dateutil.tz.tzfile or None
2031-
Time zone for time. Corresponding timestamps would be converted to
2032-
time zone of the TimeSeries.
2033-
None will remove timezone holding local time.
2034-
ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise'
2036+
Time zone to convert timestamps to. Passing ``None`` will
2037+
remove the time zone information preserving local time.
2038+
ambiguous : str {'infer', 'NaT', 'raise'} or bool array, \
2039+
default 'raise'
20352040
- 'infer' will attempt to infer fall dst-transition hours based on
20362041
order
20372042
- bool-ndarray where True signifies a DST time, False signifies a
@@ -2040,12 +2045,12 @@ def tz_localize(self, tz, ambiguous='raise', errors='raise'):
20402045
- 'NaT' will return NaT where there are ambiguous times
20412046
- 'raise' will raise an AmbiguousTimeError if there are ambiguous
20422047
times
2043-
errors : 'raise', 'coerce', default 'raise'
2048+
errors : {'raise', 'coerce'}, default 'raise'
20442049
- 'raise' will raise a NonExistentTimeError if a timestamp is not
2045-
valid in the specified timezone (e.g. due to a transition from
2050+
valid in the specified time zone (e.g. due to a transition from
20462051
or to DST time)
20472052
- 'coerce' will return NaT if the timestamp can not be converted
2048-
into the specified timezone
2053+
to the specified time zone
20492054
20502055
.. versionadded:: 0.19.0
20512056
@@ -2055,12 +2060,43 @@ def tz_localize(self, tz, ambiguous='raise', errors='raise'):
20552060
20562061
Returns
20572062
-------
2058-
localized : DatetimeIndex
2063+
DatetimeIndex
2064+
Index converted to the specified time zone.
20592065
20602066
Raises
20612067
------
20622068
TypeError
20632069
If the DatetimeIndex is tz-aware and tz is not None.
2070+
2071+
See Also
2072+
--------
2073+
DatetimeIndex.tz_convert : Convert tz-aware DatetimeIndex from
2074+
one time zone to another.
2075+
2076+
Examples
2077+
--------
2078+
>>> tz_naive = pd.date_range('2018-03-01 09:00', periods=3)
2079+
>>> tz_naive
2080+
DatetimeIndex(['2018-03-01 09:00:00', '2018-03-02 09:00:00',
2081+
'2018-03-03 09:00:00'],
2082+
dtype='datetime64[ns]', freq='D')
2083+
2084+
Localize DatetimeIndex in US/Eastern time zone:
2085+
2086+
>>> tz_aware = tz_naive.tz_localize(tz='US/Eastern')
2087+
>>> tz_aware
2088+
DatetimeIndex(['2018-03-01 09:00:00-05:00',
2089+
'2018-03-02 09:00:00-05:00',
2090+
'2018-03-03 09:00:00-05:00'],
2091+
dtype='datetime64[ns, US/Eastern]', freq='D')
2092+
2093+
With the ``tz=None``, we can remove the time zone information
2094+
while keeping the local time (not converted to UTC):
2095+
2096+
>>> tz_aware.tz_localize(None)
2097+
DatetimeIndex(['2018-03-01 09:00:00', '2018-03-02 09:00:00',
2098+
'2018-03-03 09:00:00'],
2099+
dtype='datetime64[ns]', freq='D')
20642100
"""
20652101
if self.tz is not None:
20662102
if tz is None:

0 commit comments

Comments
 (0)