Skip to content

Header option for to_latex #15536

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
mcocdawc opened this issue Mar 1, 2017 · 2 comments · Fixed by #15548
Closed

Header option for to_latex #15536

mcocdawc opened this issue Mar 1, 2017 · 2 comments · Fixed by #15548
Milestone

Comments

@mcocdawc
Copy link
Contributor

mcocdawc commented Mar 1, 2017

Code Sample, a copy-pastable example if possible

import numpy as np
import pandas as pd

data = pd.DataFrame(np.random.rand(5,2), columns=['s', 't'])
print(data.rename(columns={'s' : '$s$ in m', 't' : '$t$ in s'}).to_latex(escape=False))

# would be cool to write instead
print(data.to_latex(header=['$s$ in m', '$t$ in s'], escape=False))

# this works as expected
print(data.to_csv(header=['$s$ in m', '$t$ in s'], escape=False))

Problem description

The header option of to_latex does not rename the columns as in other exporting functions and forces to rename them before.
I am not sure if this is a bug or intended.
But it is counterintuitive (at least for me) and makes code harder to read.

Expected Output

# would be cool to write instead
print(data.to_latex(header=['$s$ in m', '$t$ in s'], escape=False))

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.5.2.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-64-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8

pandas: 0.18.1
nose: 1.3.7
pip: 9.0.1
setuptools: 23.0.0
Cython: 0.24
numpy: 1.11.2
scipy: 0.17.1
statsmodels: None
xarray: None
IPython: 4.2.0
sphinx: 1.4.6
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.4
blosc: None
bottleneck: 1.1.0
tables: 3.2.2
numexpr: 2.6.0
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: 0.9.2
lxml: 3.6.0
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.40.0
pandas_datareader: None

@jorisvandenbossche jorisvandenbossche added the IO LaTeX to_latex label Mar 1, 2017
@jorisvandenbossche
Copy link
Member

Currently, the header kwarg only takes True/False to determine whether the header file is written or not written at all.
Adding the possibility to specify a list to overwrite the column names seems fine (since this is indeed how it works for to_csv).

PRs to add this are welcome!

@mcocdawc
Copy link
Contributor Author

mcocdawc commented Mar 1, 2017

I can try to solve the bug, but I would consider myself still a python noob compared to you guys.

But anyway:
I looked around in the source and made some test.
It is not only a problem of to_latex but of export functions which rely on
fmt.DataFrameFormatter. (to_html, to_string and to_latex are affected)
Perhaps you might want to change the label.

@jreback jreback added this to the 0.20.0 milestone Mar 7, 2017
AnkurDedania pushed a commit to AnkurDedania/pandas that referenced this issue Mar 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants