Skip to content

CLN: remove unnecessary dtype checks #27889

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

Merged
merged 7 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import operator
from shutil import get_terminal_size
import textwrap
from typing import Type, Union, cast
Expand Down Expand Up @@ -77,7 +78,9 @@
)


def _cat_compare_op(opname):
def _cat_compare_op(op):
opname = "__{op}__".format(op=op.__name__)

def f(self, other):
# On python2, you can usually compare any type to any type, and
# Categoricals can be seen as a custom type, but having different
Expand Down Expand Up @@ -1240,12 +1243,12 @@ def map(self, mapper):
new_categories = new_categories.insert(len(new_categories), np.nan)
return np.take(new_categories, self._codes)

__eq__ = _cat_compare_op("__eq__")
__ne__ = _cat_compare_op("__ne__")
__lt__ = _cat_compare_op("__lt__")
__gt__ = _cat_compare_op("__gt__")
__le__ = _cat_compare_op("__le__")
__ge__ = _cat_compare_op("__ge__")
__eq__ = _cat_compare_op(operator.eq)
__ne__ = _cat_compare_op(operator.ne)
__lt__ = _cat_compare_op(operator.lt)
__gt__ = _cat_compare_op(operator.gt)
__le__ = _cat_compare_op(operator.le)
__ge__ = _cat_compare_op(operator.ge)

# for Series/ndarray like compat
@property
Expand Down
45 changes: 8 additions & 37 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
is_datetime64tz_dtype,
is_datetime_or_timedelta_dtype,
is_dtype_equal,
is_extension_array_dtype,
is_float_dtype,
is_integer_dtype,
is_list_like,
Expand Down Expand Up @@ -1230,29 +1229,17 @@ def __add__(self, other):
if not is_period_dtype(self):
maybe_integer_op_deprecated(self)
result = self._addsub_int_array(other, operator.add)
elif is_float_dtype(other):
# Explicitly catch invalid dtypes
raise TypeError(
"cannot add {dtype}-dtype to {cls}".format(
dtype=other.dtype, cls=type(self).__name__
)
)
elif is_period_dtype(other):
# if self is a TimedeltaArray and other is a PeriodArray with
# a timedelta-like (i.e. Tick) freq, this operation is valid.
# Defer to the PeriodArray implementation.
# In remaining cases, this will end up raising TypeError.
return NotImplemented
elif is_extension_array_dtype(other):
# Categorical op will raise; defer explicitly
return NotImplemented
else: # pragma: no cover
else:
# Includes Categorical, other ExtensionArrays
# For PeriodDtype, if self is a TimedeltaArray and other is a
# PeriodArray with a timedelta-like (i.e. Tick) freq, this
# operation is valid. Defer to the PeriodArray implementation.
# In remaining cases, this will end up raising TypeError.
return NotImplemented

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

# TODO: infer freq?
return TimedeltaArray(result)
return result

Expand Down Expand Up @@ -1302,29 +1289,13 @@ def __sub__(self, other):
if not is_period_dtype(self):
maybe_integer_op_deprecated(self)
result = self._addsub_int_array(other, operator.sub)
elif isinstance(other, ABCIndexClass):
raise TypeError(
"cannot subtract {cls} and {typ}".format(
cls=type(self).__name__, typ=type(other).__name__
)
)
elif is_float_dtype(other):
# Explicitly catch invalid dtypes
raise TypeError(
"cannot subtract {dtype}-dtype from {cls}".format(
dtype=other.dtype, cls=type(self).__name__
)
)
elif is_extension_array_dtype(other):
# Categorical op will raise; defer explicitly
return NotImplemented
else: # pragma: no cover
else:
# Includes ExtensionArrays, float_dtype
return NotImplemented

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

# TODO: infer freq?
return TimedeltaArray(result)
return result

Expand Down
2 changes: 0 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,6 @@ def wrapper(self, other):
result = op(self.view("i8"), other.view("i8"))
o_mask = other._isnan

result = com.values_from_object(result)

if o_mask.any():
result[o_mask] = nat_result

Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,13 @@ def test_dt64arr_add_timestamp_raises(self, box_with_array):
def test_dt64arr_add_sub_float(self, other, box_with_array):
dti = DatetimeIndex(["2011-01-01", "2011-01-02"], freq="D")
dtarr = tm.box_expected(dti, box_with_array)
msg = "|".join(["unsupported operand type", "cannot (add|subtract)"])
msg = "|".join(
[
"unsupported operand type",
"cannot (add|subtract)",
"ufunc '?(add|subtract)'? cannot use operands with types",
]
)
with pytest.raises(TypeError, match=msg):
dtarr + other
with pytest.raises(TypeError, match=msg):
Expand Down