7
7
8
8
import numpy as np
9
9
10
+ import pandas
10
11
from pandas import Series , DataFrame , Timestamp
11
12
from pandas .compat import range , lmap
12
13
import pandas .core .common as com
@@ -222,19 +223,12 @@ def test_standardize_mapping():
222
223
def test_compression_size (obj , method , compression_only ):
223
224
# Tests that compression is occurring by comparing to the bytes on disk of
224
225
# the uncompressed file.
225
- extension = _compression_to_extension [compression_only ]
226
- to_method = getattr (obj , method )
227
- with tm .ensure_clean ('no-compression' ) as path :
228
- to_method (path , compression = None )
229
- no_compression_size = os .path .getsize (path )
230
- with tm .ensure_clean ('explicit-compression' + extension ) as path :
231
- to_method (path , compression = compression_only )
232
- explicit_compression_size = os .path .getsize (path )
233
- with tm .ensure_clean ('inferred-compression' + extension ) as path :
234
- to_method (path ) # assumes that compression='infer' is the default
235
- inferred_compression_size = os .path .getsize (path )
236
- assert (no_compression_size > explicit_compression_size ==
237
- inferred_compression_size )
226
+ with tm .ensure_clean () as filename :
227
+ getattr (obj , method )(filename , compression = compression_only )
228
+ compressed = os .path .getsize (filename )
229
+ getattr (obj , method )(filename , compression = None )
230
+ uncompressed = os .path .getsize (filename )
231
+ assert uncompressed > compressed
238
232
239
233
240
234
@pytest .mark .parametrize ('obj' , [
@@ -244,6 +238,7 @@ def test_compression_size(obj, method, compression_only):
244
238
Series (100 * [0.123456 , 0.234567 , 0.567567 ], name = 'X' )])
245
239
@pytest .mark .parametrize ('method' , ['to_csv' , 'to_json' ])
246
240
def test_compression_size_fh (obj , method , compression_only ):
241
+
247
242
with tm .ensure_clean () as filename :
248
243
f , _handles = _get_handle (filename , 'w' , compression = compression_only )
249
244
with f :
@@ -261,6 +256,30 @@ def test_compression_size_fh(obj, method, compression_only):
261
256
assert uncompressed > compressed
262
257
263
258
259
+ @pytest .mark .parametrize ('input' , [
260
+ DataFrame ([[1.0 , 0 , - 4.4 ],
261
+ [3.4 , 5 , 2.4 ]], columns = ['X' , 'Y' , 'Z' ]),
262
+ Series ([0 , 1 , 2 , 4 ], name = 'X' ),
263
+ ])
264
+ @pytest .mark .parametrize ('methods' , [
265
+ ('to_csv' , pandas .read_csv ),
266
+ ('to_json' , pandas .read_json ),
267
+ ('to_pickle' , pandas .read_pickle ),
268
+ ])
269
+ def test_compression_defaults_to_infer (input , methods , compression_only ):
270
+ # Test that to_* methods default to inferring compression from paths.
271
+ # https://github.com/pandas-dev/pandas/pull/22011
272
+ write_method , read_method = methods
273
+ extension = _compression_to_extension [compression_only ]
274
+ with tm .ensure_clean ('compressed' + extension ) as path :
275
+ # assumes that compression='infer' is the default
276
+ getattr (input , write_method )(path )
277
+ output = read_method (path , compression = compression_only )
278
+ assert_equals = (tm .assert_frame_equal if isinstance (input , DataFrame )
279
+ else tm .assert_series_equal )
280
+ assert_equals (output , input )
281
+
282
+
264
283
def test_compression_warning (compression_only ):
265
284
# Assert that passing a file object to to_csv while explicitly specifying a
266
285
# compression protocol triggers a RuntimeWarning, as per
0 commit comments