-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
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
Changes from 14 commits
9bd697d
ca813e6
a5a6a09
44b86cd
1fe3b34
b757763
fba3685
5e7b205
aca527e
8954ea8
67e2597
a791db1
99a846c
3fa0a2e
c81bd02
bdc839b
fa75bf5
85f6dbe
6205b61
95fcee2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would make this But that is probably too long, so you can do:
It is a bit ugly in the code, but will render OK in plain text docstring and in html docstring. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. errors : {'raise', 'coerce'}, default 'raise' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, in this case |
||
- 'infer' will attempt to infer fall dst-transition hours based on | ||
order | ||
- bool-ndarray where True signifies a DST time, False signifies a | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will add this again. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jorisvandenbossche why adding this back? looks leftover There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jreback @jorisvandenbossche I add this again or not ??????? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you just leave the type in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @datapythonista
|
||
tz-naive | ||
|
||
>>> dti = pd.date_range('2018-03-01', '2018-03-03') | ||
|
||
>>> dti | ||
DatetimeIndex(['2018-03-01', '2018-03-02', '2018-03-03'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
dtype='datetime64[ns]', freq='D') | ||
|
||
#localize DatetimeIndex in US?Eastern time zone. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo, ? instead of / in the time zone. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure |
||
>>> tz_naive = tz_aware.tz_localize(None) | ||
|
||
>>> tz_naive | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you're not doing anything with |
||
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. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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."