Skip to content

Commit 7c84657

Browse files
committed
don't show useless dtype in repr
1 parent cb2f904 commit 7c84657

File tree

6 files changed

+83
-69
lines changed

6 files changed

+83
-69
lines changed

doc/source/whatsnew/v0.25.0.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ The repr now looks like this:
9191
pd.MultiIndex.from_product([['a', 'abc'], range(500)])
9292
9393
Previously, outputting a :class:`MultiIndex` printed all the ``levels`` and
94-
``labels`` of the ``MultiIndex``, which was visually unappealing and made
94+
``codes`` of the ``MultiIndex``, which was visually unappealing and made
9595
the output more difficult to navigate:
9696

9797
.. code-block:: ipython
9898
99-
>>>pd.MultiIndex.from_product([['a', 'abc'], range(5)])
100-
MultiIndex(levels=[['a', 'abc'], [0, 1, 2, 3]],
101-
labels=[[0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 2, 3, 0, 1, 2, 3]])
99+
In [1]: pd.MultiIndex.from_product([['a', 'abc'], range(5)])
100+
Out[1]: MultiIndex(levels=[['a', 'abc'], [0, 1, 2, 3]],
101+
...: codes=[[0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 2, 3, 0, 1, 2, 3]])
102102
103103
In the new repr, all values will be shown, if the number of rows is smaller
104104
than :attr:`options.display.max_seq_items` (default: 100 items). Horizontally,

pandas/core/indexes/base.py

