Skip to content

Commit e439e67

Browse files
Genesis929shobsi
authored andcommitted
feat: add df.take and series.take (#1509)
* feat: add df.take and series.take * update
1 parent a29a751 commit e439e67

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

bigframes/dataframe.py

+12
Original file line numberDiff line numberDiff line change
@@ -2219,6 +2219,18 @@ def add_suffix(self, suffix: str, axis: int | str | None = None) -> DataFrame:
22192219
axis = 1 if axis is None else axis
22202220
return DataFrame(self._get_block().add_suffix(suffix, axis))
22212221

2222+
def take(
2223+
self, indices: typing.Sequence[int], axis: int | str | None = 0, **kwargs
2224+
) -> DataFrame:
2225+
if not utils.is_list_like(indices):
2226+
raise ValueError("indices should be a list-like object.")
2227+
if axis == 0 or axis == "index":
2228+
return self.iloc[indices]
2229+
elif axis == 1 or axis == "columns":
2230+
return self.iloc[:, indices]
2231+
else:
2232+
raise ValueError(f"No axis named {axis} for object type DataFrame")
2233+
22222234
def filter(
22232235
self,
22242236
items: typing.Optional[typing.Iterable] = None,

bigframes/series.py

+7
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,13 @@ def add_prefix(self, prefix: str, axis: int | str | None = None) -> Series:
16521652
def add_suffix(self, suffix: str, axis: int | str | None = None) -> Series:
16531653
return Series(self._get_block().add_suffix(suffix))
16541654

1655+
def take(
1656+
self, indices: typing.Sequence[int], axis: int | str | None = 0, **kwargs
1657+
) -> Series:
1658+
if not utils.is_list_like(indices):
1659+
raise ValueError("indices should be a list-like object.")
1660+
return typing.cast(Series, self.iloc[indices])
1661+
16551662
def filter(
16561663
self,
16571664
items: typing.Optional[typing.Iterable] = None,

tests/system/small/test_dataframe.py

+18
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,24 @@ def test_get_df_column_name_duplicate(scalars_dfs):
807807
pd.testing.assert_index_equal(bf_result.columns, pd_result.columns)
808808

809809

810+
@pytest.mark.parametrize(
811+
("indices", "axis"),
812+
[
813+
([1, 3, 5], 0),
814+
([2, 4, 6], 1),
815+
([1, -3, -5, -6], "index"),
816+
([-2, -4, -6], "columns"),
817+
],
818+
)
819+
def test_take_df(scalars_dfs, indices, axis):
820+
scalars_df, scalars_pandas_df = scalars_dfs
821+
822+
bf_result = scalars_df.take(indices, axis=axis).to_pandas()
823+
pd_result = scalars_pandas_df.take(indices, axis=axis)
824+
825+
assert_pandas_df_equal(bf_result, pd_result)
826+
827+
810828
def test_filter_df(scalars_dfs):
811829
scalars_df, scalars_pandas_df = scalars_dfs
812830

tests/system/small/test_series.py

+17
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,23 @@ def test_indexing_using_selected_series(scalars_dfs):
15431543
)
15441544

15451545

1546+
@pytest.mark.parametrize(
1547+
("indices"),
1548+
[
1549+
([1, 3, 5]),
1550+
([5, -3, -5, -6]),
1551+
([-2, -4, -6]),
1552+
],
1553+
)
1554+
def test_take(scalars_dfs, indices):
1555+
scalars_df, scalars_pandas_df = scalars_dfs
1556+
1557+
bf_result = scalars_df.take(indices).to_pandas()
1558+
pd_result = scalars_pandas_df.take(indices)
1559+
1560+
assert_pandas_df_equal(bf_result, pd_result)
1561+
1562+
15461563
def test_nested_filter(scalars_dfs):
15471564
scalars_df, scalars_pandas_df = scalars_dfs
15481565
string_col = scalars_df["string_col"]

third_party/bigframes_vendored/pandas/core/generic.py

+23
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,29 @@ def notna(self) -> NDFrame:
910910

911911
notnull = notna
912912

913+
def take(self, indices, axis=0, **kwargs) -> NDFrame:
914+
"""Return the elements in the given positional indices along an axis.
915+
916+
This means that we are not indexing according to actual values in the index
917+
attribute of the object. We are indexing according to the actual position of
918+
the element in the object.
919+
920+
Args:
921+
indices (list-like):
922+
An array of ints indicating which positions to take.
923+
axis ({0 or 'index', 1 or 'columns', None}, default 0):
924+
The axis on which to select elements. 0 means that we are selecting rows,
925+
1 means that we are selecting columns. For Series this parameter is
926+
unused and defaults to 0.
927+
**kwargs:
928+
For compatibility with numpy.take(). Has no effect on the output.
929+
930+
Returns:
931+
bigframes.pandas.DataFrame or bigframes.pandas.Series:
932+
Same type as input object.
933+
"""
934+
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
935+
913936
def filter(
914937
self,
915938
items=None,

0 commit comments

Comments
 (0)