Skip to content

Commit d32d464

Browse files
jbrockmendeljreback
authored andcommitted
CLN: remove unnecessary dtype checks (#27889)
1 parent de53f6e commit d32d464

File tree

4 files changed

+25
-47
lines changed

4 files changed

+25
-47
lines changed

pandas/core/arrays/categorical.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import operator
12
from shutil import get_terminal_size
23
import textwrap
34
from typing import Type, Union, cast
@@ -77,7 +78,9 @@
7778
)
7879

7980

80-
def _cat_compare_op(opname):
81+
def _cat_compare_op(op):
82+
opname = "__{op}__".format(op=op.__name__)
83+
8184
def f(self, other):
8285
# On python2, you can usually compare any type to any type, and
8386
# Categoricals can be seen as a custom type, but having different
@@ -1243,12 +1246,12 @@ def map(self, mapper):
12431246
new_categories = new_categories.insert(len(new_categories), np.nan)
12441247
return np.take(new_categories, self._codes)
12451248

1246-
__eq__ = _cat_compare_op("__eq__")
1247-
__ne__ = _cat_compare_op("__ne__")
1248-
__lt__ = _cat_compare_op("__lt__")
1249-
__gt__ = _cat_compare_op("__gt__")
1250-
__le__ = _cat_compare_op("__le__")
1251-
__ge__ = _cat_compare_op("__ge__")
1249+
__eq__ = _cat_compare_op(operator.eq)
1250+
__ne__ = _cat_compare_op(operator.ne)
1251+
__lt__ = _cat_compare_op(operator.lt)
1252+
__gt__ = _cat_compare_op(operator.gt)
1253+
__le__ = _cat_compare_op(operator.le)
1254+
__ge__ = _cat_compare_op(operator.ge)
12521255

12531256
# for Series/ndarray like compat
12541257
@property

pandas/core/arrays/datetimelike.py

+8-37
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
is_datetime64tz_dtype,
2323
is_datetime_or_timedelta_dtype,
2424
is_dtype_equal,
25-
is_extension_array_dtype,
2625
is_float_dtype,
2726
is_integer_dtype,
2827
is_list_like,
@@ -1230,29 +1229,17 @@ def __add__(self, other):
12301229
if not is_period_dtype(self):
12311230
maybe_integer_op_deprecated(self)
12321231
result = self._addsub_int_array(other, operator.add)
1233-
elif is_float_dtype(other):
1234-
# Explicitly catch invalid dtypes
1235-
raise TypeError(
1236-
"cannot add {dtype}-dtype to {cls}".format(
1237-
dtype=other.dtype, cls=type(self).__name__
1238-
)
1239-
)
1240-
elif is_period_dtype(other):
1241-
# if self is a TimedeltaArray and other is a PeriodArray with
1242-
# a timedelta-like (i.e. Tick) freq, this operation is valid.
1243-
# Defer to the PeriodArray implementation.
1244-
# In remaining cases, this will end up raising TypeError.
1245-
return NotImplemented
1246-
elif is_extension_array_dtype(other):
1247-
# Categorical op will raise; defer explicitly
1248-
return NotImplemented
1249-
else: # pragma: no cover
1232+
else:
1233+
# Includes Categorical, other ExtensionArrays
1234+
# For PeriodDtype, if self is a TimedeltaArray and other is a
1235+
# PeriodArray with a timedelta-like (i.e. Tick) freq, this
1236+
# operation is valid. Defer to the PeriodArray implementation.
1237+
# In remaining cases, this will end up raising TypeError.
12501238
return NotImplemented
12511239

12521240
if is_timedelta64_dtype(result) and isinstance(result, np.ndarray):
12531241
from pandas.core.arrays import TimedeltaArray
12541242

1255-
# TODO: infer freq?
12561243
return TimedeltaArray(result)
12571244
return result
12581245

@@ -1302,29 +1289,13 @@ def __sub__(self, other):
13021289
if not is_period_dtype(self):
13031290
maybe_integer_op_deprecated(self)
13041291
result = self._addsub_int_array(other, operator.sub)
1305-
elif isinstance(other, ABCIndexClass):
1306-
raise TypeError(
1307-
"cannot subtract {cls} and {typ}".format(
1308-
cls=type(self).__name__, typ=type(other).__name__
1309-
)
1310-
)
1311-
elif is_float_dtype(other):
1312-
# Explicitly catch invalid dtypes
1313-
raise TypeError(
1314-
"cannot subtract {dtype}-dtype from {cls}".format(
1315-
dtype=other.dtype, cls=type(self).__name__
1316-
)
1317-
)
1318-
elif is_extension_array_dtype(other):
1319-
# Categorical op will raise; defer explicitly
1320-
return NotImplemented
1321-
else: # pragma: no cover
1292+
else:
1293+
# Includes ExtensionArrays, float_dtype
13221294
return NotImplemented
13231295

13241296
if is_timedelta64_dtype(result) and isinstance(result, np.ndarray):
13251297
from pandas.core.arrays import TimedeltaArray
13261298

1327-
# TODO: infer freq?
13281299
return TimedeltaArray(result)
13291300
return result
13301301

pandas/core/arrays/datetimes.py

-2
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ def wrapper(self, other):
223223
result = op(self.view("i8"), other.view("i8"))
224224
o_mask = other._isnan
225225

226-
result = com.values_from_object(result)
227-
228226
if o_mask.any():
229227
result[o_mask] = nat_result
230228

pandas/tests/arithmetic/test_datetime64.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,13 @@ def test_dt64arr_add_timestamp_raises(self, box_with_array):
10971097
def test_dt64arr_add_sub_float(self, other, box_with_array):
10981098
dti = DatetimeIndex(["2011-01-01", "2011-01-02"], freq="D")
10991099
dtarr = tm.box_expected(dti, box_with_array)
1100-
msg = "|".join(["unsupported operand type", "cannot (add|subtract)"])
1100+
msg = "|".join(
1101+
[
1102+
"unsupported operand type",
1103+
"cannot (add|subtract)",
1104+
"ufunc '?(add|subtract)'? cannot use operands with types",
1105+
]
1106+
)
11011107
with pytest.raises(TypeError, match=msg):
11021108
dtarr + other
11031109
with pytest.raises(TypeError, match=msg):

0 commit comments

Comments
 (0)