Skip to content

DOC: applymap calling on first column twice #24612

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

Closed
uperetz opened this issue Jan 4, 2019 · 2 comments · Fixed by #24633
Closed

DOC: applymap calling on first column twice #24612

uperetz opened this issue Jan 4, 2019 · 2 comments · Fixed by #24633

Comments

@uperetz
Copy link

uperetz commented Jan 4, 2019

Code

import pandas as pd
df=pd.DataFrame([[1,2,3],[4,5,6]])
df.index=['a','b'] #I was suspicious of the index
def bla(x):
    print('Applying to:',x)
    return x+1

df.applymap(bla)

Output

    Applying to: 1
    Applying to: 4
    Applying to: 1
    Applying to: 4
    Applying to: 2
    Applying to: 5
    Applying to: 3
    Applying to: 6
       0  1  2
    a  2  3  4
    b  5  6  7

Problem description

Seems applymap is calling on the first column twice. There is no effect in the end result, just a run-time hit as far as I can tell. This affects functions with side effects, as well as runtime.

Expected Output

    Applying to: 1
    Applying to: 4
    Applying to: 2
    Applying to: 5
    Applying to: 3
    Applying to: 6
       0  1  2
    a  2  3  4
    b  5  6  7

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.9.0-8-amd64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8

pandas: 0.19.2
nose: 1.3.7
pip: 9.0.1
setuptools: 36.0.1
Cython: None
numpy: 1.12.1
scipy: 0.18.1
statsmodels: None
xarray: None
IPython: 6.1.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: 3.3.0
numexpr: 2.6.1
matplotlib: 2.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.5.3
html5lib: 0.999999999
httplib2: 0.9.2
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
boto: None
pandas_datareader: None

@chris-b1
Copy link
Contributor

chris-b1 commented Jan 4, 2019

See the note in the apply docstring - though applymap should also apparently be updated to include this note.

Notes

In the current implementation apply calls func twice on the first column/row to decide whether it can ?> take a fast or slow code path. This can lead to unexpected behavior if func has side-effects, as they will > take effect twice for the first column/row.

@chris-b1 chris-b1 added this to the Contributions Welcome milestone Jan 4, 2019
@chris-b1 chris-b1 changed the title applymap calling on first column twice DOC: applymap calling on first column twice Jan 4, 2019
@uperetz
Copy link
Author

uperetz commented Jan 4, 2019

Thanks, I did not think to look at the apply help, guess this answers that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants