Skip to content

Commit 3bb65a8

Browse files
takluyverwesm
authored andcommitted
TST: Fixes for tests on Python 3.
Conflicts: pandas/tests/test_frame.py
1 parent 8c1292d commit 3bb65a8

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

pandas/core/common.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
"""
22
Misc tools for implementing data structures
33
"""
4-
import cPickle
4+
try:
5+
import cPickle as pickle
6+
except ImportError:
7+
import pickle
8+
59
try:
610
from io import BytesIO
711
except ImportError: # pragma: no cover
@@ -16,6 +20,7 @@
1620
import math
1721

1822
import pandas._tseries as lib
23+
from pandas.util import py3compat
1924

2025
# XXX: HACK for NumPy 1.5.1 to suppress warnings
2126
try:
@@ -788,7 +793,7 @@ def save(obj, path):
788793
"""
789794
f = open(path, 'wb')
790795
try:
791-
cPickle.dump(obj, f, protocol=cPickle.HIGHEST_PROTOCOL)
796+
pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL)
792797
finally:
793798
f.close()
794799

@@ -809,13 +814,13 @@ def load(path):
809814
"""
810815
f = open(path, 'rb')
811816
try:
812-
return cPickle.load(f)
817+
return pickle.load(f)
813818
finally:
814819
f.close()
815820

816821

817822
def console_encode(value):
818-
if not isinstance(value, unicode):
823+
if py3compat.PY3 or not isinstance(value, unicode):
819824
return value
820825

821826
try:

pandas/tests/test_frame.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
assert_frame_equal)
2525

2626
import pandas.util.testing as tm
27+
from pandas.util import py3compat
2728
import pandas._tseries as lib
2829

2930
#-------------------------------------------------------------------------------
@@ -1844,10 +1845,13 @@ def test_to_string_unicode_two(self):
18441845
def test_to_string_with_formatters_unicode(self):
18451846
df = DataFrame({u'c/\u03c3':[1,2,3]})
18461847
result = df.to_string(formatters={u'c/\u03c3': lambda x: '%s' % x})
1847-
assert(result in
1848-
(' c/\xcf\x83\n0 1 \n1 2 \n2 3 ',
1849-
u' c/\u03c3\n0 1 \n1 2 \n2 3 '.encode('cp437', 'ignore'),
1850-
' c/?\n0 1 \n1 2 \n2 3 ' ))
1848+
cp437 = u' c/\u03c3\n0 1 \n1 2 \n2 3 '.encode('cp437', 'ignore')
1849+
if py3compat.PY3:
1850+
self.assertEqual(result, u' c/\u03c3\n0 1 \n1 2 \n2 3 ')
1851+
else:
1852+
assert(result in
1853+
(' c/\xcf\x83\n0 1 \n1 2 \n2 3 ', cp437,
1854+
' c/?\n0 1 \n1 2 \n2 3 ' ))
18511855

18521856
def test_head_tail(self):
18531857
assert_frame_equal(self.frame.head(), self.frame[:5])

0 commit comments

Comments
 (0)