Skip to content

Commit 35f8d18

Browse files
committed
Added tests for new 'protocol' parameter in 'to_pickle'.
1 parent 4bf0386 commit 35f8d18

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

pandas/tests/io/test_pickle.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
from distutils.version import LooseVersion
2121
import pandas as pd
2222
from pandas import Index
23-
from pandas.compat import is_platform_little_endian
23+
from pandas.compat import is_platform_little_endian, cPickle as pkl
2424
import pandas
2525
import pandas.util.testing as tm
2626
from pandas.tseries.offsets import Day, MonthEnd
2727
import shutil
28+
import sys
2829

2930

3031
@pytest.fixture(scope='module')
@@ -489,3 +490,37 @@ def test_read_infer(self, ext, get_random_path):
489490
df2 = pd.read_pickle(p2)
490491

491492
tm.assert_frame_equal(df, df2)
493+
494+
495+
# ---------------------
496+
# test pickle compression
497+
# ---------------------
498+
499+
class TestProtocol(object):
500+
501+
@pytest.mark.parametrize('protocol', [-1, 0, 1, 2])
502+
def test_read(self, protocol, get_random_path):
503+
with tm.ensure_clean(get_random_path) as path:
504+
df = tm.makeDataFrame()
505+
df.to_pickle(path, protocol=protocol)
506+
df2 = pd.read_pickle(path)
507+
tm.assert_frame_equal(df, df2)
508+
509+
@pytest.mark.parametrize('protocol', [3, 4])
510+
@pytest.mark.skipif(sys.version_info[:2] >= (3, 4),
511+
reason="Testing invalid parameters for "
512+
"Python 2.x and 3.y (y < 4).")
513+
def test_read_bad_versions(self, protocol, get_random_path):
514+
# For Python 2.x (respectively 3.y with y < 4), [expected]
515+
# HIGHEST_PROTOCOL should be 2 (respectively 3). Hence, the protocol
516+
# parameter should not exceed 2 (respectively 3).
517+
if sys.version_info[:2] < (3, 0):
518+
expect_hp = 2
519+
else:
520+
expect_hp = 3
521+
with tm.assert_raises_regex(ValueError,
522+
"pickle protocol must be <= %d" %
523+
expect_hp):
524+
with tm.ensure_clean(get_random_path) as path:
525+
df = tm.makeDataFrame()
526+
df.to_pickle(path, protocol=protocol)

0 commit comments

Comments
 (0)