Pandas mod 0 should not give 0 #3590

Closed
darindillon opened this Issue May 13, 2013 · 4 comments

Comments

Projects
None yet
3 participants

In python:
3 % 0
is undefined and returns "ZeroDivisionError: integer division or modulo by zero"

But in pandas 0.11, that gives 0. I believe that is incorrect. It ought to give NaN or Inf

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

Member

cpcloud commented May 13, 2013

div by 0 using the div method is weird too and p % 1 fails with a TypeError.

Contributor

jreback commented May 13, 2013

This is a dtype issue (these ops need conversion to float before interacting)

Floats are fine

In [7]: x = p.astype('float')

In [8]: x
Out[8]: 
   first  second
0      3       0
1      4       0
2      5       0
3      8       3

In [9]: x['first'] / x['second']
Out[9]: 
0         inf
1         inf
2         inf
3    2.666667
dtype: float64

In [10]: x['first'] % x['second']
Out[10]: 
0   NaN
1   NaN
2   NaN
3     2
Name: first, dtype: float64
Member

cpcloud commented May 13, 2013

figured as much. kinda weird since one usually thinks about modulus as Z mod (another integer). also i can imagine a modulo for object blocks...string interp and such. @tavistmorph don't know if u know but numpy operates this way too, e.g., array_equal(p.values % 0, zeros_like(p)) evaluates to True.

Contributor

jreback commented May 14, 2013

@cpcloud @tavistmorph check out #3600

I think this makes things more consistent

jreback closed this in #3600 May 14, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment