Skip to content
Prev Previous commit
Next Next commit
Small changes for PR #28114
* Make separate change * Fix whatsnew * Use is_list_like
  • Loading branch information
Katsuya Horiuchi committed Aug 24, 2019
commit f32ed62a76d2c05065b7ef731db1ce26e62ed251
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`
- Bug in :meth:`DataFrame.drop` when passing empty :meth:`DatetimeIndex` or `list`
-

.. _whatsnew_0.252.contributors:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3888,7 +3888,7 @@ def drop(

for axis, labels in axes.items():
if labels is not None:
if isinstance(labels, (list, Index)) and len(labels) == 0:
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)

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

# Empty DatetimeIndex/list, GH 27994
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(df_index[df_index["a"] > 2].index), 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