-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Fixed IntegerArray division by 0 #30183
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
Fixed IntegerArray division by 0 #30183
Conversation
When dividing by 0, the result should be `inf`, not `NaN`. Closes pandas-dev#27398
b0693be
to
81b18ca
Compare
# may need to fill infs | ||
# and mask wraparound | ||
if is_float_dtype(result): | ||
mask |= (result == np.inf) | (result == -np.inf) |
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.
i think this line was at the root of #27829, so this might fix 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.
I think you're correct.
@@ -389,6 +396,10 @@ def test_compare_array(self, data, all_compare_operators): | |||
other = pd.Series([0] * len(data)) | |||
self._compare_other(data, op_name, other) | |||
|
|||
def test_no_shared_mask(self, data): | |||
result = data + 1 | |||
assert np.shares_memory(result._mask, data._mask) is False |
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.
i didnt know about shares_memory
, neat!
As an aside, floor division by zero looks a little suspicious to me. I'm not 100% sure what the expected behavior for it should be, but off the top of my head I think your test case should return the same values? Certainly doesn't need to be part of this PR though. In [7]: a // 0
Out[7]:
<IntegerArray>
[0, 0, 0, NaN]
Length: 4, dtype: Int64
In [8]: a // -0
Out[8]:
<IntegerArray>
[0, 0, 0, NaN]
Length: 4, dtype: Int64
In [9]: a // 0.0
Out[9]: array([nan, nan, nan, nan])
In [10]: a // -0.0
Out[10]: array([nan, nan, nan, nan]) |
values = [np.nan, -np.inf, np.inf, np.nan] | ||
else: | ||
values = [np.nan, np.inf, -np.inf, np.nan] | ||
expected = np.array(values) |
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.
small nitpick but maybe a little more concise to do:
expected = np.array([np.nan, np.inf, -np.inf, np.nan])
if negative:
expected *= -1
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.
Given that CI is passing, I think I'm OK with how things are now :)
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.
see tests.arithmetic.test_numeric.adjust_negative_zero
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.
Looks good to me.
This can go in first before the pd.NA IntegerArray PR?
@@ -858,6 +858,7 @@ Other | |||
- Bug in :meth:`DataFrame.append` that raised ``IndexError`` when appending with empty list (:issue:`28769`) | |||
- Fix :class:`AbstractHolidayCalendar` to return correct results for | |||
years after 2030 (now goes up to 2200) (:issue:`27790`) | |||
- Fixed :class:`IntegerArray` returning ``NA`` rather than ``inf`` for operations dividing by 0 (:issue:`27398`) |
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.
This is a bit confusingly worded I think, as I would interpret this that it now returns NaN and no longer inf, while it is the other way around?
Also NA -> NaN, as the result is about floats with NaNs
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.
for non-trivially to understand changes we usually have a sub-section, but up to you.
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. if you want to expand whatsnew note cool.
@@ -858,6 +858,7 @@ Other | |||
- Bug in :meth:`DataFrame.append` that raised ``IndexError`` when appending with empty list (:issue:`28769`) | |||
- Fix :class:`AbstractHolidayCalendar` to return correct results for | |||
years after 2030 (now goes up to 2200) (:issue:`27790`) | |||
- Fixed :class:`IntegerArray` returning ``NA`` rather than ``inf`` for operations dividing by 0 (:issue:`27398`) |
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.
for non-trivially to understand changes we usually have a sub-section, but up to you.
@@ -339,6 +339,19 @@ def test_error(self, data, all_arithmetic_operators): | |||
with pytest.raises(NotImplementedError): | |||
opa(np.arange(len(s)).reshape(-1, len(s))) | |||
|
|||
@pytest.mark.parametrize("zero, negative", [(0, False), (0.0, False), (-0.0, True)]) |
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.
might be good to make a fixture for zeros as we do a lot of these types of tests for series ops, cc @jbrockmendel
I'd prefer to just merge this as is. I have a followup PR fixing |
Yes, this and my next PR fixing |
Sounds good |
When dividing by 0, the result should be `inf`, not `NaN`. Closes pandas-dev#27398
When dividing by 0, the result should be `inf`, not `NaN`. Closes pandas-dev#27398
When dividing by 0, the result should be
inf
, notNaN
.closes #27398
closes #27829