Skip to content

Commit 9545d88

Browse files
Add repeat method to Categorical (ndarray compat) + fix PeriodIndex.repeat
1 parent 1d87174 commit 9545d88

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
@@ -2704,7 +2704,6 @@ def f():
27042704
df.append(df_wrong_categories)
27052705
self.assertRaises(ValueError, f)
27062706

2707-
27082707
def test_merge(self):
27092708
# GH 9426
27102709

@@ -2737,6 +2736,13 @@ def test_merge(self):
27372736
result = pd.merge(cleft, cright, how='left', left_on='b', right_on='c')
27382737
tm.assert_frame_equal(result, expected)
27392738

2739+
def test_repeat(self):
2740+
#GH10183
2741+
cat = pd.Categorical(["a","b"], categories=["a","b"])
2742+
exp = pd.Categorical(["a", "a", "b", "b"], categories=["a","b"])
2743+
res = cat.repeat(2)
2744+
self.assert_categorical_equal(res, exp)
2745+
27402746
def test_na_actions(self):
27412747

27422748
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
@@ -2680,6 +2680,14 @@ def test_get_indexer(self):
26802680
with self.assertRaisesRegexp(ValueError, 'different freq'):
26812681
idx.asfreq('D').get_indexer(idx)
26822682

2683+
def test_repeat(self):
2684+
# GH10183
2685+
idx = pd.period_range('2000-01-01', periods=3, freq='D')
2686+
res = idx.repeat(3)
2687+
exp = PeriodIndex(idx.values.repeat(3), freq='D')
2688+
self.assert_index_equal(res, exp)
2689+
self.assertEqual(res.freqstr, 'D')
2690+
26832691

26842692
class TestTimedeltaIndex(DatetimeLike, tm.TestCase):
26852693
_holder = TimedeltaIndex

pandas/tseries/period.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,17 @@ def append(self, other):
781781
for x in to_concat]
782782
return Index(com._concat_compat(to_concat), name=name)
783783

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

0 commit comments

Comments
 (0)