@@ -2022,16 +2022,21 @@ def tz_convert(self, tz):
2022
2022
mapping = {True : 'infer' , False : 'raise' })
2023
2023
def tz_localize (self , tz , ambiguous = 'raise' , errors = 'raise' ):
2024
2024
"""
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.
2027
2032
2028
2033
Parameters
2029
2034
----------
2030
2035
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'
2035
2040
- 'infer' will attempt to infer fall dst-transition hours based on
2036
2041
order
2037
2042
- bool-ndarray where True signifies a DST time, False signifies a
@@ -2040,12 +2045,12 @@ def tz_localize(self, tz, ambiguous='raise', errors='raise'):
2040
2045
- 'NaT' will return NaT where there are ambiguous times
2041
2046
- 'raise' will raise an AmbiguousTimeError if there are ambiguous
2042
2047
times
2043
- errors : 'raise', 'coerce', default 'raise'
2048
+ errors : { 'raise', 'coerce'} , default 'raise'
2044
2049
- '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
2046
2051
or to DST time)
2047
2052
- 'coerce' will return NaT if the timestamp can not be converted
2048
- into the specified timezone
2053
+ to the specified time zone
2049
2054
2050
2055
.. versionadded:: 0.19.0
2051
2056
@@ -2055,12 +2060,43 @@ def tz_localize(self, tz, ambiguous='raise', errors='raise'):
2055
2060
2056
2061
Returns
2057
2062
-------
2058
- localized : DatetimeIndex
2063
+ DatetimeIndex
2064
+ Index converted to the specified time zone.
2059
2065
2060
2066
Raises
2061
2067
------
2062
2068
TypeError
2063
2069
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')
2064
2100
"""
2065
2101
if self .tz is not None :
2066
2102
if tz is None :
0 commit comments