Skip to content

Commit f01bd95

Browse files
committed
TST: Add PeriodIndex/PeriodEngine tests
1 parent aa3f9e7 commit f01bd95

File tree

2 files changed

+220
-1
lines changed

2 files changed

+220
-1
lines changed

pandas/tests/indexes/period/test_indexing.py

+190-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import pytest
44

55
import numpy as np
6+
from numpy import testing as ntm
67
import pandas as pd
78
from pandas.util import testing as tm
89
from pandas.compat import lrange
9-
from pandas._libs import tslib
10+
from pandas._libs import tslib, tslibs
1011
from pandas import (PeriodIndex, Series, DatetimeIndex,
1112
period_range, Period)
1213

@@ -310,3 +311,191 @@ def test_take_fill_value(self):
310311

311312
with pytest.raises(IndexError):
312313
idx.take(np.array([1, -5]))
314+
315+
def test_get_loc(self):
316+
# GH 17717
317+
p0 = pd.Period('2017-09-01')
318+
p1 = pd.Period('2017-09-02')
319+
p2 = pd.Period('2017-09-03')
320+
321+
idx0 = pd.PeriodIndex([p0, p1, p2])
322+
expected_idx1_p1 = 1
323+
expected_idx1_p2 = 2
324+
325+
assert idx0.get_loc(p1) == expected_idx1_p1
326+
assert idx0.get_loc(str(p1)) == expected_idx1_p1
327+
assert idx0.get_loc(p2) == expected_idx1_p2
328+
assert idx0.get_loc(str(p2)) == expected_idx1_p2
329+
330+
pytest.raises(tslibs.parsing.DateParseError, idx0.get_loc, 'foo')
331+
pytest.raises(KeyError, idx0.get_loc, 1.1)
332+
pytest.raises(TypeError, idx0.get_loc, idx0)
333+
334+
idx1 = pd.PeriodIndex([p1, p1, p2])
335+
expected_idx1_p1 = slice(0, 2)
336+
expected_idx1_p2 = 2
337+
338+
assert idx1.get_loc(p1) == expected_idx1_p1
339+
assert idx1.get_loc(str(p1)) == expected_idx1_p1
340+
assert idx1.get_loc(p2) == expected_idx1_p2
341+
assert idx1.get_loc(str(p2)) == expected_idx1_p2
342+
343+
pytest.raises(tslibs.parsing.DateParseError, idx1.get_loc, 'foo')
344+
pytest.raises(KeyError, idx1.get_loc, 1.1)
345+
pytest.raises(TypeError, idx1.get_loc, idx1)
346+
347+
idx2 = pd.PeriodIndex([p2, p1, p2])
348+
expected_idx2_p1 = 1
349+
expected_idx2_p2 = np.array([True, False, True])
350+
351+
assert idx2.get_loc(p1) == expected_idx2_p1
352+
assert idx2.get_loc(str(p1)) == expected_idx2_p1
353+
ntm.assert_array_equal(idx2.get_loc(p2), expected_idx2_p2)
354+
ntm.assert_array_equal(idx2.get_loc(str(p2)), expected_idx2_p2)
355+
356+
def test_is_monotonic_increasing(self):
357+
# GH 17717
358+
p0 = pd.Period('2017-09-01')
359+
p1 = pd.Period('2017-09-02')
360+
p2 = pd.Period('2017-09-03')
361+
362+
idx_inc0 = pd.PeriodIndex([p0, p1, p2])
363+
idx_inc1 = pd.PeriodIndex([p0, p1, p1])
364+
idx_dec0 = pd.PeriodIndex([p2, p1, p0])
365+
idx_dec1 = pd.PeriodIndex([p2, p1, p1])
366+
idx = pd.PeriodIndex([p1, p2, p0])
367+
368+
assert idx_inc0.is_monotonic_increasing
369+
assert idx_inc1.is_monotonic_increasing
370+
assert not idx_dec0.is_monotonic_increasing
371+
assert not idx_dec1.is_monotonic_increasing
372+
assert not idx.is_monotonic_increasing
373+
374+
def test_is_monotonic_decreasing(self):
375+
# GH 17717
376+
p0 = pd.Period('2017-09-01')
377+
p1 = pd.Period('2017-09-02')
378+
p2 = pd.Period('2017-09-03')
379+
380+
idx_inc0 = pd.PeriodIndex([p0, p1, p2])
381+
idx_inc1 = pd.PeriodIndex([p0, p1, p1])
382+
idx_dec0 = pd.PeriodIndex([p2, p1, p0])
383+
idx_dec1 = pd.PeriodIndex([p2, p1, p1])
384+
idx = pd.PeriodIndex([p1, p2, p0])
385+
386+
assert not idx_inc0.is_monotonic_decreasing
387+
assert not idx_inc1.is_monotonic_decreasing
388+
assert idx_dec0.is_monotonic_decreasing
389+
assert idx_dec1.is_monotonic_decreasing
390+
assert not idx.is_monotonic_decreasing
391+
392+
def test_is_unique(self):
393+
# GH 17717
394+
p0 = pd.Period('2017-09-01')
395+
p1 = pd.Period('2017-09-02')
396+
p2 = pd.Period('2017-09-03')
397+
398+
idx0 = pd.PeriodIndex([p0, p1, p2])
399+
assert idx0.is_unique
400+
401+
idx1 = pd.PeriodIndex([p1, p1, p2])
402+
assert not idx1.is_unique
403+
404+
def test_contains(self):
405+
# GH 17717
406+
p0 = pd.Period('2017-09-01')
407+
p1 = pd.Period('2017-09-02')
408+
p2 = pd.Period('2017-09-03')
409+
p3 = pd.Period('2017-09-04')
410+
411+
ps0 = [p0, p1, p2]
412+
idx0 = pd.PeriodIndex(ps0)
413+
414+
for p in ps0:
415+
assert idx0.contains(p)
416+
assert p in idx0
417+
418+
assert idx0.contains(str(p))
419+
assert str(p) in idx0
420+
421+
assert idx0.contains('2017-09-01 00:00:01')
422+
assert '2017-09-01 00:00:01' in idx0
423+
424+
assert idx0.contains('2017-09')
425+
assert '2017-09' in idx0
426+
427+
assert not idx0.contains(p3)
428+
assert p3 not in idx0
429+
430+
def test_get_value(self):
431+
# GH 17717
432+
p0 = pd.Period('2017-09-01')
433+
p1 = pd.Period('2017-09-02')
434+
p2 = pd.Period('2017-09-03')
435+
436+
idx0 = pd.PeriodIndex([p0, p1, p2])
437+
input0 = np.array([1, 2, 3])
438+
expected0 = 2
439+
440+
result0 = idx0.get_value(input0, p1)
441+
assert result0 == expected0
442+
443+
idx1 = pd.PeriodIndex([p1, p1, p2])
444+
input1 = np.array([1, 2, 3])
445+
expected1 = np.array([1, 2])
446+
447+
result1 = idx1.get_value(input1, p1)
448+
tm.assert_numpy_array_equal(result1, expected1)
449+
450+
idx2 = pd.PeriodIndex([p1, p2, p1])
451+
input2 = np.array([1, 2, 3])
452+
expected2 = np.array([1, 3])
453+
454+
result2 = idx2.get_value(input2, p1)
455+
tm.assert_numpy_array_equal(result2, expected2)
456+
457+
def test_get_indexer(self):
458+
# GH 17717
459+
p1 = pd.Period('2017-09-01')
460+
p2 = pd.Period('2017-09-04')
461+
p3 = pd.Period('2017-09-07')
462+
463+
tp0 = pd.Period('2017-08-31')
464+
tp1 = pd.Period('2017-09-02')
465+
tp2 = pd.Period('2017-09-05')
466+
tp3 = pd.Period('2017-09-09')
467+
468+
idx = pd.PeriodIndex([p1, p2, p3])
469+
470+
tm.assert_numpy_array_equal(idx.get_indexer(idx),
471+
np.array([0, 1, 2], dtype=np.intp))
472+
473+
target = pd.PeriodIndex([tp0, tp1, tp2, tp3])
474+
tm.assert_numpy_array_equal(idx.get_indexer(target, 'pad'),
475+
np.array([-1, 0, 1, 2], dtype=np.intp))
476+
tm.assert_numpy_array_equal(idx.get_indexer(target, 'backfill'),
477+
np.array([0, 1, 2, -1], dtype=np.intp))
478+
tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest'),
479+
np.array([0, 0, 1, 2], dtype=np.intp))
480+
481+
res = idx.get_indexer(target, 'nearest',
482+
tolerance=pd.Timedelta('1 day'))
483+
tm.assert_numpy_array_equal(res,
484+
np.array([0, 0, 1, -1], dtype=np.intp))
485+
486+
def test_get_indexer_non_unique(self):
487+
# GH 17717
488+
p1 = pd.Period('2017-09-02')
489+
p2 = pd.Period('2017-09-03')
490+
p3 = pd.Period('2017-09-04')
491+
p4 = pd.Period('2017-09-05')
492+
493+
idx1 = pd.PeriodIndex([p1, p2, p1])
494+
idx2 = pd.PeriodIndex([p2, p1, p3, p4])
495+
496+
result = idx1.get_indexer_non_unique(idx2)
497+
expected_indexer = np.array([1, 0, 2, -1, -1], dtype=np.int64)
498+
expected_missing = np.array([2, 3], dtype=np.int64)
499+
500+
tm.assert_numpy_array_equal(result[0], expected_indexer)
501+
tm.assert_numpy_array_equal(result[1], expected_missing)

