Skip to content

Commit f6b260b

Browse files
jbrockmendeljreback
authored andcommitted
implement timedeltas.test_scalar_compat (pandas-dev#19503)
1 parent 6670dfc commit f6b260b

File tree

4 files changed

+120
-94
lines changed

4 files changed

+120
-94
lines changed

pandas/tests/indexes/timedeltas/test_arithmetic.py

+55-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
to_timedelta, timedelta_range, date_range,
1111
Series,
1212
Timestamp, Timedelta)
13-
from pandas.errors import PerformanceWarning
13+
from pandas.errors import PerformanceWarning, NullFrequencyError
1414

1515

1616
@pytest.fixture(params=[pd.offsets.Hour(2), timedelta(hours=2),
@@ -138,6 +138,60 @@ def test_tdi_add_str_invalid(self):
138138
with pytest.raises(TypeError):
139139
'a' + tdi
140140

141+
# -------------------------------------------------------------
142+
# TimedeltaIndex.shift is used by __add__/__sub__
143+
144+
def test_tdi_shift_empty(self):
145+
# GH#9903
146+
idx = pd.TimedeltaIndex([], name='xxx')
147+
tm.assert_index_equal(idx.shift(0, freq='H'), idx)
148+
tm.assert_index_equal(idx.shift(3, freq='H'), idx)
149+
150+
def test_tdi_shift_hours(self):
151+
# GH#9903
152+
idx = pd.TimedeltaIndex(['5 hours', '6 hours', '9 hours'], name='xxx')
153+
tm.assert_index_equal(idx.shift(0, freq='H'), idx)
154+
exp = pd.TimedeltaIndex(['8 hours', '9 hours', '12 hours'], name='xxx')
155+
tm.assert_index_equal(idx.shift(3, freq='H'), exp)
156+
exp = pd.TimedeltaIndex(['2 hours', '3 hours', '6 hours'], name='xxx')
157+
tm.assert_index_equal(idx.shift(-3, freq='H'), exp)
158+
159+
def test_tdi_shift_minutes(self):
160+
# GH#9903
161+
idx = pd.TimedeltaIndex(['5 hours', '6 hours', '9 hours'], name='xxx')
162+
tm.assert_index_equal(idx.shift(0, freq='T'), idx)
163+
exp = pd.TimedeltaIndex(['05:03:00', '06:03:00', '9:03:00'],
164+
name='xxx')
165+
tm.assert_index_equal(idx.shift(3, freq='T'), exp)
166+
exp = pd.TimedeltaIndex(['04:57:00', '05:57:00', '8:57:00'],
167+
name='xxx')
168+
tm.assert_index_equal(idx.shift(-3, freq='T'), exp)
169+
170+
def test_tdi_shift_int(self):
171+
# GH#8083
172+
trange = pd.to_timedelta(range(5), unit='d') + pd.offsets.Hour(1)
173+
result = trange.shift(1)
174+
expected = TimedeltaIndex(['1 days 01:00:00', '2 days 01:00:00',
175+
'3 days 01:00:00',
176+
'4 days 01:00:00', '5 days 01:00:00'],
177+
freq='D')
178+
tm.assert_index_equal(result, expected)
179+
180+
def test_tdi_shift_nonstandard_freq(self):
181+
# GH#8083
182+
trange = pd.to_timedelta(range(5), unit='d') + pd.offsets.Hour(1)
183+
result = trange.shift(3, freq='2D 1s')
184+
expected = TimedeltaIndex(['6 days 01:00:03', '7 days 01:00:03',
185+
'8 days 01:00:03', '9 days 01:00:03',
186+
'10 days 01:00:03'], freq='D')
187+
tm.assert_index_equal(result, expected)
188+
189+
def test_shift_no_freq(self):
190+
# GH#19147
191+
tdi = TimedeltaIndex(['1 days 01:00:00', '2 days 01:00:00'], freq=None)
192+
with pytest.raises(NullFrequencyError):
193+
tdi.shift(2)
194+
141195
# -------------------------------------------------------------
142196

143197
@pytest.mark.parametrize('box', [np.array, pd.Index])

pandas/tests/indexes/timedeltas/test_ops.py

+1-45
Original file line numberDiff line numberDiff line change
@@ -98,32 +98,6 @@ def test_numpy_minmax(self):
9898
tm.assert_raises_regex(
9999
ValueError, errmsg, np.argmax, td, out=0)
100100

101-
def test_round(self):
102-
td = pd.timedelta_range(start='16801 days', periods=5, freq='30Min')
103-
elt = td[1]
104-
105-
expected_rng = TimedeltaIndex([
106-
Timedelta('16801 days 00:00:00'),
107-
Timedelta('16801 days 00:00:00'),
108-
Timedelta('16801 days 01:00:00'),
109-
Timedelta('16801 days 02:00:00'),
110-
Timedelta('16801 days 02:00:00'),
111-
])
112-
expected_elt = expected_rng[1]
113-
114-
tm.assert_index_equal(td.round(freq='H'), expected_rng)
115-
assert elt.round(freq='H') == expected_elt
116-
117-
msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR
118-
with tm.assert_raises_regex(ValueError, msg):
119-
td.round(freq='foo')
120-
with tm.assert_raises_regex(ValueError, msg):
121-
elt.round(freq='foo')
122-
123-
msg = "<MonthEnd> is a non-fixed frequency"
124-
tm.assert_raises_regex(ValueError, msg, td.round, freq='M')
125-
tm.assert_raises_regex(ValueError, msg, elt.round, freq='M')
126-
127101
def test_representation(self):
128102
idx1 = TimedeltaIndex([], freq='D')
129103
idx2 = TimedeltaIndex(['1 days'], freq='D')
@@ -387,25 +361,7 @@ def test_nat_new(self):
387361
tm.assert_numpy_array_equal(result, exp)
388362

389363
def test_shift(self):
390-
# GH 9903
391-
idx = pd.TimedeltaIndex([], name='xxx')
392-
tm.assert_index_equal(idx.shift(0, freq='H'), idx)
393-
tm.assert_index_equal(idx.shift(3, freq='H'), idx)
394-
395-
idx = pd.TimedeltaIndex(['5 hours', '6 hours', '9 hours'], name='xxx')
396-
tm.assert_index_equal(idx.shift(0, freq='H'), idx)
397-
exp = pd.TimedeltaIndex(['8 hours', '9 hours', '12 hours'], name='xxx')
398-
tm.assert_index_equal(idx.shift(3, freq='H'), exp)
399-
exp = pd.TimedeltaIndex(['2 hours', '3 hours', '6 hours'], name='xxx')
400-
tm.assert_index_equal(idx.shift(-3, freq='H'), exp)
401-
402-
tm.assert_index_equal(idx.shift(0, freq='T'), idx)
403-
exp = pd.TimedeltaIndex(['05:03:00', '06:03:00', '9:03:00'],
404-
name='xxx')
405-
tm.assert_index_equal(idx.shift(3, freq='T'), exp)
406-
exp = pd.TimedeltaIndex(['04:57:00', '05:57:00', '8:57:00'],
407-
name='xxx')
408-
tm.assert_index_equal(idx.shift(-3, freq='T'), exp)
364+
pass # handled in test_arithmetic.py
409365

410366
def test_repeat(self):
411367
index = pd.timedelta_range('1 days', periods=2, freq='D')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Tests for TimedeltaIndex methods behaving like their Timedelta counterparts
4+
"""
5+
6+
import numpy as np
7+
8+
import pandas as pd
9+
import pandas.util.testing as tm
10+
from pandas import timedelta_range, Timedelta, TimedeltaIndex, Index, Series
11+
12+
13+
class TestVectorizedTimedelta(object):
14+
def test_tdi_total_seconds(self):
15+
# GH#10939
16+
# test index
17+
rng = timedelta_range('1 days, 10:11:12.100123456', periods=2,
18+
freq='s')
19+
expt = [1 * 86400 + 10 * 3600 + 11 * 60 + 12 + 100123456. / 1e9,
20+
1 * 86400 + 10 * 3600 + 11 * 60 + 13 + 100123456. / 1e9]
21+
tm.assert_almost_equal(rng.total_seconds(), Index(expt))
22+
23+
# test Series
24+
ser = Series(rng)
25+
s_expt = Series(expt, index=[0, 1])
26+
tm.assert_series_equal(ser.dt.total_seconds(), s_expt)
27+
28+
# with nat
29+
ser[1] = np.nan
30+
s_expt = Series([1 * 86400 + 10 * 3600 + 11 * 60 +
31+
12 + 100123456. / 1e9, np.nan], index=[0, 1])
32+
tm.assert_series_equal(ser.dt.total_seconds(), s_expt)
33+
34+
# with both nat
35+
ser = Series([np.nan, np.nan], dtype='timedelta64[ns]')
36+
tm.assert_series_equal(ser.dt.total_seconds(),
37+
Series([np.nan, np.nan], index=[0, 1]))
38+
39+
def test_tdi_round(self):
40+
td = pd.timedelta_range(start='16801 days', periods=5, freq='30Min')
41+
elt = td[1]
42+
43+
expected_rng = TimedeltaIndex([Timedelta('16801 days 00:00:00'),
44+
Timedelta('16801 days 00:00:00'),
45+
Timedelta('16801 days 01:00:00'),
46+
Timedelta('16801 days 02:00:00'),
47+
Timedelta('16801 days 02:00:00')])
48+
expected_elt = expected_rng[1]
49+
50+
tm.assert_index_equal(td.round(freq='H'), expected_rng)
51+
assert elt.round(freq='H') == expected_elt
52+
53+
msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR
54+
with tm.assert_raises_regex(ValueError, msg):
55+
td.round(freq='foo')
56+
with tm.assert_raises_regex(ValueError, msg):
57+
elt.round(freq='foo')
58+
59+
msg = "<MonthEnd> is a non-fixed frequency"
60+
with tm.assert_raises_regex(ValueError, msg):
61+
td.round(freq='M')
62+
with tm.assert_raises_regex(ValueError, msg):
63+
elt.round(freq='M')

pandas/tests/indexes/timedeltas/test_timedelta.py

+1-48
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from datetime import timedelta
55

66
import pandas as pd
7-
from pandas.errors import NullFrequencyError
87
import pandas.util.testing as tm
98
from pandas import (timedelta_range, date_range, Series, Timedelta,
109
TimedeltaIndex, Index, DataFrame,
@@ -34,28 +33,7 @@ def test_numeric_compat(self):
3433
pass
3534

3635
def test_shift(self):
37-
# test shift for TimedeltaIndex
38-
# err8083
39-
40-
drange = self.create_index()
41-
result = drange.shift(1)
42-
expected = TimedeltaIndex(['1 days 01:00:00', '2 days 01:00:00',
43-
'3 days 01:00:00',
44-
'4 days 01:00:00', '5 days 01:00:00'],
45-
freq='D')
46-
tm.assert_index_equal(result, expected)
47-
48-
result = drange.shift(3, freq='2D 1s')
49-
expected = TimedeltaIndex(['6 days 01:00:03', '7 days 01:00:03',
50-
'8 days 01:00:03', '9 days 01:00:03',
51-
'10 days 01:00:03'], freq='D')
52-
tm.assert_index_equal(result, expected)
53-
54-
def test_shift_no_freq(self):
55-
# GH#19147
56-
tdi = TimedeltaIndex(['1 days 01:00:00', '2 days 01:00:00'], freq=None)
57-
with pytest.raises(NullFrequencyError):
58-
tdi.shift(2)
36+
pass # this is handled in test_arithmetic.py
5937

6038
def test_pickle_compat_construction(self):
6139
pass
@@ -203,31 +181,6 @@ def test_map(self):
203181
exp = Int64Index([f(x) for x in rng])
204182
tm.assert_index_equal(result, exp)
205183

206-
def test_total_seconds(self):
207-
# GH 10939
208-
# test index
209-
rng = timedelta_range('1 days, 10:11:12.100123456', periods=2,
210-
freq='s')
211-
expt = [1 * 86400 + 10 * 3600 + 11 * 60 + 12 + 100123456. / 1e9,
212-
1 * 86400 + 10 * 3600 + 11 * 60 + 13 + 100123456. / 1e9]
213-
tm.assert_almost_equal(rng.total_seconds(), Index(expt))
214-
215-
# test Series
216-
s = Series(rng)
217-
s_expt = Series(expt, index=[0, 1])
218-
tm.assert_series_equal(s.dt.total_seconds(), s_expt)
219-
220-
# with nat
221-
s[1] = np.nan
222-
s_expt = Series([1 * 86400 + 10 * 3600 + 11 * 60 +
223-
12 + 100123456. / 1e9, np.nan], index=[0, 1])
224-
tm.assert_series_equal(s.dt.total_seconds(), s_expt)
225-
226-
# with both nat
227-
s = Series([np.nan, np.nan], dtype='timedelta64[ns]')
228-
tm.assert_series_equal(s.dt.total_seconds(),
229-
Series([np.nan, np.nan], index=[0, 1]))
230-
231184
def test_pass_TimedeltaIndex_to_index(self):
232185

233186
rng = timedelta_range('1 days', '10 days')

0 commit comments

Comments
 (0)