Skip to content

Commit ca888c1

Browse files
committed
CLN: move GH21227 check to pandas/io/common.py
1 parent b381e8a commit ca888c1

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

pandas/io/common.py

+15
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,21 @@ def get_filepath_or_buffer(
208208
# handle compression dict
209209
compression_method, compression = get_compression_method(compression)
210210
compression_method = infer_compression(filepath_or_buffer, compression_method)
211+
212+
# GH21227 internal compression is not used for non-binary handles.
213+
if (
214+
compression_method
215+
and hasattr(filepath_or_buffer, "write")
216+
and mode
217+
and "b" not in mode
218+
):
219+
warnings.warn(
220+
"compression has no effect when passing a non-binary object as input.",
221+
RuntimeWarning,
222+
stacklevel=2,
223+
)
224+
compression_method = None
225+
211226
compression = dict(compression, method=compression_method)
212227

213228
# bz2 and xz do not write the byte order mark for utf-16 and utf-32

pandas/io/formats/csvs.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from io import StringIO, TextIOWrapper
77
import os
88
from typing import Generator, Hashable, List, Optional, Sequence, Union
9-
import warnings
109

1110
import numpy as np
1211

@@ -59,6 +58,7 @@ def __init__(
5958
storage_options: StorageOptions = None,
6059
):
6160
self.obj = obj
61+
6262
self.encoding = encoding or "utf-8"
6363

6464
if path_or_buf is None:
@@ -71,25 +71,13 @@ def __init__(
7171
mode=mode,
7272
storage_options=storage_options,
7373
)
74+
7475
self.compression = ioargs.compression.pop("method")
7576
self.compression_args = ioargs.compression
7677
self.path_or_buf = ioargs.filepath_or_buffer
7778
self.should_close = ioargs.should_close
7879
self.mode = ioargs.mode
7980

80-
# GH21227 internal compression is not used for non-binary handles.
81-
if (
82-
self.compression
83-
and hasattr(self.path_or_buf, "write")
84-
and "b" not in self.mode
85-
):
86-
warnings.warn(
87-
"compression has no effect when passing a non-binary object as input.",
88-
RuntimeWarning,
89-
stacklevel=2,
90-
)
91-
self.compression = None
92-
9381
self.sep = sep
9482
self.na_rep = na_rep
9583
self.float_format = float_format

0 commit comments

Comments
 (0)