pandas/tests/series/test_period.py

+30
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,33 @@ def test_align_series(self):
249249
msg = "Input has different freq=D from PeriodIndex\\(freq=A-DEC\\)"
250250
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
251251
ts + ts.asfreq('D', how="end")
252+
253+
def test_truncate(self):
254+
# GH 17717
255+
idx1 = pd.PeriodIndex([
256+
pd.Period('2017-09-02'),
257+
pd.Period('2017-09-02'),
258+
pd.Period('2017-09-03')
259+
])
260+
series1 = pd.Series([1, 2, 3], index=idx1)
261+
result1 = series1.truncate(after='2017-09-02')
262+
263+
expected_idx1 = pd.PeriodIndex([
264+
pd.Period('2017-09-02'),
265+
pd.Period('2017-09-02')
266+
])
267+
tm.assert_series_equal(result1, pd.Series([1, 2], index=expected_idx1))
268+
269+
idx2 = pd.PeriodIndex([
270+
pd.Period('2017-09-03'),
271+
pd.Period('2017-09-02'),
272+
pd.Period('2017-09-03')
273+
])
274+
series2 = pd.Series([1, 2, 3], index=idx2)
275+
result2 = series2.truncate(after='2017-09-02')
276+
277+
expected_idx2 = pd.PeriodIndex([
278+
pd.Period('2017-09-03'),
279+
pd.Period('2017-09-02')
280+
])
281+
tm.assert_series_equal(result2, pd.Series([1, 2], index=expected_idx2))

0 commit comments

Comments
 (0)