-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: df.diff axis=1 mixed dtypes #36710
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 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1184a3c
BUG: df.diff axis=1 mixed dtypes
jbrockmendel 36f639b
mypy fixup
jbrockmendel 37f1021
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel 56a8813
update doctest
jbrockmendel 9f04682
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel d49771a
doctest fixup
jbrockmendel 3b86192
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,7 @@ | |
is_dict_like, | ||
is_dtype_equal, | ||
is_extension_array_dtype, | ||
is_float, | ||
is_float_dtype, | ||
is_hashable, | ||
is_integer, | ||
|
@@ -4458,7 +4459,34 @@ def _replace_columnwise( | |
return res.__finalize__(self) | ||
|
||
@doc(NDFrame.shift, klass=_shared_doc_kwargs["klass"]) | ||
def shift(self, periods=1, freq=None, axis=0, fill_value=None) -> DataFrame: | ||
def shift( | ||
self, periods=1, freq=None, axis=0, fill_value=lib.no_default | ||
) -> DataFrame: | ||
axis = self._get_axis_number(axis) | ||
|
||
ncols = len(self.columns) | ||
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 | ||
|
||
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) | ||
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 | ||
) | ||
|
||
result.columns = self.columns.copy() | ||
return result | ||
|
||
return super().shift( | ||
periods=periods, freq=freq, axis=axis, fill_value=fill_value | ||
) | ||
|
@@ -7208,13 +7236,13 @@ def melt( | |
Difference with previous column | ||
|
||
>>> df.diff(axis=1) | ||
a b c | ||
0 NaN 0.0 0.0 | ||
1 NaN -1.0 3.0 | ||
2 NaN -1.0 7.0 | ||
3 NaN -1.0 13.0 | ||
4 NaN 0.0 20.0 | ||
5 NaN 2.0 28.0 | ||
a b c | ||
0 NaN 0 0 | ||
1 NaN -1 3 | ||
2 NaN -1 7 | ||
3 NaN -1 13 | ||
4 NaN 0 20 | ||
5 NaN 2 28 | ||
|
||
Difference with 3rd previous row | ||
|
||
|
@@ -7248,12 +7276,15 @@ def melt( | |
), | ||
) | ||
def diff(self, periods: int = 1, axis: Axis = 0) -> DataFrame: | ||
if not isinstance(periods, int): | ||
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. use is_integer 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. will do |
||
if not (is_float(periods) and periods.is_integer()): | ||
raise ValueError("periods must be an integer") | ||
periods = int(periods) | ||
|
||
bm_axis = self._get_block_manager_axis(axis) | ||
self._consolidate_inplace() | ||
|
||
if bm_axis == 0 and periods != 0: | ||
return self.T.diff(periods, axis=0).T | ||
return self - self.shift(periods, axis=axis) # type: ignore[operator] | ||
|
||
new_data = self._mgr.diff(n=periods, axis=bm_axis) | ||
return self._constructor(new_data) | ||
|
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
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
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
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.
aren't these inplace? do we really want that? (or does .iloc do an implict copy the way you have it), i don't remember exactly
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.
it is inplace on
result
which we havent returned to the user yet