@@ -7328,6 +7328,63 @@ def test_to_csv_path_is_none(self):
7328
7328
recons = pd.read_csv(StringIO(csv_str), index_col=0)
7329
7329
assert_frame_equal(self.frame, recons)
7330
7330
7331
+ def test_to_csv_compression_gzip(self):
7332
+ ## GH7615
7333
+ ## use the compression kw in to_csv
7334
+ df = DataFrame([[0.123456, 0.234567, 0.567567],
7335
+ [12.32112, 123123.2, 321321.2]],
7336
+ index=['A', 'B'], columns=['X', 'Y', 'Z'])
7337
+
7338
+ with ensure_clean() as filename:
7339
+
7340
+ df.to_csv(filename, compression="gzip")
7341
+
7342
+ # test the round trip - to_csv -> read_csv
7343
+ rs = read_csv(filename, compression="gzip", index_col=0)
7344
+ assert_frame_equal(df, rs)
7345
+
7346
+ # explicitly make sure file is gziped
7347
+ import gzip
7348
+ f = gzip.open(filename, 'rb')
7349
+ text = f.read().decode('utf8')
7350
+ f.close()
7351
+ for col in df.columns:
7352
+ self.assertIn(col, text)
7353
+
7354
+ def test_to_csv_compression_bz2(self):
7355
+ ## GH7615
7356
+ ## use the compression kw in to_csv
7357
+ df = DataFrame([[0.123456, 0.234567, 0.567567],
7358
+ [12.32112, 123123.2, 321321.2]],
7359
+ index=['A', 'B'], columns=['X', 'Y', 'Z'])
7360
+
7361
+ with ensure_clean() as filename:
7362
+
7363
+ df.to_csv(filename, compression="bz2")
7364
+
7365
+ # test the round trip - to_csv -> read_csv
7366
+ rs = read_csv(filename, compression="bz2", index_col=0)
7367
+ assert_frame_equal(df, rs)
7368
+
7369
+ # explicitly make sure file is bz2ed
7370
+ import bz2
7371
+ f = bz2.BZ2File(filename, 'rb')
7372
+ text = f.read().decode('utf8')
7373
+ f.close()
7374
+ for col in df.columns:
7375
+ self.assertIn(col, text)
7376
+
7377
+ def test_to_csv_compression_value_error(self):
7378
+ ## GH7615
7379
+ ## use the compression kw in to_csv
7380
+ df = DataFrame([[0.123456, 0.234567, 0.567567],
7381
+ [12.32112, 123123.2, 321321.2]],
7382
+ index=['A', 'B'], columns=['X', 'Y', 'Z'])
7383
+
7384
+ with ensure_clean() as filename:
7385
+ # zip compression is not supported and should raise ValueError
7386
+ self.assertRaises(ValueError, df.to_csv, filename, compression="zip")
7387
+
7331
7388
def test_info(self):
7332
7389
io = StringIO()
7333
7390
self.frame.info(buf=io)
0 commit comments