Skip to content

Commit 550371d

Browse files
takluyverwesm
authored andcommitted
Pandas can now be imported successfully in Python 3.
Most of the changes were to do with division methods, as operator.div no longer exists.
1 parent b4baa64 commit 550371d

File tree

6 files changed

+65
-17
lines changed

6 files changed

+65
-17
lines changed

pandas/core/common.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,10 @@ def __init__(self, seq, key=lambda x:x):
480480
for value in seq:
481481
k = key(value)
482482
self.setdefault(k, []).append(value)
483-
__iter__ = dict.iteritems
483+
try:
484+
__iter__ = dict.iteritems
485+
except AttributeError: # Python 3
486+
__iter__ = dict.items
484487

485488
def map_indices_py(arr):
486489
"""

pandas/core/frame.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from pandas.core.internals import BlockManager, make_block, form_blocks
3131
from pandas.core.series import Series, _is_bool_indexer
3232
from pandas.util.decorators import deprecate
33+
from pandas.util import py3compat
3334
import pandas.core.common as common
3435
import pandas.core.datetools as datetools
3536
import pandas._tseries as _tseries
@@ -292,7 +293,7 @@ def __contains__(self, key):
292293
add = _arith_method(operator.add, 'add')
293294
mul = _arith_method(operator.mul, 'multiply')
294295
sub = _arith_method(operator.sub, 'subtract')
295-
div = _arith_method(operator.div, 'divide')
296+
div = _arith_method(lambda x, y: x / y, 'divide')
296297

297298
radd = _arith_method(operator.add, 'add')
298299
rmul = _arith_method(operator.mul, 'multiply')
@@ -302,19 +303,26 @@ def __contains__(self, key):
302303
__add__ = _arith_method(operator.add, '__add__', default_axis=None)
303304
__sub__ = _arith_method(operator.sub, '__sub__', default_axis=None)
304305
__mul__ = _arith_method(operator.mul, '__mul__', default_axis=None)
305-
__div__ = _arith_method(operator.div, '__div__', default_axis=None)
306306
__truediv__ = _arith_method(operator.truediv, '__truediv__',
307307
default_axis=None)
308+
__floordiv__ = _arith_method(operator.floordiv, '__floordiv__',
309+
default_axis=None)
308310
__pow__ = _arith_method(operator.pow, '__pow__', default_axis=None)
309311

310312
__radd__ = _arith_method(operator.add, '__radd__', default_axis=None)
311313
__rmul__ = _arith_method(operator.mul, '__rmul__', default_axis=None)
312314
__rsub__ = _arith_method(lambda x, y: y - x, '__rsub__', default_axis=None)
313-
__rdiv__ = _arith_method(lambda x, y: y / x, '__rdiv__', default_axis=None)
314315
__rtruediv__ = _arith_method(lambda x, y: y / x, '__rtruediv__',
315316
default_axis=None)
317+
__rfloordiv__ = _arith_method(lambda x, y: y // x, '__rfloordiv__',
318+
default_axis=None)
316319
__rpow__ = _arith_method(lambda x, y: y ** x, '__rpow__',
317320
default_axis=None)
321+
322+
# Python 2 division methods
323+
if not py3compat.PY3:
324+
__div__ = _arith_method(operator.div, '__div__', default_axis=None)
325+
__rdiv__ = _arith_method(lambda x, y: y / x, '__rdiv__', default_axis=None)
318326

319327
def __neg__(self):
320328
return self * -1

pandas/core/panel.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pandas.core.generic import AxisProperty, NDFrame
1616
from pandas.core.series import Series
1717
from pandas.util.decorators import deprecate
18+
from pandas.util import py3compat
1819
import pandas.core.common as common
1920
import pandas._tseries as _tseries
2021

@@ -170,16 +171,22 @@ class Panel(NDFrame):
170171

171172
__add__ = _arith_method(operator.add, '__add__')
172173
__sub__ = _arith_method(operator.sub, '__sub__')
174+
__truediv__ = _arith_method(operator.truediv, '__truediv__')
175+
__floordiv__ = _arith_method(operator.floordiv, '__floordiv__')
173176
__mul__ = _arith_method(operator.mul, '__mul__')
174-
__div__ = _arith_method(operator.div, '__div__')
175177
__pow__ = _arith_method(operator.pow, '__pow__')
176178

177179
__radd__ = _arith_method(operator.add, '__radd__')
178180
__rmul__ = _arith_method(operator.mul, '__rmul__')
179181
__rsub__ = _arith_method(lambda x, y: y - x, '__rsub__')
180-
__rdiv__ = _arith_method(lambda x, y: y / x, '__rdiv__')
182+
__rtruediv__ = _arith_method(lambda x, y: y / x, '__rtruediv__')
183+
__rfloordiv__ = _arith_method(lambda x, y: y // x, '__rfloordiv__')
181184
__rpow__ = _arith_method(lambda x, y: y ** x, '__rpow__')
182185

186+
if not py3compat.PY3:
187+
__div__ = _arith_method(operator.div, '__div__')
188+
__rdiv__ = _arith_method(lambda x, y: y / x, '__rdiv__')
189+
183190
def __init__(self, data, items=None, major_axis=None, minor_axis=None,
184191
copy=False, dtype=None):
185192
"""
@@ -642,8 +649,12 @@ def fillna(self, value=None, method='pad'):
642649

