DatetimeIndex.tz_localize(tz, ambiguous='raise', errors='raise')
[source]
Localize tz-naive DatetimeIndex to tz-aware DatetimeIndex.
This method takes a time zone (tz) naive DatetimeIndex object and makes this time zone aware. It does not move the time to another time zone. Time zone localization helps to switch from time zone aware to time zone unaware objects.
Parameters: |
tz : string, pytz.timezone, dateutil.tz.tzfile or None Time zone to convert timestamps to. Passing ambiguous : str {‘infer’, ‘NaT’, ‘raise’} or bool array, default ‘raise’
errors : {‘raise’, ‘coerce’}, default ‘raise’
New in version 0.19.0. |
---|---|
Returns: |
DatetimeIndex Index converted to the specified time zone. |
Raises: |
TypeError If the DatetimeIndex is tz-aware and tz is not None. |
See also
DatetimeIndex.tz_convert
>>> tz_naive = pd.date_range('2018-03-01 09:00', periods=3) >>> tz_naive DatetimeIndex(['2018-03-01 09:00:00', '2018-03-02 09:00:00', '2018-03-03 09:00:00'], dtype='datetime64[ns]', freq='D')
Localize DatetimeIndex in US/Eastern time zone:
>>> tz_aware = tz_naive.tz_localize(tz='US/Eastern') >>> tz_aware DatetimeIndex(['2018-03-01 09:00:00-05:00', '2018-03-02 09:00:00-05:00', '2018-03-03 09:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq='D')
With the tz=None
, we can remove the time zone information while keeping the local time (not converted to UTC):
>>> tz_aware.tz_localize(None) DatetimeIndex(['2018-03-01 09:00:00', '2018-03-02 09:00:00', '2018-03-03 09:00:00'], dtype='datetime64[ns]', freq='D')
© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
http://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.DatetimeIndex.tz_localize.html