test_indexing.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. from datetime import datetime, timedelta
  2. import numpy as np
  3. import pytest
  4. from pandas._libs.tslibs import period as libperiod
  5. from pandas.compat import lrange
  6. import pandas as pd
  7. from pandas import (
  8. DatetimeIndex, Period, PeriodIndex, Series, notna, period_range)
  9. from pandas.util import testing as tm
  10. class TestGetItem(object):
  11. def test_ellipsis(self):
  12. # GH#21282
  13. idx = period_range('2011-01-01', '2011-01-31', freq='D',
  14. name='idx')
  15. result = idx[...]
  16. assert result.equals(idx)
  17. assert result is not idx
  18. def test_getitem(self):
  19. idx1 = pd.period_range('2011-01-01', '2011-01-31', freq='D',
  20. name='idx')
  21. for idx in [idx1]:
  22. result = idx[0]
  23. assert result == pd.Period('2011-01-01', freq='D')
  24. result = idx[-1]
  25. assert result == pd.Period('2011-01-31', freq='D')
  26. result = idx[0:5]
  27. expected = pd.period_range('2011-01-01', '2011-01-05', freq='D',
  28. name='idx')
  29. tm.assert_index_equal(result, expected)
  30. assert result.freq == expected.freq
  31. assert result.freq == 'D'
  32. result = idx[0:10:2]
  33. expected = pd.PeriodIndex(['2011-01-01', '2011-01-03',
  34. '2011-01-05',
  35. '2011-01-07', '2011-01-09'],
  36. freq='D', name='idx')
  37. tm.assert_index_equal(result, expected)
  38. assert result.freq == expected.freq
  39. assert result.freq == 'D'
  40. result = idx[-20:-5:3]
  41. expected = pd.PeriodIndex(['2011-01-12', '2011-01-15',
  42. '2011-01-18',
  43. '2011-01-21', '2011-01-24'],
  44. freq='D', name='idx')
  45. tm.assert_index_equal(result, expected)
  46. assert result.freq == expected.freq
  47. assert result.freq == 'D'
  48. result = idx[4::-1]
  49. expected = PeriodIndex(['2011-01-05', '2011-01-04', '2011-01-03',
  50. '2011-01-02', '2011-01-01'],
  51. freq='D', name='idx')
  52. tm.assert_index_equal(result, expected)
  53. assert result.freq == expected.freq
  54. assert result.freq == 'D'
  55. def test_getitem_index(self):
  56. idx = period_range('2007-01', periods=10, freq='M', name='x')
  57. result = idx[[1, 3, 5]]
  58. exp = pd.PeriodIndex(['2007-02', '2007-04', '2007-06'],
  59. freq='M', name='x')
  60. tm.assert_index_equal(result, exp)
  61. result = idx[[True, True, False, False, False,
  62. True, True, False, False, False]]
  63. exp = pd.PeriodIndex(['2007-01', '2007-02', '2007-06', '2007-07'],
  64. freq='M', name='x')
  65. tm.assert_index_equal(result, exp)
  66. def test_getitem_partial(self):
  67. rng = period_range('2007-01', periods=50, freq='M')
  68. ts = Series(np.random.randn(len(rng)), rng)
  69. pytest.raises(KeyError, ts.__getitem__, '2006')
  70. result = ts['2008']
  71. assert (result.index.year == 2008).all()
  72. result = ts['2008':'2009']
  73. assert len(result) == 24
  74. result = ts['2008-1':'2009-12']
  75. assert len(result) == 24
  76. result = ts['2008Q1':'2009Q4']
  77. assert len(result) == 24
  78. result = ts[:'2009']
  79. assert len(result) == 36
  80. result = ts['2009':]
  81. assert len(result) == 50 - 24
  82. exp = result
  83. result = ts[24:]
  84. tm.assert_series_equal(exp, result)
  85. ts = ts[10:].append(ts[10:])
  86. msg = "left slice bound for non-unique label: '2008'"
  87. with pytest.raises(KeyError, match=msg):
  88. ts[slice('2008', '2009')]
  89. def test_getitem_datetime(self):
  90. rng = period_range(start='2012-01-01', periods=10, freq='W-MON')
  91. ts = Series(lrange(len(rng)), index=rng)
  92. dt1 = datetime(2011, 10, 2)
  93. dt4 = datetime(2012, 4, 20)
  94. rs = ts[dt1:dt4]
  95. tm.assert_series_equal(rs, ts)
  96. def test_getitem_nat(self):
  97. idx = pd.PeriodIndex(['2011-01', 'NaT', '2011-02'], freq='M')
  98. assert idx[0] == pd.Period('2011-01', freq='M')
  99. assert idx[1] is pd.NaT
  100. s = pd.Series([0, 1, 2], index=idx)
  101. assert s[pd.NaT] == 1
  102. s = pd.Series(idx, index=idx)
  103. assert (s[pd.Period('2011-01', freq='M')] ==
  104. pd.Period('2011-01', freq='M'))
  105. assert s[pd.NaT] is pd.NaT
  106. def test_getitem_list_periods(self):
  107. # GH 7710
  108. rng = period_range(start='2012-01-01', periods=10, freq='D')
  109. ts = Series(lrange(len(rng)), index=rng)
  110. exp = ts.iloc[[1]]
  111. tm.assert_series_equal(ts[[Period('2012-01-02', freq='D')]], exp)
  112. def test_getitem_seconds(self):
  113. # GH#6716
  114. didx = pd.date_range(start='2013/01/01 09:00:00', freq='S',
  115. periods=4000)
  116. pidx = period_range(start='2013/01/01 09:00:00', freq='S',
  117. periods=4000)
  118. for idx in [didx, pidx]:
  119. # getitem against index should raise ValueError
  120. values = ['2014', '2013/02', '2013/01/02', '2013/02/01 9H',
  121. '2013/02/01 09:00']
  122. for v in values:
  123. # GH7116
  124. # these show deprecations as we are trying
  125. # to slice with non-integer indexers
  126. # with pytest.raises(IndexError):
  127. # idx[v]
  128. continue
  129. s = Series(np.random.rand(len(idx)), index=idx)
  130. tm.assert_series_equal(s['2013/01/01 10:00'], s[3600:3660])
  131. tm.assert_series_equal(s['2013/01/01 9H'], s[:3600])
  132. for d in ['2013/01/01', '2013/01', '2013']:
  133. tm.assert_series_equal(s[d], s)
  134. def test_getitem_day(self):
  135. # GH#6716
  136. # Confirm DatetimeIndex and PeriodIndex works identically
  137. didx = pd.date_range(start='2013/01/01', freq='D', periods=400)
  138. pidx = period_range(start='2013/01/01', freq='D', periods=400)
  139. for idx in [didx, pidx]:
  140. # getitem against index should raise ValueError
  141. values = ['2014', '2013/02', '2013/01/02', '2013/02/01 9H',
  142. '2013/02/01 09:00']
  143. for v in values:
  144. # GH7116
  145. # these show deprecations as we are trying
  146. # to slice with non-integer indexers
  147. # with pytest.raises(IndexError):
  148. # idx[v]
  149. continue
  150. s = Series(np.random.rand(len(idx)), index=idx)
  151. tm.assert_series_equal(s['2013/01'], s[0:31])
  152. tm.assert_series_equal(s['2013/02'], s[31:59])
  153. tm.assert_series_equal(s['2014'], s[365:])
  154. invalid = ['2013/02/01 9H', '2013/02/01 09:00']
  155. for v in invalid:
  156. with pytest.raises(KeyError):
  157. s[v]
  158. class TestWhere(object):
  159. @pytest.mark.parametrize('klass', [list, tuple, np.array, Series])
  160. def test_where(self, klass):
  161. i = period_range('20130101', periods=5, freq='D')
  162. cond = [True] * len(i)
  163. expected = i
  164. result = i.where(klass(cond))
  165. tm.assert_index_equal(result, expected)
  166. cond = [False] + [True] * (len(i) - 1)
  167. expected = PeriodIndex([pd.NaT] + i[1:].tolist(), freq='D')
  168. result = i.where(klass(cond))
  169. tm.assert_index_equal(result, expected)
  170. def test_where_other(self):
  171. i = period_range('20130101', periods=5, freq='D')
  172. for arr in [np.nan, pd.NaT]:
  173. result = i.where(notna(i), other=np.nan)
  174. expected = i
  175. tm.assert_index_equal(result, expected)
  176. i2 = i.copy()
  177. i2 = pd.PeriodIndex([pd.NaT, pd.NaT] + i[2:].tolist(),
  178. freq='D')
  179. result = i.where(notna(i2), i2)
  180. tm.assert_index_equal(result, i2)
  181. i2 = i.copy()
  182. i2 = pd.PeriodIndex([pd.NaT, pd.NaT] + i[2:].tolist(),
  183. freq='D')
  184. result = i.where(notna(i2), i2.values)
  185. tm.assert_index_equal(result, i2)
  186. class TestTake(object):
  187. def test_take(self):
  188. # GH#10295
  189. idx1 = pd.period_range('2011-01-01', '2011-01-31', freq='D',
  190. name='idx')
  191. for idx in [idx1]:
  192. result = idx.take([0])
  193. assert result == pd.Period('2011-01-01', freq='D')
  194. result = idx.take([5])
  195. assert result == pd.Period('2011-01-06', freq='D')
  196. result = idx.take([0, 1, 2])
  197. expected = pd.period_range('2011-01-01', '2011-01-03', freq='D',
  198. name='idx')
  199. tm.assert_index_equal(result, expected)
  200. assert result.freq == 'D'
  201. assert result.freq == expected.freq
  202. result = idx.take([0, 2, 4])
  203. expected = pd.PeriodIndex(['2011-01-01', '2011-01-03',
  204. '2011-01-05'], freq='D', name='idx')
  205. tm.assert_index_equal(result, expected)
  206. assert result.freq == expected.freq
  207. assert result.freq == 'D'
  208. result = idx.take([7, 4, 1])
  209. expected = pd.PeriodIndex(['2011-01-08', '2011-01-05',
  210. '2011-01-02'],
  211. freq='D', name='idx')
  212. tm.assert_index_equal(result, expected)
  213. assert result.freq == expected.freq
  214. assert result.freq == 'D'
  215. result = idx.take([3, 2, 5])
  216. expected = PeriodIndex(['2011-01-04', '2011-01-03', '2011-01-06'],
  217. freq='D', name='idx')
  218. tm.assert_index_equal(result, expected)
  219. assert result.freq == expected.freq
  220. assert result.freq == 'D'
  221. result = idx.take([-3, 2, 5])
  222. expected = PeriodIndex(['2011-01-29', '2011-01-03', '2011-01-06'],
  223. freq='D', name='idx')
  224. tm.assert_index_equal(result, expected)
  225. assert result.freq == expected.freq
  226. assert result.freq == 'D'
  227. def test_take_misc(self):
  228. index = period_range(start='1/1/10', end='12/31/12', freq='D',
  229. name='idx')
  230. expected = PeriodIndex([datetime(2010, 1, 6), datetime(2010, 1, 7),
  231. datetime(2010, 1, 9), datetime(2010, 1, 13)],
  232. freq='D', name='idx')
  233. taken1 = index.take([5, 6, 8, 12])
  234. taken2 = index[[5, 6, 8, 12]]
  235. for taken in [taken1, taken2]:
  236. tm.assert_index_equal(taken, expected)
  237. assert isinstance(taken, PeriodIndex)
  238. assert taken.freq == index.freq
  239. assert taken.name == expected.name
  240. def test_take_fill_value(self):
  241. # GH#12631
  242. idx = pd.PeriodIndex(['2011-01-01', '2011-02-01', '2011-03-01'],
  243. name='xxx', freq='D')
  244. result = idx.take(np.array([1, 0, -1]))
  245. expected = pd.PeriodIndex(['2011-02-01', '2011-01-01', '2011-03-01'],
  246. name='xxx', freq='D')
  247. tm.assert_index_equal(result, expected)
  248. # fill_value
  249. result = idx.take(np.array([1, 0, -1]), fill_value=True)
  250. expected = pd.PeriodIndex(['2011-02-01', '2011-01-01', 'NaT'],
  251. name='xxx', freq='D')
  252. tm.assert_index_equal(result, expected)
  253. # allow_fill=False
  254. result = idx.take(np.array([1, 0, -1]), allow_fill=False,
  255. fill_value=True)
  256. expected = pd.PeriodIndex(['2011-02-01', '2011-01-01', '2011-03-01'],
  257. name='xxx', freq='D')
  258. tm.assert_index_equal(result, expected)
  259. msg = ('When allow_fill=True and fill_value is not None, '
  260. 'all indices must be >= -1')
  261. with pytest.raises(ValueError, match=msg):
  262. idx.take(np.array([1, 0, -2]), fill_value=True)
  263. with pytest.raises(ValueError, match=msg):
  264. idx.take(np.array([1, 0, -5]), fill_value=True)
  265. with pytest.raises(IndexError):
  266. idx.take(np.array([1, -5]))
  267. class TestIndexing(object):
  268. def test_get_loc_msg(self):
  269. idx = period_range('2000-1-1', freq='A', periods=10)
  270. bad_period = Period('2012', 'A')
  271. pytest.raises(KeyError, idx.get_loc, bad_period)
  272. try:
  273. idx.get_loc(bad_period)
  274. except KeyError as inst:
  275. assert inst.args[0] == bad_period
  276. def test_get_loc_nat(self):
  277. didx = DatetimeIndex(['2011-01-01', 'NaT', '2011-01-03'])
  278. pidx = PeriodIndex(['2011-01-01', 'NaT', '2011-01-03'], freq='M')
  279. # check DatetimeIndex compat
  280. for idx in [didx, pidx]:
  281. assert idx.get_loc(pd.NaT) == 1
  282. assert idx.get_loc(None) == 1
  283. assert idx.get_loc(float('nan')) == 1
  284. assert idx.get_loc(np.nan) == 1
  285. def test_get_loc(self):
  286. # GH 17717
  287. p0 = pd.Period('2017-09-01')
  288. p1 = pd.Period('2017-09-02')
  289. p2 = pd.Period('2017-09-03')
  290. # get the location of p1/p2 from
  291. # monotonic increasing PeriodIndex with non-duplicate
  292. idx0 = pd.PeriodIndex([p0, p1, p2])
  293. expected_idx1_p1 = 1
  294. expected_idx1_p2 = 2
  295. assert idx0.get_loc(p1) == expected_idx1_p1
  296. assert idx0.get_loc(str(p1)) == expected_idx1_p1
  297. assert idx0.get_loc(p2) == expected_idx1_p2
  298. assert idx0.get_loc(str(p2)) == expected_idx1_p2
  299. msg = "Cannot interpret 'foo' as period"
  300. with pytest.raises(KeyError, match=msg):
  301. idx0.get_loc('foo')
  302. pytest.raises(KeyError, idx0.get_loc, 1.1)
  303. pytest.raises(TypeError, idx0.get_loc, idx0)
  304. # get the location of p1/p2 from
  305. # monotonic increasing PeriodIndex with duplicate
  306. idx1 = pd.PeriodIndex([p1, p1, p2])
  307. expected_idx1_p1 = slice(0, 2)
  308. expected_idx1_p2 = 2
  309. assert idx1.get_loc(p1) == expected_idx1_p1
  310. assert idx1.get_loc(str(p1)) == expected_idx1_p1
  311. assert idx1.get_loc(p2) == expected_idx1_p2
  312. assert idx1.get_loc(str(p2)) == expected_idx1_p2
  313. msg = "Cannot interpret 'foo' as period"
  314. with pytest.raises(KeyError, match=msg):
  315. idx1.get_loc('foo')
  316. pytest.raises(KeyError, idx1.get_loc, 1.1)
  317. pytest.raises(TypeError, idx1.get_loc, idx1)
  318. # get the location of p1/p2 from
  319. # non-monotonic increasing/decreasing PeriodIndex with duplicate
  320. idx2 = pd.PeriodIndex([p2, p1, p2])
  321. expected_idx2_p1 = 1
  322. expected_idx2_p2 = np.array([True, False, True])
  323. assert idx2.get_loc(p1) == expected_idx2_p1
  324. assert idx2.get_loc(str(p1)) == expected_idx2_p1
  325. tm.assert_numpy_array_equal(idx2.get_loc(p2), expected_idx2_p2)
  326. tm.assert_numpy_array_equal(idx2.get_loc(str(p2)), expected_idx2_p2)
  327. def test_is_monotonic_increasing(self):
  328. # GH 17717
  329. p0 = pd.Period('2017-09-01')
  330. p1 = pd.Period('2017-09-02')
  331. p2 = pd.Period('2017-09-03')
  332. idx_inc0 = pd.PeriodIndex([p0, p1, p2])
  333. idx_inc1 = pd.PeriodIndex([p0, p1, p1])
  334. idx_dec0 = pd.PeriodIndex([p2, p1, p0])
  335. idx_dec1 = pd.PeriodIndex([p2, p1, p1])
  336. idx = pd.PeriodIndex([p1, p2, p0])
  337. assert idx_inc0.is_monotonic_increasing is True
  338. assert idx_inc1.is_monotonic_increasing is True
  339. assert idx_dec0.is_monotonic_increasing is False
  340. assert idx_dec1.is_monotonic_increasing is False
  341. assert idx.is_monotonic_increasing is False
  342. def test_is_monotonic_decreasing(self):
  343. # GH 17717
  344. p0 = pd.Period('2017-09-01')
  345. p1 = pd.Period('2017-09-02')
  346. p2 = pd.Period('2017-09-03')
  347. idx_inc0 = pd.PeriodIndex([p0, p1, p2])
  348. idx_inc1 = pd.PeriodIndex([p0, p1, p1])
  349. idx_dec0 = pd.PeriodIndex([p2, p1, p0])
  350. idx_dec1 = pd.PeriodIndex([p2, p1, p1])
  351. idx = pd.PeriodIndex([p1, p2, p0])
  352. assert idx_inc0.is_monotonic_decreasing is False
  353. assert idx_inc1.is_monotonic_decreasing is False
  354. assert idx_dec0.is_monotonic_decreasing is True
  355. assert idx_dec1.is_monotonic_decreasing is True
  356. assert idx.is_monotonic_decreasing is False
  357. def test_contains(self):
  358. # GH 17717
  359. p0 = pd.Period('2017-09-01')
  360. p1 = pd.Period('2017-09-02')
  361. p2 = pd.Period('2017-09-03')
  362. p3 = pd.Period('2017-09-04')
  363. ps0 = [p0, p1, p2]
  364. idx0 = pd.PeriodIndex(ps0)
  365. for p in ps0:
  366. assert idx0.contains(p)
  367. assert p in idx0
  368. assert idx0.contains(str(p))
  369. assert str(p) in idx0
  370. assert idx0.contains('2017-09-01 00:00:01')
  371. assert '2017-09-01 00:00:01' in idx0
  372. assert idx0.contains('2017-09')
  373. assert '2017-09' in idx0
  374. assert not idx0.contains(p3)
  375. assert p3 not in idx0
  376. def test_get_value(self):
  377. # GH 17717
  378. p0 = pd.Period('2017-09-01')
  379. p1 = pd.Period('2017-09-02')
  380. p2 = pd.Period('2017-09-03')
  381. idx0 = pd.PeriodIndex([p0, p1, p2])
  382. input0 = np.array([1, 2, 3])
  383. expected0 = 2
  384. result0 = idx0.get_value(input0, p1)
  385. assert result0 == expected0
  386. idx1 = pd.PeriodIndex([p1, p1, p2])
  387. input1 = np.array([1, 2, 3])
  388. expected1 = np.array([1, 2])
  389. result1 = idx1.get_value(input1, p1)
  390. tm.assert_numpy_array_equal(result1, expected1)
  391. idx2 = pd.PeriodIndex([p1, p2, p1])
  392. input2 = np.array([1, 2, 3])
  393. expected2 = np.array([1, 3])
  394. result2 = idx2.get_value(input2, p1)
  395. tm.assert_numpy_array_equal(result2, expected2)
  396. def test_get_indexer(self):
  397. # GH 17717
  398. p1 = pd.Period('2017-09-01')
  399. p2 = pd.Period('2017-09-04')
  400. p3 = pd.Period('2017-09-07')
  401. tp0 = pd.Period('2017-08-31')
  402. tp1 = pd.Period('2017-09-02')
  403. tp2 = pd.Period('2017-09-05')
  404. tp3 = pd.Period('2017-09-09')
  405. idx = pd.PeriodIndex([p1, p2, p3])
  406. tm.assert_numpy_array_equal(idx.get_indexer(idx),
  407. np.array([0, 1, 2], dtype=np.intp))
  408. target = pd.PeriodIndex([tp0, tp1, tp2, tp3])
  409. tm.assert_numpy_array_equal(idx.get_indexer(target, 'pad'),
  410. np.array([-1, 0, 1, 2], dtype=np.intp))
  411. tm.assert_numpy_array_equal(idx.get_indexer(target, 'backfill'),
  412. np.array([0, 1, 2, -1], dtype=np.intp))
  413. tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest'),
  414. np.array([0, 0, 1, 2], dtype=np.intp))
  415. res = idx.get_indexer(target, 'nearest',
  416. tolerance=pd.Timedelta('1 day'))
  417. tm.assert_numpy_array_equal(res,
  418. np.array([0, 0, 1, -1], dtype=np.intp))
  419. def test_get_indexer_non_unique(self):
  420. # GH 17717
  421. p1 = pd.Period('2017-09-02')
  422. p2 = pd.Period('2017-09-03')
  423. p3 = pd.Period('2017-09-04')
  424. p4 = pd.Period('2017-09-05')
  425. idx1 = pd.PeriodIndex([p1, p2, p1])
  426. idx2 = pd.PeriodIndex([p2, p1, p3, p4])
  427. result = idx1.get_indexer_non_unique(idx2)
  428. expected_indexer = np.array([1, 0, 2, -1, -1], dtype=np.intp)
  429. expected_missing = np.array([2, 3], dtype=np.int64)
  430. tm.assert_numpy_array_equal(result[0], expected_indexer)
  431. tm.assert_numpy_array_equal(result[1], expected_missing)
  432. # TODO: This method came from test_period; de-dup with version above
  433. def test_get_loc2(self):
  434. idx = pd.period_range('2000-01-01', periods=3)
  435. for method in [None, 'pad', 'backfill', 'nearest']:
  436. assert idx.get_loc(idx[1], method) == 1
  437. assert idx.get_loc(idx[1].asfreq('H', how='start'), method) == 1
  438. assert idx.get_loc(idx[1].to_timestamp(), method) == 1
  439. assert idx.get_loc(idx[1].to_timestamp()
  440. .to_pydatetime(), method) == 1
  441. assert idx.get_loc(str(idx[1]), method) == 1
  442. idx = pd.period_range('2000-01-01', periods=5)[::2]
  443. assert idx.get_loc('2000-01-02T12', method='nearest',
  444. tolerance='1 day') == 1
  445. assert idx.get_loc('2000-01-02T12', method='nearest',
  446. tolerance=pd.Timedelta('1D')) == 1
  447. assert idx.get_loc('2000-01-02T12', method='nearest',
  448. tolerance=np.timedelta64(1, 'D')) == 1
  449. assert idx.get_loc('2000-01-02T12', method='nearest',
  450. tolerance=timedelta(1)) == 1
  451. msg = 'unit abbreviation w/o a number'
  452. with pytest.raises(ValueError, match=msg):
  453. idx.get_loc('2000-01-10', method='nearest', tolerance='foo')
  454. msg = 'Input has different freq=None from PeriodArray\\(freq=D\\)'
  455. with pytest.raises(ValueError, match=msg):
  456. idx.get_loc('2000-01-10', method='nearest', tolerance='1 hour')
  457. with pytest.raises(KeyError):
  458. idx.get_loc('2000-01-10', method='nearest', tolerance='1 day')
  459. with pytest.raises(
  460. ValueError,
  461. match='list-like tolerance size must match target index size'):
  462. idx.get_loc('2000-01-10', method='nearest',
  463. tolerance=[pd.Timedelta('1 day').to_timedelta64(),
  464. pd.Timedelta('1 day').to_timedelta64()])
  465. # TODO: This method came from test_period; de-dup with version above
  466. def test_get_indexer2(self):
  467. idx = pd.period_range('2000-01-01', periods=3).asfreq('H', how='start')
  468. tm.assert_numpy_array_equal(idx.get_indexer(idx),
  469. np.array([0, 1, 2], dtype=np.intp))
  470. target = pd.PeriodIndex(['1999-12-31T23', '2000-01-01T12',
  471. '2000-01-02T01'], freq='H')
  472. tm.assert_numpy_array_equal(idx.get_indexer(target, 'pad'),
  473. np.array([-1, 0, 1], dtype=np.intp))
  474. tm.assert_numpy_array_equal(idx.get_indexer(target, 'backfill'),
  475. np.array([0, 1, 2], dtype=np.intp))
  476. tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest'),
  477. np.array([0, 1, 1], dtype=np.intp))
  478. tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest',
  479. tolerance='1 hour'),
  480. np.array([0, -1, 1], dtype=np.intp))
  481. msg = 'Input has different freq=None from PeriodArray\\(freq=H\\)'
  482. with pytest.raises(ValueError, match=msg):
  483. idx.get_indexer(target, 'nearest', tolerance='1 minute')
  484. tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest',
  485. tolerance='1 day'),
  486. np.array([0, 1, 1], dtype=np.intp))
  487. tol_raw = [pd.Timedelta('1 hour'),
  488. pd.Timedelta('1 hour'),
  489. np.timedelta64(1, 'D'), ]
  490. tm.assert_numpy_array_equal(
  491. idx.get_indexer(target, 'nearest',
  492. tolerance=[np.timedelta64(x) for x in tol_raw]),
  493. np.array([0, -1, 1], dtype=np.intp))
  494. tol_bad = [pd.Timedelta('2 hour').to_timedelta64(),
  495. pd.Timedelta('1 hour').to_timedelta64(),
  496. np.timedelta64(1, 'M'), ]
  497. with pytest.raises(
  498. libperiod.IncompatibleFrequency,
  499. match='Input has different freq=None from'):
  500. idx.get_indexer(target, 'nearest', tolerance=tol_bad)
  501. def test_indexing(self):
  502. # GH 4390, iat incorrectly indexing
  503. index = period_range('1/1/2001', periods=10)
  504. s = Series(np.random.randn(10), index=index)
  505. expected = s[index[0]]
  506. result = s.iat[0]
  507. assert expected == result
  508. def test_period_index_indexer(self):
  509. # GH4125
  510. idx = pd.period_range('2002-01', '2003-12', freq='M')
  511. df = pd.DataFrame(pd.np.random.randn(24, 10), index=idx)
  512. tm.assert_frame_equal(df, df.loc[idx])
  513. tm.assert_frame_equal(df, df.loc[list(idx)])
  514. tm.assert_frame_equal(df, df.loc[list(idx)])
  515. tm.assert_frame_equal(df.iloc[0:5], df.loc[idx[0:5]])
  516. tm.assert_frame_equal(df, df.loc[list(idx)])