-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
implemented additionally functionality of formatters_col in to_latex #37552
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3034,7 +3034,7 @@ def to_latex( | |
Write row names (index). | ||
na_rep : str, default 'NaN' | ||
Missing data representation. | ||
formatters : list of functions or dict of {{str: function}}, optional | ||
formatters : list of functions/str or dict of {{str: function}}, optional | ||
Formatter functions to apply to columns' elements by position or | ||
name. The result of each function must be a unicode string. | ||
List must be of length equal to the number of columns. | ||
|
@@ -3136,6 +3136,22 @@ def to_latex( | |
if multirow is None: | ||
multirow = config.get_option("display.latex.multirow") | ||
|
||
if is_list_like(formatters) and not isinstance(formatters, dict): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add an Example that uses this (e.g. similar to your test is fine) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was having an issue providing an example with lambda functions in doc, so I included a simpler example. |
||
formatter_elems_type = all( | ||
isinstance(elem, str) or callable(elem) for elem in formatters | ||
wgranados marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
if formatter_elems_type: | ||
formatters = [ | ||
(lambda style: lambda x: "{0:{1}}".format(x, style))(style) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest not to use two folded lambda functions. This is not easily readable. Can it be replaced with an f-string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initially they were simpler lambda functions to take advantage of the existing callable implementation, but there was a python memory specific error where it created the first anonymous function and replicated it multiple times. I believe this is using fstrings? At least for the substitution. But yeah I agree it's a little hard to read. |
||
if isinstance(style, str) | ||
else style | ||
for style in formatters | ||
] | ||
else: | ||
raise ValueError( | ||
"Formatters elements should be f-strings or callable functions" | ||
) | ||
|
||
self = cast("DataFrame", self) | ||
formatter = DataFrameFormatter( | ||
self, | ||
|
Uh oh!
There was an error while loading. Please reload this page.