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 14 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
44 changes: 40 additions & 4 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1937,16 +1937,21 @@ 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 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."

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 : {'infer', 'NaT', default 'raise'}, bool-ndarray
Copy link
Member

Choose a reason for hiding this comment

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

I would make this str {'infer', 'NaT', 'raise'} or boolean array, default 'raise'

But that is probably too long, so you can do:

        ambiguous : str {'infer', 'NaT', 'raise'} or bool array, \
default 'raise'
            - 'infer': ...

It is a bit ugly in the code, but will render OK in plain text docstring and in html docstring.

Copy link
Member

Choose a reason for hiding this comment

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

A little bit below for the "errors" keyword, can you make the type description there: errors : {'raise', 'coerce'}, default 'raise' (so with curly braces)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

errors : {'raise', 'coerce'}, default 'raise'
we have to define str{'raise', 'coerce'} I thought its already understood

Copy link
Member

Choose a reason for hiding this comment

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

Yes, in this case errors : {'raise', 'coerce'}, default 'raise' is good (without the 'str')

- '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 @@ -1968,14 +1973,45 @@ 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

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-03')

>>> dti
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.
Copy link
Member

Choose a reason for hiding this comment

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

Typo, ? instead of / in the time zone.

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 also remove the # at the beginning of the line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops

>>> tz_aware = dti.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 aware time zone into naive time zone.
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, that was my fault, but when I said to add comments to the examples, I didn't mean Python comments, but just the sentence. So, without the #, in capital letters, and with a blank line after it (also the one before you already have).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure

>>> tz_naive = tz_aware.tz_localize(None)

>>> 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.

As you're not doing anything with tz_naive more than displaying it, I wouldn't save it to a variable first, I'd show the output direclty.

DatetimeIndex(['2018-03-01', '2018-03-02', '2018-03-03'],
dtype='datetime64[ns]', freq='D')


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