-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
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
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -6949,7 +6949,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): | ||
| new_index.freq = self.freq | ||
|
||
|
|
||
| return new_index | ||
|
|
||
| @final | ||
| def infer_objects(self, copy: bool = True) -> Index: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| deepcopy, | ||
| ) | ||
|
|
||
| from datetime import datetime | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
|
|
@@ -12,6 +13,7 @@ | |
| DataFrame, | ||
| Index, | ||
| Series, | ||
| Timestamp, | ||
| date_range, | ||
| ) | ||
| import pandas._testing as tm | ||
|
|
@@ -483,3 +485,17 @@ 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", ["Y", "M", "D"]) | ||
| def test_drop_method_freq_preservation(self, freq): | ||
| start = "1970-01-01" | ||
|
Member
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
Author
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 |
||
| 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], inplace=False) | ||
| tm.assert_equal(test_df.index.freq, index.freq) | ||
|
|
||
| # set inplace as true | ||
| df.drop(index=df.index[0], inplace=True) | ||
| tm.assert_equal(df.index.freq, index.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