pandas icon indicating copy to clipboard operation
pandas copied to clipboard

BUG: Styler puts label before caption in to_latex()

Open bileamScheuvens opened this issue 1 year ago • 1 comments
trafficstars

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

commit : f538741432edf55c6b9fb5d0d496d2dd1d7c2457 python : 3.10.8.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.22631 machine : AMD64 processor : Intel64 Family 6 Model 183 Stepping 1, GenuineIntel byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : de_DE.cp1252

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

bileamScheuvens avatar Feb 17 '24 15:02 bileamScheuvens

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}
###

attack68 avatar Feb 17 '24 22:02 attack68

Embarassing, Artifact of my hacky post processing workflow, not a pandas issue. Sorry

bileamScheuvens avatar Feb 18 '24 10:02 bileamScheuvens