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 13 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
35 changes: 33 additions & 2 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1937,8 +1937,13 @@ 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.

tz_localize() takes a naive DatetimeIndex object and interprets it as
if it is in that timezone. 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."

tz_localize function helps to switch b/w time zone aware and time zone
unaware objects.

Parameters
----------
Expand Down Expand Up @@ -1968,14 +1973,40 @@ 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

Examples
--------
In the example below, We put the date range from 01 March 2018
to 08 March 2018 & localize this to US/Eastern Time zone & again
we perform reverse operation where we remove tize zone & make it
Copy link
Member

Choose a reason for hiding this comment

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

Can you just leave the type in Returns (so, get rid of the "localized" name). Also, a short explanation on what is returned would be useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@datapythonista
I'm understand that I have to remove localized from return section & write like this :
Returns

DatetimeIndex

or I should write some explanation on returns section like this:
Returns

Some Explanation
I'm confused about it ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

and parameter ambiguous : {'infer', 'NaT', default 'raise'}, bool-ndarray . write like this ???

Copy link
Member

Choose a reason for hiding this comment

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

For return, first line as you say, then another line with explanation. You can check the first example here:
https://python-sprints.github.io/pandas/guide/pandas_docstring.html#section-4-returns-or-yields.

For parameter, I didn't see a case exactly like this before, but for the convention I'd say something like:

{'raise', 'infer', 'NaT'} or bool array-like, default 'raise'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is there any change with summary & with example section

tz-naive

>>> dti = pd.date_range('2018-03-01', '2018-03-05')

>>> dti
DatetimeIndex(['2018-03-01', '2018-03-02', '2018-03-03',
'2018-03-04', '2018-03-05'], dtype='datetime64[ns]', freq='D')
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a blank line between cases


>>> dti.tz_localize(tz='US/Eastern')
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',
'2018-03-04 00:00:00-05:00', '2018-03-05 00:00:00-05:00'],
Copy link
Contributor

Choose a reason for hiding this comment

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

showing the reverse example is also useful, IOW .tz_localize(None), removes the timezone and makes it tz-naive.

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 when I add another example it shows error how I add that example in the docstring

Copy link
Contributor

Choose a reason for hiding this comment

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

n [11]: pd.date_range('2018-03-01', '2018-03-05').tz_localize('US/Eastern').tz_localize(None)
Out[11]: 
DatetimeIndex(['2018-03-01', '2018-03-02', '2018-03-03', '2018-03-04',
               '2018-03-05'],
              dtype='datetime64[ns]', freq='D')

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 made required change.

dtype='datetime64[ns, US/Eastern]', freq='D')

>>> dti.tz_localize('US/Eastern').tz_localize(None)
DatetimeIndex(['2018-03-01', '2018-03-02', '2018-03-03',
'2018-03-04', '2018-03-05'], dtype='datetime64[ns]', freq='D')
Copy link
Member

Choose a reason for hiding this comment

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

More a personal opinion, but couple of things that I think would make the examples more clear:

  • Use 3 dates instead of 5
  • Add short sentences between commands on what's going on (e.g. "The values for the times are the same, but the index contains the timezone")
  • Save the output of dti.tz_localize(tz='US/Eastern') so it can be reused in the last command, instead of doing to two localizations in the same line. May be naming dti tz_naive first, and tz_aware later would make them more clear too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okey sure



Raises
------
TypeError
If the DatetimeIndex is tz-aware and tz is not None.

Copy link
Member

Choose a reason for hiding this comment

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

I think we don't leave this line here, the validation script probably complains.

"""
if self.tz is not None:
if tz is None:
Expand Down