-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Fix+test timedelta64(nat) ops #23425
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 all commits
fddcb14
57ec46a
f28f639
8b5bf2c
ed82665
6d3d38d
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 |
---|---|---|
|
@@ -35,7 +35,7 @@ | |
from pandas.core.dtypes.generic import ( | ||
ABCSeries, ABCIndexClass, ABCPeriodIndex | ||
) | ||
from pandas.core.dtypes.missing import isna | ||
from pandas.core.dtypes.missing import isna, notna | ||
from pandas.core.missing import pad_1d, backfill_1d | ||
|
||
import pandas.core.common as com | ||
|
@@ -149,6 +149,8 @@ class PeriodArray(dtl.DatetimeLikeArrayMixin, ExtensionArray): | |
period_array : Create a new PeriodArray | ||
pandas.PeriodIndex : Immutable Index for period data | ||
""" | ||
# array priority higher than numpy scalars | ||
__array_priority__ = 1000 | ||
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. is there a test that needs this? I agree should set 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. Yes. IIRC it was timedelta64 - TimedeltaIndex |
||
_attributes = ["freq"] | ||
_typ = "periodarray" # ABCPeriodArray | ||
|
||
|
@@ -761,12 +763,15 @@ def _add_timedeltalike_scalar(self, other): | |
assert isinstance(self.freq, Tick) # checked by calling function | ||
assert isinstance(other, (timedelta, np.timedelta64, Tick)) | ||
|
||
delta = self._check_timedeltalike_freq_compat(other) | ||
if notna(other): | ||
# special handling for np.timedelta64("NaT"), avoid calling | ||
# _check_timedeltalike_freq_compat as that would raise TypeError | ||
other = self._check_timedeltalike_freq_compat(other) | ||
|
||
# Note: when calling parent class's _add_timedeltalike_scalar, | ||
# it will call delta_to_nanoseconds(delta). Because delta here | ||
# is an integer, delta_to_nanoseconds will return it unchanged. | ||
ordinals = super(PeriodArray, self)._add_timedeltalike_scalar(delta) | ||
ordinals = super(PeriodArray, self)._add_timedeltalike_scalar(other) | ||
return ordinals | ||
|
||
def _add_delta_tdi(self, other): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1188,6 +1188,25 @@ def test_dti_isub_timedeltalike(self, tz_naive_fixture, two_hours): | |
rng -= two_hours | ||
tm.assert_index_equal(rng, expected) | ||
|
||
def test_dt64arr_add_sub_td64_nat(self, box, tz_naive_fixture): | ||
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 parameterize other with pd.NaT as well here (I bet we already have a test for that, can you consolidate) 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. Not really; pd.NaT behavior is pretty starkly different from the behavior we’re testing here. That said, there are several rounds of de duplication and parameterization coming up 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. how is it different at all? 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. Because this is a timedelta64 and pd.NaT is in most cases datetime-like. The "most cases" part makes it especially hairy. |
||
# GH#23320 special handling for timedelta64("NaT") | ||
tz = tz_naive_fixture | ||
dti = pd.date_range("1994-04-01", periods=9, tz=tz, freq="QS") | ||
other = np.timedelta64("NaT") | ||
expected = pd.DatetimeIndex(["NaT"] * 9, tz=tz) | ||
|
||
obj = tm.box_expected(dti, box) | ||
expected = tm.box_expected(expected, box) | ||
|
||
result = obj + other | ||
tm.assert_equal(result, expected) | ||
result = other + obj | ||
tm.assert_equal(result, expected) | ||
result = obj - other | ||
tm.assert_equal(result, expected) | ||
with pytest.raises(TypeError): | ||
other - obj | ||
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 see a lot of error message changes. Are they tested anywhere with 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 way these tests are parametrized, the messages are all over the place. |
||
|
||
# ------------------------------------------------------------- | ||
# Binary operations DatetimeIndex and TimedeltaIndex/array | ||
def test_dti_add_tdi(self, tz_naive_fixture): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -419,7 +419,7 @@ def test_pi_add_sub_td64_array_non_tick_raises(self): | |
|
||
with pytest.raises(period.IncompatibleFrequency): | ||
rng - tdarr | ||
with pytest.raises(period.IncompatibleFrequency): | ||
with pytest.raises(TypeError): | ||
tdarr - rng | ||
|
||
def test_pi_add_sub_td64_array_tick(self): | ||
|
@@ -801,6 +801,24 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_monthly(self, | |
with tm.assert_raises_regex(period.IncompatibleFrequency, msg): | ||
rng -= other | ||
|
||
def test_parr_add_sub_td64_nat(self, box): | ||
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. same as above |
||
# GH#23320 special handling for timedelta64("NaT") | ||
pi = pd.period_range("1994-04-01", periods=9, freq="19D") | ||
other = np.timedelta64("NaT") | ||
expected = pd.PeriodIndex(["NaT"] * 9, freq="19D") | ||
|
||
obj = tm.box_expected(pi, box) | ||
expected = tm.box_expected(expected, box) | ||
|
||
result = obj + other | ||
tm.assert_equal(result, expected) | ||
result = other + obj | ||
tm.assert_equal(result, expected) | ||
result = obj - other | ||
tm.assert_equal(result, expected) | ||
with pytest.raises(TypeError): | ||
other - obj | ||
|
||
|
||
class TestPeriodSeriesArithmetic(object): | ||
def test_ops_series_timedelta(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -735,6 +735,24 @@ def test_td64arr_add_sub_tdi(self, box_df_broadcast_failure, names): | |
else: | ||
assert result.dtypes[0] == 'timedelta64[ns]' | ||
|
||
def test_td64arr_add_sub_td64_nat(self, box): | ||
# GH#23320 special handling for timedelta64("NaT") | ||
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. same |
||
tdi = pd.TimedeltaIndex([NaT, Timedelta('1s')]) | ||
other = np.timedelta64("NaT") | ||
expected = pd.TimedeltaIndex(["NaT"] * 2) | ||
|
||
obj = tm.box_expected(tdi, box) | ||
expected = tm.box_expected(expected, box) | ||
|
||
result = obj + other | ||
tm.assert_equal(result, expected) | ||
result = other + obj | ||
tm.assert_equal(result, expected) | ||
result = obj - other | ||
tm.assert_equal(result, expected) | ||
result = other - obj | ||
tm.assert_equal(result, expected) | ||
|
||
def test_td64arr_sub_NaT(self, box): | ||
# GH#18808 | ||
ser = Series([NaT, Timedelta('1s')]) | ||
|
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.
isn't this pretty much what _nat_new did? I find this is repeating lots of code, why don't you make a method to create the null array instead.
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.
_nat_new
also did casting+boxing that this doesn't. These two lines aren't repeated often enough to merit a method.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.
ok