@@ -160,24 +160,33 @@ def test_indexing_with_datetimeindex_tz(self):
160
160
expected = Series ([0 , 5 ], index = index )
161
161
tm .assert_series_equal (result , expected )
162
162
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 ):
164
165
# GH 11497
165
166
166
167
idx = date_range ("2011-01-01" , "2011-01-02" , freq = "D" , name = "idx" )
168
+ if to_period :
169
+ idx = idx .to_period ("D" )
167
170
ser = Series ([0.1 , 0.2 ], index = idx , name = "s" )
168
171
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 ]
170
176
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 )
172
179
tm .assert_series_equal (result , exp , check_index_type = True )
173
180
174
181
keys = [
175
182
Timestamp ("2011-01-02" ),
176
183
Timestamp ("2011-01-02" ),
177
184
Timestamp ("2011-01-01" ),
178
185
]
186
+ if to_period :
187
+ keys = [x .to_period ("D" ) for x in keys ]
179
188
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"
181
190
)
182
191
result = ser .loc [keys ]
183
192
tm .assert_series_equal (result , exp , check_index_type = True )
@@ -187,35 +196,9 @@ def test_series_partial_set_datetime(self):
187
196
Timestamp ("2011-01-02" ),
188
197
Timestamp ("2011-01-03" ),
189
198
]
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 ]
204
201
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
- ]
219
202
with pytest .raises (KeyError , match = "with any missing labels" ):
220
203
ser .loc [keys ]
221
204
0 commit comments