Skip to content

Commit ea17794

Browse files
committed
don't show useless dtype in repr
1 parent bbde4ad commit ea17794

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
@@ -1312,20 +1312,20 @@ def set_names(self, names, level=None, inplace=False):
13121312
('python', 2019),
13131313
( 'cobra', 2018),
13141314
( 'cobra', 2019)],
1315-
dtype='object')
1315+
)
13161316
>>> idx.set_names(['kind', 'year'], inplace=True)
13171317
>>> idx
13181318
MultiIndex([('python', 2018),
13191319
('python', 2019),
13201320
( 'cobra', 2018),
13211321
( 'cobra', 2019)],
1322-
dtype='object', names=['kind', 'year'])
1322+
names=['kind', 'year'])
13231323
>>> idx.set_names('species', level=0)
13241324
MultiIndex([('python', 2018),
13251325
('python', 2019),
13261326
( 'cobra', 2018),
13271327
( 'cobra', 2019)],
1328-
dtype='object', names=['species', 'year'])
1328+
names=['species', 'year'])
13291329
"""
13301330

13311331
if level is not None and not isinstance(self, ABCMultiIndex):
@@ -1390,13 +1390,13 @@ def rename(self, name, inplace=False):
13901390
('python', 2019),
13911391
( 'cobra', 2018),
13921392
( 'cobra', 2019)],
1393-
dtype='object', names=['kind', 'year'])
1393+
names=['kind', 'year'])
13941394
>>> idx.rename(['species', 'year'])
13951395
MultiIndex([('python', 2018),
13961396
('python', 2019),
13971397
( 'cobra', 2018),
13981398
( 'cobra', 2019)],
1399-
dtype='object', names=['species', 'year'])
1399+
names=['species', 'year'])
14001400
>>> idx.rename('species')
14011401
Traceback (most recent call last):
14021402
TypeError: Must pass list-like as `names`.
@@ -5326,7 +5326,7 @@ def ensure_index_from_sequences(sequences, names=None):
53265326
names=['L1', 'L2'])
53275327
MultiIndex([('a', 'a'),
53285328
('a', 'b')],
5329-
dtype='object', names=['L1', 'L2'])
5329+
names=['L1', 'L2'])
53305330
53315331
See Also
53325332
--------
@@ -5365,14 +5365,9 @@ def ensure_index(index_like, copy=False):
53655365
Index([('a', 'a'), ('b', 'c')], dtype='object')
53665366
53675367
>>> ensure_index([['a', 'a'], ['b', 'c']])
5368-
<<<<<<< HEAD
5369-
MultiIndex(levels=[['a'], ['b', 'c']],
5370-
codes=[[0, 0], [0, 1]])
5371-
=======
53725368
MultiIndex([('a', 'b'),
53735369
('a', 'c')],
5374-
dtype='object')
5375-
>>>>>>> Improve docs
5370+
)
53765371
53775372
See Also
53785373
--------

pandas/core/indexes/multi.py

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

35-
from pandas.io.formats.printing import format_object_summary, pprint_thing
35+
from pandas.io.formats.printing import (
36+
format_object_attrs, format_object_summary, pprint_thing)
3637

3738
_index_doc_kwargs = dict(ibase._index_doc_kwargs)
3839
_index_doc_kwargs.update(
@@ -190,8 +191,10 @@ class MultiIndex(Index):
190191
191192
>>> arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]
192193
>>> pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
193-
MultiIndex(levels=[[1, 2], ['blue', 'red']],
194-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
194+
MultiIndex([(1, 'red'),
195+
(1, 'blue'),
196+
(2, 'red'),
197+
(2, 'blue')],
195198
names=['number', 'color'])
196199
197200
See further examples for how to construct a MultiIndex in the doc strings
@@ -321,8 +324,10 @@ def from_arrays(cls, arrays, sortorder=None, names=None):
321324
--------
322325
>>> arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]
323326
>>> pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
324-
MultiIndex(levels=[[1, 2], ['blue', 'red']],
325-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
327+
MultiIndex([(1, 'red'),
328+
(1, 'blue'),
329+
(2, 'red'),
330+
(2, 'blue')],
326331
names=['number', 'color'])
327332
"""
328333
error_msg = "Input must be a list / sequence of array-likes."
@@ -386,7 +391,7 @@ def from_tuples(cls, tuples, sortorder=None, names=None):
386391
(1, 'blue'),
387392
(2, 'red'),
388393
(2, 'blue')],
389-
dtype='object', names=['number', 'color'])
394+
names=['number', 'color'])
390395
"""
391396
if not is_list_like(tuples):
392397
raise TypeError('Input must be a list / sequence of tuple-likes.')
@@ -447,7 +452,7 @@ def from_product(cls, iterables, sortorder=None, names=None):
447452
(1, 'purple'),
448453
(2, 'green'),
449454
(2, 'purple')],
450-
dtype='object', names=['number', 'color'])
455+
names=['number', 'color'])
451456
"""
452457
from pandas.core.arrays.categorical import _factorize_from_iterables
453458
from pandas.core.reshape.util import cartesian_product
@@ -505,15 +510,19 @@ def from_frame(cls, df, sortorder=None, names=None):
505510
3 NJ Precip
506511
507512
>>> pd.MultiIndex.from_frame(df)
508-
MultiIndex(levels=[['HI', 'NJ'], ['Precip', 'Temp']],
509-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
513+
MultiIndex([('HI', 'Temp'),
514+
('HI', 'Precip'),
515+
('NJ', 'Temp'),
516+
('NJ', 'Precip')],
510517
names=['a', 'b'])
511518
512519
Using explicit names, instead of the column names
513520
514521
>>> pd.MultiIndex.from_frame(df, names=['state', 'observation'])
515-
MultiIndex(levels=[['HI', 'NJ'], ['Precip', 'Temp']],
516-
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
522+
MultiIndex([('HI', 'Temp'),
523+
('HI', 'Precip'),
524+
('NJ', 'Temp'),
525+
('NJ', 'Precip')],
517526
names=['state', 'observation'])
518527
"""
519528
if not isinstance(df, ABCDataFrame):
@@ -630,49 +639,30 @@ def set_levels(self, levels, level=None, inplace=False,
630639
>>> idx = pd.MultiIndex.from_tuples([(1, 'one'), (1, 'two'),
631640
(2, 'one'), (2, 'two')],
632641
names=['foo', 'bar'])
633-
<<<<<<< HEAD
634-
>>> idx.set_levels([['a','b'], [1,2]])
635-
MultiIndex(levels=[['a', 'b'], [1, 2]],
636-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
637-
names=['foo', 'bar'])
638-
>>> idx.set_levels(['a','b'], level=0)
639-
MultiIndex(levels=[['a', 'b'], ['one', 'two']],
640-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
641-
names=['foo', 'bar'])
642-
>>> idx.set_levels(['a','b'], level='bar')
643-
MultiIndex(levels=[[1, 2], ['a', 'b']],
644-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
645-
names=['foo', 'bar'])
646-
>>> idx.set_levels([['a','b'], [1,2]], level=[0,1])
647-
MultiIndex(levels=[['a', 'b'], [1, 2]],
648-
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
649-
names=['foo', 'bar'])
650-
=======
651642
>>> idx.set_levels([['a', 'b'], [1, 2]])
652643
MultiIndex([('a', 1),
653644
('a', 2),
654645
('b', 1),
655646
('b', 2)],
656-
dtype='object', names=['foo', 'bar'])
647+
names=['foo', 'bar'])
657648
>>> idx.set_levels(['a', 'b'], level=0)
658649
MultiIndex([('a', 'one'),
659650
('a', 'two'),
660651
('b', 'one'),
661652
('b', 'two')],
662-
dtype='object', names=['foo', 'bar'])
653+
names=['foo', 'bar'])
663654
>>> idx.set_levels(['a', 'b'], level='bar')
664655
MultiIndex([(1, 'a'),
665656
(1, 'b'),
666657
(2, 'a'),
667658
(2, 'b')],
668-
dtype='object', names=['foo', 'bar'])
659+
names=['foo', 'bar'])
669660
>>> idx.set_levels([['a', 'b'], [1, 2]], level=[0, 1])
670661
MultiIndex([('a', 1),
671662
('a', 2),
672663
('b', 1),
673664
('b', 2)],
674-
dtype='object', names=['foo', 'bar'])
675-
>>>>>>> Update doc string examples and docs
665+
names=['foo', 'bar'])
676666
"""
677667
if is_list_like(levels) and not isinstance(levels, Index):
678668
levels = list(levels)
@@ -799,26 +789,30 @@ def set_codes(self, codes, level=None, inplace=False,
799789
(1, 'one'),
800790
(2, 'two'),
801791
(1, 'two')],
802-
dtype='object', names=['foo', 'bar'])
792+
names=['foo', 'bar'])
803793
>>> idx.set_codes([1, 0, 1, 0], level=0)
804794
MultiIndex([(2, 'one'),
805795
(1, 'two'),
806796
(2, 'one'),
807797
(1, 'two')],
808-
dtype='object', names=['foo', 'bar'])
798+
names=['foo', 'bar'])
809799
>>> idx.set_codes([0, 0, 1, 1], level='bar')
810800
MultiIndex([(1, 'one'),
811801
(1, 'one'),
812802
(2, 'two'),
813803
(2, 'two')],
814-
dtype='object', names=['foo', 'bar'])
804+
names=['foo', 'bar'])
815805
>>> idx.set_codes([[1, 0, 1, 0], [0, 0, 1, 1]], level=[0, 1])
816806
MultiIndex([(2, 'one'),
817807
(1, 'one'),
818808
(2, 'two'),
819809
(1, 'two')],
810+
<<<<<<< HEAD
820811
dtype='object', names=['foo', 'bar'])
821812
>>>>>>> Update doc string examples and docs
813+
=======
814+
names=['foo', 'bar'])
815+
>>>>>>> don't show useless dtype in repr
822816
"""
823817
if level is not None and not is_list_like(level):
824818
if not is_list_like(codes):
@@ -982,6 +976,12 @@ def _format_data(self, name=None):
982976
return format_object_summary(self, self._formatter_func,
983977
name=name, line_break_each_value=True)
984978

979+
def _format_attrs(self):
980+
"""
981+
Return a list of tuples of the (attr,formatted_value).
982+
"""
983+
return format_object_attrs(self, include_dtype=False)
984+
985985
def _format_native_types(self, na_rep='nan', **kwargs):
986986
new_levels = []
987987
new_codes = []
@@ -1578,7 +1578,7 @@ def to_hierarchical(self, n_repeat, n_shuffle=1):
15781578
(2, 'two'),
15791579
(2, 'two'),
15801580
(2, 'two')],
1581-
dtype='object')
1581+
)
15821582
"""
15831583
levels = self.levels
15841584
codes = [np.repeat(level_codes, n_repeat) for
@@ -1672,14 +1672,14 @@ def _sort_levels_monotonic(self):
16721672
('a', 'aa'),
16731673
('b', 'bb'),
16741674
('b', 'aa')],
1675-
dtype='object')
1675+
)
16761676
16771677
>>> mi.sort_values()
16781678
MultiIndex([('a', 'aa'),
16791679
('a', 'bb'),
16801680
('b', 'aa'),
16811681
('b', 'bb')],
1682-
dtype='object')
1682+
)
16831683
"""
16841684

16851685
if self.is_lexsorted() and self.is_monotonic:
@@ -1734,12 +1734,12 @@ def remove_unused_levels(self):
17341734
(0, 'b'),
17351735
(1, 'a'),
17361736
(1, 'b')],
1737-
dtype='object')
1737+
)
17381738
17391739
>>> mi[2:]
17401740
MultiIndex([(1, 'a'),
17411741
(1, 'b')],
1742-
dtype='object')
1742+
)
17431743
17441744
The 0 from the first level is not represented
17451745
and can be removed
@@ -2056,12 +2056,13 @@ def swaplevel(self, i=-2, j=-1):
20562056
('a', 'aa'),
20572057
('b', 'bb'),
20582058
('b', 'aa')],
2059-
dtype='object')
2059+
)
20602060
>>> mi.swaplevel(0, 1)
20612061
MultiIndex([('bb', 'a'),
20622062
('aa', 'a'),
20632063
('bb', 'b'),
20642064
('aa', 'b')],
2065+
)
20652066
"""
20662067
new_levels = list(self.levels)
20672068
new_codes = list(self.codes)

pandas/io/formats/printing.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ def _justify(head, tail):
474474
return head, tail
475475

476476

477-
def format_object_attrs(obj):
477+
def format_object_attrs(obj, include_dtype=True):
478478
"""
479479
Return a list of tuples of the (attr, formatted_value)
480480
for common attrs, including dtype, name, length
@@ -483,14 +483,16 @@ def format_object_attrs(obj):
483483
----------
484484
obj : object
485485
must be iterable
486+
include_dtype : bool
487+
If False, dtype won't be in the returned list
486488
487489
Returns
488490
-------
489491
list
490492
491493
"""
492494
attrs = []
493-
if hasattr(obj, 'dtype'):
495+
if hasattr(obj, 'dtype') and include_dtype:
494496
attrs.append(('dtype', "'{}'".format(obj.dtype)))
495497
if getattr(obj, 'name', None) is not None:
496498
attrs.append(('name', default_pprint(obj.name)))

0 commit comments

Comments
 (0)