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

Conversation

IHackPy
Copy link
Contributor

@IHackPy IHackPy commented Mar 8, 2018

Kindly review this docstring DatetimeIndex.tz_localize

@jorisvandenbossche jorisvandenbossche changed the title Updated DocString DatetimeIndex.tz_localize DOC: Updated docstring DatetimeIndex.tz_localize Mar 8, 2018
Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! I added a first round of comments

pytz/dateutil), or remove timezone from tz-aware DatetimeIndex
DatetimeIndex.tz_localize used for Localize tz-naive DatetimeIndex.

tz_localize method is use to convert naive DatetimeIndex into any other localize
Copy link
Member

Choose a reason for hiding this comment

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

Your indentation is one space off (this line should be indented the same as the first line of the docstring)

@@ -1937,15 +1937,18 @@ 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
DatetimeIndex.tz_localize used for Localize tz-naive DatetimeIndex.
Copy link
Member

Choose a reason for hiding this comment

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

We should try to avoid to repeat the name of the method in the beginning of this summary line. What do you think of "Localize a tz-naive DatetimeIndex" ?
At the same time, we could try to make this even clearer. For example use "timezone" instead of "tz".

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


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.
None will remove timezone holding local time
Copy link
Member

Choose a reason for hiding this comment

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

Please leave this final '.' (the updated validation script will also give a message about this)


Returns
-------
localized : DatetimeIndex

Examples
--------
Copy link
Member

Choose a reason for hiding this comment

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

same comment here about the indentation (it seems you are mixing tabs and spaces. It is best to configure your editor to introduce 4 spaces when you do tab)

In the example below, We put the date range from 01 March 2018 to 08 March 2018
& convert this to US/Eastern Time zone

>>> import pandas as pd
Copy link
Member

Choose a reason for hiding this comment

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

typescript Outdated
@@ -0,0 +1,135 @@
Script started on 2018-03-06 11:15:57+0530
# ]2;root@localhost: /home/ihackpy/Panda/pandas-dir]1;..da/pandas-dir➜ pandas-dir git:(docstringhead) ✗ [?1h=[?2004hlls[?1l>[?2004l
]2;ls --color=tty]1;lsappveyor.yml conda.recipe pandas setup.cfg test.sh
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 remove this file again?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I don't know HOw this file added sorry for the inconvenience

Raises
------
TypeError
If the DatetimeIndex is tz-aware and tz is not None.
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.

Same here: please keep the '.'

@jorisvandenbossche
Copy link
Member

