Skip to content

REF: remove replace_list kludge #33445

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 8 commits into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ Timedelta
- Bug in dividing ``np.nan`` or ``None`` by :class:`Timedelta`` incorrectly returning ``NaT`` (:issue:`31869`)
- Timedeltas now understand ``µs`` as identifier for microsecond (:issue:`32899`)
- :class:`Timedelta` string representation now includes nanoseconds, when nanoseconds are non-zero (:issue:`9309`)
- Bug in comparing a :class:`Timedelta`` object against a ``np.ndarray`` with ``timedelta64`` dtype incorrectly viewing all entries as unequal (:issue:`33441`)

Timezones
^^^^^^^^^
Expand Down
52 changes: 24 additions & 28 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -778,36 +778,32 @@ cdef class _Timedelta(timedelta):

if isinstance(other, _Timedelta):
ots = other
elif PyDelta_Check(other) or isinstance(other, Tick):
elif (is_timedelta64_object(other) or PyDelta_Check(other)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any performance implications of checking this first now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we have a timedelta64 object we're avoiding a few unnecessary checks below (L784 of which occurs in python-space). otherwise we are delaying everything by the duration of a is_timedelta64_object call, which is small

or isinstance(other, Tick)):
ots = Timedelta(other)
else:
ndim = getattr(other, "ndim", -1)
# TODO: watch out for overflows

if ndim != -1:
if ndim == 0:
if is_timedelta64_object(other):
other = Timedelta(other)
else:
if op == Py_EQ:
return False
elif op == Py_NE:
return True
# only allow ==, != ops
raise TypeError(f'Cannot compare type '
f'{type(self).__name__} with '
f'type {type(other).__name__}')
if util.is_array(other):
return PyObject_RichCompare(np.array([self]), other, op)
return PyObject_RichCompare(other, self, reverse_ops[op])
else:
if other is NaT:
return PyObject_RichCompare(other, self, reverse_ops[op])
elif op == Py_EQ:
return False
elif op == Py_NE:
return True
raise TypeError(f'Cannot compare type {type(self).__name__} with '
f'type {type(other).__name__}')
elif other is NaT:
return op == Py_NE

elif util.is_array(other):
# TODO: watch out for zero-dim
if other.dtype.kind == "m":
return PyObject_RichCompare(self.asm8, other, op)
elif other.dtype.kind == "O":
# operate element-wise
return np.array(
[PyObject_RichCompare(self, x, op) for x in other],
dtype=bool,
)
if op == Py_EQ:
return np.zeros(other.shape, dtype=bool)
elif op == Py_NE:
return np.ones(other.shape, dtype=bool)
return NotImplemented # let other raise TypeError

else:
return NotImplemented

return cmp_scalar(self.value, ots.value, op)

Expand Down
12 changes: 4 additions & 8 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@

import numpy as np

from pandas._libs import Timedelta, Timestamp, internals as libinternals, lib
from pandas._libs import internals as libinternals, lib
from pandas._typing import ArrayLike, DtypeObj, Label
from pandas.util._validators import validate_bool_kwarg

from pandas.core.dtypes.cast import (
find_common_type,
infer_dtype_from_scalar,
maybe_convert_objects,
maybe_promote,
)
from pandas.core.dtypes.common import (
Expand All @@ -33,6 +32,7 @@
import pandas.core.algorithms as algos
from pandas.core.arrays.sparse import SparseDtype
from pandas.core.base import PandasObject
import pandas.core.common as com
from pandas.core.construction import extract_array
from pandas.core.indexers import maybe_convert_indices
from pandas.core.indexes.api import Index, ensure_index
Expand Down Expand Up @@ -626,11 +626,8 @@ def comp(s, regex=False):
"""
if isna(s):
return isna(values)
if isinstance(s, (Timedelta, Timestamp)) and getattr(s, "tz", None) is None:

return _compare_or_regex_search(
maybe_convert_objects(values), s.asm8, regex
)
s = com.maybe_box_datetimelike(s)
return _compare_or_regex_search(values, s, regex)

masks = [comp(s, regex) for s in src_list]
Expand All @@ -643,11 +640,10 @@ def comp(s, regex=False):
# replace ALWAYS will return a list
rb = [blk if inplace else blk.copy()]
for i, (s, d) in enumerate(zip(src_list, dest_list)):
# TODO: assert/validate that `d` is always a scalar?
new_rb: List[Block] = []
for b in rb:
m = masks[i][b.mgr_locs.indexer]
convert = i == src_len
convert = i == src_len # only convert once at the end
result = b._replace_coerce(
mask=m,
to_replace=s,
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ def test_dti_cmp_object_dtype(self):
result = dti == other
expected = np.array([True] * 5 + [False] * 5)
tm.assert_numpy_array_equal(result, expected)
msg = "Cannot compare type"
msg = ">=' not supported between instances of 'Timestamp' and 'Timedelta'"
with pytest.raises(TypeError, match=msg):
dti >= other

Expand Down
23 changes: 21 additions & 2 deletions pandas/tests/scalar/timedelta/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,25 @@ def test_compare_timedelta_ndarray(self):
expected = np.array([False, False])
tm.assert_numpy_array_equal(result, expected)

def test_compare_td64_ndarray(self):
# GG#33441
arr = np.arange(5).astype("timedelta64[ns]")
td = pd.Timedelta(arr[1])

expected = np.array([False, True, False, False, False], dtype=bool)

result = td == arr
tm.assert_numpy_array_equal(result, expected)

result = arr == td
tm.assert_numpy_array_equal(result, expected)

result = td != arr
tm.assert_numpy_array_equal(result, ~expected)

result = arr != td
tm.assert_numpy_array_equal(result, ~expected)

@pytest.mark.skip(reason="GH#20829 is reverted until after 0.24.0")
def test_compare_custom_object(self):
"""
Expand Down Expand Up @@ -943,7 +962,7 @@ def __gt__(self, other):
def test_compare_unknown_type(self, val):
# GH#20829
t = Timedelta("1s")
msg = "Cannot compare type Timedelta with type (int|str)"
msg = "not supported between instances of 'Timedelta' and '(int|str)'"
with pytest.raises(TypeError, match=msg):
t >= val
with pytest.raises(TypeError, match=msg):
Expand Down Expand Up @@ -984,7 +1003,7 @@ def test_ops_error_str():
with pytest.raises(TypeError, match=msg):
left + right

msg = "Cannot compare type"
msg = "not supported between instances of"
with pytest.raises(TypeError, match=msg):
left > right

Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ def test_replace_gh5319(self):
expected = pd.Series([pd.Timestamp.min, ts], dtype=object)
tm.assert_series_equal(expected, result)

def test_replace_timedelta_td64(self):
tdi = pd.timedelta_range(0, periods=5)
ser = pd.Series(tdi)

# Using a single dict argument means we go through replace_list
result = ser.replace({ser[1]: ser[3]})

expected = pd.Series([ser[0], ser[3], ser[2], ser[3], ser[4]])
tm.assert_series_equal(result, expected)

def test_replace_with_single_list(self):
ser = pd.Series([0, 1, 2, 3, 4])
result = ser.replace([1, 2, 3])
Expand Down