-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG/ENH: Handle AmbiguousTimeError in date rounding #22647
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 4 commits
0bc93b0
bba384d
a71afb9
88116bf
1c3d193
ef26311
71a0ddb
798e70c
ba7eddd
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 |
---|---|---|
|
@@ -478,6 +478,11 @@ class NaTType(_NaT): | |
Parameters | ||
---------- | ||
freq : a freq string indicating the rounding resolution | ||
ambiguous : bool, 'NaT', default 'raise' | ||
- bool contains flags to determine if time is dst or not (note | ||
that this flag is only applicable for ambiguous fall dst dates) | ||
- 'NaT' will return NaT for an ambiguous time | ||
- 'raise' will raise an AmbiguousTimeError for an ambiguous time | ||
|
||
Raises | ||
------ | ||
|
@@ -490,6 +495,15 @@ class NaTType(_NaT): | |
Parameters | ||
---------- | ||
freq : a freq string indicating the flooring resolution | ||
ambiguous : bool, 'NaT', default 'raise' | ||
- bool contains flags to determine if time is dst or not (note | ||
that this flag is only applicable for ambiguous fall dst dates) | ||
- 'NaT' will return NaT for an ambiguous time | ||
- 'raise' will raise an AmbiguousTimeError for an ambiguous time | ||
|
||
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. may make sense to provide a template for the doc-strings (can be followup) |
||
Raises | ||
------ | ||
ValueError if the freq cannot be converted | ||
""") | ||
ceil = _make_nat_func('ceil', # noqa:E128 | ||
""" | ||
|
@@ -498,6 +512,15 @@ class NaTType(_NaT): | |
Parameters | ||
---------- | ||
freq : a freq string indicating the ceiling resolution | ||
ambiguous : bool, 'NaT', default 'raise' | ||
- bool contains flags to determine if time is dst or not (note | ||
that this flag is only applicable for ambiguous fall dst dates) | ||
- 'NaT' will return NaT for an ambiguous time | ||
- 'raise' will raise an AmbiguousTimeError for an ambiguous time | ||
|
||
Raises | ||
------ | ||
ValueError if the freq cannot be converted | ||
""") | ||
|
||
tz_convert = _make_nat_func('tz_convert', # noqa:E128 | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -99,6 +99,16 @@ class TimelikeOps(object): | |||
frequency like 'S' (second) not 'ME' (month end). See | ||||
:ref:`frequency aliases <timeseries.offset_aliases>` for | ||||
a list of possible `freq` values. | ||||
ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise' | ||||
- 'infer' will attempt to infer fall dst-transition hours based on | ||||
order | ||||
- bool-ndarray where True signifies a DST time, False designates | ||||
a non-DST time (note that this flag is only applicable for | ||||
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. versionadded can you test for .dt accessors as well? 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. The .dt accessor tests are here:
|
||||
ambiguous times) | ||||
- 'NaT' will return NaT where there are ambiguous times | ||||
- 'raise' will raise an AmbiguousTimeError if there are ambiguous | ||||
times | ||||
Only relevant for DatetimeIndex | ||||
|
||||
Returns | ||||
------- | ||||
|
@@ -168,7 +178,7 @@ class TimelikeOps(object): | |||
""" | ||||
) | ||||
|
||||
def _round(self, freq, rounder): | ||||
def _round(self, freq, rounder, ambiguous): | ||||
# round the local times | ||||
values = _ensure_datetimelike_to_i8(self) | ||||
result = round_ns(values, rounder, freq) | ||||
|
@@ -180,19 +190,20 @@ def _round(self, freq, rounder): | |||
if 'tz' in attribs: | ||||
attribs['tz'] = None | ||||
return self._ensure_localized( | ||||
self._shallow_copy(result, **attribs)) | ||||
self._shallow_copy(result, **attribs), ambiguous | ||||
) | ||||
|
||||
@Appender((_round_doc + _round_example).format(op="round")) | ||||
def round(self, freq, *args, **kwargs): | ||||
return self._round(freq, np.round) | ||||
def round(self, freq, ambiguous='raise'): | ||||
return self._round(freq, np.round, ambiguous) | ||||
|
||||
@Appender((_round_doc + _floor_example).format(op="floor")) | ||||
def floor(self, freq): | ||||
return self._round(freq, np.floor) | ||||
def floor(self, freq, ambiguous='raise'): | ||||
return self._round(freq, np.floor, ambiguous) | ||||
|
||||
@Appender((_round_doc + _ceil_example).format(op="ceil")) | ||||
def ceil(self, freq): | ||||
return self._round(freq, np.ceil) | ||||
def ceil(self, freq, ambiguous='raise'): | ||||
return self._round(freq, np.ceil, ambiguous) | ||||
|
||||
|
||||
class DatetimeIndexOpsMixin(DatetimeLikeArrayMixin): | ||||
|
@@ -264,7 +275,7 @@ def _evaluate_compare(self, other, op): | |||
except TypeError: | ||||
return result | ||||
|
||||
def _ensure_localized(self, result): | ||||
def _ensure_localized(self, result, ambiguous='raise'): | ||||
""" | ||||
ensure that we are re-localized | ||||
|
||||
|
@@ -274,6 +285,8 @@ def _ensure_localized(self, result): | |||
Parameters | ||||
---------- | ||||
result : DatetimeIndex / i8 ndarray | ||||
ambiguous : str, bool, or bool-ndarray | ||||
default 'raise' | ||||
|
||||
Returns | ||||
------- | ||||
|
@@ -284,7 +297,7 @@ def _ensure_localized(self, result): | |||
if getattr(self, 'tz', None) is not None: | ||||
if not isinstance(result, ABCIndexClass): | ||||
result = self._simple_new(result) | ||||
result = result.tz_localize(self.tz) | ||||
result = result.tz_localize(self.tz, ambiguous=ambiguous) | ||||
return result | ||||
|
||||
def _box_values_as_index(self): | ||||
|
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.
can you add versionadded tags here (and other new arg places)