Skip to content

take method on NDDataFrame #1209

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 2 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2298,7 +2298,6 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
) -> Series: ...
def swapaxes(self, axis1: Axis, axis2: Axis, copy: _bool = ...) -> Self: ...
def tail(self, n: int = ...) -> Self: ...
def take(self, indices: list, axis: Axis = ..., **kwargs: Any) -> Self: ...
@overload
def to_json(
self,
Expand Down
3 changes: 3 additions & 0 deletions pandas-stubs/core/generic.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ from pandas._typing import (
P,
StorageOptions,
T,
TakeIndexer,
TimedeltaConvertibleTypes,
TimeGrouperOrigin,
TimestampConvention,
Expand Down Expand Up @@ -418,3 +419,5 @@ class NDFrame(indexing.IndexingMixin):
offset: TimedeltaConvertibleTypes | None = ...,
group_keys: _bool = ...,
) -> DatetimeIndexResampler[Self]: ...
@final
def take(self, indices: TakeIndexer, axis: Axis = ..., **kwargs: Any) -> Self: ...
3 changes: 0 additions & 3 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,6 @@ class Series(IndexOpsMixin[S1], NDFrame):
def __array__(self, dtype=...) -> np.ndarray: ...
@property
def axes(self) -> list: ...
def take(
self, indices: Sequence, axis: AxisIndex = ..., **kwargs: Any
) -> Series[S1]: ...
def __getattr__(self, name: _str) -> S1: ...
@overload
def __getitem__(
Expand Down
11 changes: 11 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2984,6 +2984,17 @@ def test_iloc_tuple() -> None:
df = df.iloc[0:2,]


def test_take() -> None:
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
check(assert_type(df.take([0, 1]), pd.DataFrame), pd.DataFrame)
check(assert_type(df.take([np.int64(0), np.int64(1)]), pd.DataFrame), pd.DataFrame)
check(assert_type(df.take(np.array([0, 1])), pd.DataFrame), pd.DataFrame)
check(assert_type(df.take([0, 1], "index"), pd.DataFrame), pd.DataFrame)
check(assert_type(df.take([0, 1], 0), pd.DataFrame), pd.DataFrame)
check(assert_type(df.take([0, 1], "columns"), pd.DataFrame), pd.DataFrame)
check(assert_type(df.take([0, 1], 1), pd.DataFrame), pd.DataFrame)


def test_set_columns() -> None:
# GH 73
df = pd.DataFrame({"a": [1, 2, 3], "b": [0.0, 1, 1]})
Expand Down
10 changes: 10 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,16 @@ def test_iloc_getitem_ndarray() -> None:
check(assert_type(values_s.iloc[indices_u64], pd.Series), pd.Series)


def test_take() -> None:
s = pd.Series(np.arange(10), name="a")
check(assert_type(s.take([0, 1]), pd.Series), pd.Series)
check(
assert_type(s.take([np.int64(0), np.int64(1)]), pd.Series),
pd.Series,
)
check(assert_type(s.take(np.array([0, 1])), pd.Series), pd.Series)


def test_iloc_setitem_ndarray() -> None:
# GH 85
# GH 86
Expand Down