Skip to content

Commit e46026f

Browse files
fujiaxiangjreback
authored andcommitted
BUG: Fix wrong error in df drop with non unique datetime index and invalid keys (#30446)
1 parent 114d552 commit e46026f

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

doc/source/whatsnew/v1.0.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -715,8 +715,10 @@ Datetimelike
715715
- Bug in :func:`pandas.to_datetime` failing for `deques` when using ``cache=True`` (the default) (:issue:`29403`)
716716
- Bug in :meth:`Series.item` with ``datetime64`` or ``timedelta64`` dtype, :meth:`DatetimeIndex.item`, and :meth:`TimedeltaIndex.item` returning an integer instead of a :class:`Timestamp` or :class:`Timedelta` (:issue:`30175`)
717717
- Bug in :class:`DatetimeIndex` addition when adding a non-optimized :class:`DateOffset` incorrectly dropping timezone information (:issue:`30336`)
718+
- Bug in :meth:`DataFrame.drop` where attempting to drop non-existent values from a DatetimeIndex would yield a confusing error message (:issue:`30399`)
718719
- Bug in :meth:`DataFrame.append` would remove the timezone-awareness of new data (:issue:`30238`)
719720

721+
720722
Timedelta
721723
^^^^^^^^^
722724
- Bug in subtracting a :class:`TimedeltaIndex` or :class:`TimedeltaArray` from a ``np.datetime64`` object (:issue:`29558`)

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4551,7 +4551,7 @@ def get_indexer_non_unique(self, target):
45514551

45524552
if is_categorical(target):
45534553
tgt_values = np.asarray(target)
4554-
elif self.is_all_dates:
4554+
elif self.is_all_dates and target.is_all_dates: # GH 30399
45554555
tgt_values = target.asi8
45564556
else:
45574557
tgt_values = target._ndarray_values

pandas/tests/indexes/multi/test_drop.py

+16
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,19 @@ def test_drop_not_lexsorted():
139139
tm.assert_index_equal(lexsorted_mi, not_lexsorted_mi)
140140
with tm.assert_produces_warning(PerformanceWarning):
141141
tm.assert_index_equal(lexsorted_mi.drop("a"), not_lexsorted_mi.drop("a"))
142+
143+
144+
def test_drop_with_non_unique_datetime_index_and_invalid_keys():
145+
# GH 30399
146+
147+
# define dataframe with unique datetime index
148+
df = pd.DataFrame(
149+
np.random.randn(5, 3),
150+
columns=["a", "b", "c"],
151+
index=pd.date_range("2012", freq="H", periods=5),
152+
)
153+
# create dataframe with non-unique datetime index
154+
df = df.iloc[[0, 2, 2, 3]].copy()
155+
156+
with pytest.raises(KeyError, match="not found in axis"):
157+
df.drop(["a", "b"]) # Dropping with labels not exist in the index

0 commit comments

Comments
 (0)