On Sun, Jun 2, 2013 at 1:49 AM, Jeff Tratner <
jtra...@gmail.com> wrote:
> Hi all,
>
> When using pandas, I sometimes get bitten by the fact that `div` in Python
> 2.7 doesn't respect `from __future__ import division` (it's defined in a
> file that doesn't start with the import, so it always does integer
> division). Was there an explicit decision made at some point that pandas
> should be this way? It doesn't come up in the test suite, because the
> arithmetic methods (except for modulo) appear to only test against floats.
>
> This issue also means that Python 3 pandas behaves differently than Python 2
> pandas even with the future import, since `div` in Python 3 always does
> `truediv`.
>
> To make this clear, try running this snippet in Python 2 and in Python 3:
>
> ```
>
> from __future__ import division
> import pandas
> from pandas.util.testing import assert_frame_equal
> df = pandas.DataFrame({"A": range(4)})
> result = df.div(4)
> expected = df / 4
> assert_frame_equal(result, expected)
>
> ```
>
> If this is something you all would be amenable to, I have a patch pretty
> much prepared that changes series, frame, and panel to use `truediv`
> exclusively, plus some additional test cases that test that div actually
> works as truediv everywhere. `__div__`, `__rdiv__`, `__idiv__` would all
> remain the same.
>
> This has some benefits. For one, if you use `from __future__ import
> division`, python 3 and python 2 will behave exactly the same in regards to
> division *and* I'd argue that this would be less surprising for new users.
> If div is going to be inconsistent in one way or another, it's better for it
> to be consistent with true division and inconsistent with integer division.
> Also, this removes the need for a few tests for python 3, since both python
> 2 and python 3 support `operator.truediv`.
>
> Does this make sense? Does changing how div works seem worth the benefits in
> less confusion for new-comers and easier use on the command line?
>
> Best,
>
> Jeff
I'd be willing to change it to consistently be truediv. Want to open a
github issue and we'll consider it for 0.12?
Thanks