BUG: dataframe.apply() loops on first row when applied method attempts to modify the row · Issue #35462 · pandas-dev/pandas (original) (raw)
With pandas 1.1.0 on Python 3.6.8:
>>> import pandas as pd
>>> df = pd.DataFrame({'a': list(range(0,100)), 'b': list(range(100,200))})
>>> def func(row):
... row.loc['a'] += 1
... return row
...
>>> df
a b
0 0 100
1 1 101
2 2 102
3 3 103
4 4 104
.. .. ...
95 95 195
96 96 196
97 97 197
98 98 198
99 99 199
[100 rows x 2 columns]
>>> df.apply(func, axis=1)
a b
0 100 100
1 100 100
2 100 100
3 100 100
4 100 100
.. ... ...
95 100 100
96 100 100
97 100 100
98 100 100
99 100 100
[100 rows x 2 columns]
>>> df
a b
0 100 100
1 1 101
2 2 102
3 3 103
4 4 104
.. ... ...
95 95 195
96 96 196
97 97 197
98 98 198
99 99 199
[100 rows x 2 columns]
>>> pd.show_versions()
INSTALLED VERSIONS
------------------
commit : d9fff2792bf16178d4e450fe7384244e50635733
python : 3.6.8.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.17763
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
pandas : 1.1.0
numpy : 1.18.2
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 42.0.2
Cython : None
pytest : 5.4.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : 4.9.1
bottleneck : 1.3.2
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.2.1
numexpr : 2.7.1
odfpy : None
openpyxl : 2.5.14
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.3.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
numba : None
With pandas 1.0.5:
>>> import pandas as pd
>>> df = pd.DataFrame({'a': list(range(0,100)), 'b': list(range(100,200))})
>>> def func(row):
... row.loc['a'] += 1
... return row
...
>>> df
a b
0 0 100
1 1 101
2 2 102
3 3 103
4 4 104
.. .. ...
95 95 195
96 96 196
97 97 197
98 98 198
99 99 199
[100 rows x 2 columns]
>>> df.apply(func, axis=1)
a b
0 1 100
1 2 101
2 3 102
3 4 103
4 5 104
.. ... ...
95 96 195
96 97 196
97 98 197
98 99 198
99 100 199
[100 rows x 2 columns]
>>> df
a b
0 1 100
1 2 101
2 3 102
3 4 103
4 5 104
.. ... ...
95 96 195
96 97 196
97 98 197
98 99 198
99 100 199
[100 rows x 2 columns]
>>> pd.show_versions()
INSTALLED VERSIONS
------------------
commit : None
python : 3.6.8.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
pandas : 1.0.5
numpy : 1.18.2
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 42.0.2
Cython : None
pytest : 5.4.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : 4.9.1
bottleneck : 1.3.2
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.2.1
numexpr : 2.7.1
odfpy : None
openpyxl : 2.5.14
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : None
scipy : 1.3.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : None
numba : None
I expected the behavior of 1.0.5 in 1.1.0, did I misunderstood the apply method?
Thank you for your help.