BUG: overriden methods of subclasses of Styler are not called during rendering · Issue #52728 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

import pandas from pandas.io.formats.style import Styler

class CustomStyler(Styler): def _translate(self, *args, **kwargs): # override internal _translate method result = super()._translate(*args, **kwargs) raise NotImplementedError('TODO')

data = pandas.DataFrame({'column A': [4,3,7]}) CustomStyler(data).to_html() # CustomStyler._translate never gets called

type(CustomStyler(data)._copy()) # returns Styler not CustomStyler

Issue Description

rendering behaviour of Styler can't be customised by users, because internally Styler.render methods use ._copy to get a copy of the Styler that some internal changes are then applied to

This could be worked around by turning ._copy into a classmethod e.g. Styler._copy(cls, self) which returns a copy with the same type as self

Expected Behavior

type(CustomStyler(data)._copy()) == CustomStyler

Installed Versions

INSTALLED VERSIONS

commit : 478d340
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-69-generic
Version : #76~20.04.1-Ubuntu SMP Mon Mar 20 15:54:19 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 2.0.0
numpy : 1.22.0
pytz : 2021.3
dateutil : 2.8.2
setuptools : 56.0.0
pip : 21.1.1
Cython : None
pytest : 6.2.5
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.0.2
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 7.31.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None