Skip to content

Commit d5fdbc6

Browse files
committed
Merge pull request #9901 from jreback/repr-name
Index repr changes to make them consistent
2 parents 3c0e66d + ac5aa58 commit d5fdbc6

File tree

12 files changed

+384
-195
lines changed

12 files changed

+384
-195
lines changed

doc/source/advanced.rst

+1-4
Original file line numberDiff line numberDiff line change
@@ -675,10 +675,7 @@ values NOT in the categories, similarly to how you can reindex ANY pandas index.
675675
}).set_index('B')
676676
677677
In [11]: df3.index
678-
Out[11]:
679-
CategoricalIndex([u'a', u'a', u'b', u'b', u'c', u'a'],
680-
categories=[u'a', u'b', u'c'],
681-
ordered=False)
678+
Out[11]: CategoricalIndex([u'a', u'a', u'b', u'b', u'c', u'a'], categories=[u'a', u'b', u'c'], ordered=False, name=u'B', dtype='category')
682679
683680
In [12]: pd.concat([df2,df3]
684681
TypeError: categories must match existing categories when appending

doc/source/whatsnew/v0.16.1.txt

+50-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ Highlights include:
1313
- New section on how-to-contribute to *pandas*, see :ref:`here <contributing>`
1414
- Revised "Merge, join, and concatenate" documentation, including graphical examples to make it easier to understand each operations, see :ref:`here <merging>`
1515
- New method ``sample`` for drawing random samples from Series, DataFrames and Panels. See :ref:`here <whatsnew_0161.enhancements.sample>`
16-
- ``BusinessHour`` date-offset is now supported, see :ref:`here <timeseries.businesshour>`
16+
- The default ``Index`` printing has changed to a more uniform format, see :ref:`here <whatsnew_0161.index_repr>`
17+
- ``BusinessHour`` datetime-offset is now supported, see :ref:`here <timeseries.businesshour>`
18+
19+
>>>>>>> more fixes
1720
- Further enhancement to the ``.str`` accessor to make string operations easier, see :ref:`here <whatsnew_0161.enhancements.string>`
1821

1922
.. contents:: What's new in v0.16.1
@@ -268,6 +271,52 @@ API changes
268271

269272
- By default, ``read_csv`` and ``read_table`` will now try to infer the compression type based on the file extension. Set ``compression=None`` to restore the previous behavior (no decompression). (:issue:`9770`)
270273

274+
.. _whatsnew_0161.index_repr:
275+
276+
Index Representation
277+
~~~~~~~~~~~~~~~~~~~~
278+
279+
The string representation of ``Index`` and its sub-classes have now been unified. These will show a single-line display if there are few values; a wrapped multi-line display for a lot of values (but less than ``display.max_seq_items``; if lots of items (> ``display.max_seq_items``) will show a truncated display (the head and tail of the data). The formatting for ``MultiIndex`` is unchanges (a multi-line wrapped display). The display width responds to the option ``display.max_seq_items``, which is defaulted to 100. (:issue:`6482`)
280+
281+
Previous Behavior
282+
283+
.. code-block:: python
284+
285+
In [2]: pd.Index(range(4),name='foo')
286+
Out[2]: Int64Index([0, 1, 2, 3], dtype='int64')
287+
288+
In [3]: pd.Index(range(104),name='foo')
289+
Out[3]: Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, ...], dtype='int64')
290+
291+
In [4]: pd.date_range('20130101',periods=4,name='foo',tz='US/Eastern')
292+
Out[4]:
293+
<class 'pandas.tseries.index.DatetimeIndex'>
294+
[2013-01-01 00:00:00-05:00, ..., 2013-01-04 00:00:00-05:00]
295+
Length: 4, Freq: D, Timezone: US/Eastern
296+
297+
In [5]: pd.date_range('20130101',periods=104,name='foo',tz='US/Eastern')
298+
Out[5]:
299+
<class 'pandas.tseries.index.DatetimeIndex'>
300+
[2013-01-01 00:00:00-05:00, ..., 2013-04-14 00:00:00-04:00]
301+
Length: 104, Freq: D, Timezone: US/Eastern
302+
303+
New Behavior
304+
305+
.. ipython:: python
306+
307+
pd.set_option('display.width',100)
308+
pd.Index(range(4),name='foo')
309+
pd.Index(range(25),name='foo')
310+
pd.Index(range(104),name='foo')
311+
pd.Index(['datetime', 'sA', 'sB', 'sC', 'flow', 'error', 'temp', 'ref', 'a_bit_a_longer_one']*2)
312+
pd.CategoricalIndex(['a','bb','ccc','dddd'],ordered=True,name='foobar')
313+
pd.CategoricalIndex(['a','bb','ccc','dddd']*10,ordered=True,name='foobar')
314+
pd.CategoricalIndex(['a','bb','ccc','dddd']*100,ordered=True,name='foobar')
315+
pd.CategoricalIndex(np.arange(1000),ordered=True,name='foobar')
316+
pd.date_range('20130101',periods=4,name='foo',tz='US/Eastern')
317+
pd.date_range('20130101',periods=25,name='foo',tz='US/Eastern')
318+
pd.date_range('20130101',periods=104,name='foo',tz='US/Eastern')
319+
271320
.. _whatsnew_0161.deprecations:
272321

