-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
REG: DataFrame.shift with axis=1 and CategoricalIndex columns #38504
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cead7e6
REG: DataFrame.shift with axis=1 and CategoricalIndex columns
jbrockmendel 0c7ec6e
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel 3c07dcc
test with periods=2, remove whatsnew
jbrockmendel f725bef
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel cadbe64
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel 9bb37bb
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4586,20 +4586,23 @@ def shift( | |
if axis == 1 and periods != 0 and fill_value is lib.no_default and ncols > 0: | ||
# We will infer fill_value to match the closest column | ||
|
||
# Use a column that we know is valid for our column's dtype GH#38434 | ||
label = self.columns[0] | ||
|
||
if periods > 0: | ||
result = self.iloc[:, :-periods] | ||
for col in range(min(ncols, abs(periods))): | ||
# TODO(EA2D): doing this in a loop unnecessary with 2D EAs | ||
# Define filler inside loop so we get a copy | ||
filler = self.iloc[:, 0].shift(len(self)) | ||
result.insert(0, col, filler, allow_duplicates=True) | ||
result.insert(0, label, filler, allow_duplicates=True) | ||
else: | ||
result = self.iloc[:, -periods:] | ||
for col in range(min(ncols, abs(periods))): | ||
# Define filler inside loop so we get a copy | ||
filler = self.iloc[:, -1].shift(len(self)) | ||
result.insert( | ||
len(result.columns), col, filler, allow_duplicates=True | ||
len(result.columns), label, filler, allow_duplicates=True | ||
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. Same comment as above. |
||
) | ||
|
||
result.columns = self.columns.copy() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
import pytest | ||
|
||
import pandas as pd | ||
from pandas import DataFrame, Index, Series, date_range, offsets | ||
from pandas import CategoricalIndex, DataFrame, Index, Series, date_range, offsets | ||
import pandas._testing as tm | ||
|
||
|
||
|
@@ -292,3 +292,25 @@ def test_shift_dt64values_int_fill_deprecated(self): | |
|
||
expected = DataFrame({"A": [pd.Timestamp(0), pd.Timestamp(0)], "B": df2["A"]}) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_shift_axis1_categorical_columns(self): | ||
# GH#38434 | ||
ci = CategoricalIndex(["a", "b", "c"]) | ||
df = DataFrame( | ||
{"a": [1, 3], "b": [2, 4], "c": [5, 6]}, index=ci[:-1], columns=ci | ||
) | ||
result = df.shift(axis=1) | ||
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 test a 3-column dataframe with |
||
|
||
expected = DataFrame( | ||
{"a": [np.nan, np.nan], "b": [1, 3], "c": [2, 4]}, index=ci[:-1], columns=ci | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# periods != 1 | ||
result = df.shift(2, axis=1) | ||
expected = DataFrame( | ||
{"a": [np.nan, np.nan], "b": [np.nan, np.nan], "c": [1, 3]}, | ||
index=ci[:-1], | ||
columns=ci, | ||
) | ||
tm.assert_frame_equal(result, expected) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Does this work correctly for periods > 1? Looks like it inserts the same label in all locations, where Id think it should insert
self.columns[col]
?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.
This should be benign because we set result.columns directly below. ill add a test to be on the safe side