Skip to content

Commit f6bdff6

Browse files
Add errors argument to to_csv() call to enable error handling for encoders
1 parent 9223d19 commit f6bdff6

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
@@ -3039,6 +3039,7 @@ def to_csv(
30393039
index_label: Optional[Union[bool_t, str, Sequence[Label]]] = None,
30403040
mode: str = "w",
30413041
encoding: Optional[str] = None,
3042+
errors: str = "strict",
30423043
compression: Optional[Union[str, Mapping[str, str]]] = "infer",
30433044
quoting: Optional[int] = None,
30443045
quotechar: str = '"',
@@ -3092,6 +3093,10 @@ def to_csv(
30923093
for easier importing in R.
30933094
mode : str
30943095
Python write mode, default 'w'.
3096+
errors : str, default 'strict'
3097+
Specifies how encoding and decoding errors are to be handled.
3098+
See the errors argument for :func:`open` for a full list
3099+
of options.
30953100
encoding : str, optional
30963101
A string representing the encoding to use in the output file,
30973102
defaults to 'utf-8'.
@@ -3179,6 +3184,7 @@ def to_csv(
31793184
line_terminator=line_terminator,
31803185
sep=sep,
31813186
encoding=encoding,
3187+
errors=errors,
31823188
compression=compression,
31833189
quoting=quoting,
31843190
na_rep=na_rep,

pandas/io/common.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ def get_handle(
349349
path_or_buf,
350350
mode: str,
351351
encoding=None,
352+
errors=None,
352353
compression: Optional[Union[str, Mapping[str, Any]]] = None,
353354
memory_map: bool = False,
354355
is_text: bool = True,
@@ -475,7 +476,7 @@ def get_handle(
475476
elif is_path:
476477
if encoding:
477478
# Encoding
478-
f = open(path_or_buf, mode, encoding=encoding, newline="")
479+
f = open(path_or_buf, mode, encoding=encoding, errors=errors, newline="")
479480
elif is_text:
480481
# No explicit encoding
481482
f = open(path_or_buf, mode, errors="replace", newline="")
@@ -488,7 +489,7 @@ def get_handle(
488489
if is_text and (compression or isinstance(f, need_text_wrapping)):
489490
from io import TextIOWrapper
490491

491-
g = TextIOWrapper(f, encoding=encoding, newline="")
492+
g = TextIOWrapper(f, encoding=encoding, errors=errors, newline="")
492493
if not isinstance(f, (BufferedIOBase, RawIOBase)):
493494
handles.append(g)
494495
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:
@@ -184,6 +186,7 @@ def save(self) -> None:
184186
self.path_or_buf,
185187
self.mode,
186188
encoding=self.encoding,
189+
errors=self.errors,
187190
compression=dict(self.compression_args, method=self.compression),
188191
)
189192
close = True
@@ -215,6 +218,7 @@ def save(self) -> None:
215218
self.path_or_buf,
216219
self.mode,
217220
encoding=self.encoding,
221+
errors=self.errors,
218222
compression=compression,
219223
)
220224
f.write(buf)

0 commit comments

Comments
 (0)