|
26 | 26 |
|
27 | 27 |
|
28 | 28 | class TestSeriesGetitemScalars:
|
| 29 | + def test_getitem_negative_out_of_bounds(self): |
| 30 | + ser = Series(tm.rands_array(5, 10), index=tm.rands_array(10, 10)) |
| 31 | + |
| 32 | + msg = "index -11 is out of bounds for axis 0 with size 10" |
| 33 | + with pytest.raises(IndexError, match=msg): |
| 34 | + ser[-11] |
| 35 | + |
29 | 36 | def test_getitem_out_of_bounds_indexerror(self, datetime_series):
|
30 | 37 | # don't segfault, GH#495
|
31 | 38 | msg = r"index \d+ is out of bounds for axis 0 with size \d+"
|
@@ -186,6 +193,17 @@ def test_getitem_slice_date(self, slc, positions):
|
186 | 193 | expected = ser.take(positions)
|
187 | 194 | tm.assert_series_equal(result, expected)
|
188 | 195 |
|
| 196 | + def test_getitem_slice_float_raises(self, datetime_series): |
| 197 | + msg = ( |
| 198 | + "cannot do slice indexing on DatetimeIndex with these indexers " |
| 199 | + r"\[{key}\] of type float" |
| 200 | + ) |
| 201 | + with pytest.raises(TypeError, match=msg.format(key=r"4\.0")): |
| 202 | + datetime_series[4.0:10.0] |
| 203 | + |
| 204 | + with pytest.raises(TypeError, match=msg.format(key=r"4\.5")): |
| 205 | + datetime_series[4.5:10.0] |
| 206 | + |
189 | 207 |
|
190 | 208 | class TestSeriesGetitemListLike:
|
191 | 209 | @pytest.mark.parametrize("box", [list, np.array, Index, pd.Series])
|
@@ -461,3 +479,14 @@ def test_getitem_1tuple_slice_without_multiindex():
|
461 | 479 | result = ser[key]
|
462 | 480 | expected = ser[key[0]]
|
463 | 481 | tm.assert_series_equal(result, expected)
|
| 482 | + |
| 483 | + |
| 484 | +def test_getitem_preserve_name(datetime_series): |
| 485 | + result = datetime_series[datetime_series > 0] |
| 486 | + assert result.name == datetime_series.name |
| 487 | + |
| 488 | + result = datetime_series[[0, 2, 4]] |
| 489 | + assert result.name == datetime_series.name |
| 490 | + |
| 491 | + result = datetime_series[5:10] |
| 492 | + assert result.name == datetime_series.name |
0 commit comments