Skip to content

Commit 0028df7

Browse files
committed
Add test for bz2; mypy ignore non-typesafe use of kwargs
1 parent a7ee476 commit 0028df7

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

pandas/io/common.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,16 +403,20 @@ def get_handle(
403403
# GZ Compression
404404
if compression == "gzip":
405405
if is_path:
406-
f = gzip.open(path_or_buf, mode, **compression_args)
406+
f = gzip.open(
407+
path_or_buf, mode, **compression_args) # type: ignore
407408
else:
408-
f = gzip.GzipFile(fileobj=path_or_buf, **compression_args)
409+
f = gzip.GzipFile(
410+
fileobj=path_or_buf, **compression_args) # type: ignore
409411

410412
# BZ Compression
411413
elif compression == "bz2":
412414
if is_path:
413-
f = bz2.BZ2File(path_or_buf, mode, **compression_args)
415+
f = bz2.BZ2File(
416+
path_or_buf, mode, **compression_args) # type: ignore
414417
else:
415-
f = bz2.BZ2File(path_or_buf, **compression_args)
418+
f = bz2.BZ2File(
419+
path_or_buf, **compression_args) # type: ignore
416420

417421
# ZIP Compression
418422
elif compression == "zip":

pandas/tests/io/test_compression.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,33 @@ def test_with_missing_lzma_runtime():
156156
],
157157
)
158158
@pytest.mark.parametrize("method", ["to_pickle", "to_json", "to_csv"])
159-
def test_gzip_compression_level_path(obj, method):
160-
"""GH#33398 Ideally this test should be repeated for bz2 as well,
161-
but that is not practical because a file size of >100k is needed to see any
162-
size difference between bz2 compression settings."""
159+
def test_gzip_compression_level(obj, method):
160+
#GH33196
163161
with tm.ensure_clean() as path:
164162
getattr(obj, method)(path, compression="gzip")
165163
compressed_size_default = os.path.getsize(path)
166-
getattr(obj, method)(path, compression={"method": "gzip", "compresslevel": 1})
164+
getattr(obj, method)(
165+
path, compression={ "method": "gzip", "compresslevel": 1})
167166
compressed_size_fast = os.path.getsize(path)
168167
assert compressed_size_default < compressed_size_fast
168+
169+
170+
@pytest.mark.parametrize(
171+
"obj",
172+
[
173+
pd.DataFrame(
174+
100 * [[0.123456, 0.234567, 0.567567], [12.32112, 123123.2, 321321.2]],
175+
columns=["X", "Y", "Z"],
176+
),
177+
pd.Series(100 * [0.123456, 0.234567, 0.567567], name="X"),
178+
],
179+
)
180+
@pytest.mark.parametrize("method", ["to_pickle", "to_json", "to_csv"])
181+
def test_bzip_compression_level(obj, method):
182+
"""GH33196 bzip needs file size > 100k to show a size difference between
183+
compression levels, so here we just check if the call works when
184+
compression is passed as a dict.
185+
"""
186+
with tm.ensure_clean() as path:
187+
getattr(obj, method)(
188+
path, compression={ "method": "bz2", "compresslevel": 1})

0 commit comments

Comments
 (0)