|
11 | 11 | from pandas.compat import range, lmap |
12 | 12 | import pandas.core.common as com |
13 | 13 | from pandas.core import ops |
| 14 | +from pandas.io.common import _get_handle |
14 | 15 | import pandas.util.testing as tm |
15 | 16 |
|
16 | 17 |
|
@@ -246,19 +247,34 @@ def test_compression_size(obj, method, compression_only): |
246 | 247 | [12.32112, 123123.2, 321321.2]], |
247 | 248 | columns=['X', 'Y', 'Z']), |
248 | 249 | Series(100 * [0.123456, 0.234567, 0.567567], name='X')]) |
249 | | -@pytest.mark.parametrize('method', ['to_csv']) |
| 250 | +@pytest.mark.parametrize('method', ['to_csv', 'to_json']) |
250 | 251 | def test_compression_size_fh(obj, method, compression_only): |
251 | 252 |
|
252 | 253 | with tm.ensure_clean() as filename: |
253 | | - with open(filename, 'w') as fh: |
254 | | - getattr(obj, method)(fh, compression=compression_only) |
255 | | - assert not fh.closed |
256 | | - assert fh.closed |
| 254 | + f, _handles = _get_handle(filename, 'w', compression=compression_only) |
| 255 | + with f: |
| 256 | + getattr(obj, method)(f) |
| 257 | + assert not f.closed |
| 258 | + assert f.closed |
257 | 259 | compressed = os.path.getsize(filename) |
258 | 260 | with tm.ensure_clean() as filename: |
259 | | - with open(filename, 'w') as fh: |
260 | | - getattr(obj, method)(fh, compression=None) |
261 | | - assert not fh.closed |
262 | | - assert fh.closed |
| 261 | + f, _handles = _get_handle(filename, 'w', compression=None) |
| 262 | + with f: |
| 263 | + getattr(obj, method)(f) |
| 264 | + assert not f.closed |
| 265 | + assert f.closed |
263 | 266 | uncompressed = os.path.getsize(filename) |
264 | 267 | assert uncompressed > compressed |
| 268 | + |
| 269 | + |
| 270 | +# GH 21227 |
| 271 | +def test_compression_warning(compression_only): |
| 272 | + df = DataFrame(100 * [[0.123456, 0.234567, 0.567567], |
| 273 | + [12.32112, 123123.2, 321321.2]], |
| 274 | + columns=['X', 'Y', 'Z']) |
| 275 | + with tm.ensure_clean() as filename: |
| 276 | + f, _handles = _get_handle(filename, 'w', compression=compression_only) |
| 277 | + with tm.assert_produces_warning(RuntimeWarning, |
| 278 | + check_stacklevel=False): |
| 279 | + with f: |
| 280 | + df.to_csv(f, compression=compression_only) |
0 commit comments