Skip to content
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

Pandas mod 0 should not give 0 #3590

Closed
darindillon opened this issue May 13, 2013 · 4 comments · Fixed by #3600
Closed

Pandas mod 0 should not give 0 #3590

darindillon opened this issue May 13, 2013 · 4 comments · Fixed by #3600
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions
Milestone

Comments

@darindillon
Copy link

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

@cpcloud
Copy link
Member

cpcloud commented May 13, 2013

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

@jreback
Copy link
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

@cpcloud
Copy link
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.

@jreback
Copy link
Contributor

jreback commented May 14, 2013

@cpcloud @tavistmorph check out #3600

I think this makes things more consistent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
3 participants