Header option for to_latex #15536

Closed
mcocdawc opened this Issue Mar 1, 2017 · 2 comments

Comments

Projects
None yet
3 participants
Contributor

mcocdawc commented Mar 1, 2017 edited

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

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!

Contributor

mcocdawc commented Mar 1, 2017 edited

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.

mcocdawc referenced this issue Mar 2, 2017

Merged

ENH: IO header formatting #15548

4 of 4 tasks complete

jreback added this to the 0.20.0 milestone Mar 7, 2017

jreback added the Enhancement label Mar 7, 2017

@AnkurDedania AnkurDedania added a commit to AnkurDedania/pandas that referenced this issue Mar 21, 2017

@mcocdawc @AnkurDedania mcocdawc + AnkurDedania ENH: to_string/to_latex now accept list-like header arg for overwriti…
…ng column names (#15548)


closes #15536
250dc8d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment