Skip to content

pd.DataFrame.take should allow np.ndarray indices (current stub only accepts list) #1208

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
ritsuki1227 opened this issue May 5, 2025 · 1 comment · Fixed by #1209
Closed

Comments

@ritsuki1227
Copy link
Contributor

The following code executes without any runtime error, but static type checkers such as mypy flag it as invalid:

import numpy as np
import pandas as pd

pd.DataFrame({"a": [1, 2, 3]}).take(np.array([0, 1]))

In pandas-stubs, the signature for DataFrame.take currently restricts indices to list:

def take(self, indices: list, axis: Axis = ..., **kwargs) -> Self: ...

However, the actual pandas implementation clearly accepts a NumPy array (and other array-like objects). For reference, the stub for _iLocIndexerFrame already accommodates NumPy arrays.

class _iLocIndexerFrame(_iLocIndexer, Generic[_T]):
    @overload
    def __getitem__(
        self,
        idx: (
            IndexType
            | MaskType
            | tuple[IndexType | MaskType, IndexType | MaskType]
            | tuple[slice]
        ),
    ) -> _T: ...

It would be helpful if the take method signature could be fixed!

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented May 5, 2025

It should be TakeIndexer, which is defined in _typing.pyi. PR with tests welcome.

Ideally, there should be some reconciliation done between the definitions of take() in series.pyi and frame.pyi, and if they are supposed to be the same, removed from those 2 files and placed in generic.pyi in NDFrame.

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 a pull request may close this issue.

2 participants