Skip to content

Commit 05cb960

Browse files
committed
Merge pull request #5439 from jtratner/make-div-do-truediv
ENH: Always do true division on Python 2.X
2 parents 2d2e8b5 + 98857c1 commit 05cb960

File tree

12 files changed

+80
-50
lines changed

12 files changed

+80
-50
lines changed

doc/source/release.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,25 @@ API Changes
376376
dates are given (:issue:`5242`)
377377
- ``Timestamp`` now supports ``now/today/utcnow`` class methods
378378
(:issue:`5339`)
379+
- **All** division with ``NDFrame`` - likes is now truedivision, regardless
380+
of the future import. You can use ``//`` and ``floordiv`` to do integer
381+
division.
382+
383+
.. code-block:: python
384+
In [3]: arr = np.array([1, 2, 3, 4])
385+
386+
In [4]: arr2 = np.array([5, 3, 2, 1])
387+
388+
In [5]: arr / arr2
389+
Out[5]: array([0, 0, 1, 4])
390+
391+
In [6]: pd.Series(arr) / pd.Series(arr2) # no future import required
392+
Out[6]:
393+
0 0.200000
394+
1 0.666667
395+
2 1.500000
396+
3 4.000000
397+
dtype: float64
379398
380399
Internal Refactoring
381400
~~~~~~~~~~~~~~~~~~~~

doc/source/v0.13.0.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ API changes
5555
# and all methods take an inplace kwarg - but returns None
5656
index.set_names(["bob", "cranberry"], inplace=True)
5757

58+
- **All** division with ``NDFrame`` - likes is now truedivision, regardless
59+
of the future import. You can use ``//`` and ``floordiv`` to do integer
60+
division.
61+
62+
.. code-block:: python
63+
In [3]: arr = np.array([1, 2, 3, 4])
64+
65+
In [4]: arr2 = np.array([5, 3, 2, 1])
66+
67+
In [5]: arr / arr2
68+
Out[5]: array([0, 0, 1, 4])
69+
70+
In [6]: pd.Series(arr) / pd.Series(arr2) # no future import required
71+
Out[6]:
72+
0 0.200000
73+
1 0.666667
74+
2 1.500000
75+
3 4.000000
76+
dtype: float64
77+
5878
- Infer and downcast dtype if ``downcast='infer'`` is passed to ``fillna/ffill/bfill`` (:issue:`4604`)
5979
- ``__nonzero__`` for all NDFrame objects, will now raise a ``ValueError``, this reverts back to (:issue:`1073`, :issue:`4633`)
6080
behavior. See :ref:`gotchas<gotchas.truth>` for a more detailed discussion.

pandas/computation/expressions.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def _evaluate_standard(op, op_str, a, b, raise_on_error=True, **eval_kwargs):
6161
_store_test_result(False)
6262
return op(a, b)
6363

64+
6465
def _can_use_numexpr(op, op_str, a, b, dtype_check):
6566
""" return a boolean if we WILL be using numexpr """
6667
if op_str is not None:
@@ -86,7 +87,8 @@ def _can_use_numexpr(op, op_str, a, b, dtype_check):
8687
return False
8788

8889

89-
def _evaluate_numexpr(op, op_str, a, b, raise_on_error=False, **eval_kwargs):
90+
def _evaluate_numexpr(op, op_str, a, b, raise_on_error=False, truediv=True,
91+
**eval_kwargs):
9092
result = None
9193

9294
if _can_use_numexpr(op, op_str, a, b, 'evaluate'):
@@ -96,7 +98,8 @@ def _evaluate_numexpr(op, op_str, a, b, raise_on_error=False, **eval_kwargs):
9698
result = ne.evaluate('a_value %s b_value' % op_str,
9799
local_dict={'a_value': a_value,
98100
'b_value': b_value},
99-
casting='safe', **eval_kwargs)
101+
casting='safe', truediv=truediv,
102+
**eval_kwargs)
100103
except (ValueError) as detail:
101104
if 'unknown type object' in str(detail):
102105
pass
@@ -112,10 +115,12 @@ def _evaluate_numexpr(op, op_str, a, b, raise_on_error=False, **eval_kwargs):
112115

