Skip to content

Commit eed76c1

Browse files
committed
don't show useless dtype in repr
1 parent 64b66a7 commit eed76c1

File tree

6 files changed

+86
-74
lines changed

6 files changed

+86
-74
lines changed

doc/source/whatsnew/v0.25.0.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ The repr now looks like this:
3535
pd.MultiIndex.from_product([['a', 'abc'], range(500)])
3636
3737
Previously, outputting a :class:`MultiIndex` printed all the ``levels`` and
38-
``labels`` of the ``MultiIndex``, which was visually unappealing and made
38+
``codes`` of the ``MultiIndex``, which was visually unappealing and made
3939
the output more difficult to navigate:
4040

4141
.. code-block:: ipython
4242
43-
>>>pd.MultiIndex.from_product([['a', 'abc'], range(5)])
44-
MultiIndex(levels=[['a', 'abc'], [0, 1, 2, 3]],
45-
labels=[[0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 2, 3, 0, 1, 2, 3]])
43+
In [1]: pd.MultiIndex.from_product([['a', 'abc'], range(5)])
44+
Out[1]: MultiIndex(levels=[['a', 'abc'], [0, 1, 2, 3]],
45+
...: codes=[[0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 2, 3, 0, 1, 2, 3]])
4646
4747
In the new repr, all values will be shown, if the number of rows is smaller
4848
than :attr:`options.display.max_seq_items` (default: 100 items). Horizontally,