273322
Deprecations

pandas/core/common.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -3132,7 +3132,7 @@ def in_ipython_frontend():
31323132
# working with straight ascii.
31333133

31343134

3135-
def _pprint_seq(seq, _nest_lvl=0, **kwds):
3135+
def _pprint_seq(seq, _nest_lvl=0, max_seq_items=None, **kwds):
31363136
"""
31373137
internal. pprinter for iterables. you should probably use pprint_thing()
31383138
rather then calling this directly.
@@ -3144,12 +3144,15 @@ def _pprint_seq(seq, _nest_lvl=0, **kwds):
31443144
else:
31453145
fmt = u("[%s]") if hasattr(seq, '__setitem__') else u("(%s)")
31463146

3147-
nitems = get_option("max_seq_items") or len(seq)
3147+
if max_seq_items is False:
3148+
nitems = len(seq)
3149+
else:
3150+
nitems = max_seq_items or get_option("max_seq_items") or len(seq)
31483151

31493152
s = iter(seq)
31503153
r = []
31513154
for i in range(min(nitems, len(seq))): # handle sets, no slicing
3152-
r.append(pprint_thing(next(s), _nest_lvl + 1, **kwds))
3155+
r.append(pprint_thing(next(s), _nest_lvl + 1, max_seq_items=max_seq_items, **kwds))
31533156
body = ", ".join(r)
31543157

31553158
if nitems < len(seq):
@@ -3160,7 +3163,7 @@ def _pprint_seq(seq, _nest_lvl=0, **kwds):
31603163
return fmt % body
31613164

31623165

3163-
def _pprint_dict(seq, _nest_lvl=0, **kwds):
3166+
def _pprint_dict(seq, _nest_lvl=0, max_seq_items=None, **kwds):
31643167
"""
31653168
internal. pprinter for iterables. you should probably use pprint_thing()
31663169
rather then calling this directly.
@@ -3170,11 +3173,14 @@ def _pprint_dict(seq, _nest_lvl=0, **kwds):
31703173

31713174
pfmt = u("%s: %s")
31723175

3173-
nitems = get_option("max_seq_items") or len(seq)
3176+
if max_seq_items is False:
3177+
nitems = len(seq)
3178+
else:
3179+
nitems = max_seq_items or get_option("max_seq_items") or len(seq)
31743180

31753181
for k, v in list(seq.items())[:nitems]:
3176-
pairs.append(pfmt % (pprint_thing(k, _nest_lvl + 1, **kwds),
3177-
pprint_thing(v, _nest_lvl + 1, **kwds)))
3182+
pairs.append(pfmt % (pprint_thing(k, _nest_lvl + 1, max_seq_items=max_seq_items, **kwds),
3183+
pprint_thing(v, _nest_lvl + 1, max_seq_items=max_seq_items, **kwds)))
31783184

31793185
if nitems < len(seq):
31803186
return fmt % (", ".join(pairs) + ", ...")
@@ -3183,7 +3189,7 @@ def _pprint_dict(seq, _nest_lvl=0, **kwds):
31833189

31843190

31853191
def pprint_thing(thing, _nest_lvl=0, escape_chars=None, default_escapes=False,
3186-
quote_strings=False):
3192+
quote_strings=False, max_seq_items=None):
31873193
"""
31883194
This function is the sanctioned way of converting objects
31893195
to a unicode representation.
@@ -3202,6 +3208,8 @@ def pprint_thing(thing, _nest_lvl=0, escape_chars=None, default_escapes=False,
32023208
replacements
32033209
default_escapes : bool, default False
32043210
Whether the input escape characters replaces or adds to the defaults
3211+
max_seq_items : False, int, default None
3212+
Pass thru to other pretty printers to limit sequence printing
32053213
32063214
Returns
32073215
-------
@@ -3240,11 +3248,11 @@ def as_escaped_unicode(thing, escape_chars=escape_chars):
32403248
return compat.text_type(thing)
32413249
elif (isinstance(thing, dict) and
32423250
_nest_lvl < get_option("display.pprint_nest_depth")):
3243-
result = _pprint_dict(thing, _nest_lvl, quote_strings=True)
3251+
result = _pprint_dict(thing, _nest_lvl, quote_strings=True, max_seq_items=max_seq_items)
32443252
elif is_sequence(thing) and _nest_lvl < \
32453253
get_option("display.pprint_nest_depth"):
32463254
result = _pprint_seq(thing, _nest_lvl, escape_chars=escape_chars,
3247-
quote_strings=quote_strings)
3255+
quote_strings=quote_strings, max_seq_items=max_seq_items)
32483256
elif isinstance(thing, compat.string_types) and quote_strings:
32493257
if compat.PY3:
32503258
fmt = "'%s'"

0 commit comments

Comments
 (0)