Skip to content

fix args to DataFrame.set_index #147

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 1 commit into from
Jul 17, 2022
Merged

Conversation

Dr-Irv
Copy link
Collaborator

@Dr-Irv Dr-Irv commented Jul 17, 2022

@Dr-Irv Dr-Irv requested a review from twoertwein July 17, 2022 02:19
@@ -577,7 +577,9 @@ class DataFrame(NDFrame, OpsMixin):
@overload
def set_index(
self,
keys: Union[Label, Sequence],
keys: Union[
Label, Series, Index, np.ndarray, Iterator[Hashable], List[Hashable]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Iterator[Hashable] includes List[Hashable], but I didn't test that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Iterator[Hashable] includes List[Hashable], but I didn't test that.

No, it's not. Here's an example where pyright shows the incompatibility:

from typing import Iterator


def fun(a: Iterator[int]):
    k = next(a, None)
    while k is not None:
        print(k)


li = [1, 2, 3]
fun(li)

On the last line, pyright reports:

iterlist.py:11:5 - error: Argument of type "list[int]" cannot be assigned to parameter "a" of type "Iterator[int]" in function "fun"
    "list[int]" is incompatible with protocol "Iterator[int]"
      "__next__" is not present (reportGeneralTypeIssues)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterable[int] wouldn't require __next__ but I'm not sure whether set_index works with any iterable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterable[int] wouldn't require __next__ but I'm not sure whether set_index works with any iterable.

pandas has no type annotations for keys. Let's keep it as-is.

Copy link
Contributor

@bashtage bashtage Jul 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you believe the docs, then the types that could be used are

Label
Series
Index
np.ndarray
Iterator
List[Union[Label, Series, Index, np.ndarray, Iterator]]

Clearly it also supports List everywhere Series is used.

@twoertwein twoertwein merged commit 33fbbe1 into pandas-dev:main Jul 17, 2022
@twoertwein
Copy link
Member

Thanks @Dr-Irv !

@Dr-Irv Dr-Irv deleted the setindex branch December 28, 2022 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] DataFrame.set_index does not accept pd.Index anymore.
3 participants