1
1
from datetime import datetime , timedelta
2
+ import re
2
3
3
4
import numpy as np
4
5
import pytest
8
9
import pandas as pd
9
10
from pandas import DatetimeIndex , Period , PeriodIndex , Series , notna , period_range
10
11
import pandas ._testing as tm
12
+ from pandas .core .indexes .base import InvalidIndexError
11
13
12
14
13
15
class TestGetItem :
@@ -408,11 +410,7 @@ def test_get_loc(self):
408
410
with pytest .raises (KeyError , match = r"^1\.1$" ):
409
411
idx0 .get_loc (1.1 )
410
412
411
- msg = (
412
- r"'PeriodIndex\(\['2017-09-01', '2017-09-02', '2017-09-03'\], "
413
- r"dtype='period\[D\]', freq='D'\)' is an invalid key"
414
- )
415
- with pytest .raises (TypeError , match = msg ):
413
+ with pytest .raises (InvalidIndexError , match = re .escape (str (idx0 ))):
416
414
idx0 .get_loc (idx0 )
417
415
418
416
# get the location of p1/p2 from
@@ -433,11 +431,7 @@ def test_get_loc(self):
433
431
with pytest .raises (KeyError , match = r"^1\.1$" ):
434
432
idx1 .get_loc (1.1 )
435
433
436
- msg = (
437
- r"'PeriodIndex\(\['2017-09-02', '2017-09-02', '2017-09-03'\], "
438
- r"dtype='period\[D\]', freq='D'\)' is an invalid key"
439
- )
440
- with pytest .raises (TypeError , match = msg ):
434
+ with pytest .raises (InvalidIndexError , match = re .escape (str (idx1 ))):
441
435
idx1 .get_loc (idx1 )
442
436
443
437
# get the location of p1/p2 from
@@ -461,16 +455,46 @@ def test_get_loc_integer(self):
461
455
with pytest .raises (KeyError , match = "46" ):
462
456
pi2 .get_loc (46 )
463
457
458
+ @pytest .mark .parametrize ("freq" , ["H" , "D" ])
459
+ def test_get_value_datetime_hourly (self , freq ):
460
+ # get_loc and get_value should treat datetime objects symmetrically
461
+ dti = pd .date_range ("2016-01-01" , periods = 3 , freq = "MS" )
462
+ pi = dti .to_period (freq )
463
+ ser = pd .Series (range (7 , 10 ), index = pi )
464
+
465
+ ts = dti [0 ]
466
+
467
+ assert pi .get_loc (ts ) == 0
468
+ assert pi .get_value (ser , ts ) == 7
469
+ assert ser [ts ] == 7
470
+ assert ser .loc [ts ] == 7
471
+
472
+ ts2 = ts + pd .Timedelta (hours = 3 )
473
+ if freq == "H" :
474
+ with pytest .raises (KeyError , match = "2016-01-01 03:00" ):
475
+ pi .get_loc (ts2 )
476
+ with pytest .raises (KeyError , match = "2016-01-01 03:00" ):
477
+ pi .get_value (ser , ts2 )
478
+ with pytest .raises (KeyError , match = "2016-01-01 03:00" ):
479
+ ser [ts2 ]
480
+ with pytest .raises (KeyError , match = "2016-01-01 03:00" ):
481
+ ser .loc [ts2 ]
482
+ else :
483
+ assert pi .get_loc (ts2 ) == 0
484
+ assert pi .get_value (ser , ts2 ) == 7
485
+ assert ser [ts2 ] == 7
486
+ assert ser .loc [ts2 ] == 7
487
+
464
488
def test_get_value_integer (self ):
465
489
dti = pd .date_range ("2016-01-01" , periods = 3 )
466
490
pi = dti .to_period ("D" )
467
491
ser = pd .Series (range (3 ), index = pi )
468
- with pytest .raises (IndexError , match = "is out of bounds for axis 0 with size 3 " ):
492
+ with pytest .raises (IndexError , match = "index out of bounds" ):
469
493
pi .get_value (ser , 16801 )
470
494
471
495
pi2 = dti .to_period ("Y" ) # duplicates, ordinals are all 46
472
496
ser2 = pd .Series (range (3 ), index = pi2 )
473
- with pytest .raises (IndexError , match = "is out of bounds for axis 0 with size 3 " ):
497
+ with pytest .raises (IndexError , match = "index out of bounds" ):
474
498
pi2 .get_value (ser2 , 46 )
475
499
476
500
def test_is_monotonic_increasing (self ):
@@ -544,25 +568,25 @@ def test_get_value(self):
544
568
p2 = pd .Period ("2017-09-03" )
545
569
546
570
idx0 = pd .PeriodIndex ([p0 , p1 , p2 ])
547
- input0 = np .array ([1 , 2 , 3 ])
571
+ input0 = pd . Series ( np .array ([1 , 2 , 3 ]), index = idx0 )
548
572
expected0 = 2
549
573
550
574
result0 = idx0 .get_value (input0 , p1 )
551
575
assert result0 == expected0
552
576
553
577
idx1 = pd .PeriodIndex ([p1 , p1 , p2 ])
554
- input1 = np .array ([1 , 2 , 3 ])
555
- expected1 = np . array ([ 1 , 2 ])
578
+ input1 = pd . Series ( np .array ([1 , 2 , 3 ]), index = idx1 )
579
+ expected1 = input1 . iloc [[ 0 , 1 ]]
556
580
557
581
result1 = idx1 .get_value (input1 , p1 )
558
- tm .assert_numpy_array_equal (result1 , expected1 )
582
+ tm .assert_series_equal (result1 , expected1 )
559
583
560
584
idx2 = pd .PeriodIndex ([p1 , p2 , p1 ])
561
- input2 = np .array ([1 , 2 , 3 ])
562
- expected2 = np . array ([ 1 , 3 ])
585
+ input2 = pd . Series ( np .array ([1 , 2 , 3 ]), index = idx2 )
586
+ expected2 = input2 . iloc [[ 0 , 2 ]]
563
587
564
588
result2 = idx2 .get_value (input2 , p1 )
565
- tm .assert_numpy_array_equal (result2 , expected2 )
589
+ tm .assert_series_equal (result2 , expected2 )
566
590
567
591
def test_get_indexer (self ):
568
592
# GH 17717
0 commit comments