Lines changed: 7 additions & 6 deletions
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`.
@@ -5433,7 +5433,7 @@ def ensure_index_from_sequences(sequences, names=None):
54335433
names=['L1', 'L2'])
54345434
MultiIndex([('a', 'a'),
54355435
('a', 'b')],
5436-
dtype='object', names=['L1', 'L2'])
5436+
names=['L1', 'L2'])
54375437
54385438
See Also
54395439
--------
@@ -5475,6 +5475,7 @@ def ensure_index(index_like, copy=False):
54755475
MultiIndex([('a', 'b'),
54765476
('a', 'c')],
54775477
dtype='object')
5478+
)
54785479
54795480
See Also
54805481
--------

pandas/core/indexes/multi.py

Lines changed: 41 additions & 44 deletions
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(
@@ -193,8 +194,10 @@ class MultiIndex(Index):
193194
194195
>>> arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]
195196
>>> pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
196-
MultiIndex(levels=[[1, 2], ['blue', 'red']],
197-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
197+
MultiIndex([(1, 'red'),
198+
(1, 'blue'),
199+
(2, 'red'),
200+
(2, 'blue')],
198201
names=['number', 'color'])
199202
200203
See further examples for how to construct a MultiIndex in the doc strings
@@ -359,8 +362,10 @@ def from_arrays(cls, arrays, sortorder=None, names=None):
359362
--------
360363
>>> arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]
361364
>>> pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
362-
MultiIndex(levels=[[1, 2], ['blue', 'red']],
363-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
365+
MultiIndex([(1, 'red'),
366+
(1, 'blue'),
367+
(2, 'red'),
368+
(2, 'blue')],
364369
names=['number', 'color'])
365370
"""
366371
error_msg = "Input must be a list / sequence of array-likes."
@@ -424,7 +429,7 @@ def from_tuples(cls, tuples, sortorder=None, names=None):
424429
(1, 'blue'),
425430
(2, 'red'),
426431
(2, 'blue')],
427-
dtype='object', names=['number', 'color'])
432+
names=['number', 'color'])
428433
"""
429434
if not is_list_like(tuples):
430435
raise TypeError('Input must be a list / sequence of tuple-likes.')
@@ -485,7 +490,7 @@ def from_product(cls, iterables, sortorder=None, names=None):
485490
(1, 'purple'),
486491
(2, 'green'),
487492
(2, 'purple')],
488-
dtype='object', names=['number', 'color'])
493+
names=['number', 'color'])
489494
"""
490495
from pandas.core.arrays.categorical import _factorize_from_iterables
491496
from pandas.core.reshape.util import cartesian_product
@@ -543,15 +548,19 @@ def from_frame(cls, df, sortorder=None, names=None):
543548
3 NJ Precip
544549
545550
>>> pd.MultiIndex.from_frame(df)
546-
MultiIndex(levels=[['HI', 'NJ'], ['Precip', 'Temp']],
547-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
551+
MultiIndex([('HI', 'Temp'),
552+
('HI', 'Precip'),
553+
('NJ', 'Temp'),
554+
('NJ', 'Precip')],
548555
names=['a', 'b'])
549556
550557
Using explicit names, instead of the column names
551558
552559
>>> pd.MultiIndex.from_frame(df, names=['state', 'observation'])
553-
MultiIndex(levels=[['HI', 'NJ'], ['Precip', 'Temp']],
554-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
560+
MultiIndex([('HI', 'Temp'),
561+
('HI', 'Precip'),
562+
('NJ', 'Temp'),
563+
('NJ', 'Precip')],
555564
names=['state', 'observation'])
556565
"""
557566
if not isinstance(df, ABCDataFrame):
@@ -669,49 +678,30 @@ def set_levels(self, levels, level=None, inplace=False,
669678
>>> idx = pd.MultiIndex.from_tuples([(1, 'one'), (1, 'two'),
670679
(2, 'one'), (2, 'two')],
671680
names=['foo', 'bar'])
672-
<<<<<<< HEAD
673-
>>> idx.set_levels([['a','b'], [1,2]])
674-
MultiIndex(levels=[['a', 'b'], [1, 2]],
675-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
676-
names=['foo', 'bar'])
677-
>>> idx.set_levels(['a','b'], level=0)
678-
MultiIndex(levels=[['a', 'b'], ['one', 'two']],
679-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
680-
names=['foo', 'bar'])
681-
>>> idx.set_levels(['a','b'], level='bar')
682-
MultiIndex(levels=[[1, 2], ['a', 'b']],
683-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
684-
names=['foo', 'bar'])
685-
>>> idx.set_levels([['a','b'], [1,2]], level=[0,1])
686-
MultiIndex(levels=[['a', 'b'], [1, 2]],
687-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
688-
names=['foo', 'bar'])
689-
=======
690681
>>> idx.set_levels([['a', 'b'], [1, 2]])
691682
MultiIndex([('a', 1),
692683
('a', 2),
693684
('b', 1),
694685
('b', 2)],
695-
dtype='object', names=['foo', 'bar'])
686+
names=['foo', 'bar'])
696687
>>> idx.set_levels(['a', 'b'], level=0)
697688
MultiIndex([('a', 'one'),
698689
('a', 'two'),
699690
('b', 'one'),
700691
('b', 'two')],
701-
dtype='object', names=['foo', 'bar'])
692+
names=['foo', 'bar'])
702693
>>> idx.set_levels(['a', 'b'], level='bar')
703694
MultiIndex([(1, 'a'),
704695
(1, 'b'),
705696
(2, 'a'),
706697
(2, 'b')],
707-
dtype='object', names=['foo', 'bar'])
698+
names=['foo', 'bar'])
708699
>>> idx.set_levels([['a', 'b'], [1, 2]], level=[0, 1])
709700
MultiIndex([('a', 1),
710701
('a', 2),
711702
('b', 1),
712703
('b', 2)],
713-
dtype='object', names=['foo', 'bar'])
714-
>>>>>>> Update doc string examples and docs
704+
names=['foo', 'bar'])
715705
"""
716706
if is_list_like(levels) and not isinstance(levels, Index):
717707
levels = list(levels)
@@ -820,25 +810,25 @@ def set_codes(self, codes, level=None, inplace=False,
820810
(1, 'one'),
821811
(2, 'two'),
822812
(1, 'two')],
823-
dtype='object', names=['foo', 'bar'])
813+
names=['foo', 'bar'])
824814
>>> idx.set_codes([1, 0, 1, 0], level=0)
825815
MultiIndex([(2, 'one'),
826816
(1, 'two'),
827817
(2, 'one'),
828818
(1, 'two')],
829-
dtype='object', names=['foo', 'bar'])
819+
names=['foo', 'bar'])
830820
>>> idx.set_codes([0, 0, 1, 1], level='bar')
831821
MultiIndex([(1, 'one'),
832822
(1, 'one'),
833823
(2, 'two'),
834824
(2, 'two')],
835-
dtype='object', names=['foo', 'bar'])
825+
names=['foo', 'bar'])
836826
>>> idx.set_codes([[1, 0, 1, 0], [0, 0, 1, 1]], level=[0, 1])
837827
MultiIndex([(2, 'one'),
838828
(1, 'one'),
839829
(2, 'two'),
840830
(1, 'two')],
841-
dtype='object', names=['foo', 'bar'])
831+
names=['foo', 'bar'])
842832
"""
843833
if level is not None and not is_list_like(level):
844834
if not is_list_like(codes):
@@ -1002,6 +992,12 @@ def _format_data(self, name=None):
1002992
return format_object_summary(self, self._formatter_func,
1003993
name=name, line_break_each_value=True)
1004994

995+
def _format_attrs(self):
996+
"""
997+
Return a list of tuples of the (attr,formatted_value).
998+
"""
999+
return format_object_attrs(self, include_dtype=False)
1000+
10051001
def _format_native_types(self, na_rep='nan', **kwargs):
10061002
new_levels = []
10071003
new_codes = []
@@ -1599,7 +1595,7 @@ def to_hierarchical(self, n_repeat, n_shuffle=1):
15991595
(2, 'two'),
16001596
(2, 'two'),
16011597
(2, 'two')],
1602-
dtype='object')
1598+
)
16031599
"""
16041600
levels = self.levels
16051601
codes = [np.repeat(level_codes, n_repeat) for
@@ -1697,14 +1693,14 @@ def _sort_levels_monotonic(self):
16971693
('a', 'aa'),
16981694
('b', 'bb'),
16991695
('b', 'aa')],
1700-
dtype='object')
1696+
)
17011697
17021698
>>> mi.sort_values()
17031699
MultiIndex([('a', 'aa'),
17041700
('a', 'bb'),
17051701
('b', 'aa'),
17061702
('b', 'bb')],
1707-
dtype='object')
1703+
)
17081704
"""
17091705

17101706
if self.is_lexsorted() and self.is_monotonic:
@@ -1759,12 +1755,12 @@ def remove_unused_levels(self):
17591755
(0, 'b'),
17601756
(1, 'a'),
17611757
(1, 'b')],
1762-
dtype='object')
1758+
)
17631759
17641760
>>> mi[2:]
17651761
MultiIndex([(1, 'a'),
17661762
(1, 'b')],
1767-
dtype='object')
1763+
)
17681764
17691765
The 0 from the first level is not represented
17701766
and can be removed
@@ -2082,12 +2078,13 @@ def swaplevel(self, i=-2, j=-1):
20822078
('a', 'aa'),
20832079
('b', 'bb'),
20842080
('b', 'aa')],
2085-
dtype='object')
2081+
)
20862082
>>> mi.swaplevel(0, 1)
20872083
MultiIndex([('bb', 'a'),
20882084
('aa', 'a'),
20892085
('bb', 'b'),
20902086
('aa', 'b')],
2087+
)
20912088
"""
20922089
new_levels = list(self.levels)
20932090
new_codes = list(self.codes)

pandas/io/formats/printing.py

Lines changed: 4 additions & 2 deletions
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)