@@ -25,6 +25,13 @@ def test_ellipsis(self):
25
25
assert result .equals (idx )
26
26
assert result is not idx
27
27
28
+ def test_getitem_slice_keeps_name (self ):
29
+ # GH4226
30
+ st = pd .Timestamp ("2013-07-01 00:00:00" , tz = "America/Los_Angeles" )
31
+ et = pd .Timestamp ("2013-07-02 00:00:00" , tz = "America/Los_Angeles" )
32
+ dr = pd .date_range (st , et , freq = "H" , name = "timebucket" )
33
+ assert dr [1 :].name == dr .name
34
+
28
35
def test_getitem (self ):
29
36
idx1 = pd .date_range ("2011-01-01" , "2011-01-31" , freq = "D" , name = "idx" )
30
37
idx2 = pd .date_range (
@@ -119,6 +126,21 @@ def test_dti_custom_getitem_matplotlib_hackaround(self):
119
126
expected = rng .values [:, None ]
120
127
tm .assert_numpy_array_equal (values , expected )
121
128
129
+ def test_getitem_int_list (self ):
130
+ dti = date_range (start = "1/1/2005" , end = "12/1/2005" , freq = "M" )
131
+ dti2 = dti [[1 , 3 , 5 ]]
132
+
133
+ v1 = dti2 [0 ]
134
+ v2 = dti2 [1 ]
135
+ v3 = dti2 [2 ]
136
+
137
+ assert v1 == Timestamp ("2/28/2005" )
138
+ assert v2 == Timestamp ("4/30/2005" )
139
+ assert v3 == Timestamp ("6/30/2005" )
140
+
141
+ # getitem with non-slice drops freq
142
+ assert dti2 .freq is None
143
+
122
144
123
145
class TestWhere :
124
146
def test_where_doesnt_retain_freq (self ):
@@ -483,6 +505,69 @@ def test_dti_contains_with_duplicates(self):
483
505
assert d in ix
484
506
485
507
508
+ class TestGetIndexer :
509
+ def test_get_indexer (self ):
510
+ idx = pd .date_range ("2000-01-01" , periods = 3 )
511
+ exp = np .array ([0 , 1 , 2 ], dtype = np .intp )
512
+ tm .assert_numpy_array_equal (idx .get_indexer (idx ), exp )
513
+
514
+ target = idx [0 ] + pd .to_timedelta (["-1 hour" , "12 hours" , "1 day 1 hour" ])
515
+ tm .assert_numpy_array_equal (
516
+ idx .get_indexer (target , "pad" ), np .array ([- 1 , 0 , 1 ], dtype = np .intp )
517
+ )
518
+ tm .assert_numpy_array_equal (
519
+ idx .get_indexer (target , "backfill" ), np .array ([0 , 1 , 2 ], dtype = np .intp )
520
+ )
521
+ tm .assert_numpy_array_equal (
522
+ idx .get_indexer (target , "nearest" ), np .array ([0 , 1 , 1 ], dtype = np .intp )
523
+ )
524
+ tm .assert_numpy_array_equal (
525
+ idx .get_indexer (target , "nearest" , tolerance = pd .Timedelta ("1 hour" )),
526
+ np .array ([0 , - 1 , 1 ], dtype = np .intp ),
527
+ )
528
+ tol_raw = [
529
+ pd .Timedelta ("1 hour" ),
530
+ pd .Timedelta ("1 hour" ),
531
+ pd .Timedelta ("1 hour" ).to_timedelta64 (),
532
+ ]
533
+ tm .assert_numpy_array_equal (
534
+ idx .get_indexer (
535
+ target , "nearest" , tolerance = [np .timedelta64 (x ) for x in tol_raw ]
536
+ ),
537
+ np .array ([0 , - 1 , 1 ], dtype = np .intp ),
538
+ )
539
+ tol_bad = [
540
+ pd .Timedelta ("2 hour" ).to_timedelta64 (),
541
+ pd .Timedelta ("1 hour" ).to_timedelta64 (),
542
+ "foo" ,
543
+ ]
544
+ with pytest .raises (ValueError , match = "abbreviation w/o a number" ):
545
+ idx .get_indexer (target , "nearest" , tolerance = tol_bad )
546
+ with pytest .raises (ValueError , match = "abbreviation w/o a number" ):
547
+ idx .get_indexer (idx [[0 ]], method = "nearest" , tolerance = "foo" )
548
+
549
+
550
+ class TestMaybeCastSliceBound :
551
+ def test_maybe_cast_slice_bounds_empty (self ):
552
+ # GH#14354
553
+ empty_idx = date_range (freq = "1H" , periods = 0 , end = "2015" )
554
+
555
+ right = empty_idx ._maybe_cast_slice_bound ("2015-01-02" , "right" , "loc" )
556
+ exp = Timestamp ("2015-01-02 23:59:59.999999999" )
557
+ assert right == exp
558
+
559
+ left = empty_idx ._maybe_cast_slice_bound ("2015-01-02" , "left" , "loc" )
560
+ exp = Timestamp ("2015-01-02 00:00:00" )
561
+ assert left == exp
562
+
563
+ def test_maybe_cast_slice_duplicate_monotonic (self ):
564
+ # https://github.com/pandas-dev/pandas/issues/16515
565
+ idx = DatetimeIndex (["2017" , "2017" ])
566
+ result = idx ._maybe_cast_slice_bound ("2017-01-01" , "left" , "loc" )
567
+ expected = Timestamp ("2017-01-01" )
568
+ assert result == expected
569
+
570
+
486
571
class TestDatetimeIndex :
487
572
@pytest .mark .parametrize (
488
573
"null" , [None , np .nan , np .datetime64 ("NaT" ), pd .NaT , pd .NA ]
@@ -777,43 +862,3 @@ def test_get_value(self):
777
862
778
863
result = dti .get_value (ser , key .to_datetime64 ())
779
864
assert result == 7
780
-
781
- def test_get_indexer (self ):
782
- idx = pd .date_range ("2000-01-01" , periods = 3 )
783
- exp = np .array ([0 , 1 , 2 ], dtype = np .intp )
784
- tm .assert_numpy_array_equal (idx .get_indexer (idx ), exp )
785
-
786
- target = idx [0 ] + pd .to_timedelta (["-1 hour" , "12 hours" , "1 day 1 hour" ])
787
- tm .assert_numpy_array_equal (
788
- idx .get_indexer (target , "pad" ), np .array ([- 1 , 0 , 1 ], dtype = np .intp )
789
- )
790
- tm .assert_numpy_array_equal (
791
- idx .get_indexer (target , "backfill" ), np .array ([0 , 1 , 2 ], dtype = np .intp )
792
- )
793
- tm .assert_numpy_array_equal (
794
- idx .get_indexer (target , "nearest" ), np .array ([0 , 1 , 1 ], dtype = np .intp )
795
- )
796
- tm .assert_numpy_array_equal (
797
- idx .get_indexer (target , "nearest" , tolerance = pd .Timedelta ("1 hour" )),
798
- np .array ([0 , - 1 , 1 ], dtype = np .intp ),
799
- )
800
- tol_raw = [
801
- pd .Timedelta ("1 hour" ),
802
- pd .Timedelta ("1 hour" ),
803
- pd .Timedelta ("1 hour" ).to_timedelta64 (),
804
- ]
805
- tm .assert_numpy_array_equal (
806
- idx .get_indexer (
807
- target , "nearest" , tolerance = [np .timedelta64 (x ) for x in tol_raw ]
808
- ),
809
- np .array ([0 , - 1 , 1 ], dtype = np .intp ),
810
- )
811
- tol_bad = [
812
- pd .Timedelta ("2 hour" ).to_timedelta64 (),
813
- pd .Timedelta ("1 hour" ).to_timedelta64 (),
814
- "foo" ,
815
- ]
816
- with pytest .raises (ValueError , match = "abbreviation w/o a number" ):
817
- idx .get_indexer (target , "nearest" , tolerance = tol_bad )
818
- with pytest .raises (ValueError , match = "abbreviation w/o a number" ):
819
- idx .get_indexer (idx [[0 ]], method = "nearest" , tolerance = "foo" )
0 commit comments