643650
add = _panel_arith_method(operator.add, 'add')
644651
subtract = sub = _panel_arith_method(operator.sub, 'subtract')
645-
divide = div = _panel_arith_method(operator.div, 'divide')
646652
multiply = mul = _panel_arith_method(operator.mul, 'multiply')
653+
654+
try:
655+
divide = div = _panel_arith_method(operator.div, 'divide')
656+
except AttributeError: # Python 3
657+
divide = div = _panel_arith_method(operator.truediv, 'divide')
647658

648659
def major_xs(self, key, copy=True):
649660
"""
@@ -1214,8 +1225,12 @@ def _combine_panel_frame(self, other, func, axis='items'):
12141225

12151226
add = _panel_arith_method(operator.add, 'add')
12161227
subtract = sub = _panel_arith_method(operator.sub, 'subtract')
1217-
divide = div = _panel_arith_method(operator.div, 'divide')
12181228
multiply = mul = _panel_arith_method(operator.mul, 'multiply')
1229+
1230+
try:
1231+
divide = div = _panel_arith_method(operator.div, 'divide')
1232+
except AttributeError: # Python 3
1233+
divide = div = _panel_arith_method(operator.truediv, 'divide')
12191234

12201235
def to_wide(self):
12211236
"""

pandas/core/series.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pandas.core.index import Index, MultiIndex, _ensure_index
2222
from pandas.core.indexing import _SeriesIndexer, _maybe_droplevels
2323
from pandas.util.decorators import deprecate
24+
from pandas.util import py3compat
2425
import pandas.core.common as common
2526
import pandas.core.datetools as datetools
2627
import pandas._tseries as lib
@@ -425,24 +426,30 @@ def iteritems(self):
425426
__add__ = _arith_method(operator.add, '__add__')
426427
__sub__ = _arith_method(operator.sub, '__sub__')
427428
__mul__ = _arith_method(operator.mul, '__mul__')
428-
__div__ = _arith_method(operator.div, '__div__')
429429
__truediv__ = _arith_method(operator.truediv, '__truediv__')
430+
__floordiv__ = _arith_method(operator.floordiv, '__floordiv__')
430431
__pow__ = _arith_method(operator.pow, '__pow__')
431-
__truediv__ = _arith_method(operator.truediv, '__truediv__')
432432

