pandas
pandas copied to clipboard
BUG: Styler puts label before caption in to_latex()
Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
pd.DataFrame().to_latex(caption="table", label="tab:1")
Issue Description
Inside a float environment a label before a caption refs to the current section nr, not the figure nr displayed in the caption. This is rarely desirable and shouldn't be the only behavior.
Expected Behavior
Either these should be swapped:
# pandas/io/formats/style.py:1178-1185
if label:
obj.set_table_styles(
[{"selector": "label", "props": f":{{{label.replace(':', '§')}}}"}],
overwrite=False)
if caption:
obj.set_caption(caption)
or there should be an option for the order imo.
Installed Versions
pandas : 2.2.0 numpy : 1.26.2 pytz : 2023.3.post1 dateutil : 2.8.2 setuptools : 69.0.3 pip : 23.1.2 Cython : None pytest : 8.0.0 hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : 5.0.1 html5lib : None pymysql : None psycopg2 : None jinja2 : 3.1.2 IPython : 8.20.0 pandas_datareader : None adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : 4.12.2 bottleneck : None dataframe-api-compat : None fastparquet : None fsspec : 2023.12.2 gcsfs : None matplotlib : None numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None python-calamine : None pyxlsb : None s3fs : None scipy : 1.11.4 sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None zstandard : None tzdata : 2023.4 qtpy : None pyqt5 : None
This isn't true. The label is placed after the caption command in both table and long tables.
df = pd.DataFrame([[1,2],[3,4]])
print(df.to_latex(label="table:1", caption="Caption"))
###
\begin{table}
\caption{Caption}
\label{table:1}
\begin{tabular}{lrr}
\toprule
& 0 & 1 \\
\midrule
0 & 1 & 2 \\
1 & 3 & 4 \\
\bottomrule
\end{tabular}
\end{table}
###
Embarassing, Artifact of my hacky post processing workflow, not a pandas issue. Sorry