Skip to content

ENH: Change NDFrame.div() to be truediv in Python 2.X #5356

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

Closed
jtratner opened this issue Oct 28, 2013 · 6 comments · Fixed by #5439
Closed

ENH: Change NDFrame.div() to be truediv in Python 2.X #5356

jtratner opened this issue Oct 28, 2013 · 6 comments · Fixed by #5439
Milestone

Comments

@jtratner
Copy link
Contributor

Now that we have all the arithmetic methods, there's no reason to keep it as before. Not having this means that operating on an int column with missing values switches operation result from floordiv to truediv.

@wesm You had, at one point, said that you would consider this. I believe this change would iron over a long-standing Python wart and make pandas easier to use:

Existing behavior (note how the future import doesn't affect div at all because it's defined in a separate file, but does affect division because Python will call __truediv__ instead of __div__):

In [22]: from __future__ import division

In [23]: ser1 / ser2
Out[23]:
0    0.0
1    0.5
2    1.0
3    1.5
dtype: float64

In [24]: ser1.div(ser2)
Out[24]:
0    0
1    0
2    1
3    1
dtype: int64

And missing values immediately causes this behavior to change (because coerces to float)

In [11]: from pandas import Series

In [12]: ser1 = Series([0, 1, 2, 3])

In [13]: ser2 = Series([2, 2, 2, 2])

In [14]: ser1.div(ser2)
Out[14]:
0    0
1    0
2    1
3    1
dtype: int64
In [15]: ser1[0] = np.nan

In [16]: ser1.div(ser2)
Out[18]:
0    NaN
1    0.5
2    1.0
3    1.5
dtype: float64

And this distinction is totally eliminated in Python 3:

In [11]: from pandas import Series

In [12]: ser1 = Series([0, 1, 2, 3])

In [13]: ser2 = Series([2, 2, 2, 2])

In [14]: ser1.div(ser2)
Out[14]:
0    0.0
1    0.5
2    1.0
3    1.5
dtype: float64
In [15]: ser1[0] = np.nan

In [16]: ser1.div(ser2)
Out[18]:
0    NaN
1    0.5
2    1.0
3    1.5
dtype: float64

Trivial to fix and, while slightly backwards incompatible, would mostly just make things simpler. We should still leave a / b to be dependent upon the future import.

@jreback
Copy link
Contributor

jreback commented Oct 28, 2013

you don't think it would more consistent to break all truediv? (e.g. '/' as well).

would be very consistent

@jtratner
Copy link
Contributor Author

I would not be opposed that (we aren't doing anything special on an int column that gets an nan anyways)

@jreback
Copy link
Contributor

jreback commented Nov 2, 2013

IIRC this is pretty triviall to implement (just flip a paramter ?), but need a v0.13 mention....should we defer to 0.14?

@jtratner
Copy link
Contributor Author

jtratner commented Nov 2, 2013

It's incredibly trivial to implement. I'm skeptical that anyone would be
depending on it (given that it needs to be int col with no missing + other
must be int too) and now there's floordiv too. I'll put up a PR we can
discuss.
On Nov 2, 2013 5:33 PM, "jreback" [email protected] wrote:

IIRC this is pretty triviall to implement (just flip a paramter ?), but
need a v0.13 mention....should we defer to 0.14?


Reply to this email directly or view it on GitHubhttps://github.com//issues/5356#issuecomment-27633070
.

@wesm
Copy link
Member

wesm commented Nov 4, 2013

I don't see a problem with having consistent behavior between Python 2 and 3. I'm sure plenty of developers are importing division from __future__ in their pandas code.

@jtratner
Copy link
Contributor Author

jtratner commented Nov 4, 2013

Great, I'll put up a PR

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

Successfully merging a pull request may close this issue.

3 participants