BUG: Regression: Styler.to_html
and to_latex
do not work if buf
is a io.TextIOWrapper
since 1.4.0rc0
#47053
Closed
3 tasks done
Labels
Bug
IO HTML
read_html, to_html, Styler.apply, Styler.applymap
IO LaTeX
to_latex
Regression
Functionality that used to work in a prior pandas version
Styler
conditional formatting using DataFrame.style
Milestone
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
The Styler object has, ever since version 1.3.0, a
to_html
method that takes as its first argumentbuf
aThe context manager in the example used above creates a
io.TextIOWrapper
object (here namedf
), which inherits awrite()
method taking a string from its parent classio.TextIOBase
. As such, it should fit the criterion for a valid argument tobuf
. Attempting to use it as such, however, incurs the anger of lines 1221 and 1222 ofio/formats/format.py
(https://github.com/pandas-dev/pandas/blob/main/pandas/io/formats/format.py#L1221), producing a traceback as follows:This is gated in the function
get_buffer
by a conditional block that checks firstif encoding is None:
, which fails, and then checkselif not isinstance(buf, str):
, which succeeds, following to theraise
on line 1222. It should be noted that commenting out both theelif
line withisinstance
and the immediately followingraise
produces the expected output below with no observed errors. As suggested in title, pandas version 1.3.5 (the last before 1.4.0rc0) does not produce an errorEverything above is nearly identical for
Styler.to_latex
except the with (kaboom.tex
instead of.html
) and the traceback (differing only in the following beginning lines)Expected Behavior
Either an .html (

to_html
) or .tex (to_latex
) file whose contents are a table containing columns A and B, rows 0 and 1, and four red-colored cells of 1, 2, 3, and 4.Installed Versions
As mentioned by others,
pandas.showversions()
fails.The apparent cause for this is that setuptools has a possibly-buggy assertion statement in its local version of distutils, but (temporarily) commenting out the assert allows
pandas.showversions()
to run successfully, producing the following output:INSTALLED VERSIONS
commit : 98ca9f0
python : 3.10.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 1.5.0.dev0+785.g98ca9f05b
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
pip : 22.1
setuptools : 62.2.0
Cython : None
pytest : 6.2.5
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.8.0
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.3.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli :
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : 2.1.1
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
A venv created with pandas 1.3.5 (where the error doesn't occur) has the following results of
show_versions()
(which also runs without error)INSTALLED VERSIONS
commit : 66e3805
python : 3.10.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 1.3.5
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
pip : 22.0.4
setuptools : 58.1.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
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: