@@ -344,6 +344,115 @@ def test_take_fill_value_with_timezone(self):
344344 idx .take (np .array ([1 , - 5 ]))
345345
346346
347+ class TestGetLoc :
348+ @pytest .mark .parametrize ("method" , [None , "pad" , "backfill" , "nearest" ])
349+ def test_get_loc_method_exact_match (self , method ):
350+ idx = pd .date_range ("2000-01-01" , periods = 3 )
351+ assert idx .get_loc (idx [1 ], method ) == 1
352+ assert idx .get_loc (idx [1 ].to_pydatetime (), method ) == 1
353+ assert idx .get_loc (str (idx [1 ]), method ) == 1
354+
355+ if method is not None :
356+ assert idx .get_loc (idx [1 ], method , tolerance = pd .Timedelta ("0 days" )) == 1
357+
358+ def test_get_loc (self ):
359+ idx = pd .date_range ("2000-01-01" , periods = 3 )
360+
361+ assert idx .get_loc ("2000-01-01" , method = "nearest" ) == 0
362+ assert idx .get_loc ("2000-01-01T12" , method = "nearest" ) == 1
363+
364+ assert idx .get_loc ("2000-01-01T12" , method = "nearest" , tolerance = "1 day" ) == 1
365+ assert (
366+ idx .get_loc ("2000-01-01T12" , method = "nearest" , tolerance = pd .Timedelta ("1D" ))
367+ == 1
368+ )
369+ assert (
370+ idx .get_loc (
371+ "2000-01-01T12" , method = "nearest" , tolerance = np .timedelta64 (1 , "D" )
372+ )
373+ == 1
374+ )
375+ assert (
376+ idx .get_loc ("2000-01-01T12" , method = "nearest" , tolerance = timedelta (1 )) == 1
377+ )
378+ with pytest .raises (ValueError , match = "unit abbreviation w/o a number" ):
379+ idx .get_loc ("2000-01-01T12" , method = "nearest" , tolerance = "foo" )
380+ with pytest .raises (KeyError , match = "'2000-01-01T03'" ):
381+ idx .get_loc ("2000-01-01T03" , method = "nearest" , tolerance = "2 hours" )
382+ with pytest .raises (
383+ ValueError , match = "tolerance size must match target index size"
384+ ):
385+ idx .get_loc (
386+ "2000-01-01" ,
387+ method = "nearest" ,
388+ tolerance = [
389+ pd .Timedelta ("1day" ).to_timedelta64 (),
390+ pd .Timedelta ("1day" ).to_timedelta64 (),
391+ ],
392+ )
393+
394+ assert idx .get_loc ("2000" , method = "nearest" ) == slice (0 , 3 )
395+ assert idx .get_loc ("2000-01" , method = "nearest" ) == slice (0 , 3 )
396+
397+ assert idx .get_loc ("1999" , method = "nearest" ) == 0
398+ assert idx .get_loc ("2001" , method = "nearest" ) == 2
399+
400+ with pytest .raises (KeyError , match = "'1999'" ):
401+ idx .get_loc ("1999" , method = "pad" )
402+ with pytest .raises (KeyError , match = "'2001'" ):
403+ idx .get_loc ("2001" , method = "backfill" )
404+
405+ with pytest .raises (KeyError , match = "'foobar'" ):
406+ idx .get_loc ("foobar" )
407+ with pytest .raises (InvalidIndexError , match = r"slice\(None, 2, None\)" ):
408+ idx .get_loc (slice (2 ))
409+
410+ idx = pd .to_datetime (["2000-01-01" , "2000-01-04" ])
411+ assert idx .get_loc ("2000-01-02" , method = "nearest" ) == 0
412+ assert idx .get_loc ("2000-01-03" , method = "nearest" ) == 1
413+ assert idx .get_loc ("2000-01" , method = "nearest" ) == slice (0 , 2 )
414+
415+ # time indexing
416+ idx = pd .date_range ("2000-01-01" , periods = 24 , freq = "H" )
417+ tm .assert_numpy_array_equal (
418+ idx .get_loc (time (12 )), np .array ([12 ]), check_dtype = False
419+ )
420+ tm .assert_numpy_array_equal (
421+ idx .get_loc (time (12 , 30 )), np .array ([]), check_dtype = False
422+ )
423+ with pytest .raises (NotImplementedError ):
424+ idx .get_loc (time (12 , 30 ), method = "pad" )
425+
426+ def test_get_loc_nat (self ):
427+ # GH#20464
428+ index = DatetimeIndex (["1/3/2000" , "NaT" ])
429+ assert index .get_loc (pd .NaT ) == 1
430+
431+ assert index .get_loc (None ) == 1
432+
433+ assert index .get_loc (np .nan ) == 1
434+
435+ assert index .get_loc (pd .NA ) == 1
436+
437+ assert index .get_loc (np .datetime64 ("NaT" )) == 1
438+
439+ with pytest .raises (KeyError , match = "NaT" ):
440+ index .get_loc (np .timedelta64 ("NaT" ))
441+
442+ @pytest .mark .parametrize ("key" , [pd .Timedelta (0 ), pd .Timedelta (1 ), timedelta (0 )])
443+ def test_get_loc_timedelta_invalid_key (self , key ):
444+ # GH#20464
445+ dti = pd .date_range ("1970-01-01" , periods = 10 )
446+ with pytest .raises (TypeError ):
447+ dti .get_loc (key )
448+
449+ def test_get_loc_reasonable_key_error (self ):
450+ # GH#1062
451+ index = DatetimeIndex (["1/3/2000" ])
452+ with pytest .raises (KeyError , match = "2000" ):
453+ index .get_loc ("1/1/2000" )
454+
455+
347456class TestDatetimeIndex :
348457 @pytest .mark .parametrize (
349458 "null" , [None , np .nan , np .datetime64 ("NaT" ), pd .NaT , pd .NA ]
@@ -639,84 +748,6 @@ def test_get_value(self):
639748 result = dti .get_value (ser , key .to_datetime64 ())
640749 assert result == 7
641750
642- def test_get_loc (self ):
643- idx = pd .date_range ("2000-01-01" , periods = 3 )
644-
645- for method in [None , "pad" , "backfill" , "nearest" ]:
646- assert idx .get_loc (idx [1 ], method ) == 1
647- assert idx .get_loc (idx [1 ].to_pydatetime (), method ) == 1
648- assert idx .get_loc (str (idx [1 ]), method ) == 1
649-
650- if method is not None :
651- assert (
652- idx .get_loc (idx [1 ], method , tolerance = pd .Timedelta ("0 days" )) == 1
653- )
654-
655- assert idx .get_loc ("2000-01-01" , method = "nearest" ) == 0
656- assert idx .get_loc ("2000-01-01T12" , method = "nearest" ) == 1
657-
658- assert idx .get_loc ("2000-01-01T12" , method = "nearest" , tolerance = "1 day" ) == 1
659- assert (
660- idx .get_loc ("2000-01-01T12" , method = "nearest" , tolerance = pd .Timedelta ("1D" ))
661- == 1
662- )
663- assert (
664- idx .get_loc (
665- "2000-01-01T12" , method = "nearest" , tolerance = np .timedelta64 (1 , "D" )
666- )
667- == 1
668- )
669- assert (
670- idx .get_loc ("2000-01-01T12" , method = "nearest" , tolerance = timedelta (1 )) == 1
671- )
672- with pytest .raises (ValueError , match = "unit abbreviation w/o a number" ):
673- idx .get_loc ("2000-01-01T12" , method = "nearest" , tolerance = "foo" )
674- with pytest .raises (KeyError , match = "'2000-01-01T03'" ):
675- idx .get_loc ("2000-01-01T03" , method = "nearest" , tolerance = "2 hours" )
676- with pytest .raises (
677- ValueError , match = "tolerance size must match target index size"
678- ):
679- idx .get_loc (
680- "2000-01-01" ,
681- method = "nearest" ,
682- tolerance = [
683- pd .Timedelta ("1day" ).to_timedelta64 (),
684- pd .Timedelta ("1day" ).to_timedelta64 (),
685- ],
686- )
687-
688- assert idx .get_loc ("2000" , method = "nearest" ) == slice (0 , 3 )
689- assert idx .get_loc ("2000-01" , method = "nearest" ) == slice (0 , 3 )
690-
691- assert idx .get_loc ("1999" , method = "nearest" ) == 0
692- assert idx .get_loc ("2001" , method = "nearest" ) == 2
693-
694- with pytest .raises (KeyError , match = "'1999'" ):
695- idx .get_loc ("1999" , method = "pad" )
696- with pytest .raises (KeyError , match = "'2001'" ):
697- idx .get_loc ("2001" , method = "backfill" )
698-
699- with pytest .raises (KeyError , match = "'foobar'" ):
700- idx .get_loc ("foobar" )
701- with pytest .raises (InvalidIndexError , match = r"slice\(None, 2, None\)" ):
702- idx .get_loc (slice (2 ))
703-
704- idx = pd .to_datetime (["2000-01-01" , "2000-01-04" ])
705- assert idx .get_loc ("2000-01-02" , method = "nearest" ) == 0
706- assert idx .get_loc ("2000-01-03" , method = "nearest" ) == 1
707- assert idx .get_loc ("2000-01" , method = "nearest" ) == slice (0 , 2 )
708-
709- # time indexing
710- idx = pd .date_range ("2000-01-01" , periods = 24 , freq = "H" )
711- tm .assert_numpy_array_equal (
712- idx .get_loc (time (12 )), np .array ([12 ]), check_dtype = False
713- )
714- tm .assert_numpy_array_equal (
715- idx .get_loc (time (12 , 30 )), np .array ([]), check_dtype = False
716- )
717- with pytest .raises (NotImplementedError ):
718- idx .get_loc (time (12 , 30 ), method = "pad" )
719-
720751 def test_get_indexer (self ):
721752 idx = pd .date_range ("2000-01-01" , periods = 3 )
722753 exp = np .array ([0 , 1 , 2 ], dtype = np .intp )
@@ -756,32 +787,3 @@ def test_get_indexer(self):
756787 idx .get_indexer (target , "nearest" , tolerance = tol_bad )
757788 with pytest .raises (ValueError ):
758789 idx .get_indexer (idx [[0 ]], method = "nearest" , tolerance = "foo" )
759-
760- def test_reasonable_key_error (self ):
761- # GH#1062
762- index = DatetimeIndex (["1/3/2000" ])
763- with pytest .raises (KeyError , match = "2000" ):
764- index .get_loc ("1/1/2000" )
765-
766- @pytest .mark .parametrize ("key" , [pd .Timedelta (0 ), pd .Timedelta (1 ), timedelta (0 )])
767- def test_timedelta_invalid_key (self , key ):
768- # GH#20464
769- dti = pd .date_range ("1970-01-01" , periods = 10 )
770- with pytest .raises (TypeError ):
771- dti .get_loc (key )
772-
773- def test_get_loc_nat (self ):
774- # GH#20464
775- index = DatetimeIndex (["1/3/2000" , "NaT" ])
776- assert index .get_loc (pd .NaT ) == 1
777-
778- assert index .get_loc (None ) == 1
779-
780- assert index .get_loc (np .nan ) == 1
781-
782- assert index .get_loc (pd .NA ) == 1
783-
784- assert index .get_loc (np .datetime64 ("NaT" )) == 1
785-
786- with pytest .raises (KeyError , match = "NaT" ):
787- index .get_loc (np .timedelta64 ("NaT" ))
0 commit comments