Skip to content
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

Use set literal syntax for set prettyprinting #11215

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/source/whatsnew/v0.17.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Other Enhancements
API changes
~~~~~~~~~~~

- Prettyprinting sets (e.g. in DataFrame cells) now uses set literal syntax (``{x, y}``) instead of
Legacy Python syntax (``set([x, y])``).

.. _whatsnew_0171.deprecations:

Deprecations
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3194,7 +3194,7 @@ def _pprint_seq(seq, _nest_lvl=0, max_seq_items=None, **kwds):
bounds length of printed sequence, depending on options
"""
if isinstance(seq, set):
fmt = u("set([%s])")
fmt = u("{%s}")
else:
fmt = u("[%s]") if hasattr(seq, '__setitem__') else u("(%s)")

Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,14 @@ def test_repr_chop_threshold(self):
self.assertEqual(repr(df), ' 0 1\n0 0.1 0.5\n1 0.5 -0.1')

def test_repr_obeys_max_seq_limit(self):
import pandas.core.common as com

with option_context("display.max_seq_items",2000):
self.assertTrue(len(com.pprint_thing(lrange(1000))) > 1000)

with option_context("display.max_seq_items",5):
self.assertTrue(len(com.pprint_thing(lrange(1000)))< 100)
self.assertTrue(len(com.pprint_thing(lrange(1000))) < 100)

def test_repr_set(self):
self.assertEqual(com.pprint_thing(set([1])), '{1}')

def test_repr_is_valid_construction_code(self):
# for the case of Index, where the repr is traditional rather then stylized
Expand Down