BUG/ENH: Fix apply to only call `func` once on the first column/row
State: closed

closes #33879
closes #30815
closes #31620
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Labels:
name: Reshaping
description: Concat, Merge/Join, Stack/Unstack, Explode

Comments:
Hello @alonme! Thanks for updating this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:









There are currently no PEP 8 issues detected in this Pull Request. Cheers! :beers: 

##### Comment last updated at 2020-06-02 14:00:03 UTC
Hope to get this will be merged instead of https://github.com/pandas-dev/pandas/pull/33880

There are couple of points i am not sure of, and would like to discuss before wrapping it up completely. they are also pointed in comments to the code

1. object conversion is done outside of the reduction, i couldn't find a way to do wrap it up in `get_result`
1.1 If we use current method, i wasn't sure if `DataFrame.infer_objects` is more fitting than `convert_dtypes`.
1.2 i hope that by having another method to do that, i would be able to not imoprt Series and DataFrame

2. I did not provide a full solution for apply_raw, i wanted to make sure the general idea for a solution is valid.

3. Should i add tests to check that the function is only called once?


as this actually solves also the case for #31695, i can add the doc change (the PR for this issue seems a bit stuck)
#31111 wasn't fixed by me.
i was confused as it was solved (?) somewhere between 1.0.3 and master
Hey guys, waiting for your review of how i fixed your comments.
And for a decision regarding the use of `unittest.mock`
@jreback thanks for the review!

Important to say that this solution is not complete, i have not solved the case for `apply_raw`.
I already tried to look at it a bit and couldn't find an elegant way to insert the pre-computed result and not processing it again.

I will try to spend a bit more time on that, However i won't have much time in the near future,
So if you prefer we can merge this and open another issue for `raw`.

@jreback i fixed/asked a question regarding all issues,

I don't believe i will have time to fix the problem in `raw_apply` in the next month or so(meaning it will still run twice as it does now for the raw code path).
I believe there should be a simple solution there, its just finding the right way to slice the values passed to `apply_along_axis` so the first object is not passed. and then add the pre-computed object instead of that first value.
> @jreback i fixed/asked a question regarding all issues,
> 
> I don't believe i will have time to fix the problem in `raw_apply` in the next month or so(meaning it will still run twice as it does now for the raw code path).
> I believe there should be a simple solution there, its just finding the right way to slice the values passed to `apply_along_axis` so the first object is not passed. and then add the pre-computed object instead of that first value.

can u add a test that shows this case and xfail it; also pls open an issue for this case
@jreback 
Added the test and opened the issue,

There are 3 open conversations for which i answered reviewers questions and i am waiting for a reviewer to close them/ decide if we need to change code/ open an issue.


@jreback green!
Just tell me if you want me to add the issue number also to the xfail reason
thanks @alonme very nice & very responsive!