Skip to content

BUG: Add type check for encoding_errors in pd.read_csv #59075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ I/O
- Bug in :meth:`DataFrame.to_stata` when writing :class:`DataFrame` and ``byteorder=`big```. (:issue:`58969`)
- Bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
- Bug in :meth:`HDFStore.get` was failing to save data of dtype datetime64[s] correctly (:issue:`59004`)
- Bug in :meth:`read_csv` causing segmentation fault when ``encoding_errors`` is not a string or None. (:issue:`59059`)
- Bug in :meth:`read_csv` raising ``TypeError`` when ``index_col`` is specified and ``na_values`` is a dict containing the key ``None``. (:issue:`57547`)
- Bug in :meth:`read_csv` raising ``TypeError`` when ``nrows`` and ``iterator`` are specified without specifying a ``chunksize``. (:issue:`59079`)
- Bug in :meth:`read_stata` raising ``KeyError`` when input file is stored in big-endian format and contains strL data. (:issue:`58638`)
Expand Down
9 changes: 9 additions & 0 deletions pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,14 @@ def _read(
# Extract some of the arguments (pass chunksize on).
iterator = kwds.get("iterator", False)
chunksize = kwds.get("chunksize", None)

# Check type of encoding_errors
errors = kwds.get("encoding_errors", "strict")
if not isinstance(errors, str) and errors is not None:
raise ValueError(
f"encoding_errors must be a string or None, got {type(errors).__name__}"
)

if kwds.get("engine") == "pyarrow":
if iterator:
raise ValueError(
Expand Down Expand Up @@ -1413,6 +1421,7 @@ def _make_engine(
raise ValueError(
f"Unknown engine: {engine} (valid options are {mapping.keys()})"
)

if not isinstance(f, list):
# open file here
is_text = True
Expand Down