Skip to content

Commit ba69a49

Browse files
Merge pull request #10183 from jorisvandenbossche/index-repr-setting
FIX printing index with display.max_seq_items=None (GH10182)
2 parents 2619889 + 4360afa commit ba69a49

File tree

6 files changed

+52
-4
lines changed

6 files changed

+52
-4
lines changed

doc/source/whatsnew/v0.16.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ Bug Fixes
137137
- Bug in ``Timestamp``'s' ``microsecond``, ``quarter``, ``dayofyear``, ``week`` and ``daysinmonth`` properties return ``np.int`` type, not built-in ``int``. (:issue:`10050`)
138138
- Bug in ``NaT`` raises ``AttributeError`` when accessing to ``daysinmonth``, ``dayofweek`` properties. (:issue:`10096`)
139139

140+
- Bug in Index repr when using the ``max_seq_items=None`` setting (:issue:`10182`).
140141

141142
- Bug in getting timezone data with ``dateutil`` on various platforms ( :issue:`9059`, :issue:`8639`, :issue:`9663`, :issue:`10121`)
142143
- Bug in display datetimes with mixed frequencies uniformly; display 'ms' datetimes to the proper precision. (:issue:`10170`)

pandas/core/categorical.py

+14
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,20 @@ def describe(self):
16071607

16081608
return result
16091609

1610+
def repeat(self, repeats):
1611+
"""
1612+
Repeat elements of a Categorical.
1613+
1614+
See also
1615+
--------
1616+
numpy.ndarray.repeat
1617+
1618+
"""
1619+
codes = self._codes.repeat(repeats)
1620+
return Categorical(values=codes, categories=self.categories,
1621+
ordered=self.ordered, name=self.name, fastpath=True)
1622+
1623+
16101624
##### The Series.cat accessor #####
16111625

16121626
class CategoricalAccessor(PandasDelegate):

pandas/core/index.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ def _format_data(self):
443443

444444
n = len(self)
445445
sep = ','
446-
max_seq_items = get_option('display.max_seq_items')
446+
max_seq_items = get_option('display.max_seq_items') or n
447447
formatter = self._formatter_func
448448

449449
# do we want to justify (only do so for non-objects)
@@ -534,7 +534,7 @@ def _format_attrs(self):
534534
attrs.append(('dtype',"'%s'" % self.dtype))
535535
if self.name is not None:
536536
attrs.append(('name',default_pprint(self.name)))
537-
max_seq_items = get_option('display.max_seq_items')
537+
max_seq_items = get_option('display.max_seq_items') or len(self)
538538
if len(self) > max_seq_items:
539539
attrs.append(('length',len(self)))
540540
return attrs
@@ -2950,7 +2950,7 @@ def _format_attrs(self):
29502950
if self.name is not None:
29512951
attrs.append(('name',default_pprint(self.name)))
29522952
attrs.append(('dtype',"'%s'" % self.dtype))
2953-
max_seq_items = get_option('display.max_seq_items')
2953+
max_seq_items = get_option('display.max_seq_items') or len(self)
29542954
if len(self) > max_seq_items:
29552955
attrs.append(('length',len(self)))
29562956
return attrs

pandas/tests/test_categorical.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -2736,7 +2736,6 @@ def f():
27362736
df.append(df_wrong_categories)
27372737
self.assertRaises(ValueError, f)
27382738

2739-
27402739
def test_merge(self):
27412740
# GH 9426
27422741

@@ -2769,6 +2768,13 @@ def test_merge(self):
27692768
result = pd.merge(cleft, cright, how='left', left_on='b', right_on='c')
27702769
tm.assert_frame_equal(result, expected)
27712770

2771+
def test_repeat(self):
2772+
#GH10183
2773+
cat = pd.Categorical(["a","b"], categories=["a","b"])
2774+
exp = pd.Categorical(["a", "a", "b", "b"], categories=["a","b"])
2775+
res = cat.repeat(2)
2776+
self.assert_categorical_equal(res, exp)
2777+
27722778
def test_na_actions(self):
27732779

27742780
cat = pd.Categorical([1,2,3,np.nan], categories=[1,2,3])

pandas/tests/test_index.py

+16
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ def test_str(self):
133133
self.assertTrue("'foo'" in str(idx))
134134
self.assertTrue(idx.__class__.__name__ in str(idx))
135135

136+
def test_repr_max_seq_item_setting(self):
137+
# GH10182
138+
idx = self.create_index()
139+
idx = idx.repeat(50)
140+
with pd.option_context("display.max_seq_items", None):
141+
repr(idx)
142+
self.assertFalse('...' in str(idx))
143+
136144
def test_wrong_number_names(self):
137145
def testit(ind):
138146
ind.names = ["apple", "banana", "carrot"]
@@ -2857,6 +2865,14 @@ def test_get_indexer(self):
28572865
with self.assertRaisesRegexp(ValueError, 'different freq'):
28582866
idx.asfreq('D').get_indexer(idx)
28592867

2868+
def test_repeat(self):
2869+
# GH10183
2870+
idx = pd.period_range('2000-01-01', periods=3, freq='D')
2871+
res = idx.repeat(3)
2872+
exp = PeriodIndex(idx.values.repeat(3), freq='D')
2873+
self.assert_index_equal(res, exp)
2874+
self.assertEqual(res.freqstr, 'D')
2875+
28602876

28612877
class TestTimedeltaIndex(DatetimeLike, tm.TestCase):
28622878
_holder = TimedeltaIndex

pandas/tseries/period.py

+11
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,17 @@ def append(self, other):
783783
for x in to_concat]
784784
return Index(com._concat_compat(to_concat), name=name)
785785

786+
def repeat(self, n):
787+
"""
788+
Return a new Index of the values repeated n times.
789+
790+
See also
791+
--------
792+
numpy.ndarray.repeat
793+
"""
794+
# overwrites method from DatetimeIndexOpsMixin
795+
return self._shallow_copy(self.values.repeat(n))
796+
786797
def __setstate__(self, state):
787798
"""Necessary for making this object picklable"""
788799

0 commit comments

Comments
 (0)