@@ -920,71 +920,34 @@ def test_to_csv_path_is_none(self):
920
920
recons = pd .read_csv (StringIO (csv_str ), index_col = 0 )
921
921
assert_frame_equal (self .frame , recons )
922
922
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 ):
945
930
946
- def test_to_csv_compression_bz2 (self ):
947
- # GH7615
948
- # use the compression kw in to_csv
949
931
df = DataFrame ([[0.123456 , 0.234567 , 0.567567 ],
950
932
[12.32112 , 123123.2 , 321321.2 ]],
951
933
index = ['A' , 'B' ], columns = ['X' , 'Y' , 'Z' ])
952
934
953
935
with ensure_clean () as filename :
954
936
955
- df .to_csv (filename , compression = "bz2" )
937
+ df .to_csv (filename , compression = compression )
956
938
957
939
# 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 )
959
941
assert_frame_equal (df , rs )
960
942
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 )
964
945
text = f .read ().decode ('utf8' )
965
- f .close ()
966
946
for col in df .columns :
967
947
assert col in text
948
+ f .close ()
968
949
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 )
988
951
assert_frame_equal (df , read_csv (f , index_col = 0 ))
989
952
f .close ()
990
953
0 commit comments