|
9 | 9 | import numpy as np |
10 | 10 |
|
11 | 11 | from pandas.compat import (lmap, range, lrange, StringIO, u) |
| 12 | +from pandas.io.common import _get_handle |
12 | 13 | import pandas.core.common as com |
13 | 14 | from pandas.errors import ParserError |
14 | 15 | from pandas import (DataFrame, Index, Series, MultiIndex, Timestamp, |
@@ -935,17 +936,19 @@ def test_to_csv_compression(self, df, encoding, compression): |
935 | 936 | with ensure_clean() as filename: |
936 | 937 |
|
937 | 938 | df.to_csv(filename, compression=compression, encoding=encoding) |
938 | | - |
939 | 939 | # test the round trip - to_csv -> read_csv |
940 | 940 | result = read_csv(filename, compression=compression, |
941 | 941 | index_col=0, encoding=encoding) |
942 | | - |
943 | | - with open(filename, 'w') as fh: |
944 | | - df.to_csv(fh, compression=compression, encoding=encoding) |
945 | | - |
946 | | - result_fh = read_csv(filename, compression=compression, |
947 | | - index_col=0, encoding=encoding) |
948 | 942 | assert_frame_equal(df, result) |
| 943 | + |
| 944 | + # test the round trip using file handle - to_csv -> read_csv |
| 945 | + f, _handles = _get_handle(filename, 'w', compression=compression, |
| 946 | + encoding=encoding) |
| 947 | + with f: |
| 948 | + df.to_csv(f, encoding=encoding) |
| 949 | + result_fh = pd.read_csv(filename, compression=compression, |
| 950 | + encoding=encoding, index_col=0, |
| 951 | + squeeze=True) |
949 | 952 | assert_frame_equal(df, result_fh) |
950 | 953 |
|
951 | 954 | # explicitly make sure file is compressed |
|
0 commit comments