Skip to content

Commit 0635b88

Browse files
committed
TST: Add PeriodIndex/PeriodEngine tests
1 parent 6bab80a commit 0635b88

File tree

2 files changed

+226
-1
lines changed

2 files changed

+226
-1
lines changed

pandas/tests/indexes/period/test_indexing.py

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

pandas/tests/series/test_period.py

Lines changed: 30 additions & 0 deletions
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)