|
2 | 2 | import pytest
|
3 | 3 |
|
4 | 4 | import numpy as np
|
5 |
| -from datetime import date, timedelta, time |
| 5 | +from datetime import date |
6 | 6 |
|
7 | 7 | import dateutil
|
8 | 8 | import pandas as pd
|
|
18 | 18 |
|
19 | 19 | class TestDatetimeIndex(object):
|
20 | 20 |
|
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 |
| - |
127 | 21 | def test_roundtrip_pickle_with_tz(self):
|
128 | 22 |
|
129 | 23 | # GH 8367
|
|
0 commit comments