Skip to content

CLN: Move PeriodIndex.repeat to DatetimeIndexOpsMixin #13995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pandas/tseries/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,12 @@ def repeat(self, repeats, *args, **kwargs):
Analogous to ndarray.repeat
"""
nv.validate_repeat(args, kwargs)
return self._shallow_copy(self.values.repeat(repeats), freq=None)
if isinstance(self, ABCPeriodIndex):
freq = self.freq
else:
freq = None
return self._shallow_copy(self.asi8.repeat(repeats),
freq=freq)

def where(self, cond, other=None):
"""
Expand Down
14 changes: 0 additions & 14 deletions pandas/tseries/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from pandas.indexes.base import _index_shared_docs

from pandas import compat
from pandas.compat.numpy import function as nv
from pandas.util.decorators import Appender, cache_readonly, Substitution
from pandas.lib import Timedelta
import pandas.tslib as tslib
Expand Down Expand Up @@ -941,19 +940,6 @@ def append(self, other):
for x in to_concat]
return Index(com._concat_compat(to_concat), name=name)

def repeat(self, n, *args, **kwargs):
"""
Return a new Index of the values repeated `n` times.

See also
--------
numpy.ndarray.repeat
"""
nv.validate_repeat(args, kwargs)

# overwrites method from DatetimeIndexOpsMixin
return self._shallow_copy(self.values.repeat(n))

def __setstate__(self, state):
"""Necessary for making this object picklable"""

Expand Down
83 changes: 69 additions & 14 deletions pandas/tseries/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,39 @@ def test_round(self):
tm.assertRaisesRegexp(ValueError, msg, rng.round, freq='M')
tm.assertRaisesRegexp(ValueError, msg, elt.round, freq='M')

def test_repeat(self):
reps = 2

for tz in self.tz:
rng = pd.date_range(start='2016-01-01', periods=2,
freq='30Min', tz=tz)
def test_repeat_range(self):
rng = date_range('1/1/2000', '1/1/2001')

expected_rng = DatetimeIndex([
Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
Timestamp('2016-01-01 00:30:00', tz=tz, freq='30T'),
Timestamp('2016-01-01 00:30:00', tz=tz, freq='30T'),
])
result = rng.repeat(5)
self.assertIsNone(result.freq)
self.assertEqual(len(result), 5 * len(rng))

tm.assert_index_equal(rng.repeat(reps), expected_rng)
for tz in self.tz:
index = pd.date_range('2001-01-01', periods=2, freq='D', tz=tz)
exp = pd.DatetimeIndex(['2001-01-01', '2001-01-01',
'2001-01-02', '2001-01-02'], tz=tz)
for res in [index.repeat(2), np.repeat(index, 2)]:
tm.assert_index_equal(res, exp)
self.assertIsNone(res.freq)

index = pd.date_range('2001-01-01', periods=2, freq='2D', tz=tz)
exp = pd.DatetimeIndex(['2001-01-01', '2001-01-01',
'2001-01-03', '2001-01-03'], tz=tz)
for res in [index.repeat(2), np.repeat(index, 2)]:
tm.assert_index_equal(res, exp)
self.assertIsNone(res.freq)

index = pd.DatetimeIndex(['2001-01-01', 'NaT', '2003-01-01'],
tz=tz)
exp = pd.DatetimeIndex(['2001-01-01', '2001-01-01', '2001-01-01',
'NaT', 'NaT', 'NaT',
'2003-01-01', '2003-01-01', '2003-01-01'],
tz=tz)
for res in [index.repeat(3), np.repeat(index, 3)]:
tm.assert_index_equal(res, exp)
self.assertIsNone(res.freq)

def test_numpy_repeat(self):
def test_repeat(self):
reps = 2
msg = "the 'axis' parameter is not supported"

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

res = rng.repeat(reps)
tm.assert_index_equal(res, expected_rng)
self.assertIsNone(res.freq)

tm.assert_index_equal(np.repeat(rng, reps), expected_rng)
tm.assertRaisesRegexp(ValueError, msg, np.repeat,
rng, reps, axis=1)
Expand Down Expand Up @@ -1605,6 +1625,21 @@ def test_shift(self):
name='xxx')
tm.assert_index_equal(idx.shift(-3, freq='T'), exp)

def test_repeat(self):
index = pd.timedelta_range('1 days', periods=2, freq='D')
exp = pd.TimedeltaIndex(['1 days', '1 days', '2 days', '2 days'])
for res in [index.repeat(2), np.repeat(index, 2)]:
tm.assert_index_equal(res, exp)
self.assertIsNone(res.freq)

index = TimedeltaIndex(['1 days', 'NaT', '3 days'])
exp = TimedeltaIndex(['1 days', '1 days', '1 days',
'NaT', 'NaT', 'NaT',
'3 days', '3 days', '3 days'])
for res in [index.repeat(3), np.repeat(index, 3)]:
tm.assert_index_equal(res, exp)
self.assertIsNone(res.freq)


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

def test_repeat(self):
index = pd.period_range('2001-01-01', periods=2, freq='D')
exp = pd.PeriodIndex(['2001-01-01', '2001-01-01',
'2001-01-02', '2001-01-02'], freq='D')
for res in [index.repeat(2), np.repeat(index, 2)]:
tm.assert_index_equal(res, exp)

index = pd.period_range('2001-01-01', periods=2, freq='2D')
exp = pd.PeriodIndex(['2001-01-01', '2001-01-01',
'2001-01-03', '2001-01-03'], freq='2D')
for res in [index.repeat(2), np.repeat(index, 2)]:
tm.assert_index_equal(res, exp)

index = pd.PeriodIndex(['2001-01', 'NaT', '2003-01'], freq='M')
exp = pd.PeriodIndex(['2001-01', '2001-01', '2001-01',
'NaT', 'NaT', 'NaT',
'2003-01', '2003-01', '2003-01'], freq='M')
for res in [index.repeat(3), np.repeat(index, 3)]:
tm.assert_index_equal(res, exp)


if __name__ == '__main__':
import nose
Expand Down
9 changes: 0 additions & 9 deletions pandas/tseries/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -2558,15 +2558,6 @@ def test_constructor(self):
vals = np.array(vals)
self.assertRaises(ValueError, PeriodIndex, vals)

def test_repeat(self):
index = period_range('20010101', periods=2)
expected = PeriodIndex([
Period('2001-01-01'), Period('2001-01-01'),
Period('2001-01-02'), Period('2001-01-02'),
])

tm.assert_index_equal(index.repeat(2), expected)

def test_numpy_repeat(self):
index = period_range('20010101', periods=2)
expected = PeriodIndex([Period('2001-01-01'), Period('2001-01-01'),
Expand Down
7 changes: 0 additions & 7 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1342,13 +1342,6 @@ def test_format_pre_1900_dates(self):
ts = Series(1, index=rng)
repr(ts)

def test_repeat(self):
rng = date_range('1/1/2000', '1/1/2001')

result = rng.repeat(5)
self.assertIsNone(result.freq)
self.assertEqual(len(result), 5 * len(rng))

def test_at_time(self):
rng = date_range('1/1/2000', '1/5/2000', freq='5min')
ts = Series(np.random.randn(len(rng)), index=rng)
Expand Down