Skip to content

BUG: UnboundLocalError: Variable referenced before assignment on df.style.to_latex(clines="all;data") #47203

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

Closed
3 tasks done
tomaarsen opened this issue Jun 2, 2022 · 1 comment · Fixed by #47204
Closed
3 tasks done
Assignees
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@tomaarsen
Copy link
Contributor

tomaarsen commented Jun 2, 2022

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

import pandas as pd
df = pd.DataFrame()
print(df.style.to_latex(clines="all;data"))

(The same bug exists with clines="skip-last;data")

Issue Description

Running the example above produces:

Traceback (most recent call last):
  File "/workspaces/pandas/demo.py", line 9, in <module>
    print(df.style.to_latex(clines="all;data"))
  File "/workspaces/pandas/pandas/io/formats/style.py", line 1145, in to_latex
    latex = obj._render_latex(
  File "/workspaces/pandas/pandas/io/formats/style_render.py", line 217, in _render_latex
    self._translate_latex(d, clines=clines)
  File "/workspaces/pandas/pandas/io/formats/style_render.py", line 911, in _translate_latex
    data_len = len(row_body_cells) if "data" in clines else 0
UnboundLocalError: local variable 'row_body_cells' referenced before assignment

Here is a permalink to the line that throws the Exception. The issue is somewhat obvious - if the DataFrame is empty, then there are no lines to iterate over, and row_body_cells is never defined.

Expected Behavior

I would expect an empty tabular being printed, e.g.

\begin{tabular}{l}
\end{tabular}

Furthermore, if the DataFrame does have a header, but no rows, then I expect only the header row to be printed, e.g.

import pandas as pd
df = pd.DataFrame(columns=["a", "b"])
print(df.style.to_latex(clines="all;data"))

should produce

\begin{tabular}{lll}
 & a & b \\
\end{tabular}

As of right now, that snippet produces the same exact Exception as the Reproducible Example.

Installed Versions

INSTALLED VERSIONS

commit : a2f5815
python : 3.8.13.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.72-microsoft-standard-WSL2
Version : #1 SMP Wed Oct 28 23:40:43 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : C.UTF-8
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.0.dev0+854.ga2f5815ddc
numpy : 1.22.4
pytz : 2022.1
dateutil : 2.8.2
setuptools : 56.0.0
pip : 22.1.2
Cython : 0.29.30
pytest : 7.1.2
hypothesis : 6.46.11
sphinx : 5.0.1
blosc : 1.10.6
feather : None
xlsxwriter : 3.0.3
lxml.etree : 4.9.0
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : 1.3.4
brotli : None
fastparquet : 0.8.1
fsspec : 2022.5.0
gcsfs : 2022.5.0
matplotlib : 3.5.2
numba : 0.55.2
numexpr : 2.8.1
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 8.0.0
pyreadstat : 1.1.6
pyxlsb : None
s3fs : 2022.5.0
scipy : 1.8.1
snappy :
sqlalchemy : 1.4.37
tables : 3.7.0
tabulate : 0.8.9
xarray : 2022.3.0
xlrd : 2.0.1
xlwt : 1.3.0
zstandard : None

@tomaarsen tomaarsen added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 2, 2022
@tomaarsen
Copy link
Contributor Author

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant