Skip to content

Commit c627549

Browse files
committed
CLN: Move PeriodIndex.repeat to DatetimeIndexOpsMixin
1 parent 29d9e24 commit c627549

File tree

5 files changed

+75
-45
lines changed

5 files changed

+75
-45
lines changed

pandas/tseries/base.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,12 @@ def repeat(self, repeats, *args, **kwargs):
749749
Analogous to ndarray.repeat
750750
"""
751751
nv.validate_repeat(args, kwargs)
752-
return self._shallow_copy(self.values.repeat(repeats), freq=None)
752+
if isinstance(self, ABCPeriodIndex):
753+
freq = self.freq
754+
else:
755+
freq = None
756+
return self._shallow_copy(self.asi8.repeat(repeats),
757+
freq=freq)
753758

754759
def where(self, cond, other=None):
755760
"""

pandas/tseries/period.py

-14
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
from pandas.indexes.base import _index_shared_docs
3535

3636
from pandas import compat
37-
from pandas.compat.numpy import function as nv
3837
from pandas.util.decorators import Appender, cache_readonly, Substitution
3938
from pandas.lib import Timedelta
4039
import pandas.tslib as tslib
@@ -941,19 +940,6 @@ def append(self, other):
941940
for x in to_concat]
942941
return Index(com._concat_compat(to_concat), name=name)
943942

944-
def repeat(self, n, *args, **kwargs):
945-
"""
946-
Return a new Index of the values repeated `n` times.
947-
948-
See also
949-
--------
950-
numpy.ndarray.repeat
951-
"""
952-
nv.validate_repeat(args, kwargs)
953-
954-
# overwrites method from DatetimeIndexOpsMixin
955-
return self._shallow_copy(self.values.repeat(n))
956-
957943
def __setstate__(self, state):
958944
"""Necessary for making this object picklable"""
959945

pandas/tseries/tests/test_base.py

+69-14
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,39 @@ def test_round(self):
170170
tm.assertRaisesRegexp(ValueError, msg, rng.round, freq='M')
171171
tm.assertRaisesRegexp(ValueError, msg, elt.round, freq='M')
172172

173-
def test_repeat(self):
174-
reps = 2
175-
176-
for tz in self.tz:
177-
rng = pd.date_range(start='2016-01-01', periods=2,
178-
freq='30Min', tz=tz)
173+
def test_repeat_range(self):
174+
rng = date_range('1/1/2000', '1/1/2001')
179175

180-
expected_rng = DatetimeIndex([
181-
Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
182-
Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
183-
Timestamp('2016-01-01 00:30:00', tz=tz, freq='30T'),
184-
Timestamp('2016-01-01 00:30:00', tz=tz, freq='30T'),
185-
])
176+
result = rng.repeat(5)
177+
self.assertIsNone(result.freq)
178+
self.assertEqual(len(result), 5 * len(rng))
186179

187-
tm.assert_index_equal(rng.repeat(reps), expected_rng)
180+
for tz in self.tz:
181+
index = pd.date_range('2001-01-01', periods=2, freq='D', tz=tz)
182+
exp = pd.DatetimeIndex(['2001-01-01', '2001-01-01',
183+
'2001-01-02', '2001-01-02'], tz=tz)
184+
for res in [index.repeat(2), np.repeat(index, 2)]:
185+
tm.assert_index_equal(res, exp)
186+
self.assertIsNone(res.freq)
187+
188+
index = pd.date_range('2001-01-01', periods=2, freq='2D', tz=tz)
189+
exp = pd.DatetimeIndex(['2001-01-01', '2001-01-01',
190+
'2001-01-03', '2001-01-03'], tz=tz)
191+
for res in [index.repeat(2), np.repeat(index, 2)]:
192+
tm.assert_index_equal(res, exp)
193+
self.assertIsNone(res.freq)
194+
195+
index = pd.DatetimeIndex(['2001-01-01', 'NaT', '2003-01-01'],
196+
tz=tz)
197+
exp = pd.DatetimeIndex(['2001-01-01', '2001-01-01', '2001-01-01',
198+
'NaT', 'NaT', 'NaT',
199+
'2003-01-01', '2003-01-01', '2003-01-01'],
200+
tz=tz)
201+
for res in [index.repeat(3), np.repeat(index, 3)]:
202+
tm.assert_index_equal(res, exp)
203+
self.assertIsNone(res.freq)
188204

189-
def test_numpy_repeat(self):
205+
def test_repeat(self):
190206
reps = 2
191207
msg = "the 'axis' parameter is not supported"
192208

@@ -201,6 +217,10 @@ def test_numpy_repeat(self):
201217
Timestamp('2016-01-01 00:30:00', tz=tz, freq='30T'),
202218
])
203219

