Skip to content

Commit 2e33bb8

Browse files
Add errors argument to to_csv() call to enable error handling for encoders
1 parent 6620dc6 commit 2e33bb8

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
@@ -3030,6 +3030,7 @@ def to_csv(
30303030
index_label: Optional[Union[bool_t, str, Sequence[Label]]] = None,
30313031
mode: str = "w",
30323032
encoding: Optional[str] = None,
3033+
errors: str = "strict",
30333034
compression: Optional[Union[str, Mapping[str, str]]] = "infer",
30343035
quoting: Optional[int] = None,
30353036
quotechar: str = '"',
@@ -3083,6 +3084,10 @@ def to_csv(
30833084
for easier importing in R.
30843085
mode : str
30853086
Python write mode, default 'w'.
3087+
errors : str, default 'strict'
3088+
Specifies how encoding and decoding errors are to be handled.
3089+
See the errors argument for :func:`open` for a full list
3090+
of options.
30863091
encoding : str, optional
30873092
A string representing the encoding to use in the output file,
30883093
defaults to 'utf-8'.
@@ -3163,6 +3168,7 @@ def to_csv(
31633168
line_terminator=line_terminator,
31643169
sep=sep,
31653170
encoding=encoding,
3171+
errors=errors,
31663172
compression=compression,
31673173
quoting=quoting,
31683174
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)