Skip to content

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

Closed
wants to merge 5 commits into from

Conversation

wgranados
Copy link

@wgranados wgranados commented Nov 1, 2020

Was having some issues rebasing on #32666 so making a new PR with same changes. Closing other PR as well.

@wgranados wgranados changed the title rebased earlier change with master implemented additionally functionality of formatters_col in to_latex Nov 1, 2020
@arw2019
Copy link
Member

arw2019 commented Nov 1, 2020

cc @WillAyd @mroeschke

@@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The 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)

Copy link
Author

Choose a reason for hiding this comment

The 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.

@jreback
Copy link
Contributor

jreback commented Nov 4, 2020

cc @ivanovmg if you'd have a look

Copy link
Member

@ivanovmg ivanovmg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Jeff on moving the formatters logic into DataFrameFormatter.
There is a method _initialize_formatters, which can be updated.
However, it will alter formatters not only for latex output, but for others as well (to_string, to_html, to_csv).
Is that ok?

)
if formatter_elems_type:
formatters = [
(lambda style: lambda x: "{0:{1}}".format(x, style))(style)
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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.

@jreback
Copy link
Contributor

jreback commented Nov 4, 2020

I agree with Jeff on moving the formatters logic into DataFrameFormatter.

There is a method _initialize_formatters, which can be updated.

However, it will alter formatters not only for latex output, but for others as well (to_string, to_html, to_csv).

Is that ok?

i think this is ok but we should have some additional tests in place

@wgranados
Copy link
Author

I agree with Jeff on moving the formatters logic into DataFrameFormatter.
There is a method _initialize_formatters, which can be updated.
However, it will alter formatters not only for latex output, but for others as well (to_string, to_html, to_csv).
Is that ok?

i think this is ok but we should have some additional tests in place

Which tests did you have in mind? I think the FormattersType change looks self contained with the DataFrameFormatter class

Copy link
Member

@ivanovmg ivanovmg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments

Comment on lines +583 to +585
if is_list_like(formatters) and not isinstance(formatters, dict):
formatter_elems_type = all(
isinstance(elem, str) or callable(elem) for elem in formatters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the failures in tests occur because the logic added is not properly incorporated into the remaining items in this method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should look like this:

if formatters is None:
    return {}
elif len(self.frame.columns) == len(formatters) or isinstance(formatters, dict):
    return formatters
elif is_list_like(formatters) and not isinstance(formatters, dict):
    ... and so on

Also raising ValueError should be in agreement (probably need to update the error message on what formatter elements should look like).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the ordering you proposed that second if statement would always be evaluated true and it wouldn't convert the fstrings to corresponding functions.

I'm not too sure how to fix the error, been looking over it slowly past few days. I think mypy is treating the two-folded lambda function as a different type (Like List[Callable[... some params]) than the intended List[Callable]. Kinda odd that it didn't have this issue before though 🤔

@github-actions
Copy link
Contributor

This pull request is stale because it has been open for thirty days with no activity. Please update or respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Dec 11, 2020
Copy link
Member

@arw2019 arw2019 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wlgranados how's this going? If you're interested in continuing please merge master to see if you can get to green and ping when ready for another round of reviews

@jreback
Copy link
Contributor

jreback commented Feb 11, 2021

closing as stale. if you want to continue, pls ping and can re-open.

@jreback jreback closed this Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: easier digit format in to_latex()
5 participants