|
| 1 | +""" |
| 2 | +Series.__getitem__ test classes are organized by the type of key passed. |
| 3 | +""" |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import pytest |
| 8 | + |
| 9 | +from pandas._libs.tslibs import conversion, timezones |
| 10 | + |
| 11 | +import pandas as pd |
| 12 | +from pandas import Series, Timestamp, date_range, period_range |
| 13 | +import pandas._testing as tm |
| 14 | + |
| 15 | + |
| 16 | +class TestSeriesGetitemScalars: |
| 17 | + |
| 18 | + # TODO: better name/GH ref? |
| 19 | + def test_getitem_regression(self): |
| 20 | + ser = Series(range(5), index=list(range(5))) |
| 21 | + result = ser[list(range(5))] |
| 22 | + tm.assert_series_equal(result, ser) |
| 23 | + |
| 24 | + # ------------------------------------------------------------------ |
| 25 | + # Series with DatetimeIndex |
| 26 | + |
| 27 | + @pytest.mark.parametrize("tzstr", ["Europe/Berlin", "dateutil/Europe/Berlin"]) |
| 28 | + def test_getitem_pydatetime_tz(self, tzstr): |
| 29 | + tz = timezones.maybe_get_tz(tzstr) |
| 30 | + |
| 31 | + index = date_range( |
| 32 | + start="2012-12-24 16:00", end="2012-12-24 18:00", freq="H", tz=tzstr |
| 33 | + ) |
| 34 | + ts = Series(index=index, data=index.hour) |
| 35 | + time_pandas = Timestamp("2012-12-24 17:00", tz=tzstr) |
| 36 | + |
| 37 | + dt = datetime(2012, 12, 24, 17, 0) |
| 38 | + time_datetime = conversion.localize_pydatetime(dt, tz) |
| 39 | + assert ts[time_pandas] == ts[time_datetime] |
| 40 | + |
| 41 | + @pytest.mark.parametrize("tz", ["US/Eastern", "dateutil/US/Eastern"]) |
| 42 | + def test_string_index_alias_tz_aware(self, tz): |
| 43 | + rng = date_range("1/1/2000", periods=10, tz=tz) |
| 44 | + ser = Series(np.random.randn(len(rng)), index=rng) |
| 45 | + |
| 46 | + result = ser["1/3/2000"] |
| 47 | + tm.assert_almost_equal(result, ser[2]) |
| 48 | + |
| 49 | + |
| 50 | +class TestSeriesGetitemSlices: |
| 51 | + def test_getitem_slice_2d(self, datetime_series): |
| 52 | + # GH#30588 multi-dimensional indexing deprecated |
| 53 | + |
| 54 | + # This is currently failing because the test was relying on |
| 55 | + # the DeprecationWarning coming through Index.__getitem__. |
| 56 | + # We want to implement a warning specifically for Series.__getitem__ |
| 57 | + # at which point this will become a Deprecation/FutureWarning |
| 58 | + with tm.assert_produces_warning(None): |
| 59 | + # GH#30867 Don't want to support this long-term, but |
| 60 | + # for now ensure that the warning from Index |
| 61 | + # doesn't comes through via Series.__getitem__. |
| 62 | + result = datetime_series[:, np.newaxis] |
| 63 | + expected = datetime_series.values[:, np.newaxis] |
| 64 | + tm.assert_almost_equal(result, expected) |
| 65 | + |
| 66 | + # FutureWarning from NumPy. |
| 67 | + @pytest.mark.filterwarnings("ignore:Using a non-tuple:FutureWarning") |
| 68 | + def test_getitem_median_slice_bug(self): |
| 69 | + index = date_range("20090415", "20090519", freq="2B") |
| 70 | + s = Series(np.random.randn(13), index=index) |
| 71 | + |
| 72 | + indexer = [slice(6, 7, None)] |
| 73 | + with tm.assert_produces_warning(FutureWarning): |
| 74 | + # GH#31299 |
| 75 | + result = s[indexer] |
| 76 | + expected = s[indexer[0]] |
| 77 | + tm.assert_series_equal(result, expected) |
| 78 | + |
| 79 | + |
| 80 | +class TestSeriesGetitemListLike: |
| 81 | + def test_getitem_intlist_intindex_periodvalues(self): |
| 82 | + ser = Series(period_range("2000-01-01", periods=10, freq="D")) |
| 83 | + |
| 84 | + result = ser[[2, 4]] |
| 85 | + exp = pd.Series( |
| 86 | + [pd.Period("2000-01-03", freq="D"), pd.Period("2000-01-05", freq="D")], |
| 87 | + index=[2, 4], |
| 88 | + dtype="Period[D]", |
| 89 | + ) |
| 90 | + tm.assert_series_equal(result, exp) |
| 91 | + assert result.dtype == "Period[D]" |
| 92 | + |
| 93 | + |
| 94 | +def test_getitem_generator(string_series): |
| 95 | + gen = (x > 0 for x in string_series) |
| 96 | + result = string_series[gen] |
| 97 | + result2 = string_series[iter(string_series > 0)] |
| 98 | + expected = string_series[string_series > 0] |
| 99 | + tm.assert_series_equal(result, expected) |
| 100 | + tm.assert_series_equal(result2, expected) |
0 commit comments