Skip to content
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

BUG: Period.__eq__ numpy scalar (#44182 (comment)) #44285

Merged
merged 10 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1656,7 +1656,7 @@ cdef class _Period(PeriodMixin):
return PyObject_RichCompareBool(self.ordinal, other.ordinal, op)
elif other is NaT:
return _nat_scalar_rules[op]
elif util.is_array(other):
elif util.is_array(other) and not np.ndim(other) == 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

np.ndim(..) > 0

# in particular ndarray[object]; see test_pi_cmp_period
return np.array([PyObject_RichCompare(self, x, op) for x in other])
return NotImplemented
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/arithmetic/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ def test_pi_cmp_period(self):
result = idx.values.reshape(10, 2) < idx[10]
tm.assert_numpy_array_equal(result, exp.reshape(10, 2))

# Tests Period.__richcmp__ against ndarray[object, ndim=0]
result = idx < np.array(idx[10])
tm.assert_numpy_array_equal(result, exp)

# TODO: moved from test_datetime64; de-duplicate with version below
def test_parr_cmp_period_scalar2(self, box_with_array):
xbox = get_expected_box(box_with_array)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,16 @@ def test_period_cmp_nat(self):
assert not left <= right
assert not left >= right

@pytest.mark.parametrize(
"scalar, expected", ((np.array(0), np.bool_(False)), (np.int_(0), False))
)
def test_comparison_numpy_scalar(self, scalar, expected):
Copy link
Member

Choose a reason for hiding this comment

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

scalars are already tested; its zerodim we need to catch. in particular a zero-dim ndarray containing a Period object

p = Period("2000-01", "M")

for left, right in ((p, scalar), (scalar, p)):
Copy link
Member

Choose a reason for hiding this comment

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

the loop here is unnecessary; its two lines either way and clearer without

assert not left == right
assert (left == right) is expected


class TestArithmetic:
def test_sub_delta(self):
Expand Down