-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
ENH: use '=' width in multirow with compatible column types #60281
Comments
@tuetenk0pp - can you include a reproducible example. We cannot reproduce your example without your
|
@rhshadrach, here is a more in depth example of what is happening: >>> import pandas as pd
>>> d = {'location': ['Supermarket', 'Supermarket', 'Supermarket', 'Supermarket', 'Drugstore', 'Drugstore', 'Drugstore', 'Drugstore', 'Farmers Market', 'Farmers Market', 'Farmers Market', 'Farmers Market'], 'category': ['Produce', 'Produce', 'Dry Goods', 'Dry Goods', 'Personal Care', 'Personal Care', 'Medicine', 'Medicine', 'Vegetables', 'Vegetables', 'Fruits', 'Fruits'], 'item': ['Apples', 'Bananas', 'Rice', 'Pasta', 'Shampoo', 'Toothpaste', 'Pain Reliever', 'Cough Sirup', 'Carrots', 'Spinach', 'Strawberrys', 'Oranges'], 'count': [6, 12, 2, 1, 1, 1, 1, 1, 1, 2, 1, 6]}
>>> df = pd.DataFrame(data=d)
>>> df = df.set_index(df.columns[:3].tolist())
>>> df = df.sort_index() The DataFrame now looks like this: >>> df
count
location category item
Drugstore Medicine Cough Sirup 1
Pain Reliever 1
Personal Care Shampoo 1
Toothpaste 1
Farmers Market Fruits Oranges 6
Strawberrys 1
Vegetables Carrots 1
Spinach 2
Supermarket Dry Goods Pasta 1
Rice 2
Produce Apples 6
Bananas 12 Now I want to export the DataFrame to LaTeX: >>> latex = r"""
... \documentclass[border=0.5cm]{standalone}
...
... \usepackage{array}
... \usepackage{multirow}
... \usepackage{booktabs}
...
... \begin{document}
... """
>>> column_format: str = r'p{2cm}<{\raggedright} p{2cm}<{\raggedright} p{2cm}<{\raggedright} r'
>>> latex += '\n' + df.to_latex(column_format=column_format, multirow=True) + '\n' + r'\end{document}' The LaTeX Code now looks like this: >>> print(latex) \documentclass[border=0.5cm]{standalone}
\usepackage{array}
\usepackage{multirow}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{p{2cm}<{\raggedright} p{2cm}<{\raggedright} p{2cm}<{\raggedright} r}
\toprule
& & & count \\
location & category & item & \\
\midrule
\multirow[t]{4}{*}{Drugstore} & \multirow[t]{2}{*}{Medicine} & Cough Sirup & 1 \\
& & Pain Reliever & 1 \\
\cline{2-4}
& \multirow[t]{2}{*}{Personal Care} & Shampoo & 1 \\
& & Toothpaste & 1 \\
\cline{1-4} \cline{2-4}
\multirow[t]{4}{*}{Farmers Market} & \multirow[t]{2}{*}{Fruits} & Oranges & 6 \\
& & Strawberrys & 1 \\
\cline{2-4}
& \multirow[t]{2}{*}{Vegetables} & Carrots & 1 \\
& & Spinach & 2 \\
\cline{1-4} \cline{2-4}
\multirow[t]{4}{*}{Supermarket} & \multirow[t]{2}{*}{Dry Goods} & Pasta & 1 \\
& & Rice & 2 \\
\cline{2-4}
& \multirow[t]{2}{*}{Produce} & Apples & 6 \\
& & Bananas & 12 \\
\cline{1-4} \cline{2-4}
\bottomrule
\end{tabular}
\end{document} Here I have a screenshot of the compiled document: As you can see, the latex_wrap = latex.replace('{*}', '{=}') The LaTeX Code now looks like this: >>> print(latex_wrap) \documentclass[border=0.5cm]{standalone}
\usepackage{array}
\usepackage{multirow}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{p{2cm}<{\raggedright} p{2cm}<{\raggedright} p{2cm}<{\raggedright} r}
\toprule
& & & count \\
location & category & item & \\
\midrule
\multirow[t]{4}{=}{Drugstore} & \multirow[t]{2}{=}{Medicine} & Cough Sirup & 1 \\
& & Pain Reliever & 1 \\
\cline{2-4}
& \multirow[t]{2}{=}{Personal Care} & Shampoo & 1 \\
& & Toothpaste & 1 \\
\cline{1-4} \cline{2-4}
\multirow[t]{4}{=}{Farmers Market} & \multirow[t]{2}{=}{Fruits} & Oranges & 6 \\
& & Strawberrys & 1 \\
\cline{2-4}
& \multirow[t]{2}{=}{Vegetables} & Carrots & 1 \\
& & Spinach & 2 \\
\cline{1-4} \cline{2-4}
\multirow[t]{4}{=}{Supermarket} & \multirow[t]{2}{=}{Dry Goods} & Pasta & 1 \\
& & Rice & 2 \\
\cline{2-4}
& \multirow[t]{2}{=}{Produce} & Apples & 6 \\
& & Bananas & 12 \\
\cline{1-4} \cline{2-4}
\bottomrule
\end{tabular}
\end{document} As you can see, the issue no longer exists in the compiled document. The Note Finding Here is the summarized code for reproduction: import pandas as pd
import subprocess
d = {'location': ['Supermarket', 'Supermarket', 'Supermarket', 'Supermarket', 'Drugstore', 'Drugstore', 'Drugstore', 'Drugstore', 'Farmers Market', 'Farmers Market', 'Farmers Market', 'Farmers Market'], 'category': ['Produce', 'Produce', 'Dry Goods', 'Dry Goods', 'Personal Care', 'Personal Care', 'Medicine', 'Medicine', 'Vegetables', 'Vegetables', 'Fruits', 'Fruits'], 'item': ['Apples', 'Bananas', 'Rice', 'Pasta', 'Shampoo', 'Toothpaste', 'Pain Reliever', 'Cough Sirup', 'Carrots', 'Spinach', 'Strawberrys', 'Oranges'], 'count': [6, 12, 2, 1, 1, 1, 1, 1, 1, 2, 1, 6]}
df = pd.DataFrame(data=d)
df = df.set_index(df.columns[:3].tolist())
df = df.sort_index()
print(df)
latex = r"""
\documentclass[border=0.5cm]{standalone}
\usepackage{array}
\usepackage{multirow}
\usepackage{booktabs}
\begin{document}
"""
column_format: str = r'p{2cm}<{\raggedright} p{2cm}<{\raggedright} p{2cm}<{\raggedright} r'
latex += '\n' + df.to_latex(column_format=column_format, multirow=True) + '\n' + r'\end{document}'
latex_flex = latex.replace('{*}', '{=}')
with open('multirow.tex', 'w') as f:
f.write(latex)
with open('multirow_flex.tex', 'w') as f:
f.write(latex_flex)
for file in ['multirow.tex', 'multirow_flex.tex']:
cmd = ['latexmk', '-lualatex', '-interaction=nonstopmode', '-file-line-error', '-silent', '-auxdir=./tmp', file]
try:
subprocess.run(cmd)
except:
print(f'could not run command: {cmd}') |
Thanks for the additional information! From the OP, you have the logic:
I do not understand branching on this condition, can you elaborate here? |
The
So this actually means that Thats why I thought it would be a good idea to check the columntype (from the With the added
Or a more safe approach but with no support for any other column types than mentioned in the quote above:
|
Thanks, PRs to fix are welcome! cc @attack68 |
What happens if "=" is given in all cases? Does it still compile even for 'standard' columns? |
The documentation states:
|
Feature Type
Adding new functionality to pandas
Changing existing functionality in pandas
Removing existing functionality in pandas
Problem Description
It would be nice to be able to use the
=
option with multirow. Maybe this is something that can be decided automatically according to the column type.Feature Description
Add logic to
_parse_latex_header_span()
to check for cloumn types.Alternative Solutions
One could also find/replace the return value of
pandas.DataFrame.to_latex
manually:Additional Context
pandas/pandas/io/formats/style_render.py
Line 2451 in 156e67e
See this snippet from the multirow documentation:
The text was updated successfully, but these errors were encountered: