Skip to content

Commit 124bee2

Browse files
authored
WARN: Clarify future warnings for read_csv (#44651)
* WARN: Clarify future warnings for read_csv * Adjust test * Use namedtuple * Fix tests
1 parent 36df78e commit 124bee2

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

pandas/io/parsers/readers.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import csv
88
import sys
99
from textwrap import fill
10-
from typing import Any
10+
from typing import (
11+
Any,
12+
NamedTuple,
13+
)
1114
import warnings
1215

1316
import numpy as np
@@ -446,10 +449,18 @@
446449
"low_memory",
447450
}
448451

449-
_deprecated_defaults: dict[str, Any] = {
450-
"error_bad_lines": None,
451-
"warn_bad_lines": None,
452-
"squeeze": None,
452+
453+
class _DeprecationConfig(NamedTuple):
454+
default_value: Any
455+
msg: str | None
456+
457+
458+
_deprecated_defaults: dict[str, _DeprecationConfig] = {
459+
"error_bad_lines": _DeprecationConfig(None, "Use on_bad_lines in the future."),
460+
"warn_bad_lines": _DeprecationConfig(None, "Use on_bad_lines in the future."),
461+
"squeeze": _DeprecationConfig(
462+
None, 'Append .squeeze("columns") to the call to squeeze.'
463+
),
453464
}
454465

455466

@@ -926,15 +937,22 @@ def _get_options_with_defaults(self, engine):
926937
if engine != "c" and value != default:
927938
if "python" in engine and argname not in _python_unsupported:
928939
pass
929-
elif value == _deprecated_defaults.get(argname, default):
940+
elif (
941+
value
942+
== _deprecated_defaults.get(
943+
argname, _DeprecationConfig(default, None)
944+
).default_value
945+
):
930946
pass
931947
else:
932948
raise ValueError(
933949
f"The {repr(argname)} option is not supported with the "
934950
f"{repr(engine)} engine"
935951
)
936952
else:
937-
value = _deprecated_defaults.get(argname, default)
953+
value = _deprecated_defaults.get(
954+
argname, _DeprecationConfig(default, None)
955+
).default_value
938956
options[argname] = value
939957

940958
if engine == "python-fwf":
@@ -1059,10 +1077,10 @@ def _clean_options(self, options, engine):
10591077
for arg in _deprecated_defaults.keys():
10601078
parser_default = _c_parser_defaults.get(arg, parser_defaults[arg])
10611079
depr_default = _deprecated_defaults[arg]
1062-
if result.get(arg, depr_default) != depr_default:
1080+
if result.get(arg, depr_default) != depr_default.default_value:
10631081
msg = (
10641082
f"The {arg} argument has been deprecated and will be "
1065-
"removed in a future version.\n\n"
1083+
f"removed in a future version. {depr_default.msg}\n\n"
10661084
)
10671085
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
10681086
else:

pandas/tests/io/excel/test_readers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,8 @@ def test_read_excel_squeeze(self, read_ext):
11951195
with tm.assert_produces_warning(
11961196
FutureWarning,
11971197
match="The squeeze argument has been deprecated "
1198-
"and will be removed in a future version.\n\n",
1198+
"and will be removed in a future version. "
1199+
'Append .squeeze\\("columns"\\) to the call to squeeze.\n\n',
11991200
):
12001201
actual = pd.read_excel(
12011202
f, sheet_name="two_columns", index_col=0, squeeze=True

pandas/tests/io/parser/common/test_common_basic.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ def test_squeeze(all_parsers, squeeze):
143143
result = parser.read_csv_check_warnings(
144144
FutureWarning,
145145
"The squeeze argument has been deprecated "
146-
"and will be removed in a future version.\n\n",
146+
"and will be removed in a future version. "
147+
'Append .squeeze\\("columns"\\) to the call to squeeze.\n\n',
147148
StringIO(data),
148149
index_col=0,
149150
header=None,
@@ -877,7 +878,8 @@ def test_deprecated_bad_lines_warns(all_parsers, csv1, on_bad_lines):
877878
parser.read_csv_check_warnings(
878879
FutureWarning,
879880
f"The {on_bad_lines}_bad_lines argument has been deprecated "
880-
"and will be removed in a future version.\n\n",
881+
"and will be removed in a future version. "
882+
"Use on_bad_lines in the future.\n\n",
881883
csv1,
882884
**kwds,
883885
)

0 commit comments

Comments
 (0)