Skip to content

Commit faf1b4b

Browse files
committed
REF: extract compression property once again
Compression property was previously eliminated after merging master. The current implementation once again consolidates data validation inside the setter.
1 parent 9fd8d13 commit faf1b4b

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

pandas/io/formats/csvs.py

+23-14
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,17 @@ def __init__(
6060
if path_or_buf is None:
6161
path_or_buf = StringIO()
6262

63-
# Extract compression mode as given, if dict
64-
compression, self.compression_args = get_compression_method(compression)
65-
self.compression = infer_compression(path_or_buf, compression)
66-
6763
ioargs = get_filepath_or_buffer(
6864
path_or_buf,
6965
encoding=encoding,
70-
compression=self.compression,
66+
compression=compression,
7167
mode=mode,
7268
storage_options=storage_options,
7369
)
7470
self.path_or_buf = ioargs.filepath_or_buffer
7571
self.should_close = ioargs.should_close
7672
self.mode = ioargs.mode
7773

78-
# GH21227 internal compression is not used for non-binary handles.
79-
if compression and hasattr(self.path_or_buf, "write") and "b" not in self.mode:
80-
warnings.warn(
81-
"compression has no effect when passing a non-binary object as input.",
82-
RuntimeWarning,
83-
stacklevel=2,
84-
)
85-
compression = None
86-
8774
self.sep = sep
8875
self.na_rep = na_rep
8976
self.float_format = float_format
@@ -93,6 +80,7 @@ def __init__(
9380
self.index_label = index_label
9481
self.encoding = encoding or "utf-8"
9582
self.errors = errors
83+
self.compression = compression
9684
self.quoting = quoting or csvlib.QUOTE_MINIMAL
9785
self.quotechar = quotechar
9886
self.doublequote = doublequote
@@ -106,6 +94,27 @@ def __init__(
10694
ncols = self.obj.shape[-1]
10795
self.data = [None] * ncols
10896

97+
@property
98+
def compression(self):
99+
return self._compression
100+
101+
@compression.setter
102+
def compression(self, compression):
103+
# Extract compression mode as given, if dict
104+
compression, self.compression_args = get_compression_method(compression)
105+
compression = infer_compression(self.path_or_buf, compression)
106+
107+
# GH21227 internal compression is not used for non-binary handles.
108+
if compression and hasattr(self.path_or_buf, "write") and "b" not in self.mode:
109+
warnings.warn(
110+
"compression has no effect when passing a non-binary object as input.",
111+
RuntimeWarning,
112+
stacklevel=2,
113+
)
114+
compression = None
115+
116+
self._compression = compression
117+
109118
@property
110119
def index_label(self):
111120
return self._index_label

0 commit comments

Comments
 (0)