Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit d271d0c

Browse files
feat: implement inplace parameter for drop method
This commit implements the `inplace` parameter for the `DataFrame.drop` method. When `inplace=True`, the DataFrame is modified in place and the method returns `None`. When `inplace=False` (the default), a new DataFrame is returned. Unit tests have been added to verify the functionality for both column and row dropping.
1 parent c4efa68 commit d271d0c

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

bigframes/dataframe.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,6 +2006,7 @@ def insert(
20062006

20072007
self._set_block(block)
20082008

2009+
@overload
20092010
def drop(
20102011
self,
20112012
labels: typing.Any = None,
@@ -2014,7 +2015,33 @@ def drop(
20142015
index: typing.Any = None,
20152016
columns: Union[blocks.Label, Sequence[blocks.Label]] = None,
20162017
level: typing.Optional[LevelType] = None,
2018+
inplace: Literal[False] = False,
20172019
) -> DataFrame:
2020+
...
2021+
2022+
@overload
2023+
def drop(
2024+
self,
2025+
labels: typing.Any = None,
2026+
*,
2027+
axis: typing.Union[int, str] = 0,
2028+
index: typing.Any = None,
2029+
columns: Union[blocks.Label, Sequence[blocks.Label]] = None,
2030+
level: typing.Optional[LevelType] = None,
2031+
inplace: Literal[True],
2032+
) -> None:
2033+
...
2034+
2035+
def drop(
2036+
self,
2037+
labels: typing.Any = None,
2038+
*,
2039+
axis: typing.Union[int, str] = 0,
2040+
index: typing.Any = None,
2041+
columns: Union[blocks.Label, Sequence[blocks.Label]] = None,
2042+
level: typing.Optional[LevelType] = None,
2043+
inplace: bool = False,
2044+
) -> Optional[DataFrame]:
20182045
if labels:
20192046
if index or columns:
20202047
raise ValueError("Cannot specify both 'labels' and 'index'/'columns")
@@ -2056,7 +2083,11 @@ def drop(
20562083
inverse_condition_id, ops.invert_op
20572084
)
20582085
elif isinstance(index, indexes.Index):
2059-
return self._drop_by_index(index)
2086+
dropped_block = self._drop_by_index(index)._get_block()
2087+
if inplace:
2088+
self._set_block(dropped_block)
2089+
return None
2090+
return DataFrame(dropped_block)
20602091
else:
20612092
block, condition_id = block.project_expr(
20622093
ops.ne_op.as_expr(level_id, ex.const(index))
@@ -2068,7 +2099,12 @@ def drop(
20682099
block = block.drop_columns(self._sql_names(columns))
20692100
if index is None and not columns:
20702101
raise ValueError("Must specify 'labels' or 'index'/'columns")
2071-
return DataFrame(block)
2102+
2103+
if inplace:
2104+
self._set_block(block)
2105+
return None
2106+
else:
2107+
return DataFrame(block)
20722108

20732109
def _drop_by_index(self, index: indexes.Index) -> DataFrame:
20742110
block = index._block

tests/unit/test_dataframe.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,35 @@ def test_dataframe_rename_axis_inplace_returns_none(monkeypatch: pytest.MonkeyPa
129129
assert list(dataframe.index.names) == ["a", "b"]
130130

131131

132+
def test_dataframe_drop_columns_inplace_returns_none(monkeypatch: pytest.MonkeyPatch):
133+
dataframe = mocks.create_dataframe(
134+
monkeypatch, data={"col1": [1], "col2": [2], "col3": [3]}
135+
)
136+
assert dataframe.columns.to_list() == ["col1", "col2", "col3"]
137+
assert dataframe.drop(columns=["col1", "col3"], inplace=True) is None
138+
assert dataframe.columns.to_list() == ["col2"]
139+
140+
141+
def test_dataframe_drop_index_inplace_returns_none(monkeypatch: pytest.MonkeyPatch):
142+
dataframe = mocks.create_dataframe(
143+
monkeypatch, data={"col1": [1, 2, 3], "index_col": [0, 1, 2]}
144+
).set_index("index_col")
145+
# TODO(swast): Support dataframe.index.to_list()
146+
# assert dataframe.index.to_list() == [0, 1, 2]
147+
assert dataframe.drop(index=[0, 2], inplace=True) is None
148+
# assert dataframe.index.to_list() == [1]
149+
150+
151+
def test_dataframe_drop_columns_returns_new_dataframe(monkeypatch: pytest.MonkeyPatch):
152+
dataframe = mocks.create_dataframe(
153+
monkeypatch, data={"col1": [1], "col2": [2], "col3": [3]}
154+
)
155+
assert dataframe.columns.to_list() == ["col1", "col2", "col3"]
156+
new_dataframe = dataframe.drop(columns=["col1", "col3"])
157+
assert dataframe.columns.to_list() == ["col1", "col2", "col3"]
158+
assert new_dataframe.columns.to_list() == ["col2"]
159+
160+
132161
def test_dataframe_semantics_property_future_warning(
133162
monkeypatch: pytest.MonkeyPatch,
134163
):

0 commit comments

Comments
 (0)