Skip to content

[index] DataFrame.loc does not accept tuple[slice, tuple] #573

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

Closed
randolf-scholz opened this issue Mar 14, 2023 · 1 comment
Closed

[index] DataFrame.loc does not accept tuple[slice, tuple] #573

randolf-scholz opened this issue Mar 14, 2023 · 1 comment

Comments

@randolf-scholz
Copy link
Contributor

randolf-scholz commented Mar 14, 2023

Describe the bug

The current type hint contains:

tuple[
    IndexType | MaskType | list[HashableT] | Hashable,
    list[HashableT] | slice | Series[bool] | Callable
]

The following modification should be considered

  1. Ideally, replace list[HashableT] with Sequece[HashableT]. But this doesn't work because str satsifies Sequence protocol. So at the least one could consider adding tuple manually.
    • Since this issue seems to be common, one might consider creating a custom container TypeAlias that in the future could be replaced with Sequence[H] & ~H if we ever get intersection and not types.
  2. add slice to the first component.

Suggestion

tuple[
    # in the index `Iterable[H]` works fine!
    slice | Hashable | Iterable[HashableT] | IndexType | MaskType,
    # For the columns use `list[HashableT] | tuple[HashableT, ...]` instead of `Iterable[H]` to avoid matching `str`.
    slice | list[HashableT] | tuple[HashableT, ...] | Series[bool] | Callable,  
]

To Reproduce

import numpy as np
from pandas import DataFrame

foo = DataFrame(np.random.rand(10,3), columns=["a", "b", "c"])
bar = foo.loc[4:5, ("a", "b")]  # [index]

Please complete the following information:

  • pandas 1.5.3
  • pandas-stubs 1.5.3.230304
randolf-scholz added a commit to randolf-scholz/pandas-stubs that referenced this issue Mar 14, 2023
@randolf-scholz
Copy link
Contributor Author

Actually, if one restricts to loc[slice, list] it works, issue is with Multiindex columns allowing tuple to index a single column, which intersects with selecting multiple columns by an iterable of hashables. Closing.

randolf-scholz added a commit to randolf-scholz/pandas-stubs that referenced this issue Mar 14, 2023
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

No branches or pull requests

1 participant