Skip to content

Commit c08fa82

Browse files
committed
TST: Clean up DataFrame.to_csv compression tests
1 parent 4ebdc50 commit c08fa82

File tree

1 file changed

+13
-50
lines changed

1 file changed

+13
-50
lines changed

pandas/tests/frame/test_to_csv.py

+13-50
Original file line numberDiff line numberDiff line change
@@ -920,71 +920,34 @@ def test_to_csv_path_is_none(self):
920920
recons = pd.read_csv(StringIO(csv_str), index_col=0)
921921
assert_frame_equal(self.frame, recons)
922922

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
923+
@pytest.mark.parametrize('compression', [
924+
None,
925+
'gzip',
926+
'bz2',
927+
pytest.param('xz', marks=td.skip_if_no_lzma),
928+
])
929+
def test_to_csv_compression(self, compression):
945930

946-
def test_to_csv_compression_bz2(self):
947-
# GH7615
948-
# use the compression kw in to_csv
949931
df = DataFrame([[0.123456, 0.234567, 0.567567],
950932
[12.32112, 123123.2, 321321.2]],
951933
index=['A', 'B'], columns=['X', 'Y', 'Z'])
952934

953935
with ensure_clean() as filename:
954936

955-
df.to_csv(filename, compression="bz2")
937+
df.to_csv(filename, compression=compression)
956938

957939
# test the round trip - to_csv -> read_csv
958-
rs = read_csv(filename, compression="bz2", index_col=0)
940+
rs = read_csv(filename, compression=compression, index_col=0)
959941
assert_frame_equal(df, rs)
960942

961-
# explicitly make sure file is bz2ed
962-
import bz2
963-
f = bz2.BZ2File(filename, 'rb')
943+
# explicitly make sure file is compressed
944+
f = tm.decompress_file(filename, compression)
964945
text = f.read().decode('utf8')
965-
f.close()
966946
for col in df.columns:
967947
assert col in text
948+
f.close()
968949

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)
984-
985-
# explicitly make sure file is xzipped
986-
lzma = compat.import_lzma()
987-
f = lzma.open(filename, 'rb')
950+
f = tm.decompress_file(filename, compression)
988951
assert_frame_equal(df, read_csv(f, index_col=0))
989952
f.close()
990953

0 commit comments

Comments
 (0)