Can you do a check of PEP8 with running git diff upstream/master -u -- "*.py" | flake8 --diff (see https://python-sprints.github.io/pandas/guide/pandas_pr.html#push-your-changes-to-pandas for more explanation)

@codecov
Copy link

codecov bot commented Mar 8, 2018

Codecov Report

Merging #20050 into master will decrease coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #20050      +/-   ##
==========================================
- Coverage   91.71%    91.7%   -0.02%     
==========================================
  Files         150      150              
  Lines       49112    49122      +10     
==========================================
  Hits        45045    45045              
- Misses       4067     4077      +10
Flag Coverage Δ
#multiple 90.08% <ø> (-0.02%) ⬇️
#single 41.86% <ø> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexes/datetimes.py 95.65% <ø> (ø) ⬆️
pandas/plotting/_converter.py 65.07% <0%> (-1.74%) ⬇️
pandas/core/window.py 96.31% <0%> (ø) ⬆️
pandas/core/indexes/base.py 96.66% <0%> (ø) ⬆️
pandas/util/testing.py 83.85% <0%> (+0.2%) ⬆️
pandas/core/reshape/tile.py 93.37% <0%> (+0.42%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9273bf5...95fcee2. Read the comment docs.

@@ -1937,8 +1937,11 @@ 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
DatetimeIndex.tz_localize used for Localize tz-naive DatetimeIndex.
Copy link
Contributor

Choose a reason for hiding this comment

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

used to localize a tz-aware DatetimeIndex to tz-naive

pytz/dateutil), or remove timezone from tz-aware DatetimeIndex
DatetimeIndex.tz_localize used for Localize tz-naive DatetimeIndex.

tz_localize method is use to convert naive DatetimeIndex into any other
Copy link
Contributor

Choose a reason for hiding this comment

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

tz-aware -> tz-naive

@pep8speaks
Copy link

pep8speaks commented Mar 8, 2018

Hello @himanshuawasthi95! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on March 14, 2018 at 08:54 Hours UTC

@jorisvandenbossche
Copy link
Member

@himanshuawasthi95 You are still mixing tabs and spaces. Please try to fix that first.

@IHackPy
Copy link
Contributor Author

IHackPy commented Mar 8, 2018

Locally showing perfect I use vim for editing but don't know when I push code it shows wrong indentation is any tool which help me to remove this issue

@jorisvandenbossche
Copy link
Member

I am not familiar with vim myself, but you will somehow have to say to vim that tabs should not be introduces as tabs but as 4 spaces (it may 'look' good, but that doesn't mean it is not mixing tabs and spaces). See eg https://stackoverflow.com/questions/1878974/redefine-tab-as-4-spaces

@jorisvandenbossche
Copy link
Member

Or also see the second answer in https://stackoverflow.com/questions/426963/replace-tab-with-spaces-in-vim

@jorisvandenbossche
Copy link
Member

I am afraid it is not yet solved.
You can check the output of git diff upstream/master -u -- "*.py" | flake8 --diff , it will still contain errors related to tabs/spaces. See also #20050 (comment)

@jorisvandenbossche
Copy link
Member

Tabs/spaces issues seems to be solved!

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Can you add an example for the None behaviour as well? (removing the timezone)

@@ -1937,8 +1937,10 @@ 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
Convert aware Datetime index to TimeZone naive Datetime index.
Copy link
Member

Choose a reason for hiding this comment

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

"Datetime index" -> DatetimeIndex
"TimeZone" -> time zone

And it is the other way around: you now say "convert aware to naive", but it is "localize naive to aware"

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jorisvandenbossche can you illustrate more "Can you add an example for the None behaviour as well? (removing the timezone)"

@@ -1964,18 +1966,32 @@ def tz_localize(self, tz, ambiguous='raise', errors='raise'):

.. versionadded:: 0.19.0

infer_dst : boolean, default False
.. 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

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

Can you clean up the alignment a bit here? If I run this in a console, I get this:

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

Ideally it should look the same (notice the different vertical alignment with your code)

@@ -1937,8 +1937,10 @@ 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
Convert aware to naive.
Copy link
Contributor

Choose a reason for hiding this comment

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

Localize a tz-naive DatetimeIndex to tz-aware DatetimeIndex

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
can I add this line in extended summary :
tz_localize() takes a naive datetime object and interprets it as if it is in that timezone. It does not move the time to another timezone. tz_localize function helps to switch b/w time zone aware and time zone unaware objects.

Copy link
Contributor

Choose a reason for hiding this comment

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

interprets -> sets it in that timezone with the current timestamps.

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

not sure this last line is necessary

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 If it is give more clearity then It is okey other wise I can remove ??????

Convert aware to naive.

Localize timezone-naive DatetimeIndex to given time zone,
or remove timezone from timezone aware DatetimeIndex.
Copy link
Contributor

Choose a reason for hiding this comment

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

this is almost a duplicate of the top

Examples
--------
In the example below, We put the date range from 01 March 2018
to 08 March 2018 & convert this to US/Eastern Time zone
Copy link
Contributor

Choose a reason for hiding this comment

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

don't say convert. this is localizing, almost like 'setting' a time-zone.

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

@jreback jreback added Datetime Datetime data dtype Timezones Timezone data dtype labels Mar 10, 2018
>>> df.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.

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

In the example below, We put the date range from 01 March 2018
to 08 March 2018 & convert this to US/Eastern Time zone

>>> df = pd.date_range('2018-03-01', '2018-03-05')
Copy link
Contributor

Choose a reason for hiding this comment

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

don't use df, dti is better

Copy link
Contributor Author

Choose a reason for hiding this comment

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

any thing else in summary section ???

pytz/dateutil), or remove timezone from tz-aware DatetimeIndex
Localize tz-naive DatetimeIndex to tz-aware DatetimeIndex.

tz_localize() takes a naive datetime object and interprets it as if it
Copy link
Contributor

Choose a reason for hiding this comment

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

takes a naive DatetimeIndex

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

'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'],
dtype='datetime64[ns, US/Eastern]', freq='D')
>>> dti.tz_localize(None)
Copy link
Contributor

Choose a reason for hiding this comment

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

this last won't work, you need to do
dti.tz_localize('US/Eastern').tz_localize(None), which gets you back the original

Copy link
Contributor Author

Choose a reason for hiding this comment

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

screenshot from 2018-03-10 21-00-32
it works well

Copy link
Contributor

Choose a reason for hiding this comment

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

it works but its not very useful. The point is to take a tz-aware, IOW some you previously used tz_localize on, or just constructed directly and the remove the tz.

How about you construct a new dti with a tz (IOW pass directly to date_range), then use .tz_localize(None) on 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.

@jreback both gives same result

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh K Will do that

@IHackPy
Copy link
Contributor Author

IHackPy commented Mar 10, 2018

@jreback i changes example as required

@IHackPy
Copy link
Contributor Author

