From cf57c85c096124e876945ad2f77f0bdedac0249a Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Wed, 26 May 2021 21:04:05 +0100 Subject: [PATCH] deprecate positional args in read_csv --- doc/source/whatsnew/v1.3.0.rst | 2 ++ pandas/io/parsers/readers.py | 8 +++++++- pandas/tests/io/parser/common/test_common_basic.py | 12 ++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index fe88cf93886ce..963164528bba2 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -691,6 +691,8 @@ Deprecations - Deprecated passing arguments as positional in :meth:`DataFrame.reset_index` (other than ``"level"``) and :meth:`Series.reset_index` (:issue:`41485`) - Deprecated construction of :class:`Series` or :class:`DataFrame` with ``DatetimeTZDtype`` data and ``datetime64[ns]`` dtype. Use ``Series(data).dt.tz_localize(None)`` instead (:issue:`41555`,:issue:`33401`) - Deprecated passing arguments as positional in :meth:`DataFrame.where` and :meth:`Series.where` (other than ``"cond"`` and ``"other"``) (:issue:`41485`) +- Deprecated passing arguments as positional (other than ``filepath_or_buffer``) in :func:`read_csv` (:issue:`41485`) +- .. _whatsnew_130.deprecations.nuisance_columns: diff --git a/pandas/io/parsers/readers.py b/pandas/io/parsers/readers.py index ad08b8d4b7097..d957a669351c1 100644 --- a/pandas/io/parsers/readers.py +++ b/pandas/io/parsers/readers.py @@ -30,7 +30,10 @@ AbstractMethodError, ParserWarning, ) -from pandas.util._decorators import Appender +from pandas.util._decorators import ( + Appender, + deprecate_nonkeyword_arguments, +) from pandas.core.dtypes.common import ( is_file_like, @@ -472,6 +475,9 @@ def _read(filepath_or_buffer: FilePathOrBuffer, kwds): return parser.read(nrows) +@deprecate_nonkeyword_arguments( + version=None, allowed_args=["filepath_or_buffer"], stacklevel=3 +) @Appender( _doc_read_csv_and_table.format( func_name="read_csv", diff --git a/pandas/tests/io/parser/common/test_common_basic.py b/pandas/tests/io/parser/common/test_common_basic.py index adafbf38439d5..eba5e52516b4c 100644 --- a/pandas/tests/io/parser/common/test_common_basic.py +++ b/pandas/tests/io/parser/common/test_common_basic.py @@ -733,6 +733,18 @@ def test_read_csv_delimiter_and_sep_no_default(all_parsers): parser.read_csv(f, sep=" ", delimiter=".") +def test_read_csv_posargs_deprecation(all_parsers): + # GH 41485 + f = StringIO("a,b\n1,2") + parser = all_parsers + msg = ( + "In a future version of pandas all arguments of read_csv " + "except for the argument 'filepath_or_buffer' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + parser.read_csv(f, " ") + + @pytest.mark.parametrize("delimiter", [",", "\t"]) def test_read_table_delim_whitespace_non_default_sep(all_parsers, delimiter): # GH: 35958