Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Truncade columns list to match truncated frame for correct dict forma…
…tters lookup
  • Loading branch information
Kevin D Smith committed Aug 26, 2020
commit 09feb7dc812037c4f007e7a8e58d72eecc094e08
1 change: 1 addition & 0 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ def _chk_truncate(self) -> None:
self.truncate_h = truncate_h
self.truncate_v = truncate_v
self.is_truncated = bool(self.truncate_h or self.truncate_v)
self.columns = self.tr_frame.columns

def _to_str_columns(self) -> List[List[str]]:
"""
Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,31 @@ def test_to_string_with_formatters(self):
)
assert result == result2

def test_to_string_with_truncated_formatters(self):
df = DataFrame(
{
"int": [1, 2, 3],
"float": [1.0, 2.0, 3.0],
"object": [(1, 2), True, False],
},
columns=["int", "float", "object"],
)

formatters = [
("int", lambda x: f"[1] {x}"),
("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

result2 = df.to_string(formatters=list(zip(*formatters))[1], max_cols=2)
assert result == (
" int ... object\n"
"0 [1] 1 ... [3] (1, 2)\n"
"1 [1] 2 ... [3] True\n"
"2 [1] 3 ... [3] False"
)
assert result == result2

def test_to_string_with_datetime64_monthformatter(self):
months = [datetime(2016, 1, 1), datetime(2016, 2, 2)]
x = DataFrame({"months": months})
Expand Down