|
20 | 20 | from distutils.version import LooseVersion
|
21 | 21 | import pandas as pd
|
22 | 22 | 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 |
24 | 24 | import pandas
|
25 | 25 | import pandas.util.testing as tm
|
26 | 26 | from pandas.tseries.offsets import Day, MonthEnd
|
27 | 27 | import shutil
|
| 28 | +import sys |
28 | 29 |
|
29 | 30 |
|
30 | 31 | @pytest.fixture(scope='module')
|
@@ -489,3 +490,37 @@ def test_read_infer(self, ext, get_random_path):
|
489 | 490 | df2 = pd.read_pickle(p2)
|
490 | 491 |
|
491 | 492 | 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