113116
return result
114117

118+
115119
def _where_standard(cond, a, b, raise_on_error=True):
116120
return np.where(_values_from_object(cond), _values_from_object(a),
117121
_values_from_object(b))
118122

123+
119124
def _where_numexpr(cond, a, b, raise_on_error=False):
120125
result = None
121126

@@ -190,10 +195,10 @@ def where(cond, a, b, raise_on_error=False, use_numexpr=True):
190195
return _where_standard(cond, a, b, raise_on_error=raise_on_error)
191196

192197

193-
def set_test_mode(v = True):
198+
def set_test_mode(v=True):
194199
"""
195-
Keeps track of whether numexpr was used. Stores an additional ``True`` for
196-
every successful use of evaluate with numexpr since the last
200+
Keeps track of whether numexpr was used. Stores an additional ``True``
201+
for every successful use of evaluate with numexpr since the last
197202
``get_test_result``
198203
"""
199204
global _TEST_MODE, _TEST_RESULT

pandas/core/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Generic data algorithms. This module is experimental at the moment and not
33
intended for public consumption
44
"""
5-
5+
from __future__ import division
66
from warnings import warn
77
import numpy as np
88

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
alignment and a host of useful data manipulation methods having to do with the
99
labeling information
1010
"""
11-
11+
from __future__ import division
1212
# pylint: disable=E1101,E1103
1313
# pylint: disable=W0212,W0231,W0703,W0622
1414

pandas/core/ops.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
44
This is not a public API.
55
"""
6+
# necessary to enforce truediv in Python 2.X
7+
from __future__ import division
68
import operator
79
import numpy as np
810
import pandas as pd
@@ -80,24 +82,10 @@ def names(x):
8082
rmod=arith_method(lambda x, y: y % x, names('rmod'),
8183
default_axis=default_axis),
8284
)
83-
if not compat.PY3:
84-
new_methods["div"] = arith_method(operator.div, names('div'), op('/'),
85-
truediv=False, fill_zeros=np.inf,
86-
default_axis=default_axis)
87-
new_methods["rdiv"] = arith_method(lambda x, y: operator.div(y, x),
88-
names('rdiv'), truediv=False,
89-
fill_zeros=np.inf,
90-
default_axis=default_axis)
91-
else:
92-
new_methods["div"] = arith_method(operator.truediv, names('div'),
93-
op('/'), truediv=True,
94-
fill_zeros=np.inf,
95-
default_axis=default_axis)
96-
new_methods["rdiv"] = arith_method(lambda x, y: operator.truediv(y, x),
97-
names('rdiv'), truediv=False,
98-
fill_zeros=np.inf,
99-
default_axis=default_axis)
100-
# Comp methods never had a default axis set
85+
new_methods['div'] = new_methods['truediv']
86+
new_methods['rdiv'] = new_methods['rtruediv']
87+
88+
# Comp methods never had a default axis set
10189
if comp_method:
10290
new_methods.update(dict(
10391
eq=comp_method(operator.eq, names('eq'), op('==')),

pandas/core/panel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Contains data structures designed for manipulating panel (3-dimensional) data
33
"""
44
# pylint: disable=E1103,W0231,W0212,W0621
5-
5+
from __future__ import division
66
from pandas.compat import map, zip, range, lrange, lmap, u, OrderedDict, OrderedDefaultdict
77
from pandas import compat
88
import sys

pandas/core/series.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Data structure for 1-dimensional cross-sectional and time series data
33
"""
4+
from __future__ import division
45

56
# pylint: disable=E1101,E1103
67
# pylint: disable=W0703,W0622,W0613,W0201

pandas/sparse/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
SparseArray data structure
33
"""
4-
4+
from __future__ import division
55
# pylint: disable=E1101,E1103,W0231
66

77
from numpy import nan, ndarray

pandas/sparse/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Data structures for sparse float data. Life is made simpler by dealing only
33
with float64 data
44
"""
5-
5+
from __future__ import division
66
# pylint: disable=E1101,E1103,W0231,E0202
77

88
from numpy import nan

0 commit comments

Comments
 (0)