IHackPy commented Mar 10, 2018

Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

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

Looks good, added some comments.

Also, in the extended summary, I'd avoid repeating the name of the method at the beginning of each parameter.

In the ambigous parameter, the accepted values should be in curly brackets (except bool-ndarray), and include 'raise'.

Btw, no need to add comments to ask for review when you push new changes. github notifies reviewers when you push.

@@ -1985,23 +1985,20 @@ def tz_localize(self, tz, ambiguous='raise', errors='raise'):
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


Out[5]: DatetimeIndex(['2018-03-01', '2018-03-02', '2018-03-03',
>>> 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

Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

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

Looking great, added couple of last comments.


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

'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

DatetimeIndex(['2018-03-01', '2018-03-02', '2018-03-03'],
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

DatetimeIndex(['2018-03-01', '2018-03-02', '2018-03-03'],
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.

Can you also remove the # at the beginning of the line?


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

Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

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

Almost there, looks good, just couple of comments for the Return and Examples sections.

@@ -1991,15 +1992,17 @@ def tz_localize(self, tz, ambiguous='raise', errors='raise'):
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.

@@ -1991,15 +1992,17 @@ def tz_localize(self, tz, ambiguous='raise', errors='raise'):
DatetimeIndex(['2018-03-01', '2018-03-02', '2018-03-03'],
dtype='datetime64[ns]', freq='D')

#localize DatetimeIndex in US?Eastern time zone.
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.

The explanations of the examples need improvements. The first paragraph is outdated (end date is not the 8th). And I don't think you need to say that, it's clear from the code. So I would just get rid of it.

In this case, it should start with a capital L. May be you can add here what you were explaining before, that dti is naive (and also what you're already saying, that you're localizing to US/Eastern). Personally I'd rename dti to tz_aware, so it's more obvious.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are saying this :

In [2]: tz_aware = pd.date_range('2018-03-01', '2018-03-03')

In [3]: tz_aware
Out[3]: DatetimeIndex(['2018-03-01', '2018-03-02', '2018-03-03'], dtype='datetime64[ns]', freq='D')

In [4]: tz_aware.tz_localize(tz='US/Eastern')
Out[4]:
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')

In [5]: tz_aware.tz_localize(None)
Out[5]: DatetimeIndex(['2018-03-01', '2018-03-02', '2018-03-03'], 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.

Almost. But in In [2] that's not a tz_aware index, it's a naive one. And in In [4] you do want to save the output, as that's the aware one that you'll one to convert to naive in In [5].

Make sure that the example makes sense for you too. Reviewers can also be wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but thats why we added comment before where we analyse aware and naive

Copy link
Member

Choose a reason for hiding this comment

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

I agree, but I think the variable names can still help by being descriptive, even if you explain what you're doing. Feel free to keep dti if you prefer it over tz_naive. But for me it's more clear to see what the example does, if the variables are more descriptive. All objects in the example could be dti, and it doesn't help much IMO, but naming one tz_naive and the other tz_aware, makes it clear what every object used has.

Sorry if I'm not being able to explain what I mean clearer enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I hope this commit define exactly what you want in example section

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry for disappointing you :(

>>> 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.
localize aware time zone into naive time zone.

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

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

Same as before.

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

"time zone aware index" instead of "aware time zone"?

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


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.

Returns
-------
localized : DatetimeIndex
DatetimeIndex
It returns naive time or aware time
Copy link
Member

Choose a reason for hiding this comment

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

I'd describe it more in terms of the functionality than the types. Something like "Index converted to the specified time zone." (The "It returns" seems redundant, and I think the spec requires a period at the end)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I copied your line "index converted to the specified time zone". because It gives perfect means

@datapythonista
Copy link
Member

The example looks much better now. Just added couple of minor comments.

Sorry if I made you feel you were disappointing anyone. You're doing a great job, discussions in reviews are a normal and very beneficial part of open source. As you get more experience probably the discussions will be a bit shorter. But we've all been there (and we often are), in these PRs that require several iterations to look great. :)

@datapythonista
Copy link
Member

lgtm

@IHackPy
Copy link
Contributor Author

IHackPy commented Mar 11, 2018

Thats why I love community thanks for supporting and guiding

@IHackPy
Copy link
Contributor Author

IHackPy commented Mar 11, 2018

Thanks alot :)

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Did some final edits, looks good!

@jorisvandenbossche jorisvandenbossche merged commit 6126ad6 into pandas-dev:master Mar 14, 2018
@jorisvandenbossche
Copy link
Member

@himanshuawasthi95 Thanks for the PR!

@IHackPy
Copy link
Contributor Author

IHackPy commented Mar 14, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Docs Timezones Timezone data dtype
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants