-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG/ENH: Fix apply to only call func
once on the first column/row
#34183
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
15 commits
Select commit
Hold shift + click to select a range
515de45
Fix apply to only call `func` once on the first column/row
alonme b71921b
Fix groupby reducer tests
alonme a8a6fd5
fix pep8
alonme 3957956
Fix imports for linting
alonme 4564e9f
Add some tests, use ABCseries, remove wrong docs for applymap
alonme e5bdb20
Use mock to test call_count
alonme 1eb9442
Remove DataFrame import
alonme a59934d
Reduction returns success value and partial result is returned in res…
alonme 7ee1faa
Remove unittest.mock
alonme fa6299d
Add whatsnew, small CR refactor
alonme 58cefd6
Fix doc linting
alonme 07edc87
Fix code blocks in doc
alonme 2cd8ee6
Add test for apply_raw with xfail
alonme ec22529
Fix whatsnew python blocks
alonme 55db871
fix indent
alonme 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -718,12 +718,73 @@ def apply_list(row): | |
|
||
def test_apply_noreduction_tzaware_object(self): | ||
# https://github.com/pandas-dev/pandas/issues/31505 | ||
df = pd.DataFrame({"foo": [pd.Timestamp("2020", tz="UTC")]}, dtype="object") | ||
df = pd.DataFrame( | ||
{"foo": [pd.Timestamp("2020", tz="UTC")]}, dtype="datetime64[ns, UTC]" | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
result = df.apply(lambda x: x) | ||
tm.assert_frame_equal(result, df) | ||
result = df.apply(lambda x: x.copy()) | ||
tm.assert_frame_equal(result, df) | ||
|
||
def test_apply_function_runs_once(self): | ||
alonme marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# https://github.com/pandas-dev/pandas/issues/30815 | ||
|
||
df = pd.DataFrame({"a": [1, 2, 3]}) | ||
names = [] # Save row names function is applied to | ||
|
||
def reducing_function(row): | ||
names.append(row.name) | ||
|
||
def non_reducing_function(row): | ||
names.append(row.name) | ||
return row | ||
|
||
for func in [reducing_function, non_reducing_function]: | ||
del names[:] | ||
|
||
df.apply(func, axis=1) | ||
assert names == list(df.index) | ||
|
||
@pytest.mark.xfail( | ||
reason="The 'run once' enhancement for apply_raw not implemented yet." | ||
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 the issue number you create here 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. Its listed in comment in the first row of test function, |
||
) | ||
def test_apply_raw_function_runs_once(self): | ||
# https://github.com/pandas-dev/pandas/issues/34506 | ||
|
||
df = pd.DataFrame({"a": [1, 2, 3]}) | ||
values = [] # Save row values function is applied to | ||
|
||
def reducing_function(row): | ||
values.extend(row) | ||
|
||
def non_reducing_function(row): | ||
values.extend(row) | ||
return row | ||
|
||
for func in [reducing_function, non_reducing_function]: | ||
del values[:] | ||
|
||
df.apply(func, raw=True, axis=1) | ||
assert values == list(df.a.to_list()) | ||
|
||
def test_applymap_function_runs_once(self): | ||
|
||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
df = pd.DataFrame({"a": [1, 2, 3]}) | ||
values = [] # Save values function is applied to | ||
|
||
def reducing_function(val): | ||
values.append(val) | ||
|
||
def non_reducing_function(val): | ||
values.append(val) | ||
return val | ||
|
||
for func in [reducing_function, non_reducing_function]: | ||
del values[:] | ||
|
||
df.applymap(func) | ||
assert values == df.a.to_list() | ||
|
||
|
||
class TestInferOutputShape: | ||
# the user has supplied an opaque UDF where | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.