Skip to content

Commit ce77b79

Browse files
jbrockmendeljreback
authored andcommitted
Separate TimedeltaIndex mul/div tests (#19848)
1 parent 01e99de commit ce77b79

File tree

6 files changed

+677
-647
lines changed

6 files changed

+677
-647
lines changed

pandas/tests/indexes/datetimes/test_datetime.py

+1-107
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44
import numpy as np
5-
from datetime import date, timedelta, time
5+
from datetime import date
66

77
import dateutil
88
import pandas as pd
@@ -18,112 +18,6 @@
1818

1919
class TestDatetimeIndex(object):
2020

21-
def test_get_loc(self):
22-
idx = pd.date_range('2000-01-01', periods=3)
23-
24-
for method in [None, 'pad', 'backfill', 'nearest']:
25-
assert idx.get_loc(idx[1], method) == 1
26-
assert idx.get_loc(idx[1].to_pydatetime(), method) == 1
27-
assert idx.get_loc(str(idx[1]), method) == 1
28-
29-
if method is not None:
30-
assert idx.get_loc(idx[1], method,
31-
tolerance=pd.Timedelta('0 days')) == 1
32-
33-
assert idx.get_loc('2000-01-01', method='nearest') == 0
34-
assert idx.get_loc('2000-01-01T12', method='nearest') == 1
35-
36-
assert idx.get_loc('2000-01-01T12', method='nearest',
37-
tolerance='1 day') == 1
38-
assert idx.get_loc('2000-01-01T12', method='nearest',
39-
tolerance=pd.Timedelta('1D')) == 1
40-
assert idx.get_loc('2000-01-01T12', method='nearest',
41-
tolerance=np.timedelta64(1, 'D')) == 1
42-
assert idx.get_loc('2000-01-01T12', method='nearest',
43-
tolerance=timedelta(1)) == 1
44-
with tm.assert_raises_regex(ValueError,
45-
'unit abbreviation w/o a number'):
46-
idx.get_loc('2000-01-01T12', method='nearest', tolerance='foo')
47-
with pytest.raises(KeyError):
48-
idx.get_loc('2000-01-01T03', method='nearest', tolerance='2 hours')
49-
with pytest.raises(
50-
ValueError,
51-
match='tolerance size must match target index size'):
52-
idx.get_loc('2000-01-01', method='nearest',
53-
tolerance=[pd.Timedelta('1day').to_timedelta64(),
54-
pd.Timedelta('1day').to_timedelta64()])
55-
56-
assert idx.get_loc('2000', method='nearest') == slice(0, 3)
57-
assert idx.get_loc('2000-01', method='nearest') == slice(0, 3)
58-
59-
assert idx.get_loc('1999', method='nearest') == 0
60-
assert idx.get_loc('2001', method='nearest') == 2
61-
62-
with pytest.raises(KeyError):
63-
idx.get_loc('1999', method='pad')
64-
with pytest.raises(KeyError):
65-
idx.get_loc('2001', method='backfill')
66-
67-
with pytest.raises(KeyError):
68-
idx.get_loc('foobar')
69-
with pytest.raises(TypeError):
70-
idx.get_loc(slice(2))
71-
72-
idx = pd.to_datetime(['2000-01-01', '2000-01-04'])
73-
assert idx.get_loc('2000-01-02', method='nearest') == 0
74-
assert idx.get_loc('2000-01-03', method='nearest') == 1
75-
assert idx.get_loc('2000-01', method='nearest') == slice(0, 2)
76-
77-
# time indexing
78-
idx = pd.date_range('2000-01-01', periods=24, freq='H')
79-
tm.assert_numpy_array_equal(idx.get_loc(time(12)),
80-
np.array([12]), check_dtype=False)
81-
tm.assert_numpy_array_equal(idx.get_loc(time(12, 30)),
82-
np.array([]), check_dtype=False)
83-
with pytest.raises(NotImplementedError):
84-
idx.get_loc(time(12, 30), method='pad')
85-
86-
def test_get_indexer(self):
87-
idx = pd.date_range('2000-01-01', periods=3)
88-
exp = np.array([0, 1, 2], dtype=np.intp)
89-
tm.assert_numpy_array_equal(idx.get_indexer(idx), exp)
90-
91-
target = idx[0] + pd.to_timedelta(['-1 hour', '12 hours',
92-
'1 day 1 hour'])
93-
tm.assert_numpy_array_equal(idx.get_indexer(target, 'pad'),
94-
np.array([-1, 0, 1], dtype=np.intp))
95-
tm.assert_numpy_array_equal(idx.get_indexer(target, 'backfill'),
96-
np.array([0, 1, 2], dtype=np.intp))
97-
tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest'),
98-
np.array([0, 1, 1], dtype=np.intp))
99-
tm.assert_numpy_array_equal(
100-
idx.get_indexer(target, 'nearest',
101-
tolerance=pd.Timedelta('1 hour')),
102-
np.array([0, -1, 1], dtype=np.intp))
103-
tol_raw = [pd.Timedelta('1 hour'),
104-
pd.Timedelta('1 hour'),
105-
pd.Timedelta('1 hour').to_timedelta64(), ]
106-
tm.assert_numpy_array_equal(
107-
idx.get_indexer(target, 'nearest',
108-
tolerance=[np.timedelta64(x) for x in tol_raw]),
109-
np.array([0, -1, 1], dtype=np.intp))
110-
tol_bad = [pd.Timedelta('2 hour').to_timedelta64(),
111-
pd.Timedelta('1 hour').to_timedelta64(),
112-
'foo', ]
113-
with pytest.raises(
114-
ValueError, match='abbreviation w/o a number'):
115-
idx.get_indexer(target, 'nearest', tolerance=tol_bad)
116-
with pytest.raises(ValueError):
117-
idx.get_indexer(idx[[0]], method='nearest', tolerance='foo')
118-
119-
def test_reasonable_keyerror(self):
120-
# GH #1062
121-
index = DatetimeIndex(['1/3/2000'])
122-
try:
123-
index.get_loc('1/1/2000')
124-
except KeyError as e:
125-
assert '2000' in str(e)
126-
12721
def test_roundtrip_pickle_with_tz(self):
12822

12923
# GH 8367

0 commit comments

Comments
 (0)