Skip to content

BUG: DataFrame.drop unexpectedly drops frequency #58846

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ Categorical
Datetimelike
^^^^^^^^^^^^
- Bug in :class:`Timestamp` constructor failing to raise when ``tz=None`` is explicitly specified in conjunction with timezone-aware ``tzinfo`` or data (:issue:`48688`)
- Bug in :func:`Datafreme.drop` returning ``Freq=None`` when the dataframe has a ``DatetimeIndex`` (:issue:`58743`)
- Bug in :func:`date_range` where the last valid timestamp would sometimes not be produced (:issue:`56134`)
- Bug in :func:`date_range` where using a negative frequency value would not include all points between the start and end values (:issue:`56382`)
- Bug in :func:`tseries.api.guess_datetime_format` would fail to infer time format when "%Y" == "%H%M" (:issue:`57452`)
Expand Down
15 changes: 14 additions & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@
from pandas.core.tools.times import to_time

if TYPE_CHECKING:
from collections.abc import Hashable
from collections.abc import Hashable, Iterable

from pandas._typing import (
Dtype,
DtypeObj,
Frequency,
IgnoreRaise,
IntervalClosedType,
Self,
TimeAmbiguous,
Expand Down Expand Up @@ -813,6 +814,18 @@ def indexer_between_time(

return mask.nonzero()[0]

# --------------------------------------------------------------------

def drop(
self,
labels: Index | np.ndarray | Iterable[Hashable],
errors: IgnoreRaise = "raise",
) -> DatetimeIndex:
if self.freq is not None:
return Index.drop(self, labels, errors)._with_freq("infer") # type: ignore[attr-defined]
else:
return Index.drop(self, labels, errors) # type: ignore[return-value]


def date_range(
start=None,
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,20 @@ def test_flags_identity(self, frame_or_series):
assert obj.flags is obj.flags
obj2 = obj.copy()
assert obj2.flags is not obj.flags

@pytest.mark.parametrize("freq", ["YE", "ME", "D"])
def test_drop_method_freq_preservation(self, freq):
# GH 58846
start = "1970-01-01"
Copy link
Member

Choose a reason for hiding this comment

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

can you add a "# GH#???" reference

Copy link
Author

Choose a reason for hiding this comment

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

Added # GH 58846

periods = 10

index = date_range(start=start, periods=periods, freq=freq)
df = DataFrame((np.ones(len(index))), index=index)

# set inplace as false
test_df = df.drop(index=df.index[[0, periods - 1]], inplace=False)
tm.assert_equal(test_df.index.freq, index.freq)

# set inplace as true
df.drop(index=df.index[[0, periods - 1]], inplace=True)
tm.assert_equal(df.index.freq, index.freq)
3 changes: 1 addition & 2 deletions pandas/tests/indexes/datetimes/methods/test_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ def test_delete_slice2(self, tz, unit):
assert result.freq == expected.freq
assert result.tz == expected.tz

# reset freq to None
result = ts.drop(ts.index[[1, 3, 5, 7, 9]]).index
expected = dti[::2]._with_freq(None)
expected = dti[::2]
tm.assert_index_equal(result, expected)
assert result.name == expected.name
assert result.freq == expected.freq
Expand Down
Loading