Skip to content

Commit 337bfeb

Browse files
Add errors argument to to_csv() call to enable error handling for encoders
1 parent 89a8295 commit 337bfeb

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

pandas/core/generic.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,6 +3028,7 @@ def to_csv(
30283028
index_label: Optional[Union[bool_t, str, Sequence[Label]]] = None,
30293029
mode: str = "w",
30303030
encoding: Optional[str] = None,
3031+
errors: str = "strict",
30313032
compression: Optional[Union[str, Mapping[str, str]]] = "infer",
30323033
quoting: Optional[int] = None,
30333034
quotechar: str = '"',
@@ -3081,6 +3082,10 @@ def to_csv(
30813082
for easier importing in R.
30823083
mode : str
30833084
Python write mode, default 'w'.
3085+
errors : str, default 'strict'
3086+
Specifies how encoding and decoding errors are to be handled.
3087+
See the errors argument for :func:`open` for a full list
3088+
of options.
30843089
encoding : str, optional
30853090
A string representing the encoding to use in the output file,
30863091
defaults to 'utf-8'.
@@ -3161,6 +3166,7 @@ def to_csv(
31613166
line_terminator=line_terminator,
31623167
sep=sep,
31633168
encoding=encoding,
3169+
errors=errors,
31643170
compression=compression,
31653171
quoting=quoting,
31663172
na_rep=na_rep,

pandas/io/common.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ def get_handle(
330330
path_or_buf,
331331
mode: str,
332332
encoding=None,
333+
errors=None,
333334
compression: Optional[Union[str, Mapping[str, Any]]] = None,
334335
memory_map: bool = False,
335336
is_text: bool = True,
@@ -441,7 +442,7 @@ def get_handle(
441442
elif is_path:
442443
if encoding:
443444
# Encoding
444-
f = open(path_or_buf, mode, encoding=encoding, newline="")
445+
f = open(path_or_buf, mode, encoding=encoding, errors=errors, newline="")
445446
elif is_text:
446447
# No explicit encoding
447448
f = open(path_or_buf, mode, errors="replace", newline="")
@@ -454,7 +455,7 @@ def get_handle(
454455
if is_text and (compression or isinstance(f, need_text_wrapping)):
455456
from io import TextIOWrapper
456457

457-
g = TextIOWrapper(f, encoding=encoding, newline="")
458+
g = TextIOWrapper(f, encoding=encoding, errors=errors, newline="")
458459
if not isinstance(f, (BufferedIOBase, RawIOBase)):
459460
handles.append(g)
460461
f = g

pandas/io/formats/csvs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def __init__(
4444
index_label: Optional[Union[bool, Hashable, Sequence[Hashable]]] = None,
4545
mode: str = "w",
4646
encoding: Optional[str] = None,
47+
errors: str = "strict",
4748
compression: Union[str, Mapping[str, str], None] = "infer",
4849
quoting: Optional[int] = None,
4950
line_terminator="\n",
@@ -77,6 +78,7 @@ def __init__(
7778
if encoding is None:
7879
encoding = "utf-8"
7980
self.encoding = encoding
81+
self.errors = errors
8082
self.compression = infer_compression(self.path_or_buf, compression)
8183

8284
if quoting is None:
@@ -185,6 +187,7 @@ def save(self) -> None:
185187
self.path_or_buf,
186188
self.mode,
187189
encoding=self.encoding,
190+
errors=self.errors,
188191
compression=dict(self.compression_args, method=self.compression),
189192
)
190193
close = True
@@ -216,6 +219,7 @@ def save(self) -> None:
216219
self.path_or_buf,
217220
self.mode,
218221
encoding=self.encoding,
222+
errors=self.errors,
219223
compression=compression,
220224
)
221225
f.write(buf)

0 commit comments

Comments
 (0)