-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
ENH: Add additional options to nonexistent in tz_localize #24493
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
Conversation
pandas/_libs/tslibs/conversion.pyx
Outdated
shift_forward = True | ||
elif nonexistent == 'shift_backward': | ||
shift_backward = True | ||
elif isinstance(nonexistent, timedelta): |
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.
PyDelta_Check(nonexistent)
- 'NaT' will return NaT where there are nonexistent times | ||
- timedelta objects will shift nonexistent times by the timedelta |
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.
IIRC cython doesn't play nicely with docstring templating; might be worth opening an issue over there to request it
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.
looks good. can you add an Example or 2 somewhere in a doc-string.
Codecov Report
@@ Coverage Diff @@
## master #24493 +/- ##
===========================================
- Coverage 92.31% 43.06% -49.26%
===========================================
Files 166 166
Lines 52412 52415 +3
===========================================
- Hits 48382 22570 -25812
- Misses 4030 29845 +25815
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #24493 +/- ##
==========================================
+ Coverage 92.33% 92.33% +<.01%
==========================================
Files 166 166
Lines 52544 52547 +3
==========================================
+ Hits 48517 48520 +3
Misses 4027 4027
Continue to review full report at Codecov.
|
shift_forward = True | ||
elif nonexistent == 'shift_backward': | ||
shift_backward = True | ||
elif PyDelta_Check(nonexistent): |
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.
is there a practical use of this?
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.
From the original enhancement request #24466 (comment), there was no way to shift backwards, moreover by a specified amount. I can see how it would be valuable to shift nonexistent times forward or backwards by a specified amount instead of the closest time. (e.g. I want times on the half hours, so be able to shift times to 1:30 or 3:30 and not just 1:59 or 3:00)
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.
is this duplicating shift_backwards though? this seems unecessarily complex to do anything more that doing a snap, eg. to a valid time (forward or backwards). shifting is an independent operation.
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.
IMO it didn't add too much more complexity to the code, but here was @sdementen use case:
I do some calculation on a local timestamp without tz, that I shift by 2 hours backward (to say "take it two hours before") and then I localize and get a NonExistentTimeError (e.g. Timestamp("2018-03-25T04:33:00") - DateOffset(hours=2)). I would like to get as a result of the tz_localize('CET'), the time "2018-03-25T01:33:00+0100" or "2018-03-25T03:33:00+0200" (and not "2018-03-25T01:59:59.99999+0100" or "2018-03-25T03:00:00+0200")
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 can see how in principle how it's very similar to shift_forward
/ shift_backwards
then adding an offset, but it can be easier determining how you want to shift from a nonexistent time compared to a nonexistent time that has been snapped to 1:59 or 3:00.
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.
@sdementen care to chime in on thoughts?
thanks @mroeschke |
can you merge master |
thanks @mroeschke |
git diff upstream/master -u -- "*.py" | flake8 --diff
Per @sdementen suggestion, adding additional options for
nonexistent
that would prove more useful. Let me know if this is what you had in mind.shift
is nowshift_forward
: shift nonexistent times forward to the closest real timeshift_backward
: shift nonexistent times backwards to the closest real timetimedelta object: shifts nonexistent times by the timedelta duration