Skip to content

DOC: Updated docstring DatetimeIndex.tz_localize #20050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Mar 14, 2018
Merged
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1937,16 +1937,22 @@ def tz_convert(self, tz):
mapping={True: 'infer', False: 'raise'})
def tz_localize(self, tz, ambiguous='raise', errors='raise'):
"""
Localize tz-naive DatetimeIndex to given time zone (using
pytz/dateutil), or remove timezone from tz-aware DatetimeIndex
Localize tz-naive DatetimeIndex to tz-aware DatetimeIndex.

This method takes a naive DatetimeIndex object and make this
time zone aware. It does not move the time to another
timezone.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the interprets word a bit unclear. I think something like "Make a naive DatetimeIndex time zone aware, by attaching a time zone. It does not change the values."

Surely you can write it in a better way, but hopefully you see what I mean.

Copy link
Contributor Author

@IHackPy IHackPy Mar 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@datapythonista hows this otherwise I'm going to co[py paste same you suggest above :)

"This method takes a naive DatetimeIndex object and make this time zone aware. It does not move the time to another timezone.
Time zone localization helps to switch b/w time zone aware and time zone unaware objects."

Time zone localization helps to switch b/w time zone aware and time
zone unaware objects.

Parameters
----------
tz : string, pytz.timezone, dateutil.tz.tzfile or None
Time zone for time. Corresponding timestamps would be converted to
time zone of the TimeSeries.
None will remove timezone holding local time.
ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise'
ambiguous : str {'infer', 'NaT', 'raise'} or bool array, \
default 'raise'
- 'infer' will attempt to infer fall dst-transition hours based on
order
- bool-ndarray where True signifies a DST time, False signifies a
Expand All @@ -1955,7 +1961,7 @@ def tz_localize(self, tz, ambiguous='raise', errors='raise'):
- 'NaT' will return NaT where there are ambiguous times
- 'raise' will raise an AmbiguousTimeError if there are ambiguous
times
errors : 'raise', 'coerce', default 'raise'
errors : {'raise', 'coerce'}, default 'raise'
- 'raise' will raise a NonExistentTimeError if a timestamp is not
valid in the specified timezone (e.g. due to a transition from
or to DST time)
Expand All @@ -1968,9 +1974,33 @@ def tz_localize(self, tz, ambiguous='raise', errors='raise'):
.. deprecated:: 0.15.0
Attempt to infer fall dst-transition hours based on order
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can leave this one (although it will give an error in the validation script, but you can ignore that)

Copy link
Contributor Author

@IHackPy IHackPy Mar 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add this again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorisvandenbossche why adding this back? looks leftover

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback @jorisvandenbossche I add this again or not ???????

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback no, the deprecation is still there (the fact that the deprecation is still there is certainly a left-over, but let's remove that (both the actual deprecation and the docs) in a separate PR)

@himanshuawasthi95 Yes, please add back



Returns
-------
localized : DatetimeIndex
DatetimeIndex
Index converted to the specified time zone.

Examples
--------
>>> tz_naive = pd.date_range('2018-03-01', '2018-03-03')
>>> tz_naive
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The blank line between them seems unnecessary. (also later in tz_aware)

DatetimeIndex(['2018-03-01', '2018-03-02', '2018-03-03'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 00:00:00-05:00',
'2018-03-02 00:00:00-05:00', '2018-03-03 00:00:00-05:00'],
dtype='datetime64[ns, US/Eastern]', freq='D')

Localize time zone aware into naive time zone.

>>> tz_aware.tz_localize(None)
DatetimeIndex(['2018-03-01', '2018-03-02', '2018-03-03'],
dtype='datetime64[ns]', freq='D')


Raises
------
Expand Down