Skip to content

BUG: DataFrame.drop raising TypeError when passing empty DatetimeIndex/list #28114

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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Sparse
Other
^^^^^

-
- Bug in :meth:`DataFrame.drop` when passing empty :meth:`DatetimeIndex` or :meth:`list`
-

.. _whatsnew_0.252.contributors:
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3888,6 +3888,8 @@ def drop(

for axis, labels in axes.items():
if labels is not None:
if isinstance(labels, (list, Index)) and len(labels) == 0:
continue
obj = obj._drop_axis(labels, axis, level=level, errors=errors)

if inplace:
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/test_axis_select_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ def test_drop(self):
df.drop(labels=df[df.b > 0].index, inplace=True)
assert_frame_equal(df, expected)

# Empty DatetimeIndex/list, GH 27994
df_index = pd.DataFrame(
{"a": [0, 1]},
index=[pd.Timestamp("2019-01-01"), pd.Timestamp("2019-01-01")],
)
assert_frame_equal(df_index.drop([]), df_index)
assert_frame_equal(df_index.drop(df_index[df_index["a"] > 2].index), df_index)

def test_drop_multiindex_not_lexsorted(self):
# GH 11640

Expand Down