-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
BUG: DataFrame.drop unexpectedly drops frequency #58846
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
IncompatibleFrequency, | ||
OutOfBoundsDatetime, | ||
Timestamp, | ||
to_offset, | ||
tz_compare, | ||
) | ||
from pandas._typing import ( | ||
|
@@ -6949,7 +6950,15 @@ def drop( | |
if errors != "ignore": | ||
raise KeyError(f"{labels[mask].tolist()} not found in axis") | ||
indexer = indexer[~mask] | ||
return self.delete(indexer) | ||
new_index = self.delete(indexer) | ||
|
||
# check if we need to set the freq attribute | ||
from pandas import DatetimeIndex | ||
|
||
if isinstance(self, DatetimeIndex) and isinstance(new_index, DatetimeIndex): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should only do this |
||
new_index.freq = to_offset(new_index.inferred_freq) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inferred_freq can be expensive. might be cheaper to invert |
||
|
||
return new_index | ||
|
||
@final | ||
def infer_objects(self, copy: bool = True) -> Index: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -483,3 +483,19 @@ 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): | ||
start = "1970-01-01" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a "# GH#???" reference There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
periods = 10 | ||
|
||
index = date_range(start=start, periods=10, 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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,9 +132,9 @@ def test_delete_slice2(self, tz, unit): | |
assert result.freq == expected.freq | ||
assert result.tz == expected.tz | ||
|
||
# reset freq to None | ||
# reset freq | ||
result = ts.drop(ts.index[[1, 3, 5, 7, 9]]).index | ||
expected = dti[::2]._with_freq(None) | ||
expected = dti[::2]._with_freq("infer") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the "infer" here should be unnecessary if dti already has a freq There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed |
||
tm.assert_index_equal(result, expected) | ||
assert result.name == expected.name | ||
assert result.freq == expected.freq | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of doing this here, override DatetimeIndex.drop