-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
API: Handle pow & rpow special cases #30097
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
Changes from all commits
7abf40e
36d403d
f6b4062
945e8cd
a493965
8fc8b3a
a49aa65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,11 +38,14 @@ def test_arithmetic_ops(all_arithmetic_functions): | |
op = all_arithmetic_functions | ||
|
||
for other in [NA, 1, 1.0, "a", np.int64(1), np.nan]: | ||
if op.__name__ == "rmod" and isinstance(other, str): | ||
if op.__name__ in ("pow", "rpow", "rmod") and isinstance(other, str): | ||
continue | ||
if op.__name__ in ("divmod", "rdivmod"): | ||
assert op(NA, other) is (NA, NA) | ||
else: | ||
if op.__name__ == "rpow": | ||
# avoid special case | ||
other += 1 | ||
assert op(NA, other) is NA | ||
|
||
|
||
|
@@ -69,6 +72,49 @@ def test_comparison_ops(): | |
assert (other <= NA) is NA | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"value", | ||
[ | ||
0, | ||
0.0, | ||
-0, | ||
-0.0, | ||
False, | ||
np.bool_(False), | ||
np.int_(0), | ||
np.float_(0), | ||
np.int_(-0), | ||
np.float_(-0), | ||
], | ||
) | ||
def test_pow_special(value): | ||
result = pd.NA ** value | ||
assert isinstance(result, type(value)) | ||
assert result == 1 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"value", | ||
[ | ||
1, | ||
1.0, | ||
-1, | ||
-1.0, | ||
True, | ||
np.bool_(True), | ||
np.int_(1), | ||
np.float_(1), | ||
np.int_(-1), | ||
np.float_(-1), | ||
], | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. corner case to check -0.0 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. In [4]: -1 ** np.nan
Out[4]: -1.0
In [5]: np.nan ** -0
Out[5]: 1.0 Will match the NumPy behavior here. |
||
def test_rpow_special(value): | ||
result = value ** pd.NA | ||
assert result == value | ||
if not isinstance(value, (np.float_, np.bool_, np.int_)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Somebody (Cython?) is converting these to Python scalars. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. have you tried with/without numexpr? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can numexpr be involved in this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i dont know, but if we're brainstorming things that can do surprising conversions, it comes to minid |
||
assert isinstance(result, type(value)) | ||
|
||
|
||
def test_unary_ops(): | ||
assert +NA is NA | ||
assert -NA is NA | ||
|
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.
There are fixtures defined for zero / one values in pandas/tests/indexing/conftest.py - can these be combined with that?
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.
That seems to include arrays. We just want a scalar here I think.