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

Conversation

mathause
Copy link
Contributor

@mathause mathause commented Nov 2, 2021

@mathause
Copy link
Contributor Author

mathause commented Nov 2, 2021

I added three tests but only one of them fails without the code change.

Fixed by the code update:

Period("2000-01", "M") == np.array(0)

Additional tests not affected by the code update:

idx = period_range("2007-01", periods=20, freq="M")
idx == np.array(idx[10])

Period("2000-01", "M") == np.int_(0)

@@ -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

@mathause
Copy link
Contributor Author

mathause commented Nov 2, 2021

This used to return True (but now returns False), so I think we are not yet entirely there:

p = pd.Period("2000-01", "M")
p == np.array(p)

@@ -1657,8 +1657,11 @@ cdef class _Period(PeriodMixin):
elif other is NaT:
return _nat_scalar_rules[op]
elif util.is_array(other):
# in particular ndarray[object]; see test_pi_cmp_period
return np.array([PyObject_RichCompare(self, x, op) for x in other])
if np.ndim(other) > 0:
Copy link
Member

Choose a reason for hiding this comment

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

cnp.PyArray_IsZeroDim(other)

Copy link
Member

Choose a reason for hiding this comment

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

# GH#44285

def test_comparison_numpy_scalar(self, scalar, expected):
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

"scalar, expected",
((np.array(0), False), (np.int_(0), False), (Period("2000-01", "M"), True)),
)
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

@jreback jreback added Bug Period Period data type labels Nov 3, 2021
Copy link
Member

@jbrockmendel jbrockmendel left a comment

Choose a reason for hiding this comment

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

LGTM cc @jreback

@jreback jreback added this to the 1.4 milestone Nov 4, 2021
@jreback jreback merged commit a3bcbf8 into pandas-dev:master Nov 4, 2021
@jreback
Copy link
Contributor

jreback commented Nov 4, 2021

thanks @mathause

@mathause mathause deleted the fix_period_scalar branch November 4, 2021 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Period Period data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants