Skip to content

Conversation

@kesmit13
Copy link

When using a dictionary of formatters and a truncated DataFrame is being displayed, the column list used for looking up the formatter in the dict are incorrect. The columns attribute of the DataFrameFormatter needs to be truncated as well for the lookup to be correct.

  • closes #xxxx
  • tests added / passed
  • passes black pandas
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry
@WillAyd
Copy link
Member

WillAyd commented Aug 26, 2020

I'm not sure that overwriting self.columns is the way to go here - is there a way to localize the fix?

@WillAyd WillAyd added the Output-Formatting __repr__ of pandas objects, to_string label Aug 26, 2020
@kesmit13
Copy link
Author

I think the new pull request localizes it down to just the spot where the fix is needed. My original fix was based on a monkey patch I was using in my library to make it work in older versions of pandas.

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

Cool I think this is a better approach. Can you add a whatsnew note for 1.2?

("float", lambda x: f"[2] {x}"),
("object", lambda x: f"[3] {x}"),
]
result = df.to_string(formatters=dict(formatters), max_cols=2)
Copy link
Member

Choose a reason for hiding this comment

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

Rather than this can you just parametrize the inputs?

Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure what you mean.

Copy link
Member

Choose a reason for hiding this comment

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

Check out the pytest.mark.parametrize decorator - it is used by a few other tests in this module already

Copy link
Author

Choose a reason for hiding this comment

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

I haven't used that before, but I'm still not sure what you had in mind to parameterize this method.

Copy link
Author

Choose a reason for hiding this comment

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

I added parameterized inputs. I'm not sure if this is quite what you were expecting since there was only one pair of inputs / outputs used in the test.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would argue that the original version (without parametrize) was more readable. Maybe @WillAyd suggested that you parameterize separately formatters for float, int and object?

Copy link
Author

Choose a reason for hiding this comment

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

That wouldn't exercise the fix though. It needs all of the columns in one DataFrame so that a truncated string representation can be displayed.

Copy link
Member

Choose a reason for hiding this comment

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

The suggestion was to parametrize the arguments to formatters; one parametrize for the dict input and one for the list

@pep8speaks
Copy link

pep8speaks commented Sep 16, 2020

Hello @kesmit13! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2021-03-15 14:00:51 UTC
@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 Oct 18, 2020
@kesmit13
Copy link
Author

AFAIK, I've addressed all of the issues. I'm just waiting on final approval.

Copy link
Contributor

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

@kesmit13 Can you merge master again and resolve conflicts?

FWIW lgtm

@arw2019 arw2019 requested review from WillAyd and ivanovmg October 31, 2020 03:24
@jreback
Copy link
Contributor

jreback commented Oct 31, 2020

@kesmit13 yeah can you merge master and fix conflict.

@ivanovmg
Copy link
Contributor

ivanovmg commented Oct 31, 2020

Regarding solving the conflicts.
After recent changes in pandas/io/format/format.py it will be necessary to move changes in _get_formatter method into the same method in class DataFrameFormatter.
And you probably won't need to type tr_frame.

Comment on lines 467 to 468
- :class:`TableFormatter` was using the full list of columns when computing formatters for truncated displays (:issue:`35907`)

Copy link
Contributor

Choose a reason for hiding this comment

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

Currently there is no TableFormatter.
The generic functionality is moved to DataFrameFormatter.

Comment on lines 175 to 176
def test_to_string_with_truncated_formatters(self, formatters):
df = DataFrame(
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 please fix test?
Apparently, self should be removed here.

@ivanovmg
Copy link
Contributor

ivanovmg commented Nov 6, 2020

@kesmit13, can you please merge master and resolve conflicts in doc/source/whatsnew/v1.2.0.rst?
Without it we cannot witness CI checks.
Thank you!

Copy link
Contributor

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

Sorry for a really late reply but can you please merge master again?
I guess that your whatsnew entry should go now to v1.3.

@simonjayhawkins
Copy link
Member

I guess that your whatsnew entry should go now to v1.3.

@kesmit13 can you address @ivanovmg comment

@jreback
Copy link
Contributor

jreback commented Oct 4, 2021

can you merge master and will have a look (also note would need to be in 1.4)

@mroeschke
Copy link
Member

Thanks for the PR, but it appears to have gone stale. This appears close, so if you could merge master and address the remaining comments, we'd be happy to reopen and have another look over.

@mroeschke mroeschke closed this Nov 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Output-Formatting __repr__ of pandas objects, to_string

8 participants