433433
__radd__ = _arith_method(operator.add, '__add__')
434434
__rmul__ = _arith_method(operator.mul, '__mul__')
435435
__rsub__ = _arith_method(lambda x, y: y - x, '__sub__')
436-
__rdiv__ = _arith_method(lambda x, y: y / x, '__div__')
437436
__rtruediv__ = _arith_method(lambda x, y: y / x, '__truediv__')
437+
__rfloordiv__ = _arith_method(lambda x, y: y // x, '__floordiv__')
438438
__rpow__ = _arith_method(lambda x, y: y ** x, '__pow__')
439439

440440
# Inplace operators
441441
__iadd__ = __add__
442442
__isub__ = __sub__
443443
__imul__ = __mul__
444-
__idiv__ = __div__
444+
__itruediv__ = __truediv__
445+
__ifloordiv__ = __floordiv__
445446
__ipow__ = __pow__
447+
448+
# Python 2 division operators
449+
if not py3compat.PY3:
450+
__div__ = _arith_method(operator.div, '__div__')
451+
__rdiv__ = _arith_method(lambda x, y: y / x, '__div__')
452+
__idiv__ = __div__
446453

447454
#----------------------------------------------------------------------
448455
# Misc public methods
@@ -1031,7 +1038,10 @@ def _binop(self, other, func, fill_value=None):
10311038
add = _flex_method(operator.add, 'add')
10321039
sub = _flex_method(operator.sub, 'subtract')
10331040
mul = _flex_method(operator.mul, 'multiply')
1034-
div = _flex_method(operator.div, 'divide')
1041+
try:
1042+
div = _flex_method(operator.div, 'divide')
1043+
except AttributeError: # Python 3
1044+
div = _flex_method(operator.truediv, 'divide')
10351045

10361046
def combine(self, other, func, fill_value=nan):
10371047
"""

pandas/core/sparse.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import pandas.core.common as common
2121
import pandas.core.datetools as datetools
2222

23+
from pandas.util import py3compat
24+
2325
from pandas._sparse import BlockIndex, IntIndex
2426
import pandas._sparse as splib
2527

@@ -296,24 +298,31 @@ def __repr__(self):
296298
__add__ = _sparse_op_wrap(operator.add, 'add')
297299
__sub__ = _sparse_op_wrap(operator.sub, 'sub')
298300
__mul__ = _sparse_op_wrap(operator.mul, 'mul')
299-
__div__ = _sparse_op_wrap(operator.div, 'div')
300301
__truediv__ = _sparse_op_wrap(operator.truediv, 'truediv')
302+
__floordiv__ = _sparse_op_wrap(operator.floordiv, 'floordiv')
301303
__pow__ = _sparse_op_wrap(operator.pow, 'pow')
302304

303305
# reverse operators
304306
__radd__ = _sparse_op_wrap(operator.add, '__radd__')
305-
__rmul__ = _sparse_op_wrap(operator.mul, '__rmul__')
306307
__rsub__ = _sparse_op_wrap(lambda x, y: y - x, '__rsub__')
307-
__rdiv__ = _sparse_op_wrap(lambda x, y: y / x, '__rdiv__')
308+
__rmul__ = _sparse_op_wrap(operator.mul, '__rmul__')
308309
__rtruediv__ = _sparse_op_wrap(lambda x, y: y / x, '__rtruediv__')
310+
__rfloordiv__ = _sparse_op_wrap(lambda x, y: y // x, 'floordiv')
309311
__rpow__ = _sparse_op_wrap(lambda x, y: y ** x, '__rpow__')
310312

311313
# Inplace operators
312314
__iadd__ = __add__
313315
__isub__ = __sub__
314316
__imul__ = __mul__
315-
__idiv__ = __div__
317+
__itruediv__ = __truediv__
318+
__ifloordiv__ = __floordiv__
316319
__ipow__ = __pow__
320+
321+
# Python 2 division operators
322+
if not py3compat.PY3:
323+
__div__ = _sparse_op_wrap(operator.div, 'div')
324+
__rdiv__ = _sparse_op_wrap(lambda x, y: y / x, '__rdiv__')
325+
__idiv__ = __div__
317326

318327
@property
319328
def values(self):

pandas/util/py3compat.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import sys
2+
3+
PY3 = (sys.version_info[0] >= 3)

0 commit comments

Comments
 (0)