@@ -160,24 +160,33 @@ def test_indexing_with_datetimeindex_tz(self):
160160 expected = Series ([0 , 5 ], index = index )
161161 tm .assert_series_equal (result , expected )
162162
163- def test_series_partial_set_datetime (self ):
163+ @pytest .mark .parametrize ("to_period" , [True , False ])
164+ def test_loc_getitem_listlike_of_datetimelike_keys (self , to_period ):
164165 # GH 11497
165166
166167 idx = date_range ("2011-01-01" , "2011-01-02" , freq = "D" , name = "idx" )
168+ if to_period :
169+ idx = idx .to_period ("D" )
167170 ser = Series ([0.1 , 0.2 ], index = idx , name = "s" )
168171
169- result = ser .loc [[Timestamp ("2011-01-01" ), Timestamp ("2011-01-02" )]]
172+ keys = [Timestamp ("2011-01-01" ), Timestamp ("2011-01-02" )]
173+ if to_period :
174+ keys = [x .to_period ("D" ) for x in keys ]
175+ result = ser .loc [keys ]
170176 exp = Series ([0.1 , 0.2 ], index = idx , name = "s" )
171- exp .index = exp .index ._with_freq (None )
177+ if not to_period :
178+ exp .index = exp .index ._with_freq (None )
172179 tm .assert_series_equal (result , exp , check_index_type = True )
173180
174181 keys = [
175182 Timestamp ("2011-01-02" ),
176183 Timestamp ("2011-01-02" ),
177184 Timestamp ("2011-01-01" ),
178185 ]
186+ if to_period :
187+ keys = [x .to_period ("D" ) for x in keys ]
179188 exp = Series (
180- [0.2 , 0.2 , 0.1 ], index = pd . DatetimeIndex (keys , name = "idx" ), name = "s"
189+ [0.2 , 0.2 , 0.1 ], index = Index (keys , name = "idx" , dtype = idx . dtype ), name = "s"
181190 )
182191 result = ser .loc [keys ]
183192 tm .assert_series_equal (result , exp , check_index_type = True )
@@ -187,35 +196,9 @@ def test_series_partial_set_datetime(self):
187196 Timestamp ("2011-01-02" ),
188197 Timestamp ("2011-01-03" ),
189198 ]
190- with pytest .raises (KeyError , match = "with any missing labels" ):
191- ser .loc [keys ]
192-
193- def test_series_partial_set_period (self ):
194- # GH 11497
195-
196- idx = pd .period_range ("2011-01-01" , "2011-01-02" , freq = "D" , name = "idx" )
197- ser = Series ([0.1 , 0.2 ], index = idx , name = "s" )
198-
199- result = ser .loc [
200- [pd .Period ("2011-01-01" , freq = "D" ), pd .Period ("2011-01-02" , freq = "D" )]
201- ]
202- exp = Series ([0.1 , 0.2 ], index = idx , name = "s" )
203- tm .assert_series_equal (result , exp , check_index_type = True )
199+ if to_period :
200+ keys = [x .to_period ("D" ) for x in keys ]
204201
205- keys = [
206- pd .Period ("2011-01-02" , freq = "D" ),
207- pd .Period ("2011-01-02" , freq = "D" ),
208- pd .Period ("2011-01-01" , freq = "D" ),
209- ]
210- exp = Series ([0.2 , 0.2 , 0.1 ], index = pd .PeriodIndex (keys , name = "idx" ), name = "s" )
211- result = ser .loc [keys ]
212- tm .assert_series_equal (result , exp , check_index_type = True )
213-
214- keys = [
215- pd .Period ("2011-01-03" , freq = "D" ),
216- pd .Period ("2011-01-02" , freq = "D" ),
217- pd .Period ("2011-01-03" , freq = "D" ),
218- ]
219202 with pytest .raises (KeyError , match = "with any missing labels" ):
220203 ser .loc [keys ]
221204
0 commit comments