Skip to content

Commit eea9361

Browse files
Add repeat method to Categorical (ndarray compat) + fix PeriodIndex.repeat
1 parent 90e067b commit eea9361

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

pandas/core/categorical.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,20 @@ def describe(self):
15991599

16001600
return result
16011601

1602+
def repeat(self, repeats):
1603+
"""
1604+
Repeat elements of a Categorical.
1605+
1606+
See also
1607+
--------
1608+
numpy.ndarray.repeat
1609+
1610+
"""
1611+
codes = self._codes.repeat(repeats)
1612+
return Categorical(values=codes, categories=self.categories,
1613+
ordered=self.ordered, name=self.name, fastpath=True)
1614+
1615+
16021616
##### The Series.cat accessor #####
16031617

16041618
class CategoricalAccessor(PandasDelegate):

pandas/tests/test_categorical.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2714,7 +2714,6 @@ def f():
27142714
df.append(df_wrong_categories)
27152715
self.assertRaises(ValueError, f)
27162716

2717-
27182717
def test_merge(self):
27192718
# GH 9426
27202719

@@ -2747,6 +2746,13 @@ def test_merge(self):
27472746
result = pd.merge(cleft, cright, how='left', left_on='b', right_on='c')
27482747
tm.assert_frame_equal(result, expected)
27492748

2749+
def test_repeat(self):
2750+
#GH10183
2751+
cat = pd.Categorical(["a","b"], categories=["a","b"])
2752+
exp = pd.Categorical(["a", "a", "b", "b"], categories=["a","b"])
2753+
res = cat.repeat(2)
2754+
self.assert_categorical_equal(res, exp)
2755+
27502756
def test_na_actions(self):
27512757

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

pandas/tests/test_index.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,6 +2857,14 @@ def test_get_indexer(self):
28572857
with self.assertRaisesRegexp(ValueError, 'different freq'):
28582858
idx.asfreq('D').get_indexer(idx)
28592859

2860+
def test_repeat(self):
2861+
# GH10183
2862+
idx = pd.period_range('2000-01-01', periods=3, freq='D')
2863+
res = idx.repeat(3)
2864+
exp = PeriodIndex(idx.values.repeat(3), freq='D')
2865+
self.assert_index_equal(res, exp)
2866+
self.assertEqual(res.freqstr, 'D')
2867+
28602868

28612869
class TestTimedeltaIndex(DatetimeLike, tm.TestCase):
28622870
_holder = TimedeltaIndex

pandas/tseries/period.py

Lines changed: 11 additions & 0 deletions
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)