Skip to content
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

BUG: last column name is not bold when applying bold font to column names together with hiding index before converting to latex #52218

Open
3 tasks done
yangtzech opened this issue Mar 26, 2023 · 7 comments
Labels
Bug Styler conditional formatting using DataFrame.style

Comments

@yangtzech
Copy link

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(
    dict(name=['Raphael', 'Donatello'],
         mask=['red', 'purple'],
         weapon=['sai', 'bo staff']
        )
)
print(df.style.hide(axis="index").applymap_index(lambda v: "font-weight: bold;", axis="columns").to_latex(convert_css=True))

Issue Description

I am trying to convert pandas dataframe to latex with column names in bold font at the sam time , with the code above, which results in:

\begin{tabular}{lll}
\bfseries name & \bfseries mask & weapon \\
Raphael & red & sai \\
Donatello & purple & bo staff \\
\end{tabular}

where the last column name is not in bold font.

Expected Behavior

If I do not hide index with:

print(df.style.applymap_index(lambda v: "font-weight: bold;", axis="columns").to_latex(convert_css=True))

the result will be:

\begin{tabular}{llll}
 & \bfseries name & \bfseries mask & \bfseries weapon \\
0 & Raphael & red & sai \\
1 & Donatello & purple & bo staff \\
\end{tabular}

where the last column name is in bold font.

Installed Versions

INSTALLED VERSIONS

commit : 2e218d1
python : 3.8.13.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-53-generic
Version : #59~20.04.1-Ubuntu SMP Thu Oct 20 15:10:22 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : zh_CN.UTF-8
LOCALE : zh_CN.UTF-8

pandas : 1.5.3
numpy : 1.21.5
pytz : 2022.1
dateutil : 2.8.2
setuptools : 58.0.4
pip : 21.2.4
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.5
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.6.0
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.1
numba : 0.55.1
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
tzdata : 2022.7

@yangtzech yangtzech added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 26, 2023
@yangtzech yangtzech changed the title BUG: last column name is not bold when applying bold font to column names together with hiding index BUG: last column name is not bold when applying bold font to column names together with hiding index before converting to latex Mar 26, 2023
@ltauro
Copy link

ltauro commented Mar 28, 2023

take

@jgarba
Copy link

jgarba commented Mar 28, 2023

Take

@ltauro
Copy link

ltauro commented Mar 28, 2023

Hi @jgarba, I already assigned this task to myself yesterday. Would it be okay if I assign it back to myself?

@jgarba
Copy link

jgarba commented Mar 28, 2023

yup I will unassign myself

@jgarba jgarba removed their assignment Mar 28, 2023
@ltauro
Copy link

ltauro commented Mar 28, 2023

take

@rhshadrach rhshadrach added the Styler conditional formatting using DataFrame.style label Mar 29, 2023
@DeaMariaLeon DeaMariaLeon removed the Needs Triage Issue that has not been reviewed by a pandas team member label Apr 4, 2023
@ltauro ltauro removed their assignment Apr 19, 2023
@joveyee
Copy link

joveyee commented Jul 14, 2023

Panda version 2.0.3

Hi, I was facing the same issue, applying background and font color to LaTeX table headers.
when I hide column index , it affected last header cell styling went missing.
I trace the issue down to pandas.io.formats.style_render.py
def _translate_latex(self, d:dict, clines: str | None) -> None:

the issue is when we call hide(axis=0)
sum(self.hide_index_) == 1
visible_index_level_n = index_levels - sum(self.hide_index_) == 0

it causes the self.ctx_columns[r, c - visible_index_level_n] in enumerate(row) to start from 0 instead of -1.

i added the below just after
visible_index_level_n = index_levels - sum(self.hide_index_)
+ if visible_index_level_n == 0:
+ visible_index_level_n = 1

I manage to get the header color with or without hide(axis=0) works fine.
I am not expert enough to go thought all possible scenario like multi-index or group index or hiding multiple columns,
if you guys can fix this that will be great.

@aaronsnoswell
Copy link

+1 for this bug! Came across this today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Styler conditional formatting using DataFrame.style
Projects
None yet
Development

No branches or pull requests

7 participants