BUG: caching in CachedAccessor is problematic and (probably) unnecessary #47667
Labels
Accessors
accessor registration mechanism (not .str, .dt, .cat)
Bug
Performance
Memory or execution speed performance
Strings
String extension data type and string data
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
Caching pandas Series/Frame accessors like plot, str, etc.. seems to be problematic. See also pydata/xarray#3268 (pandas accessors were ported from xarray)
Caching creates some unwanted side effects:
And illustrated for pandas by:
Output:
Expected Behavior
The circular reference could be solved by using a weakref as explained in pydata/xarray#3268 (comment).
The bugs with the str accessor could be resolved by not initializing any attribute but
_data
in theStringMethods
class.However, I think the best solution is getting rid of the caching logic (which could be achieved by removing a single line:
pandas/pandas/core/accessor.py
Line 187 in 01b86ed
Installed Versions
INSTALLED VERSIONS
commit : e8093ba
python : 3.10.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19043
machine : AMD64
processor : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252
pandas : 1.4.3
numpy : 1.23.0
pytz : 2022.1
dateutil : 2.8.2
setuptools : 58.1.0
pip : 22.1.2
Cython : None
pytest : 7.0.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.0.3
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.1
IPython : 8.1.0
pandas_datareader: None
bs4 : 4.10.0
bottleneck : 1.3.5
brotli : None
fastparquet : None
fsspec : 2022.02.0
gcsfs : None
markupsafe : 2.1.0
matplotlib : 3.5.2
numba : None
numexpr : 2.8.3
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 8.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.1
snappy : None
sqlalchemy : 1.4.32
tables : None
tabulate : 0.8.9
xarray : 2022.3.0
xlrd : None
xlwt : None
zstandard : None
The text was updated successfully, but these errors were encountered: