BUG: Regression: Styler.to_html and to_latex do not work if buf is a io.TextIOWrapper since 1.4.0rc0 · Issue #47053 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

import pandas as pd

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) dfsty = df.style dfsty.applymap(lambda _: 'color: red')

with open('kaboom.html', 'w') as f: dfsty.to_html(f)

Issue Description

The Styler object has, ever since version 1.3.0, a to_html method that takes as its first argument buf a

String, path object (implementing os.PathLike[str]), or file-like object implementing a string write() function.

The context manager in the example used above creates a io.TextIOWrapper object (here named f), which inherits a write() method taking a string from its parent class io.TextIOBase. As such, it should fit the criterion for a valid argument to buf. Attempting to use it as such, however, incurs the anger of lines 1221 and 1222 of io/formats/format.py (https://github.com/pandas-dev/pandas/blob/main/pandas/io/formats/format.py#L1221), producing a traceback as follows:

Traceback (most recent call last): File "C:\Users\Thurisatic\scripts\sandbox.py", line 10, in dfsty.to_html(f) File "C:\Users\Thurisatic\miniconda3\lib\site-packages\pandas\io\formats\style.py", line 1289, in to_html return save_to_buffer( File "C:\Users\Thurisatic\miniconda3\lib\site-packages\pandas\io\formats\format.py", line 1201, in save_to_buffer with get_buffer(buf, encoding=encoding) as f: File "C:\Users\Thurisatic\miniconda3\lib\contextlib.py", line 135, in enter return next(self.gen) File "C:\Users\Thurisatic\miniconda3\lib\site-packages\pandas\io\formats\format.py", line 1222, in get_buffer raise ValueError("buf is not a file name and encoding is specified.") ValueError: buf is not a file name and encoding is specified.

This is gated in the function get_buffer by a conditional block that checks first if encoding is None:, which fails, and then checks elif not isinstance(buf, str):, which succeeds, following to the raise on line 1222. It should be noted that commenting out both the elif line with isinstance and the immediately following raise 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 error

Everything 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)

Traceback (most recent call last): File "c:\Users\Thurisatic\scripts\sandbox.py", line 10, in dfsty.to_latex(f) File "C:\Users\Thurisatic\miniconda3\lib\site-packages\pandas\io\formats\style.py", line 1157, in to_latex return save_to_buffer(

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.
pandas_expected

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