Skip to content

Commit d3dac82

Browse files
committed
use _get_handle to produce file handle
1 parent d11dcae commit d3dac82

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

pandas/tests/frame/test_to_csv.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import numpy as np
1010

1111
from pandas.compat import (lmap, range, lrange, StringIO, u)
12+
from pandas.io.common import _get_handle
1213
import pandas.core.common as com
1314
from pandas.errors import ParserError
1415
from pandas import (DataFrame, Index, Series, MultiIndex, Timestamp,
@@ -935,17 +936,19 @@ def test_to_csv_compression(self, df, encoding, compression):
935936
with ensure_clean() as filename:
936937

937938
df.to_csv(filename, compression=compression, encoding=encoding)
938-
939939
# test the round trip - to_csv -> read_csv
940940
result = read_csv(filename, compression=compression,
941941
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)
948942
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)
949952
assert_frame_equal(df, result_fh)
950953

951954
# explicitly make sure file is compressed

pandas/tests/series/test_io.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pandas import Series, DataFrame
1212

1313
from pandas.compat import StringIO, u
14+
from pandas.io.common import _get_handle
1415
from pandas.util.testing import (assert_series_equal, assert_almost_equal,
1516
assert_frame_equal, ensure_clean)
1617
import pandas.util.testing as tm
@@ -151,19 +152,19 @@ def test_to_csv_compression(self, s, encoding, compression):
151152

152153
s.to_csv(filename, compression=compression, encoding=encoding,
153154
header=True)
154-
155155
# test the round trip - to_csv -> read_csv
156156
result = pd.read_csv(filename, compression=compression,
157157
encoding=encoding, index_col=0, squeeze=True)
158+
assert_series_equal(s, result)
158159

159-
with open(filename, 'w') as fh:
160-
s.to_csv(fh, compression=compression, encoding=encoding,
161-
header=True)
162-
160+
# test the round trip using file handle - to_csv -> read_csv
161+
f, _handles = _get_handle(filename, 'w', compression=compression,
162+
encoding=encoding)
163+
with f:
164+
s.to_csv(f, encoding=encoding, header=True)
163165
result_fh = pd.read_csv(filename, compression=compression,
164166
encoding=encoding, index_col=0,
165167
squeeze=True)
166-
assert_series_equal(s, result)
167168
assert_series_equal(s, result_fh)
168169

169170
# explicitly ensure file was compressed

0 commit comments

Comments
 (0)