Skip to content

Commit e883fb9

Browse files
jbrockmendeljreback
authored andcommitted
CLN: remove keep_null_freq in ops (#30221)
1 parent 021fa9e commit e883fb9

File tree

8 files changed

+63
-100
lines changed

8 files changed

+63
-100
lines changed

pandas/core/ops/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,8 @@ def wrapper(left, right):
462462
res_name = get_op_result_name(left, right)
463463

464464
lvalues = extract_array(left, extract_numpy=True)
465-
result = arithmetic_op(lvalues, right, op, str_rep)
465+
rvalues = extract_array(right, extract_numpy=True)
466+
result = arithmetic_op(lvalues, rvalues, op, str_rep)
466467

467468
return _construct_result(left, result, index=left.index, name=res_name)
468469

pandas/core/ops/array_ops.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,14 @@
2424
)
2525
from pandas.core.dtypes.generic import (
2626
ABCDatetimeArray,
27-
ABCDatetimeIndex,
2827
ABCExtensionArray,
2928
ABCIndex,
3029
ABCIndexClass,
3130
ABCSeries,
3231
ABCTimedeltaArray,
33-
ABCTimedeltaIndex,
3432
)
3533
from pandas.core.dtypes.missing import isna, notna
3634

37-
from pandas.core.construction import extract_array
3835
from pandas.core.ops import missing
3936
from pandas.core.ops.dispatch import dispatch_to_extension_op, should_extension_dispatch
4037
from pandas.core.ops.invalid import invalid_comparison
@@ -178,22 +175,10 @@ def arithmetic_op(
178175

179176
from pandas.core.ops import maybe_upcast_for_op
180177

181-
keep_null_freq = isinstance(
182-
right,
183-
(
184-
ABCDatetimeIndex,
185-
ABCDatetimeArray,
186-
ABCTimedeltaIndex,
187-
ABCTimedeltaArray,
188-
Timestamp,
189-
),
190-
)
191-
192-
# NB: We assume that extract_array has already been called on `left`, but
193-
# cannot make the same assumption about `right`. This is because we need
194-
# to define `keep_null_freq` before calling extract_array on it.
178+
# NB: We assume that extract_array has already been called
179+
# on `left` and `right`.
195180
lvalues = left
196-
rvalues = extract_array(right, extract_numpy=True)
181+
rvalues = right
197182

198183
rvalues = maybe_upcast_for_op(rvalues, lvalues.shape)
199184

@@ -203,7 +188,7 @@ def arithmetic_op(
203188
# TimedeltaArray, DatetimeArray, and Timestamp are included here
204189
# because they have `freq` attribute which is handled correctly
205190
# by dispatch_to_extension_op.
206-
res_values = dispatch_to_extension_op(op, lvalues, rvalues, keep_null_freq)
191+
res_values = dispatch_to_extension_op(op, lvalues, rvalues)
207192

208193
else:
209194
with np.errstate(all="ignore"):

pandas/core/ops/dispatch.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
import numpy as np
77

8-
from pandas.errors import NullFrequencyError
9-
108
from pandas.core.dtypes.common import (
119
is_datetime64_dtype,
1210
is_extension_array_dtype,
@@ -97,10 +95,7 @@ def should_series_dispatch(left, right, op):
9795

9896

9997
def dispatch_to_extension_op(
100-
op,
101-
left: Union[ABCExtensionArray, np.ndarray],
102-
right: Any,
103-
keep_null_freq: bool = False,
98+
op, left: Union[ABCExtensionArray, np.ndarray], right: Any,
10499
):
105100
"""
106101
Assume that left or right is a Series backed by an ExtensionArray,
@@ -111,9 +106,6 @@ def dispatch_to_extension_op(
111106
op : binary operator
112107
left : ExtensionArray or np.ndarray
113108
right : object
114-
keep_null_freq : bool, default False
115-
Whether to re-raise a NullFrequencyError unchanged, as opposed to
116-
catching and raising TypeError.
117109
118110
Returns
119111
-------
@@ -131,20 +123,7 @@ def dispatch_to_extension_op(
131123

132124
# The op calls will raise TypeError if the op is not defined
133125
# on the ExtensionArray
134-
135-
try:
136-
res_values = op(left, right)
137-
except NullFrequencyError:
138-
# DatetimeIndex and TimedeltaIndex with freq == None raise ValueError
139-
# on add/sub of integers (or int-like). We re-raise as a TypeError.
140-
if keep_null_freq:
141-
# TODO: remove keep_null_freq after Timestamp+int deprecation
142-
# GH#22535 is enforced
143-
raise
144-
raise TypeError(
145-
"incompatible type for a datetime/timedelta "
146-
"operation [{name}]".format(name=op.__name__)
147-
)
126+
res_values = op(left, right)
148127
return res_values
149128

150129

pandas/tests/arithmetic/test_datetime64.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,6 +1844,7 @@ def test_dt64_mul_div_numeric_invalid(self, one, dt64_series):
18441844
with pytest.raises(TypeError, match=msg):
18451845
one / dt64_series
18461846

1847+
# TODO: parametrize over box
18471848
@pytest.mark.parametrize("op", ["__add__", "__radd__", "__sub__", "__rsub__"])
18481849
@pytest.mark.parametrize("tz", [None, "Asia/Tokyo"])
18491850
def test_dt64_series_add_intlike(self, tz, op):

pandas/tests/arithmetic/test_timedelta64.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,58 @@ def test_td64arr_add_sub_numeric_arr_invalid(self, box_with_array, vec, dtype):
10461046
with pytest.raises(TypeError):
10471047
vector - tdser
10481048

1049+
# TODO: parameterize over box and de-duplicate
1050+
def test_tdi_add_sub_int(self, one):
1051+
# Variants of `one` for #19012, deprecated GH#22535
1052+
rng = timedelta_range("1 days 09:00:00", freq="H", periods=10)
1053+
msg = "Addition/subtraction of integers"
1054+
1055+
with pytest.raises(TypeError, match=msg):
1056+
rng + one
1057+
with pytest.raises(TypeError, match=msg):
1058+
rng += one
1059+
with pytest.raises(TypeError, match=msg):
1060+
rng - one
1061+
with pytest.raises(TypeError, match=msg):
1062+
rng -= one
1063+
1064+
# TODO: parameterize over box and de-duplicate
1065+
@pytest.mark.parametrize("box", [np.array, pd.Index])
1066+
def test_tdi_add_sub_integer_array(self, box):
1067+
# GH#19959, deprecated GH#22535
1068+
rng = timedelta_range("1 days 09:00:00", freq="H", periods=3)
1069+
other = box([4, 3, 2])
1070+
msg = "Addition/subtraction of integers and integer-arrays"
1071+
1072+
with pytest.raises(TypeError, match=msg):
1073+
rng + other
1074+
1075+
with pytest.raises(TypeError, match=msg):
1076+
other + rng
1077+
1078+
with pytest.raises(TypeError, match=msg):
1079+
rng - other
1080+
1081+
with pytest.raises(TypeError, match=msg):
1082+
other - rng
1083+
1084+
# TODO: parameterize over box and de-duplicate
1085+
@pytest.mark.parametrize("box", [np.array, pd.Index])
1086+
def test_tdi_addsub_integer_array_no_freq(self, box):
1087+
# GH#19959
1088+
tdi = TimedeltaIndex(["1 Day", "NaT", "3 Hours"])
1089+
other = box([14, -1, 16])
1090+
msg = "Addition/subtraction of integers"
1091+
1092+
with pytest.raises(TypeError, match=msg):
1093+
tdi + other
1094+
with pytest.raises(TypeError, match=msg):
1095+
other + tdi
1096+
with pytest.raises(TypeError, match=msg):
1097+
tdi - other
1098+
with pytest.raises(TypeError, match=msg):
1099+
other - tdi
1100+
10491101
# ------------------------------------------------------------------
10501102
# Operations with timedelta-like others
10511103

pandas/tests/indexes/datetimes/test_arithmetic.py renamed to pandas/tests/indexes/datetimes/test_shift.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import pandas.util.testing as tm
1111

1212

13-
class TestDatetimeIndexArithmetic:
13+
class TestDatetimeIndexShift:
1414

1515
# -------------------------------------------------------------
1616
# DatetimeIndex.shift is used in integer addition

pandas/tests/indexes/period/test_arithmetic.py renamed to pandas/tests/indexes/period/test_shift.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pandas.util.testing as tm
77

88

9-
class TestPeriodIndexArithmetic:
9+
class TestPeriodIndexShift:
1010
# ---------------------------------------------------------------
1111
# PeriodIndex.shift is used by __add__ and __sub__
1212

pandas/tests/indexes/timedeltas/test_arithmetic.py

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -97,61 +97,6 @@ def test_shift_no_freq(self):
9797
with pytest.raises(NullFrequencyError):
9898
tdi.shift(2)
9999

100-
# -------------------------------------------------------------
101-
# Binary operations TimedeltaIndex and integer
102-
103-
def test_tdi_add_sub_int(self, one):
104-
# Variants of `one` for #19012, deprecated GH#22535
105-
rng = timedelta_range("1 days 09:00:00", freq="H", periods=10)
106-
msg = "Addition/subtraction of integers"
107-
108-
with pytest.raises(TypeError, match=msg):
109-
rng + one
110-
with pytest.raises(TypeError, match=msg):
111-
rng += one
112-
with pytest.raises(TypeError, match=msg):
113-
rng - one
114-
with pytest.raises(TypeError, match=msg):
115-
rng -= one
116-
117-
# -------------------------------------------------------------
118-
# __add__/__sub__ with integer arrays
119-
120-
@pytest.mark.parametrize("box", [np.array, pd.Index])
121-
def test_tdi_add_sub_integer_array(self, box):
122-
# GH#19959, deprecated GH#22535
123-
rng = timedelta_range("1 days 09:00:00", freq="H", periods=3)
124-
other = box([4, 3, 2])
125-
msg = "Addition/subtraction of integers and integer-arrays"
126-
127-
with pytest.raises(TypeError, match=msg):
128-
rng + other
129-
130-
with pytest.raises(TypeError, match=msg):
131-
other + rng
132-
133-
with pytest.raises(TypeError, match=msg):
134-
rng - other
135-
136-
with pytest.raises(TypeError, match=msg):
137-
other - rng
138-
139-
@pytest.mark.parametrize("box", [np.array, pd.Index])
140-
def test_tdi_addsub_integer_array_no_freq(self, box):
141-
# GH#19959
142-
tdi = TimedeltaIndex(["1 Day", "NaT", "3 Hours"])
143-
other = box([14, -1, 16])
144-
msg = "Addition/subtraction of integers"
145-
146-
with pytest.raises(TypeError, match=msg):
147-
tdi + other
148-
with pytest.raises(TypeError, match=msg):
149-
other + tdi
150-
with pytest.raises(TypeError, match=msg):
151-
tdi - other
152-
with pytest.raises(TypeError, match=msg):
153-
other - tdi
154-
155100
# -------------------------------------------------------------
156101
# Binary operations TimedeltaIndex and timedelta-like
157102
# Note: add and sub are tested in tests.test_arithmetic, in-place

0 commit comments

Comments
 (0)