-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
Conversation
mathause
commented
Nov 2, 2021
- fixes BUG: np.timedelta64 + Period #44182 (comment)
- tests added / passed
- Ensure all linting tests pass, see here for how to run them
- whatsnew entry
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) |
pandas/_libs/tslibs/period.pyx
Outdated
@@ -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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
np.ndim(..) > 0
This used to return p = pd.Period("2000-01", "M")
p == np.array(p) |
pandas/_libs/tslibs/period.pyx
Outdated
@@ -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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cnp.PyArray_IsZeroDim(other)
There was a problem hiding this comment.
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)): |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM cc @jreback
thanks @mathause |