pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.95k stars 18.04k forks source link

ENH: Add option to specify table captions to be below tables in DataFrame.to_latex #46122

Open Sleepingwell opened 2 years ago

Sleepingwell commented 2 years ago

Is your feature request related to a problem?

At present, table captions are always written above the table in the outputs. LaTeX will then render the caption above the table in the resulting document.

I prefer to put captions below tables (and figures) and would like the option to do so added to DataFrame.to_latex.

Describe the solution you'd like

I propose adding a new argument to DataFrame.to_latex, (say caption_position) with a default of 'above'and valid values of 'above' and 'below'. One could equivalently use an Enum, but I'm not sure where that would live and don't think this idiomatic w.r.t. Pandas.

An alternative would be an argument caption_above with valid values of True and False.

I think the docstring would be something like

"""
Parameters
----------
caption_position : str, default 'above'
    Position of the caption relative to the table. Acceptable values are `'above'` and `'below'`.
"""

API breaking implications

With a default, this should not change the API.

Describe alternatives you've considered

At present, I wait until I finally want to submit the document then manually change the files (where I put the output of DataFrame.to_latex) manually. Without this feature, I would consider writing a function that takes the output of DataFrame.to_latex and rearranges it.

Note

I'm happy to have a go at this — if it is not so trivial that someone can do it faster than saying OK — if people are happy with the proposed approach.

attack68 commented 2 years ago

Styler.to_latex can already completely replicate everything that DataFrame.to_latex does and has a lot more functionality. The intention is redirect DataFrame.to_latex to use the Styler implementation. If you submit a PR pls do so for the Styler implementation

DanielHabenicht commented 2 years ago

@attack68 is the api suggested by @Sleepingwell approved? If so I would add a PR (for the Styler only).

attack68 commented 2 years ago

Yes caption_position can be added. It is something that is generally quite useful. The addition to Styler will not be as simple as it seems. It will require changing the jinja2 template and constructing tests for different environments including standard and longtable for example.

True and False are better since this is a binary decision.

DanielHabenicht commented 2 years ago

So should the we call the param caption_position_top then?

Otherwise it's kind of murky: caption_position=True and caption_position=False

DanielHabenicht commented 2 years ago

Yes caption_position can be added. It is something that is generally quite useful. The addition to Styler will not be as simple as it seems. It will require changing the jinja2 template and constructing tests for different environments including standard and longtable for example.

You certainly mean: https://github.com/pandas-dev/pandas/blob/v1.5.2/pandas/io/formats/templates/latex.tpl

Is this the old implementation then?:

https://github.com/pandas-dev/pandas/blob/v1.5.2/pandas/io/formats/latex.py#L610

attack68 commented 2 years ago

The second link is the old implementation. I have not contriubuted anything to that, so dont know my way around it. Focus on

style.py, styler_render.py, and the appropriate jinja2 templates.

attack68 commented 2 years ago

caption_top : True or False is probably best

On Mon, 5 Dec 2022 at 11:39, DanielHabenicht @.***> wrote:

So should the we call the param caption_position_top then?

Otherwise it's kind of murky: caption_position=True and caption_position=False

— Reply to this email directly, view it on GitHub https://github.com/pandas-dev/pandas/issues/46122#issuecomment-1337112966, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFZCAKTN4MNLUNJRFEQVBCLWLXA7ZANCNFSM5PC5HPWQ . You are receiving this because you were mentioned.Message ID: @.***>

netphantom commented 11 months ago

This would be something really useful

mo-ba commented 6 months ago

zZZzzZZ