From fd7f62f202e83594f903d8f987c20486715c4359 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Fri, 16 Apr 2021 15:52:58 +0100 Subject: [PATCH 1/9] align reindex with supertype --- pandas/core/series.py | 13 +++++++++++-- pandas/tests/series/methods/test_reindex.py | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 5c605a6b441c6..9b1190123ea73 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4472,8 +4472,17 @@ def set_axis(self, labels, axis: Axis = 0, inplace: bool = False): optional_labels=_shared_doc_kwargs["optional_labels"], optional_axis=_shared_doc_kwargs["optional_axis"], ) - def reindex(self, index=None, **kwargs): - return super().reindex(index=index, **kwargs) + def reindex(self, *args, **kwargs) -> Series: + if len(args) > 1: + raise TypeError("Only one positional argument ('index') is allowed") + if args: + index = args[0] + if "index" in kwargs: + raise TypeError( + "'index' passed as both positional and keyword argument" + ) + kwargs.update({"index": index}) + return super().reindex(**kwargs) def drop( self, diff --git a/pandas/tests/series/methods/test_reindex.py b/pandas/tests/series/methods/test_reindex.py index 8e54cbeb313c4..0b2ebd9b2fed6 100644 --- a/pandas/tests/series/methods/test_reindex.py +++ b/pandas/tests/series/methods/test_reindex.py @@ -345,3 +345,19 @@ def test_reindex_periodindex_with_object(p_values, o_values, values, expected_va result = ser.reindex(object_index) expected = Series(expected_values, index=object_index) tm.assert_series_equal(result, expected) + + +def test_reindex_too_many_args(): + ser = Series([1, 2]) + with pytest.raises( + TypeError, match=r"Only one positional argument \('index'\) is allowed" + ): + ser.reindex([2, 3], False) + + +def test_reindex_double_index(): + ser = Series([1, 2]) + with pytest.raises( + TypeError, match=r"'index' passed as both positional and keyword argument" + ): + ser.reindex([2, 3], index=[3, 4]) From 57c6312dd59993a8fa94dc2897df82e4e89cf28f Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Fri, 16 Apr 2021 15:55:03 +0100 Subject: [PATCH 2/9] align reindex with supertype --- pandas/tests/series/methods/test_reindex.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/tests/series/methods/test_reindex.py b/pandas/tests/series/methods/test_reindex.py index 0b2ebd9b2fed6..063f509da0a8d 100644 --- a/pandas/tests/series/methods/test_reindex.py +++ b/pandas/tests/series/methods/test_reindex.py @@ -348,6 +348,7 @@ def test_reindex_periodindex_with_object(p_values, o_values, values, expected_va def test_reindex_too_many_args(): + # GH 40980 ser = Series([1, 2]) with pytest.raises( TypeError, match=r"Only one positional argument \('index'\) is allowed" @@ -356,6 +357,7 @@ def test_reindex_too_many_args(): def test_reindex_double_index(): + # GH 40980 ser = Series([1, 2]) with pytest.raises( TypeError, match=r"'index' passed as both positional and keyword argument" From 9f9c464f7c306c3db928f5f4e29493acbf70f5ae Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Fri, 16 Apr 2021 16:43:55 +0100 Subject: [PATCH 3/9] :label: --- pandas/core/describe.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pandas/core/describe.py b/pandas/core/describe.py index dfb18b2c40698..7f83a24e8dbc9 100644 --- a/pandas/core/describe.py +++ b/pandas/core/describe.py @@ -175,10 +175,13 @@ def describe(self, percentiles: Sequence[float]) -> DataFrame: ldesc.append(describe_func(series, percentiles)) col_names = reorder_columns(ldesc) - d = concat( - [x.reindex(col_names, copy=False) for x in ldesc], - axis=1, - sort=False, + d = cast( + DataFrame, + concat( + [x.reindex(col_names, copy=False) for x in ldesc], + axis=1, + sort=False, + ), ) d.columns = data.columns.copy() return d From bc08d1898eed5908c820cf58e10009684ec66f23 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Fri, 16 Apr 2021 16:50:15 +0100 Subject: [PATCH 4/9] tuple unpacking --- pandas/core/series.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 9b1190123ea73..63e6e0337e7a3 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4476,7 +4476,7 @@ def reindex(self, *args, **kwargs) -> Series: if len(args) > 1: raise TypeError("Only one positional argument ('index') is allowed") if args: - index = args[0] + (index,) = args if "index" in kwargs: raise TypeError( "'index' passed as both positional and keyword argument" From 61e03b770d259ed008c9d6dc78f4259083c8a697 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Fri, 16 Apr 2021 18:33:49 +0100 Subject: [PATCH 5/9] quote DataFrame --- pandas/core/describe.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/describe.py b/pandas/core/describe.py index 7f83a24e8dbc9..e136fa7707c51 100644 --- a/pandas/core/describe.py +++ b/pandas/core/describe.py @@ -176,7 +176,7 @@ def describe(self, percentiles: Sequence[float]) -> DataFrame: col_names = reorder_columns(ldesc) d = cast( - DataFrame, + "DataFrame", concat( [x.reindex(col_names, copy=False) for x in ldesc], axis=1, From aff30c716163a16091159d81bdd78ddf8ae515e2 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sat, 17 Apr 2021 10:35:56 +0100 Subject: [PATCH 6/9] cast -> type: ignore --- pandas/core/describe.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pandas/core/describe.py b/pandas/core/describe.py index e136fa7707c51..2f59f1f30be3d 100644 --- a/pandas/core/describe.py +++ b/pandas/core/describe.py @@ -175,16 +175,15 @@ def describe(self, percentiles: Sequence[float]) -> DataFrame: ldesc.append(describe_func(series, percentiles)) col_names = reorder_columns(ldesc) - d = cast( - "DataFrame", - concat( - [x.reindex(col_names, copy=False) for x in ldesc], - axis=1, - sort=False, - ), + d = concat( + [x.reindex(col_names, copy=False) for x in ldesc], + axis=1, + sort=False, ) d.columns = data.columns.copy() - return d + # Incompatible return value type (got "Union[DataFrame, Series]", + # expected "DataFrame") + return d # type: ignore[return-value] def _select_data(self): """Select columns to be described.""" From b09b7bc47b8af67b55b01c8ab0f24b3c4b9aa099 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Fri, 30 Apr 2021 19:44:49 +0100 Subject: [PATCH 7/9] add test with no positional arguments --- pandas/tests/series/methods/test_reindex.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tests/series/methods/test_reindex.py b/pandas/tests/series/methods/test_reindex.py index 063f509da0a8d..ab860120238dc 100644 --- a/pandas/tests/series/methods/test_reindex.py +++ b/pandas/tests/series/methods/test_reindex.py @@ -363,3 +363,11 @@ def test_reindex_double_index(): TypeError, match=r"'index' passed as both positional and keyword argument" ): ser.reindex([2, 3], index=[3, 4]) + + +def test_reindex_no_posargs(): + # GH 40980 + ser = Series([1, 2]) + result = ser.reindex(index=[1, 0]) + expected = Series([2, 1], index=[1, 0]) + tm.assert_series_equal(result, expected) From 245eee297c424374366d5383b2b545b22900a8ac Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Fri, 30 Apr 2021 19:47:32 +0100 Subject: [PATCH 8/9] add test with no positional arguments --- pandas/tests/series/methods/test_reindex.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/tests/series/methods/test_reindex.py b/pandas/tests/series/methods/test_reindex.py index ab860120238dc..82c2bf98088ea 100644 --- a/pandas/tests/series/methods/test_reindex.py +++ b/pandas/tests/series/methods/test_reindex.py @@ -359,9 +359,8 @@ def test_reindex_too_many_args(): def test_reindex_double_index(): # GH 40980 ser = Series([1, 2]) - with pytest.raises( - TypeError, match=r"'index' passed as both positional and keyword argument" - ): + msg = r"'index' passed as both positional and keyword argument" + with pytest.raises(TypeError, match=msg): ser.reindex([2, 3], index=[3, 4]) From 6a11666c23e7d9ebe0750f54d5f1969290f16208 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Mon, 11 Oct 2021 11:29:44 +0100 Subject: [PATCH 9/9] remove now unnecessary type: ignore --- pandas/core/describe.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/core/describe.py b/pandas/core/describe.py index 81497c690e4a2..2c4a340e8c8ea 100644 --- a/pandas/core/describe.py +++ b/pandas/core/describe.py @@ -179,9 +179,7 @@ def describe(self, percentiles: Sequence[float] | np.ndarray) -> DataFrame: sort=False, ) d.columns = data.columns.copy() - # Incompatible return value type (got "Union[DataFrame, Series]", - # expected "DataFrame") - return d # type: ignore[return-value] + return d def _select_data(self): """Select columns to be described."""