Skip to content

Commit 44e933a

Browse files
authored
REF: simplify CSVFormatter (#36046)
* REF: extract properties cols and has_mi_columns * REF: extract property chunksize * REF: extract property quotechar * REF: extract properties data_index and nlevels * REF: refactor _save_chunk * REF: refactor _save * REF: extract method _save_body * REF: reorder _save-like methods * REF: extract compression property * REF: Extract property index_label * REF: extract helper properties * REF: delete local variables in _save_header * REF: extract method _get_header_rows * REF: move check for header into _save function * TYP: add several type annotations * FIX: fix index labels * FIX: fix multiindex * FIX: fix test failures on compression Needed to eliminate compression setter due to the interdependencies between ioargs and compression. * REF: eliminate preallocation of self.data * REF: extract method _convert_to_native_types * REF: rename regular -> flat as reviewed * TYP: add type annotations as reviewed * REF: refactor number formatting Replace _convert_to_native_types method in favor of a number formatting dictionary. * FIX: mypy error with index_label * FIX: reorder if-statements in index_label To make sure that the newer mypy (v0.782) passes. * TYP: move IndexLabel to pandas._typing This eliminates repetition of the type annotations for index label in multiple places. * TYP: quotechar, has_mi_columns, _need_to_save... * TYP: chunksize, but ignored assignment check For some reason mypy would not recognize that chunksize turns from Optional[int] to int inside the setter. Even setting an intentional assertion ``assert chunksize is not None`` does not help. * TYP: cols property Limitations: - ignore type[assignment] error. - Created additional method _refine_cols to allow conversion from Optional[Sequence[Label]] to Sequence[Label]. * TYP: nlevels and _has_aliases * CLN: move GH21227 check to pandas/io/common.py * TYP: remove redundant bool from IndexLabel type * TYP: add to _get_index_label... methods * TYP: use Iterator instead of Generator * TYP: explicitly use List type * TYP: correct dict typing * TYP: remaining properties
1 parent b5a5268 commit 44e933a

File tree

4 files changed

+202
-179
lines changed

4 files changed

+202
-179
lines changed

pandas/_typing.py

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
List,
1616
Mapping,
1717
Optional,
18+
Sequence,
1819
Type,
1920
TypeVar,
2021
Union,
@@ -82,6 +83,7 @@
8283

8384
Axis = Union[str, int]
8485
Label = Optional[Hashable]
86+
IndexLabel = Optional[Union[Label, Sequence[Label]]]
8587
Level = Union[Label, int]
8688
Ordered = Optional[bool]
8789
JSONSerializable = Optional[Union[PythonScalar, List, Dict]]

pandas/core/generic.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
CompressionOptions,
4141
FilePathOrBuffer,
4242
FrameOrSeries,
43+
IndexLabel,
4344
JSONSerializable,
4445
Label,
4546
Level,
@@ -3160,7 +3161,7 @@ def to_csv(
31603161
columns: Optional[Sequence[Label]] = None,
31613162
header: Union[bool_t, List[str]] = True,
31623163
index: bool_t = True,
3163-
index_label: Optional[Union[bool_t, str, Sequence[Label]]] = None,
3164+
index_label: IndexLabel = None,
31643165
mode: str = "w",
31653166
encoding: Optional[str] = None,
31663167
compression: CompressionOptions = "infer",

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

0 commit comments

Comments
 (0)