@@ -314,9 +314,23 @@ def test_take_fill_value(self):
314
314
315
315
def test_get_loc (self ):
316
316
# GH 17717
317
+ p0 = pd .Period ('2017-09-01' )
317
318
p1 = pd .Period ('2017-09-02' )
318
319
p2 = pd .Period ('2017-09-03' )
319
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
+
320
334
idx1 = pd .PeriodIndex ([p1 , p1 , p2 ])
321
335
expected_idx1_p1 = slice (0 , 2 )
322
336
expected_idx1_p2 = 2
@@ -338,3 +352,150 @@ def test_get_loc(self):
338
352
assert idx2 .get_loc (str (p1 )) == expected_idx2_p1
339
353
ntm .assert_array_equal (idx2 .get_loc (p2 ), expected_idx2_p2 )
340
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 ])
498
+ expected_missing = np .array ([2 , 3 ])
499
+
500
+ tm .assert_numpy_array_equal (result [0 ], expected_indexer )
501
+ tm .assert_numpy_array_equal (result [1 ], expected_missing )
0 commit comments