BUG: 0/frame numeric ops buggy (GH9144) by Garrett-R · Pull Request #9308 · pandas-dev/pandas (original) (raw)

Hmmm... I couldn't find any 0/0 discussion (there was a discussion in #3590 about mod 0 though), but in the release notes you linked to for v.0.12.0, it said "Fix modulo and integer division on Series,DataFrames to act similary to float dtypes to return np.nan or np.inf as appropriate" and it gave 4 examples. I think the fix (PR #3600) worked except for the third example as demonstrated here:

>>> p = pd.DataFrame({ 'first' : [4,5,8], 'second' : [0,0,3] })

>>> q = p.astype('float')

>>> print(p%0,'\n', q%0)
   first  second
0    NaN     NaN
1    NaN     NaN
2    NaN     NaN 
    first  second
0    NaN     NaN
1    NaN     NaN
2    NaN     NaN

>>> print(p%p,'\n', q%q)
   first  second
0      0     NaN
1      0     NaN
2      0       0 
    first  second
0      0     NaN
1      0     NaN
2      0       0

>>> print(p/p,'\n', q/q)    # Inconsistent!
   first    second
0      1       inf
1      1       inf
2      1  1.000000 
    first  second
0      1     NaN
1      1     NaN
2      1       1

>>> print(p/0,'\n', q/0)
   first  second
0    inf     inf
1    inf     inf
2    inf     inf 
    first  second
0    inf     inf
1    inf     inf
2    inf     inf

The results are the same for Python 2.

This PR would correct this.
(Edit: Actually, my PR fails to correct the inconsistency that's also present with print(p//p,'\n', q//q) and print(p//0,'\n', q//0), but I'll wait to hear what you think I should do before looking into how to change that.)

I see NumPy has some strange things going on (although it sounds like they're discussing fixing this in numpy/numpy#899), for example,

>>> x = np.int_(0)
>>> x / x
nan
>>> x // x
0

but I haven't ever seen 0/0 being infinite. I think we should change that...

BTW, I'll also write a release note once we decide how to handle this.