From 528986c2e374838e9fab4405131b81fe70486897 Mon Sep 17 00:00:00 2001 From: phofl Date: Wed, 1 Dec 2021 21:36:45 +0100 Subject: [PATCH 1/3] Deprecate positional arguments for read_fwf --- doc/source/whatsnew/v1.4.0.rst | 1 + pandas/io/parsers/readers.py | 3 +++ 2 files changed, 4 insertions(+) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 193356a46a6ea..51a840fab8086 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -519,6 +519,7 @@ Other Deprecations - Deprecated the 'errors' keyword argument in :meth:`Series.where`, :meth:`DataFrame.where`, :meth:`Series.mask`, and meth:`DataFrame.mask`; in a future version the argument will be removed (:issue:`44294`) - Deprecated :meth:`PeriodIndex.astype` to ``datetime64[ns]`` or ``DatetimeTZDtype``, use ``obj.to_timestamp(how).tz_localize(dtype.tz)`` instead (:issue:`44398`) - Deprecated passing non boolean argument to sort in :func:`concat` (:issue:`41518`) +- Deprecated passing arguments as positional for :func:`read_fwf` other than ``filepath_or_buffer`` (:issue:`41485`): - Deprecated passing ``skipna=None`` for :meth:`DataFrame.mad` and :meth:`Series.mad`, pass ``skipna=True`` instead (:issue:`44580`) - Deprecated :meth:`DateOffset.apply`, use ``offset + other`` instead (:issue:`44522`) - A deprecation warning is now shown for :meth:`DataFrame.to_latex` indicating the arguments signature may change and emulate more the arguments to :meth:`.Styler.to_latex` in future versions (:issue:`44411`) diff --git a/pandas/io/parsers/readers.py b/pandas/io/parsers/readers.py index 63ab10e1e5362..aecfe87fd7886 100644 --- a/pandas/io/parsers/readers.py +++ b/pandas/io/parsers/readers.py @@ -758,6 +758,9 @@ def read_table( return _read(filepath_or_buffer, kwds) +@deprecate_nonkeyword_arguments( + version=None, allowed_args=["filepath_or_buffer"], stacklevel=3 +) def read_fwf( filepath_or_buffer: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str], colspecs="infer", From 0edd98f23cffe13b2294253292dc21add8a77b74 Mon Sep 17 00:00:00 2001 From: phofl Date: Wed, 1 Dec 2021 21:57:49 +0100 Subject: [PATCH 2/3] Add test --- pandas/io/parsers/readers.py | 2 +- pandas/tests/io/parser/test_read_fwf.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pandas/io/parsers/readers.py b/pandas/io/parsers/readers.py index aecfe87fd7886..ab23ee74a50ac 100644 --- a/pandas/io/parsers/readers.py +++ b/pandas/io/parsers/readers.py @@ -759,7 +759,7 @@ def read_table( @deprecate_nonkeyword_arguments( - version=None, allowed_args=["filepath_or_buffer"], stacklevel=3 + version=None, allowed_args=["filepath_or_buffer"], stacklevel=2 ) def read_fwf( filepath_or_buffer: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str], diff --git a/pandas/tests/io/parser/test_read_fwf.py b/pandas/tests/io/parser/test_read_fwf.py index 3f43ea0b8a12d..87ef1e51299db 100644 --- a/pandas/tests/io/parser/test_read_fwf.py +++ b/pandas/tests/io/parser/test_read_fwf.py @@ -907,3 +907,15 @@ def test_skiprows_with_iterator(): ] for i, result in enumerate(df_iter): tm.assert_frame_equal(result, expected_frames[i]) + + +def test_skiprows_passing_as_positional_deprecated(): + # GH#41485 + data = """0 +1 +2 +""" + with tm.assert_produces_warning(FutureWarning, match="keyword-only"): + result = read_fwf(StringIO(data), [(0, 2)]) + expected = DataFrame({"0": [1, 2]}) + tm.assert_frame_equal(result, expected) From f8664be3e07f34aeadafd34daa9bf70ff80b2abf Mon Sep 17 00:00:00 2001 From: phofl Date: Wed, 1 Dec 2021 23:28:46 +0100 Subject: [PATCH 3/3] Fix test --- pandas/tests/io/parser/test_read_fwf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/parser/test_read_fwf.py b/pandas/tests/io/parser/test_read_fwf.py index 87ef1e51299db..cdfc9f71f169c 100644 --- a/pandas/tests/io/parser/test_read_fwf.py +++ b/pandas/tests/io/parser/test_read_fwf.py @@ -218,7 +218,7 @@ def test_fwf_colspecs_is_list_or_tuple_of_two_element_tuples(): msg = "Each column specification must be.+" with pytest.raises(TypeError, match=msg): - read_fwf(StringIO(data), [("a", 1)]) + read_fwf(StringIO(data), colspecs=[("a", 1)]) @pytest.mark.parametrize(