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.
i think this line was at the root of #27829, so this might fix that
There was a problem hiding this comment.
I think you're correct.
|
|
||
| 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.
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.
small nitpick but maybe a little more concise to do:
expected = np.array([np.nan, np.inf, -np.inf, np.nan])
if negative:
expected *= -1There was a problem hiding this comment.
Given that CI is passing, I think I'm OK with how things are now :)
There was a problem hiding this comment.
see tests.arithmetic.test_numeric.adjust_negative_zero
jorisvandenbossche
left a comment
There was a problem hiding this comment.
Looks good to me.
This can go in first before the pd.NA IntegerArray PR?
| - 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.
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.
for non-trivially to understand changes we usually have a sub-section, but up to you.
jreback
left a comment
There was a problem hiding this comment.
lgtm. if you want to expand whatsnew note cool.
| - 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.
for non-trivially to understand changes we usually have a sub-section, but up to you.
| 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.
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