Description
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
(optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
In [1]: import pandas as pd
...:
...: df = pd.DataFrame({'a': [1,2,3]})
...:
...: def mul2(x):
...: print('Hello')
...: return x*2
...:
...: print('Apply:')
...: df.apply(mul2)
...:
...: print('\nApply Raw:')
...: df.apply(mul2, raw=True)
Apply:
Hello
Apply Raw:
Hello
Hello
Problem description
Because of an implementation detail, the applied function is applied twice on the first row/column,
This results in side effects happening twice.
Other than the apply_raw
code path, this issues should be fixed by #34183.
Expected Output
Apply:
Hello
Apply Raw:
Hello
Output of pd.show_versions()
INSTALLED VERSIONS
commit : 07edc87
python : 3.7.5.final.0
python-bits : 64
OS : Darwin
OS-release : 19.5.0
Version : Darwin Kernel Version 19.5.0: Thu Apr 30 18:25:59 PDT 2020; root:xnu-6153.121.1~7/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.1.0.dev0+1706.g07edc871f.dirty
numpy : 1.18.3
pytz : 2020.1
dateutil : 2.8.1
pip : 19.2.3
setuptools : 41.2.0
Cython : 0.29.17
pytest : 5.4.1
hypothesis : 5.11.0
sphinx : 3.0.3
blosc : 1.9.1
feather : None
xlsxwriter : 1.2.8
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.14.0
pandas_datareader: None
bs4 : 4.9.0
bottleneck : 1.3.2
fastparquet : 0.3.3
gcsfs : None
matplotlib : 3.2.1
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : 0.17.0
pytables : None
pyxlsb : None
s3fs : 0.4.2
scipy : 1.4.1
sqlalchemy : 1.3.16
tables : 3.6.1
tabulate : 0.8.7
xarray : 0.15.1
xlrd : 1.2.0
xlwt : 1.3.0
numba : 0.49.0