|
1 | 1 | """ |
2 | | - Tests for the pandas.io.common functionalities |
| 2 | +Tests for the pandas.io.common functionalities |
3 | 3 | """ |
4 | 4 | import mmap |
5 | 5 | import os |
| 6 | + |
6 | 7 | import pytest |
7 | 8 |
|
8 | 9 | import pandas as pd |
@@ -286,99 +287,3 @@ def test_unknown_engine(self): |
286 | 287 | df.to_csv(path) |
287 | 288 | with tm.assert_raises_regex(ValueError, 'Unknown engine'): |
288 | 289 | pd.read_csv(path, engine='pyt') |
289 | | - |
290 | | - |
291 | | -@pytest.mark.parametrize('obj', [ |
292 | | - pd.DataFrame(100 * [[0.123456, 0.234567, 0.567567], |
293 | | - [12.32112, 123123.2, 321321.2]], |
294 | | - columns=['X', 'Y', 'Z']), |
295 | | - pd.Series(100 * [0.123456, 0.234567, 0.567567], name='X')]) |
296 | | -@pytest.mark.parametrize('method', ['to_pickle', 'to_json', 'to_csv']) |
297 | | -def test_compression_size(obj, method, compression_only): |
298 | | - |
299 | | - with tm.ensure_clean() as path: |
300 | | - getattr(obj, method)(path, compression=compression_only) |
301 | | - compressed = os.path.getsize(path) |
302 | | - getattr(obj, method)(path, compression=None) |
303 | | - uncompressed = os.path.getsize(path) |
304 | | - assert uncompressed > compressed |
305 | | - |
306 | | - |
307 | | -@pytest.mark.parametrize('obj', [ |
308 | | - pd.DataFrame(100 * [[0.123456, 0.234567, 0.567567], |
309 | | - [12.32112, 123123.2, 321321.2]], |
310 | | - columns=['X', 'Y', 'Z']), |
311 | | - pd.Series(100 * [0.123456, 0.234567, 0.567567], name='X')]) |
312 | | -@pytest.mark.parametrize('method', ['to_csv', 'to_json']) |
313 | | -def test_compression_size_fh(obj, method, compression_only): |
314 | | - |
315 | | - with tm.ensure_clean() as path: |
316 | | - f, handles = icom._get_handle(path, 'w', compression=compression_only) |
317 | | - with f: |
318 | | - getattr(obj, method)(f) |
319 | | - assert not f.closed |
320 | | - assert f.closed |
321 | | - compressed = os.path.getsize(path) |
322 | | - with tm.ensure_clean() as path: |
323 | | - f, handles = icom._get_handle(path, 'w', compression=None) |
324 | | - with f: |
325 | | - getattr(obj, method)(f) |
326 | | - assert not f.closed |
327 | | - assert f.closed |
328 | | - uncompressed = os.path.getsize(path) |
329 | | - assert uncompressed > compressed |
330 | | - |
331 | | - |
332 | | -@pytest.mark.parametrize('write_method, write_kwargs, read_method', [ |
333 | | - ('to_csv', {'index': False}, pd.read_csv), |
334 | | - ('to_json', {}, pd.read_json), |
335 | | - ('to_pickle', {}, pd.read_pickle), |
336 | | -]) |
337 | | -def test_dataframe_compression_defaults_to_infer( |
338 | | - write_method, write_kwargs, read_method, compression_only): |
339 | | - # Test that DataFrame.to_* methods default to inferring compression from |
340 | | - # paths. GH 22004 |
341 | | - input = pd.DataFrame([[1.0, 0, -4], [3.4, 5, 2]], columns=['X', 'Y', 'Z']) |
342 | | - extension = icom._compression_to_extension[compression_only] |
343 | | - with tm.ensure_clean('compressed' + extension) as path: |
344 | | - getattr(input, write_method)(path, **write_kwargs) |
345 | | - output = read_method(path, compression=compression_only) |
346 | | - tm.assert_frame_equal(output, input) |
347 | | - |
348 | | - |
349 | | -@pytest.mark.parametrize('write_method,write_kwargs,read_method,read_kwargs', [ |
350 | | - ('to_csv', {'index': False, 'header': True}, |
351 | | - pd.read_csv, {'squeeze': True}), |
352 | | - ('to_json', {}, pd.read_json, {'typ': 'series'}), |
353 | | - ('to_pickle', {}, pd.read_pickle, {}), |
354 | | -]) |
355 | | -def test_series_compression_defaults_to_infer( |
356 | | - write_method, write_kwargs, read_method, read_kwargs, |
357 | | - compression_only): |
358 | | - # Test that Series.to_* methods default to inferring compression from |
359 | | - # paths. GH 22004 |
360 | | - input = pd.Series([0, 5, -2, 10], name='X') |
361 | | - extension = icom._compression_to_extension[compression_only] |
362 | | - with tm.ensure_clean('compressed' + extension) as path: |
363 | | - getattr(input, write_method)(path, **write_kwargs) |
364 | | - output = read_method(path, compression=compression_only, **read_kwargs) |
365 | | - tm.assert_series_equal(output, input, check_names=False) |
366 | | - |
367 | | - |
368 | | -def test_compression_warning(compression_only): |
369 | | - # Assert that passing a file object to to_csv while explicitly specifying a |
370 | | - # compression protocol triggers a RuntimeWarning, as per GH 21227. |
371 | | - # Note that pytest has an issue that causes assert_produces_warning to fail |
372 | | - # in Python 2 if the warning has occurred in previous tests |
373 | | - # (see https://git.io/fNEBm & https://git.io/fNEBC). Hence, should this |
374 | | - # test fail in just Python 2 builds, it likely indicates that other tests |
375 | | - # are producing RuntimeWarnings, thereby triggering the pytest bug. |
376 | | - df = pd.DataFrame(100 * [[0.123456, 0.234567, 0.567567], |
377 | | - [12.32112, 123123.2, 321321.2]], |
378 | | - columns=['X', 'Y', 'Z']) |
379 | | - with tm.ensure_clean() as path: |
380 | | - f, handles = icom._get_handle(path, 'w', compression=compression_only) |
381 | | - with tm.assert_produces_warning(RuntimeWarning, |
382 | | - check_stacklevel=False): |
383 | | - with f: |
384 | | - df.to_csv(f, compression=compression_only) |
0 commit comments