220+
res = rng.repeat(reps)
221+
tm.assert_index_equal(res, expected_rng)
222+
self.assertIsNone(res.freq)
223+
204224
tm.assert_index_equal(np.repeat(rng, reps), expected_rng)
205225
tm.assertRaisesRegexp(ValueError, msg, np.repeat,
206226
rng, reps, axis=1)
@@ -1605,6 +1625,21 @@ def test_shift(self):
16051625
name='xxx')
16061626
tm.assert_index_equal(idx.shift(-3, freq='T'), exp)
16071627

1628+
def test_repeat(self):
1629+
index = pd.timedelta_range('1 days', periods=2, freq='D')
1630+
exp = pd.TimedeltaIndex(['1 days', '1 days', '2 days', '2 days'])
1631+
for res in [index.repeat(2), np.repeat(index, 2)]:
1632+
tm.assert_index_equal(res, exp)
1633+
self.assertIsNone(res.freq)
1634+
1635+
index = TimedeltaIndex(['1 days', 'NaT', '3 days'])
1636+
exp = TimedeltaIndex(['1 days', '1 days', '1 days',
1637+
'NaT', 'NaT', 'NaT',
1638+
'3 days', '3 days', '3 days'])
1639+
for res in [index.repeat(3), np.repeat(index, 3)]:
1640+
tm.assert_index_equal(res, exp)
1641+
self.assertIsNone(res.freq)
1642+
16081643

16091644
class TestPeriodIndexOps(Ops):
16101645
def setUp(self):
@@ -2526,6 +2561,26 @@ def test_shift(self):
25262561
'2011-01-01 09:00'], name='xxx', freq='H')
25272562
tm.assert_index_equal(idx.shift(-3), exp)
25282563

2564+
def test_repeat(self):
2565+
index = pd.period_range('2001-01-01', periods=2, freq='D')
2566+
exp = pd.PeriodIndex(['2001-01-01', '2001-01-01',
2567+
'2001-01-02', '2001-01-02'], freq='D')
2568+
for res in [index.repeat(2), np.repeat(index, 2)]:
2569+
tm.assert_index_equal(res, exp)
2570+
2571+
index = pd.period_range('2001-01-01', periods=2, freq='2D')
2572+
exp = pd.PeriodIndex(['2001-01-01', '2001-01-01',
2573+
'2001-01-03', '2001-01-03'], freq='2D')
2574+
for res in [index.repeat(2), np.repeat(index, 2)]:
2575+
tm.assert_index_equal(res, exp)
2576+
2577+
index = pd.PeriodIndex(['2001-01', 'NaT', '2003-01'], freq='M')
2578+
exp = pd.PeriodIndex(['2001-01', '2001-01', '2001-01',
2579+
'NaT', 'NaT', 'NaT',
2580+
'2003-01', '2003-01', '2003-01'], freq='M')
2581+
for res in [index.repeat(3), np.repeat(index, 3)]:
2582+
tm.assert_index_equal(res, exp)
2583+
25292584

25302585
if __name__ == '__main__':
25312586
import nose

pandas/tseries/tests/test_period.py

-9
Original file line numberDiff line numberDiff line change
@@ -2558,15 +2558,6 @@ def test_constructor(self):
25582558
vals = np.array(vals)
25592559
self.assertRaises(ValueError, PeriodIndex, vals)
25602560

2561-
def test_repeat(self):
2562-
index = period_range('20010101', periods=2)
2563-
expected = PeriodIndex([
2564-
Period('2001-01-01'), Period('2001-01-01'),
2565-
Period('2001-01-02'), Period('2001-01-02'),
2566-
])
2567-
2568-
tm.assert_index_equal(index.repeat(2), expected)
2569-
25702561
def test_numpy_repeat(self):
25712562
index = period_range('20010101', periods=2)
25722563
expected = PeriodIndex([Period('2001-01-01'), Period('2001-01-01'),

pandas/tseries/tests/test_timeseries.py

-7
Original file line numberDiff line numberDiff line change
@@ -1342,13 +1342,6 @@ def test_format_pre_1900_dates(self):
13421342
ts = Series(1, index=rng)
13431343
repr(ts)
13441344

1345-
def test_repeat(self):
1346-
rng = date_range('1/1/2000', '1/1/2001')
1347-
1348-
result = rng.repeat(5)
1349-
self.assertIsNone(result.freq)
1350-
self.assertEqual(len(result), 5 * len(rng))
1351-
13521345
def test_at_time(self):
13531346
rng = date_range('1/1/2000', '1/5/2000', freq='5min')
13541347
ts = Series(np.random.randn(len(rng)), index=rng)

0 commit comments

Comments
 (0)