Skip to content

TYP: Signature of "reindex" incompatible with supertype "NDFrame" #40984

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 17 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from 10 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
4 changes: 3 additions & 1 deletion pandas/core/describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ def describe(self, percentiles: Sequence[float]) -> DataFrame:
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."""
Expand Down
13 changes: 11 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4481,8 +4481,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
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,
Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/series/methods/test_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,28 @@ 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():
# GH 40980
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():
# GH 40980
ser = Series([1, 2])
msg = r"'index' passed as both positional and keyword argument"
with pytest.raises(TypeError, match=msg):
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)