Skip to content

WARN: Clarify future warnings for read_csv #44651

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 4 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 27 additions & 9 deletions pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import csv
import sys
from textwrap import fill
from typing import Any
from typing import (
Any,
NamedTuple,
)
import warnings

import numpy as np
Expand Down Expand Up @@ -446,10 +449,18 @@
"low_memory",
}

_deprecated_defaults: dict[str, Any] = {
"error_bad_lines": None,
"warn_bad_lines": None,
"squeeze": None,

class _DeprecationConfig(NamedTuple):
default_value: Any
msg: str | None


_deprecated_defaults: dict[str, _DeprecationConfig] = {
"error_bad_lines": _DeprecationConfig(None, "Use on_bad_lines in the future."),
"warn_bad_lines": _DeprecationConfig(None, "Use on_bad_lines in the future."),
"squeeze": _DeprecationConfig(
None, 'Append .squeeze("columns") to the call to squeeze.'
),
}


Expand Down Expand Up @@ -926,15 +937,22 @@ def _get_options_with_defaults(self, engine):
if engine != "c" and value != default:
if "python" in engine and argname not in _python_unsupported:
pass
elif value == _deprecated_defaults.get(argname, default):
elif (
value
== _deprecated_defaults.get(
argname, _DeprecationConfig(default, None)
).default_value
):
pass
else:
raise ValueError(
f"The {repr(argname)} option is not supported with the "
f"{repr(engine)} engine"
)
else:
value = _deprecated_defaults.get(argname, default)
value = _deprecated_defaults.get(
argname, _DeprecationConfig(default, None)
).default_value
options[argname] = value

if engine == "python-fwf":
Expand Down Expand Up @@ -1059,10 +1077,10 @@ def _clean_options(self, options, engine):
for arg in _deprecated_defaults.keys():
parser_default = _c_parser_defaults.get(arg, parser_defaults[arg])
depr_default = _deprecated_defaults[arg]
if result.get(arg, depr_default) != depr_default:
if result.get(arg, depr_default) != depr_default.default_value:
msg = (
f"The {arg} argument has been deprecated and will be "
"removed in a future version.\n\n"
f"removed in a future version. {depr_default.msg}\n\n"
)
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
else:
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/io/excel/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,8 @@ def test_read_excel_squeeze(self, read_ext):
with tm.assert_produces_warning(
FutureWarning,
match="The squeeze argument has been deprecated "
"and will be removed in a future version.\n\n",
"and will be removed in a future version. "
'Append .squeeze\\("columns"\\) to the call to squeeze.\n\n',
):
actual = pd.read_excel(
f, sheet_name="two_columns", index_col=0, squeeze=True
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/io/parser/common/test_common_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ def test_squeeze(all_parsers, squeeze):
result = parser.read_csv_check_warnings(
FutureWarning,
"The squeeze argument has been deprecated "
"and will be removed in a future version.\n\n",
"and will be removed in a future version. "
'Append .squeeze\\("columns"\\) to the call to squeeze.\n\n',
StringIO(data),
index_col=0,
header=None,
Expand Down Expand Up @@ -877,7 +878,8 @@ def test_deprecated_bad_lines_warns(all_parsers, csv1, on_bad_lines):
parser.read_csv_check_warnings(
FutureWarning,
f"The {on_bad_lines}_bad_lines argument has been deprecated "
"and will be removed in a future version.\n\n",
"and will be removed in a future version. "
"Use on_bad_lines in the future.\n\n",
csv1,
**kwds,
)
Expand Down