|
21 | 21 | ensure_clean,
|
22 | 22 | makeCustomDataframe as mkdf)
|
23 | 23 | import pandas.util.testing as tm
|
24 |
| -import pandas.util._test_decorators as td |
25 | 24 |
|
26 | 25 | from pandas.tests.frame.common import TestData
|
27 | 26 |
|
@@ -920,73 +919,28 @@ def test_to_csv_path_is_none(self):
|
920 | 919 | recons = pd.read_csv(StringIO(csv_str), index_col=0)
|
921 | 920 | assert_frame_equal(self.frame, recons)
|
922 | 921 |
|
923 |
| - def test_to_csv_compression_gzip(self): |
924 |
| - # GH7615 |
925 |
| - # use the compression kw in to_csv |
926 |
| - df = DataFrame([[0.123456, 0.234567, 0.567567], |
927 |
| - [12.32112, 123123.2, 321321.2]], |
928 |
| - index=['A', 'B'], columns=['X', 'Y', 'Z']) |
929 |
| - |
930 |
| - with ensure_clean() as filename: |
931 |
| - |
932 |
| - df.to_csv(filename, compression="gzip") |
933 |
| - |
934 |
| - # test the round trip - to_csv -> read_csv |
935 |
| - rs = read_csv(filename, compression="gzip", index_col=0) |
936 |
| - assert_frame_equal(df, rs) |
937 |
| - |
938 |
| - # explicitly make sure file is gziped |
939 |
| - import gzip |
940 |
| - f = gzip.open(filename, 'rb') |
941 |
| - text = f.read().decode('utf8') |
942 |
| - f.close() |
943 |
| - for col in df.columns: |
944 |
| - assert col in text |
| 922 | + def test_to_csv_compression(self, compression): |
945 | 923 |
|
946 |
| - def test_to_csv_compression_bz2(self): |
947 |
| - # GH7615 |
948 |
| - # use the compression kw in to_csv |
949 | 924 | df = DataFrame([[0.123456, 0.234567, 0.567567],
|
950 | 925 | [12.32112, 123123.2, 321321.2]],
|
951 | 926 | index=['A', 'B'], columns=['X', 'Y', 'Z'])
|
952 | 927 |
|
953 | 928 | with ensure_clean() as filename:
|
954 | 929 |
|
955 |
| - df.to_csv(filename, compression="bz2") |
| 930 | + df.to_csv(filename, compression=compression) |
956 | 931 |
|
957 | 932 | # test the round trip - to_csv -> read_csv
|
958 |
| - rs = read_csv(filename, compression="bz2", index_col=0) |
| 933 | + rs = read_csv(filename, compression=compression, index_col=0) |
959 | 934 | assert_frame_equal(df, rs)
|
960 | 935 |
|
961 |
| - # explicitly make sure file is bz2ed |
962 |
| - import bz2 |
963 |
| - f = bz2.BZ2File(filename, 'rb') |
964 |
| - text = f.read().decode('utf8') |
965 |
| - f.close() |
966 |
| - for col in df.columns: |
967 |
| - assert col in text |
968 |
| - |
969 |
| - @td.skip_if_no_lzma |
970 |
| - def test_to_csv_compression_xz(self): |
971 |
| - # GH11852 |
972 |
| - # use the compression kw in to_csv |
973 |
| - df = DataFrame([[0.123456, 0.234567, 0.567567], |
974 |
| - [12.32112, 123123.2, 321321.2]], |
975 |
| - index=['A', 'B'], columns=['X', 'Y', 'Z']) |
976 |
| - |
977 |
| - with ensure_clean() as filename: |
978 |
| - |
979 |
| - df.to_csv(filename, compression="xz") |
980 |
| - |
981 |
| - # test the round trip - to_csv -> read_csv |
982 |
| - rs = read_csv(filename, compression="xz", index_col=0) |
983 |
| - assert_frame_equal(df, rs) |
| 936 | + # explicitly make sure file is compressed |
| 937 | + with tm.decompress_file(filename, compression) as fh: |
| 938 | + text = fh.read().decode('utf8') |
| 939 | + for col in df.columns: |
| 940 | + assert col in text |
984 | 941 |
|
985 |
| - # explicitly make sure file is xzipped |
986 |
| - lzma = compat.import_lzma() |
987 |
| - f = lzma.open(filename, 'rb') |
988 |
| - assert_frame_equal(df, read_csv(f, index_col=0)) |
989 |
| - f.close() |
| 942 | + with tm.decompress_file(filename, compression) as fh: |
| 943 | + assert_frame_equal(df, read_csv(fh, index_col=0)) |
990 | 944 |
|
991 | 945 | def test_to_csv_compression_value_error(self):
|
992 | 946 | # GH7615
|
|
0 commit comments