-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Different results from DataFrame.apply and str accessor #38979
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
Comments
I noticed the same issue which @pLeBlanc93 post. When using str accessor mothed with df.apply on dtype of series is object in version 1.2 , On dtype is 'string', everything is OK here is my test code temp_df = pd.DataFrame(
data=np.random.rand(5, 2)
) as type of 'str' (temp_df
.astype(str)
.apply(
func=lambda ser: ser.str.replace(' ', ''),
**axis=1**
)
) result:
(temp_df
.astype(str)
.apply(
func=lambda ser: ser.str.replace(' ', ''),
**axis=0**
)
) result:
as type of 'string' (temp_df
.astype('string')
.apply(
func=lambda ser: ser.str.replace(' ', ''),
**axis=1**
)
) result:
(temp_df
.astype('string')
.apply(
func=lambda ser: ser.str.replace(' ', ''),
**axis=0**
)
) result:
Output of pd.show_versions()INSTALLED VERSIONScommit : 3e89b4c pandas : 1.2.0 |
Beside that, I noticed when using old dtype, id of all slices would be same if axis=1, but unique when using new dtype. old dtype (temp_df
.apply(
func=id,
axis=1
)
) result
new dtype (temp_df
.convert_dtypes()
.apply(
func=id,
axis=1
)
) result
|
Going through the code I found the following: |
@jbrockmendel any thoughts here. Although the regression is in #36357, imo the problem is due to #34909 #34909 has caused several regressions. do we want to keep that performance gain and try to patch this further. I assume that, we have no control over other 3rd party registered accessors, so would want a fix here that works with any accessor and not just a fix for the str accessor. @jorisvandenbossche can you reproduce this with geopandas or am I wrong in my assumption and the problem is with the str accessor. |
Yah, best guess is we need to clear the cache between steps of the iteration |
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.
Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.
Code Sample, a copy-pastable example
Problem description
Using the
str
accessor gives different results on 1.1.5 and 1.2.x:Output of
pd.show_versions()
INSTALLED VERSIONS
commit : 6cdb4e7
python : 3.7.9.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 63 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
pandas : 1.2.0+22.g6cdb4e7fb
numpy : 1.19.5
pytz : 2020.5
dateutil : 2.8.1
pip : 20.3.3
setuptools : 51.0.0.post20201207
Cython : 0.29.21
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: