Skip to content

Commit 5baef2d

Browse files
committed
don't show useless dtype in repr
1 parent 33c44bb commit 5baef2d

File tree

6 files changed

+83
-69
lines changed

6 files changed

+83
-69
lines changed

doc/source/whatsnew/v0.25.0.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ The repr now looks like this:
7777
pd.MultiIndex.from_product([['a', 'abc'], range(500)])
7878
7979
Previously, outputting a :class:`MultiIndex` printed all the ``levels`` and
80-
``labels`` of the ``MultiIndex``, which was visually unappealing and made
80+
``codes`` of the ``MultiIndex``, which was visually unappealing and made
8181
the output more difficult to navigate:
8282

8383
.. code-block:: ipython
8484
85-
>>>pd.MultiIndex.from_product([['a', 'abc'], range(5)])
86-
MultiIndex(levels=[['a', 'abc'], [0, 1, 2, 3]],
87-
labels=[[0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 2, 3, 0, 1, 2, 3]])
85+
In [1]: pd.MultiIndex.from_product([['a', 'abc'], range(5)])
86+
Out[1]: MultiIndex(levels=[['a', 'abc'], [0, 1, 2, 3]],
87+
...: codes=[[0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 2, 3, 0, 1, 2, 3]])
8888
8989
In the new repr, all values will be shown, if the number of rows is smaller
9090
than :attr:`options.display.max_seq_items` (default: 100 items). Horizontally,