pandas/core/indexes/base.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -1306,20 +1306,20 @@ def set_names(self, names, level=None, inplace=False):
13061306
('python', 2019),
13071307
( 'cobra', 2018),
13081308
( 'cobra', 2019)],
1309-
dtype='object')
1309+
)
13101310
>>> idx.set_names(['kind', 'year'], inplace=True)
13111311
>>> idx
13121312
MultiIndex([('python', 2018),
13131313
('python', 2019),
13141314
( 'cobra', 2018),
13151315
( 'cobra', 2019)],
1316-
dtype='object', names=['kind', 'year'])
1316+
names=['kind', 'year'])
13171317
>>> idx.set_names('species', level=0)
13181318
MultiIndex([('python', 2018),
13191319
('python', 2019),
13201320
( 'cobra', 2018),
13211321
( 'cobra', 2019)],
1322-
dtype='object', names=['species', 'year'])
1322+
names=['species', 'year'])
13231323
"""
13241324

13251325
if level is not None and not isinstance(self, ABCMultiIndex):
@@ -1384,13 +1384,13 @@ def rename(self, name, inplace=False):
13841384
('python', 2019),
13851385
( 'cobra', 2018),
13861386
( 'cobra', 2019)],
1387-
dtype='object', names=['kind', 'year'])
1387+
names=['kind', 'year'])
13881388
>>> idx.rename(['species', 'year'])
13891389
MultiIndex([('python', 2018),
13901390
('python', 2019),
13911391
( 'cobra', 2018),
13921392
( 'cobra', 2019)],
1393-
dtype='object', names=['species', 'year'])
1393+
names=['species', 'year'])
13941394
>>> idx.rename('species')
13951395
Traceback (most recent call last):
13961396
TypeError: Must pass list-like as `names`.
@@ -5311,7 +5311,7 @@ def ensure_index_from_sequences(sequences, names=None):
53115311
names=['L1', 'L2'])
53125312
MultiIndex([('a', 'a'),
53135313
('a', 'b')],
5314-
dtype='object', names=['L1', 'L2'])
5314+
names=['L1', 'L2'])
53155315
53165316
See Also
53175317
--------
@@ -5350,14 +5350,9 @@ def ensure_index(index_like, copy=False):
53505350
Index([('a', 'a'), ('b', 'c')], dtype='object')
53515351
53525352
>>> ensure_index([['a', 'a'], ['b', 'c']])
5353-
<<<<<<< HEAD
5354-
MultiIndex(levels=[['a'], ['b', 'c']],
5355-
codes=[[0, 0], [0, 1]])
5356-
=======
53575353
MultiIndex([('a', 'b'),
53585354
('a', 'c')],
5359-
dtype='object')
5360-
>>>>>>> Improve docs
5355+
)
53615356
53625357
See Also
53635358
--------

pandas/core/indexes/multi.py

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

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

3536
_index_doc_kwargs = dict(ibase._index_doc_kwargs)
3637
_index_doc_kwargs.update(
@@ -188,8 +189,10 @@ class MultiIndex(Index):
188189
189190
>>> arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]
190191
>>> pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
191-
MultiIndex(levels=[[1, 2], ['blue', 'red']],
192-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
192+
MultiIndex([(1, 'red'),
193+
(1, 'blue'),
194+
(2, 'red'),
195+
(2, 'blue')],
193196
names=['number', 'color'])
194197
195198
See further examples for how to construct a MultiIndex in the doc strings
@@ -319,8 +322,10 @@ def from_arrays(cls, arrays, sortorder=None, names=None):
319322
--------
320323
>>> arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]
321324
>>> pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
322-
MultiIndex(levels=[[1, 2], ['blue', 'red']],
323-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
325+
MultiIndex([(1, 'red'),
326+
(1, 'blue'),
327+
(2, 'red'),
328+
(2, 'blue')],
324329
names=['number', 'color'])
325330
"""
326331
error_msg = "Input must be a list / sequence of array-likes."
@@ -384,7 +389,7 @@ def from_tuples(cls, tuples, sortorder=None, names=None):
384389
(1, 'blue'),
385390
(2, 'red'),
386391
(2, 'blue')],
387-
dtype='object', names=['number', 'color'])
392+
names=['number', 'color'])
388393
"""
389394
if not is_list_like(tuples):
390395
raise TypeError('Input must be a list / sequence of tuple-likes.')
@@ -445,7 +450,7 @@ def from_product(cls, iterables, sortorder=None, names=None):
445450
(1, 'purple'),
446451
(2, 'green'),
447452
(2, 'purple')],
448-
dtype='object', names=['number', 'color'])
453+
names=['number', 'color'])
449454
"""
450455
from pandas.core.arrays.categorical import _factorize_from_iterables
451456
from pandas.core.reshape.util import cartesian_product
@@ -503,15 +508,19 @@ def from_frame(cls, df, sortorder=None, names=None):
503508
3 NJ Precip
504509
505510
>>> pd.MultiIndex.from_frame(df)
506-
MultiIndex(levels=[['HI', 'NJ'], ['Precip', 'Temp']],
507-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
511+
MultiIndex([('HI', 'Temp'),
512+
('HI', 'Precip'),
513+
('NJ', 'Temp'),
514+
('NJ', 'Precip')],
508515
names=['a', 'b'])
509516
510517
Using explicit names, instead of the column names
511518
512519
>>> pd.MultiIndex.from_frame(df, names=['state', 'observation'])
513-
MultiIndex(levels=[['HI', 'NJ'], ['Precip', 'Temp']],
514-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
520+
MultiIndex([('HI', 'Temp'),
521+
('HI', 'Precip'),
522+
('NJ', 'Temp'),
523+
('NJ', 'Precip')],
515524
names=['state', 'observation'])
516525
"""
517526
if not isinstance(df, ABCDataFrame):
@@ -628,49 +637,30 @@ def set_levels(self, levels, level=None, inplace=False,
628637
>>> idx = pd.MultiIndex.from_tuples([(1, 'one'), (1, 'two'),
629638
(2, 'one'), (2, 'two')],
630639
names=['foo', 'bar'])
631-
<<<<<<< HEAD
632-
>>> idx.set_levels([['a','b'], [1,2]])
633-
MultiIndex(levels=[['a', 'b'], [1, 2]],
634-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
635-
names=['foo', 'bar'])
636-
>>> idx.set_levels(['a','b'], level=0)
637-
MultiIndex(levels=[['a', 'b'], ['one', 'two']],
638-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
639-
names=['foo', 'bar'])
640-
>>> idx.set_levels(['a','b'], level='bar')
641-
MultiIndex(levels=[[1, 2], ['a', 'b']],
642-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
643-
names=['foo', 'bar'])
644-
>>> idx.set_levels([['a','b'], [1,2]], level=[0,1])
645-
MultiIndex(levels=[['a', 'b'], [1, 2]],
646-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
647-
names=['foo', 'bar'])
648-
=======
649640
>>> idx.set_levels([['a', 'b'], [1, 2]])
650641
MultiIndex([('a', 1),
651642
('a', 2),
652643
('b', 1),
653644
('b', 2)],
654-
dtype='object', names=['foo', 'bar'])
645+
names=['foo', 'bar'])
655646
>>> idx.set_levels(['a', 'b'], level=0)
656647
MultiIndex([('a', 'one'),
657648
('a', 'two'),
658649
('b', 'one'),
659650
('b', 'two')],
660-
dtype='object', names=['foo', 'bar'])
651+
names=['foo', 'bar'])
661652
>>> idx.set_levels(['a', 'b'], level='bar')
662653
MultiIndex([(1, 'a'),
663654
(1, 'b'),
664655
(2, 'a'),
665656
(2, 'b')],
666-
dtype='object', names=['foo', 'bar'])
657+
names=['foo', 'bar'])
667658
>>> idx.set_levels([['a', 'b'], [1, 2]], level=[0, 1])
668659
MultiIndex([('a', 1),
669660
('a', 2),
670661
('b', 1),
671662
('b', 2)],
672-
dtype='object', names=['foo', 'bar'])
673-
>>>>>>> Update doc string examples and docs
663+
names=['foo', 'bar'])
674664
"""
675665
if is_list_like(levels) and not isinstance(levels, Index):
676666
levels = list(levels)
@@ -797,26 +787,30 @@ def set_codes(self, codes, level=None, inplace=False,
797787
(1, 'one'),
798788
(2, 'two'),
799789
(1, 'two')],
800-
dtype='object', names=['foo', 'bar'])
790+
names=['foo', 'bar'])
801791
>>> idx.set_codes([1, 0, 1, 0], level=0)
802792
MultiIndex([(2, 'one'),
803793
(1, 'two'),
804794
(2, 'one'),
805795
(1, 'two')],
806-
dtype='object', names=['foo', 'bar'])
796+
names=['foo', 'bar'])
807797
>>> idx.set_codes([0, 0, 1, 1], level='bar')
808798
MultiIndex([(1, 'one'),
809799
(1, 'one'),
810800
(2, 'two'),
811801
(2, 'two')],
812-
dtype='object', names=['foo', 'bar'])
802+
names=['foo', 'bar'])
813803
>>> idx.set_codes([[1, 0, 1, 0], [0, 0, 1, 1]], level=[0, 1])
814804
MultiIndex([(2, 'one'),
815805
(1, 'one'),
816806
(2, 'two'),
817807
(1, 'two')],
808+
<<<<<<< HEAD
818809
dtype='object', names=['foo', 'bar'])
819810
>>>>>>> Update doc string examples and docs
811+
=======
812+
names=['foo', 'bar'])
813+
>>>>>>> don't show useless dtype in repr
820814
"""
821815
if level is not None and not is_list_like(level):
822816
if not is_list_like(codes):
@@ -980,6 +974,12 @@ def _format_data(self, name=None):
980974
return format_object_summary(self, self._formatter_func,
981975
name=name, line_break_each_value=True)
982976

977+
def _format_attrs(self):
978+
"""
979+
Return a list of tuples of the (attr,formatted_value).
980+
"""
981+
return format_object_attrs(self, include_dtype=False)
982+
983983
def _format_native_types(self, na_rep='nan', **kwargs):
984984
new_levels = []
985985
new_codes = []
@@ -1575,7 +1575,7 @@ def to_hierarchical(self, n_repeat, n_shuffle=1):
15751575
(2, 'two'),
15761576
(2, 'two'),
15771577
(2, 'two')],
1578-
dtype='object')
1578+
)
15791579
"""
15801580
levels = self.levels
15811581
codes = [np.repeat(level_codes, n_repeat) for
@@ -1669,14 +1669,14 @@ def _sort_levels_monotonic(self):
16691669
('a', 'aa'),
16701670
('b', 'bb'),
16711671
('b', 'aa')],
1672-
dtype='object')
1672+
)
16731673
16741674
>>> mi.sort_values()
16751675
MultiIndex([('a', 'aa'),
16761676
('a', 'bb'),
16771677
('b', 'aa'),
16781678
('b', 'bb')],
1679-
dtype='object')
1679+
)
16801680
"""
16811681

16821682
if self.is_lexsorted() and self.is_monotonic:
@@ -1731,12 +1731,12 @@ def remove_unused_levels(self):
17311731
(0, 'b'),
17321732
(1, 'a'),
17331733
(1, 'b')],
1734-
dtype='object')
1734+
)
17351735
17361736
>>> mi[2:]
17371737
MultiIndex([(1, 'a'),
17381738
(1, 'b')],
1739-
dtype='object')
1739+
)
17401740
17411741
The 0 from the first level is not represented
17421742
and can be removed
@@ -2053,12 +2053,13 @@ def swaplevel(self, i=-2, j=-1):
20532053
('a', 'aa'),
20542054
('b', 'bb'),
20552055
('b', 'aa')],
2056-
dtype='object')
2056+
)
20572057
>>> mi.swaplevel(0, 1)
20582058
MultiIndex([('bb', 'a'),
20592059
('aa', 'a'),
20602060
('bb', 'b'),
20612061
('aa', 'b')],
2062+
)
20622063
"""
20632064
new_levels = list(self.levels)
20642065
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)