Skip to content
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

like old issue #2936: df.apply(foo) will run twice at the first row #30815

Closed
kerwin6182828 opened this issue Jan 8, 2020 · 2 comments · Fixed by #34183
Closed

like old issue #2936: df.apply(foo) will run twice at the first row #30815

kerwin6182828 opened this issue Jan 8, 2020 · 2 comments · Fixed by #34183
Labels
Apply Apply, Aggregate, Transform
Milestone

Comments

@kerwin6182828
Copy link

kerwin6182828 commented Jan 8, 2020

- :meth:`DataFrame.apply` and :meth:`DataFrame.applymap` now evaluates first row/column only once (:issue:`31620`, :issue:`30815`, :issue:`33879`).

.. ipython:: python

    df = pd.DataFrame({'a': [1, 2], 'b': [3, 6]})

    def func(row):
        print(row)
        return row

*Previous behavior*:

.. code-block:: ipython

    In [4]: df.apply(func, axis=1)
    a    1
    b    3
    Name: 0, dtype: int64
    a    1
    b    3
    Name: 0, dtype: int64
    a    2
    b    6
    Name: 1, dtype: int64
    Out[4]:
       a  b
    0  1  3
    1  2  6

*New behavior*:

.. ipython:: python

    df.apply(func, axis=1)

some example....

def foo(row):
    status = row.get("status")
    print(status)
    status = True if status==1 else False
    row["status"] = status
    return row
    
df = pd.DataFrame([{"name":"bob", "status":1}])
df.apply(foo, axis=1)

>>>>>>>> output:
1
True

Problem description

today, I have a problem that df.apply(foo) will run twice at the first row like old issue #2936.
I read a lot of explanations, but still can't find some help. As I wrote above, when df just have 1 row and then apply function will run twice at this row lead to print diffierent values.
(It just modified my original value "1" to "True", but it confused me a lot...)

I am wandering how to handle this issue, and is this a pandas bug?

thanks a lot~~

@jreback
Copy link
Contributor

jreback commented Jan 8, 2020

you need to show_versions()

@mroeschke mroeschke added the Needs Info Clarification about behavior needed to assess issue label Jan 9, 2020
@kerwin6182828
Copy link
Author

you need to show_versions()

pandas : 0.25.3
numpy : 1.17.0

python : 3.7.4.final.0
python-bits : 64
OS : Darwin

Did you have the same result as me ?
(I have to face to this situation many times, so it really bother me ...)

@jbrockmendel jbrockmendel added the Apply Apply, Aggregate, Transform label Feb 25, 2020
@simonjayhawkins simonjayhawkins removed the Needs Info Clarification about behavior needed to assess issue label Mar 31, 2020
@jreback jreback added this to the 1.1 milestone Jun 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants