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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ Other
^^^^^
- Trying to set the ``display.precision``, ``display.max_rows`` or ``display.max_columns`` using :meth:`set_option` to anything but a ``None`` or a positive int will raise a ``ValueError`` (:issue:`23348`)
- :meth:`DataFrame.to_csv` and :meth:`Series.to_csv` now support dicts as ``compression`` argument with key ``'method'`` being the compression method and others as additional compression options when the compression method is ``'zip'``. (:issue:`26023`)
- Bug in :meth:`DataFrame.drop` when passing empty :meth:`DatetimeIndex` or `list`


.. _whatsnew_1000.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 @@ -3915,6 +3915,8 @@ def drop(

for axis, labels in axes.items():
if labels is not None:
if is_list_like(labels) and not len(labels):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than do this, you can just handle the empty case in _drop_axis

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure @jreback? I tried a little, but I couldn't get it work while making sure to raise KeyError when passing str/List[str] to DataFrame.drop() that has DatetimeIndex.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the suggestion is to include the not len(labels) check in NDFrame._drop_axis. And when labels is empty we just return self from _drop_axis.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you try this

continue
obj = obj._drop_axis(labels, axis, level=level, errors=errors)

if inplace:
Expand Down
10 changes: 10 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,16 @@ def test_drop(self):
df.drop(labels=df[df.b > 0].index, inplace=True)
assert_frame_equal(df, expected)

def test_drop_empty_index(self):
# 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(np.array([])), df_index)
assert_frame_equal(df_index.drop(df_index[:0]), df_index)

def test_drop_multiindex_not_lexsorted(self):
# GH 11640

Expand Down