-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
REF/TST: Fix remaining DatetimeArray with DateOffset arithmetic ops #23789
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 1 commit
986fdbc
fd75931
66c866b
4dc17e2
da3459c
348a8b2
dd7e873
c8351bc
23a25d1
b4ae288
d1ebdbf
711ee61
9338b5b
5fbe9c8
c7db0e4
a4f9733
b50fedf
7e951e4
317e1e7
5de2d42
5433a71
dc137f3
2c65f3b
31c5c0b
c3d775e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,12 +134,11 @@ def _simple_new(cls, values, freq=None, dtype=_TD_DTYPE): | |
return result | ||
|
||
def __new__(cls, values, freq=None, dtype=_TD_DTYPE, copy=False): | ||
verify_integrity = True | ||
|
||
freq, freq_infer = dtl.maybe_infer_freq(freq) | ||
|
||
values, inferred_freq = sequence_to_td64ns(values, copy=copy, | ||
unit=None) | ||
values, inferred_freq = sequence_to_td64ns( | ||
values, copy=copy, unit=None) | ||
if inferred_freq is not None: | ||
if freq is not None and freq != inferred_freq: | ||
raise ValueError('Inferred frequency {inferred} from passed ' | ||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -150,11 +149,10 @@ def __new__(cls, values, freq=None, dtype=_TD_DTYPE, copy=False): | |
elif freq_infer: | ||
freq = inferred_freq | ||
freq_infer = False | ||
verify_integrity = False | ||
|
||
result = cls._simple_new(values, freq=freq) | ||
# check that we are matching freqs | ||
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 you could simplify a lot of these checks if also pass 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. ill take a look at this 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 you're right that we can change this from a 3-4 liner into a 1-2 liner. Since this pattern shows up in all four of TDA/DTA/TDI/DTI constructors (actually, future tense for DTA), I'd like to do change them all at once in a dedicated follow-up |
||
if verify_integrity and len(result) > 0: | ||
if inferred_freq is None and len(result) > 0: | ||
if freq is not None and not freq_infer: | ||
cls._validate_frequency(result, freq) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1125,10 +1125,25 @@ def test_dt64arr_add_sub_relativedelta_offsets(self, box_with_array): | |
'Easter', ('DateOffset', {'day': 4}), | ||
('DateOffset', {'month': 5})]) | ||
@pytest.mark.parametrize('normalize', [True, False]) | ||
@pytest.mark.parametrize('n', [0, 5]) | ||
def test_dt64arr_add_sub_DateOffsets(self, box_with_array, | ||
normalize, cls_and_kwargs): | ||
n, normalize, cls_and_kwargs): | ||
# GH#10699 | ||
# assert these are equal on a piecewise basis | ||
# assert vectorized operation matches pointwise operations | ||
|
||
if isinstance(cls_and_kwargs, tuple): | ||
# If cls_name param is a tuple, then 2nd entry is kwargs for | ||
# the offset constructor | ||
cls_name, kwargs = cls_and_kwargs | ||
else: | ||
cls_name = cls_and_kwargs | ||
kwargs = {} | ||
|
||
if n == 0 and cls_name in ['WeekOfMonth', 'LastWeekOfMonth', | ||
'FY5253Quarter', 'FY5253']: | ||
# passing n = 0 is invalid for these offset classes | ||
return | ||
|
||
vec = DatetimeIndex([Timestamp('2000-01-05 00:15:00'), | ||
Timestamp('2000-01-31 00:23:00'), | ||
Timestamp('2000-01-01'), | ||
|
@@ -1140,43 +1155,30 @@ def test_dt64arr_add_sub_DateOffsets(self, box_with_array, | |
vec = tm.box_expected(vec, box_with_array) | ||
vec_items = vec.squeeze() if box_with_array is pd.DataFrame else vec | ||
|
||
if isinstance(cls_and_kwargs, tuple): | ||
# If cls_name param is a tuple, then 2nd entry is kwargs for | ||
# the offset constructor | ||
cls_name, kwargs = cls_and_kwargs | ||
else: | ||
cls_name = cls_and_kwargs | ||
kwargs = {} | ||
|
||
offset_cls = getattr(pd.offsets, cls_name) | ||
|
||
with warnings.catch_warnings(record=True): | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
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 don't think the |
||
# pandas.errors.PerformanceWarning: Non-vectorized DateOffset being | ||
# applied to Series or DatetimeIndex | ||
# we aren't testing that here, so ignore. | ||
warnings.simplefilter("ignore", PerformanceWarning) | ||
for n in [0, 5]: | ||
if (cls_name in ['WeekOfMonth', 'LastWeekOfMonth', | ||
'FY5253Quarter', 'FY5253'] and n == 0): | ||
# passing n = 0 is invalid for these offset classes | ||
continue | ||
|
||
offset = offset_cls(n, normalize=normalize, **kwargs) | ||
offset = offset_cls(n, normalize=normalize, **kwargs) | ||
|
||
expected = DatetimeIndex([x + offset for x in vec_items]) | ||
expected = tm.box_expected(expected, box_with_array) | ||
tm.assert_equal(expected, vec + offset) | ||
expected = DatetimeIndex([x + offset for x in vec_items]) | ||
expected = tm.box_expected(expected, box_with_array) | ||
tm.assert_equal(expected, vec + offset) | ||
|
||
expected = DatetimeIndex([x - offset for x in vec_items]) | ||
expected = tm.box_expected(expected, box_with_array) | ||
tm.assert_equal(expected, vec - offset) | ||
expected = DatetimeIndex([x - offset for x in vec_items]) | ||
expected = tm.box_expected(expected, box_with_array) | ||
tm.assert_equal(expected, vec - offset) | ||
|
||
expected = DatetimeIndex([offset + x for x in vec_items]) | ||
expected = tm.box_expected(expected, box_with_array) | ||
tm.assert_equal(expected, offset + vec) | ||
expected = DatetimeIndex([offset + x for x in vec_items]) | ||
expected = tm.box_expected(expected, box_with_array) | ||
tm.assert_equal(expected, offset + vec) | ||
|
||
with pytest.raises(TypeError): | ||
offset - vec | ||
with pytest.raises(TypeError): | ||
offset - vec | ||
|
||
def test_dt64arr_add_sub_DateOffset(self, box_with_array): | ||
# GH#10699 | ||
|
Uh oh!
There was an error while loading. Please reload this page.