pandas/core/indexes/base.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1326,20 +1326,20 @@ def set_names(self, names, level=None, inplace=False):
13261326
('python', 2019),
13271327
( 'cobra', 2018),
13281328
( 'cobra', 2019)],
1329-
dtype='object')
1329+
)
13301330
>>> idx.set_names(['kind', 'year'], inplace=True)
13311331
>>> idx
13321332
MultiIndex([('python', 2018),
13331333
('python', 2019),
13341334
( 'cobra', 2018),
13351335
( 'cobra', 2019)],
1336-
dtype='object', names=['kind', 'year'])
1336+
names=['kind', 'year'])
13371337
>>> idx.set_names('species', level=0)
13381338
MultiIndex([('python', 2018),
13391339
('python', 2019),
13401340
( 'cobra', 2018),
13411341
( 'cobra', 2019)],
1342-
dtype='object', names=['species', 'year'])
1342+
names=['species', 'year'])
13431343
"""
13441344

13451345
if level is not None and not isinstance(self, ABCMultiIndex):
@@ -1404,13 +1404,13 @@ def rename(self, name, inplace=False):
14041404
('python', 2019),
14051405
( 'cobra', 2018),
14061406
( 'cobra', 2019)],
1407-
dtype='object', names=['kind', 'year'])
1407+
names=['kind', 'year'])
14081408
>>> idx.rename(['species', 'year'])
14091409
MultiIndex([('python', 2018),
14101410
('python', 2019),
14111411
( 'cobra', 2018),
14121412
( 'cobra', 2019)],
1413-
dtype='object', names=['species', 'year'])
1413+
names=['species', 'year'])
14141414
>>> idx.rename('species')
14151415
Traceback (most recent call last):
14161416
TypeError: Must pass list-like as `names`.
@@ -5435,7 +5435,7 @@ def ensure_index_from_sequences(sequences, names=None):
54355435
names=['L1', 'L2'])
54365436
MultiIndex([('a', 'a'),
54375437
('a', 'b')],
5438-
dtype='object', names=['L1', 'L2'])
5438+
names=['L1', 'L2'])
54395439
54405440
See Also
54415441
--------
@@ -5477,6 +5477,7 @@ def ensure_index(index_like, copy=False):
54775477
MultiIndex([('a', 'b'),
54785478
('a', 'c')],
54795479
dtype='object')
5480+
)
54805481
54815482
See Also
54825483
--------

pandas/core/indexes/multi.py

+41-44
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
from pandas.core.indexes.frozen import FrozenList, _ensure_frozen
3030
import pandas.core.missing as missing
3131

32-
from pandas.io.formats.printing import format_object_summary, pprint_thing
32+
from pandas.io.formats.printing import (
33+
format_object_attrs, format_object_summary, pprint_thing)
3334

3435
_index_doc_kwargs = dict(ibase._index_doc_kwargs)
3536
_index_doc_kwargs.update(
@@ -192,8 +193,10 @@ class MultiIndex(Index):
192193
193194
>>> arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]
194195
>>> pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
195-
MultiIndex(levels=[[1, 2], ['blue', 'red']],
196-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
196+
MultiIndex([(1, 'red'),
197+
(1, 'blue'),
198+
(2, 'red'),
199+
(2, 'blue')],
197200
names=['number', 'color'])
198201
199202
See further examples for how to construct a MultiIndex in the doc strings
@@ -318,8 +321,10 @@ def from_arrays(cls, arrays, sortorder=None, names=None):
318321
--------
319322
>>> arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]
320323
>>> pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
321-
MultiIndex(levels=[[1, 2], ['blue', 'red']],
322-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
324+
MultiIndex([(1, 'red'),
325+
(1, 'blue'),
326+
(2, 'red'),
327+
(2, 'blue')],
323328
names=['number', 'color'])
324329
"""
325330
error_msg = "Input must be a list / sequence of array-likes."
@@ -383,7 +388,7 @@ def from_tuples(cls, tuples, sortorder=None, names=None):
383388
(1, 'blue'),
384389
(2, 'red'),
385390
(2, 'blue')],
386-
dtype='object', names=['number', 'color'])
391+
names=['number', 'color'])
387392
"""
388393
if not is_list_like(tuples):
389394
raise TypeError('Input must be a list / sequence of tuple-likes.')
@@ -444,7 +449,7 @@ def from_product(cls, iterables, sortorder=None, names=None):
444449
(1, 'purple'),
445450
(2, 'green'),
446451
(2, 'purple')],
447-
dtype='object', names=['number', 'color'])
452+
names=['number', 'color'])
448453
"""
449454
from pandas.core.arrays.categorical import _factorize_from_iterables
450455
from pandas.core.reshape.util import cartesian_product
@@ -502,15 +507,19 @@ def from_frame(cls, df, sortorder=None, names=None):
502507
3 NJ Precip
503508
504509
>>> pd.MultiIndex.from_frame(df)
505-
MultiIndex(levels=[['HI', 'NJ'], ['Precip', 'Temp']],
506-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
510+
MultiIndex([('HI', 'Temp'),
511+
('HI', 'Precip'),
512+
('NJ', 'Temp'),
513+
('NJ', 'Precip')],
507514
names=['a', 'b'])
508515
509516
Using explicit names, instead of the column names
510517
511518
>>> pd.MultiIndex.from_frame(df, names=['state', 'observation'])
512-
MultiIndex(levels=[['HI', 'NJ'], ['Precip', 'Temp']],
513-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
519+
MultiIndex([('HI', 'Temp'),
520+
('HI', 'Precip'),
521+
('NJ', 'Temp'),
522+
('NJ', 'Precip')],
514523
names=['state', 'observation'])
515524
"""
516525
if not isinstance(df, ABCDataFrame):
@@ -627,49 +636,30 @@ def set_levels(self, levels, level=None, inplace=False,
627636
>>> idx = pd.MultiIndex.from_tuples([(1, 'one'), (1, 'two'),
628637
(2, 'one'), (2, 'two')],
629638
names=['foo', 'bar'])
630-
<<<<<<< HEAD
631-
>>> idx.set_levels([['a','b'], [1,2]])
632-
MultiIndex(levels=[['a', 'b'], [1, 2]],
633-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
634-
names=['foo', 'bar'])
635-
>>> idx.set_levels(['a','b'], level=0)
636-
MultiIndex(levels=[['a', 'b'], ['one', 'two']],
637-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
638-
names=['foo', 'bar'])
639-
>>> idx.set_levels(['a','b'], level='bar')
640-
MultiIndex(levels=[[1, 2], ['a', 'b']],
641-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
642-
names=['foo', 'bar'])
643-
>>> idx.set_levels([['a','b'], [1,2]], level=[0,1])
644-
MultiIndex(levels=[['a', 'b'], [1, 2]],
645-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
646-
names=['foo', 'bar'])
647-
=======
648639
>>> idx.set_levels([['a', 'b'], [1, 2]])
649640
MultiIndex([('a', 1),
650641
('a', 2),
651642
('b', 1),
652643
('b', 2)],
653-
dtype='object', names=['foo', 'bar'])
644+
names=['foo', 'bar'])
654645
>>> idx.set_levels(['a', 'b'], level=0)
655646
MultiIndex([('a', 'one'),
656647
('a', 'two'),
657648
('b', 'one'),
658649
('b', 'two')],
659-
dtype='object', names=['foo', 'bar'])
650+
names=['foo', 'bar'])
660651
>>> idx.set_levels(['a', 'b'], level='bar')
661652
MultiIndex([(1, 'a'),
662653
(1, 'b'),
663654
(2, 'a'),
664655
(2, 'b')],
665-
dtype='object', names=['foo', 'bar'])
656+
names=['foo', 'bar'])
666657
>>> idx.set_levels([['a', 'b'], [1, 2]], level=[0, 1])
667658
MultiIndex([('a', 1),
668659
('a', 2),
669660
('b', 1),
670661
('b', 2)],
671-
dtype='object', names=['foo', 'bar'])
672-
>>>>>>> Update doc string examples and docs
662+
names=['foo', 'bar'])
673663
"""
674664
if is_list_like(levels) and not isinstance(levels, Index):
675665
levels = list(levels)
@@ -778,25 +768,25 @@ def set_codes(self, codes, level=None, inplace=False,
778768
(1, 'one'),
779769
(2, 'two'),
780770
(1, 'two')],
781-
dtype='object', names=['foo', 'bar'])
771+
names=['foo', 'bar'])
782772
>>> idx.set_codes([1, 0, 1, 0], level=0)
783773
MultiIndex([(2, 'one'),
784774
(1, 'two'),
785775
(2, 'one'),
786776
(1, 'two')],
787-
dtype='object', names=['foo', 'bar'])
777+
names=['foo', 'bar'])
788778
>>> idx.set_codes([0, 0, 1, 1], level='bar')
789779
MultiIndex([(1, 'one'),
790780
(1, 'one'),
791781
(2, 'two'),
792782
(2, 'two')],
793-
dtype='object', names=['foo', 'bar'])
783+
names=['foo', 'bar'])
794784
>>> idx.set_codes([[1, 0, 1, 0], [0, 0, 1, 1]], level=[0, 1])
795785
MultiIndex([(2, 'one'),
796786
(1, 'one'),
797787
(2, 'two'),
798788
(1, 'two')],
799-
dtype='object', names=['foo', 'bar'])
789+
names=['foo', 'bar'])
800790
"""
801791
if level is not None and not is_list_like(level):
802792
if not is_list_like(codes):
@@ -960,6 +950,12 @@ def _format_data(self, name=None):
960950
return format_object_summary(self, self._formatter_func,
961951
name=name, line_break_each_value=True)
962952

953+
def _format_attrs(self):
954+
"""
955+
Return a list of tuples of the (attr,formatted_value).
956+
"""
957+
return format_object_attrs(self, include_dtype=False)
958+
963959
def _format_native_types(self, na_rep='nan', **kwargs):
964960
new_levels = []
965961
new_codes = []
@@ -1557,7 +1553,7 @@ def to_hierarchical(self, n_repeat, n_shuffle=1):
15571553
(2, 'two'),
15581554
(2, 'two'),
15591555
(2, 'two')],
1560-
dtype='object')
1556+
)
15611557
"""
15621558
levels = self.levels
15631559
codes = [np.repeat(level_codes, n_repeat) for
@@ -1655,14 +1651,14 @@ def _sort_levels_monotonic(self):
16551651
('a', 'aa'),
16561652
('b', 'bb'),
16571653
('b', 'aa')],
1658-
dtype='object')
1654+
)
16591655
16601656
>>> mi.sort_values()
16611657
MultiIndex([('a', 'aa'),
16621658
('a', 'bb'),
16631659
('b', 'aa'),
16641660
('b', 'bb')],
1665-
dtype='object')
1661+
)
16661662
"""
16671663

16681664
if self.is_lexsorted() and self.is_monotonic:
@@ -1717,12 +1713,12 @@ def remove_unused_levels(self):
17171713
(0, 'b'),
17181714
(1, 'a'),
17191715
(1, 'b')],
1720-
dtype='object')
1716+
)
17211717
17221718
>>> mi[2:]
17231719
MultiIndex([(1, 'a'),
17241720
(1, 'b')],
1725-
dtype='object')
1721+
)
17261722
17271723
The 0 from the first level is not represented
17281724
and can be removed
@@ -2039,12 +2035,13 @@ def swaplevel(self, i=-2, j=-1):
20392035
('a', 'aa'),
20402036
('b', 'bb'),
20412037
('b', 'aa')],
2042-
dtype='object')
2038+
)
20432039
>>> mi.swaplevel(0, 1)
20442040
MultiIndex([('bb', 'a'),
20452041
('aa', 'a'),
20462042
('bb', 'b'),
20472043
('aa', 'b')],
2044+
)
20482045
"""
20492046
new_levels = list(self.levels)
20502047
new_codes = list(self.codes)

pandas/io/formats/printing.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def _justify(head, tail):
468468
return head, tail
469469

470470

471-
def format_object_attrs(obj):
471+
def format_object_attrs(obj, include_dtype=True):
472472
"""
473473
Return a list of tuples of the (attr, formatted_value)
474474
for common attrs, including dtype, name, length
@@ -477,14 +477,16 @@ def format_object_attrs(obj):
477477
----------
478478
obj : object
479479
must be iterable
480+
include_dtype : bool
481+
If False, dtype won't be in the returned list
480482
481483
Returns
482484
-------
483485
list
484486
485487
"""
486488
attrs = []
487-
if hasattr(obj, 'dtype'):
489+
if hasattr(obj, 'dtype') and include_dtype:
488490
attrs.append(('dtype', "'{}'".format(obj.dtype)))
489491
if getattr(obj, 'name', None) is not None:
490492
attrs.append(('name', default_pprint(obj.name)))

0 commit comments

Comments
 (0)