diff --git a/pandas/tests/indexes/datetimes/test_indexing.py b/pandas/tests/indexes/datetimes/test_indexing.py index 58e2afc869e02..0e9af273823cb 100644 --- a/pandas/tests/indexes/datetimes/test_indexing.py +++ b/pandas/tests/indexes/datetimes/test_indexing.py @@ -25,6 +25,13 @@ def test_ellipsis(self): assert result.equals(idx) assert result is not idx + def test_getitem_slice_keeps_name(self): + # GH4226 + st = pd.Timestamp("2013-07-01 00:00:00", tz="America/Los_Angeles") + et = pd.Timestamp("2013-07-02 00:00:00", tz="America/Los_Angeles") + dr = pd.date_range(st, et, freq="H", name="timebucket") + assert dr[1:].name == dr.name + def test_getitem(self): idx1 = pd.date_range("2011-01-01", "2011-01-31", freq="D", name="idx") idx2 = pd.date_range( @@ -119,6 +126,21 @@ def test_dti_custom_getitem_matplotlib_hackaround(self): expected = rng.values[:, None] tm.assert_numpy_array_equal(values, expected) + def test_getitem_int_list(self): + dti = date_range(start="1/1/2005", end="12/1/2005", freq="M") + dti2 = dti[[1, 3, 5]] + + v1 = dti2[0] + v2 = dti2[1] + v3 = dti2[2] + + assert v1 == Timestamp("2/28/2005") + assert v2 == Timestamp("4/30/2005") + assert v3 == Timestamp("6/30/2005") + + # getitem with non-slice drops freq + assert dti2.freq is None + class TestWhere: def test_where_doesnt_retain_freq(self): @@ -483,6 +505,69 @@ def test_index_dupes_contains(self): assert d in ix +class TestGetIndexer: + def test_get_indexer(self): + idx = pd.date_range("2000-01-01", periods=3) + exp = np.array([0, 1, 2], dtype=np.intp) + tm.assert_numpy_array_equal(idx.get_indexer(idx), exp) + + target = idx[0] + pd.to_timedelta(["-1 hour", "12 hours", "1 day 1 hour"]) + tm.assert_numpy_array_equal( + idx.get_indexer(target, "pad"), np.array([-1, 0, 1], dtype=np.intp) + ) + tm.assert_numpy_array_equal( + idx.get_indexer(target, "backfill"), np.array([0, 1, 2], dtype=np.intp) + ) + tm.assert_numpy_array_equal( + idx.get_indexer(target, "nearest"), np.array([0, 1, 1], dtype=np.intp) + ) + tm.assert_numpy_array_equal( + idx.get_indexer(target, "nearest", tolerance=pd.Timedelta("1 hour")), + np.array([0, -1, 1], dtype=np.intp), + ) + tol_raw = [ + pd.Timedelta("1 hour"), + pd.Timedelta("1 hour"), + pd.Timedelta("1 hour").to_timedelta64(), + ] + tm.assert_numpy_array_equal( + idx.get_indexer( + target, "nearest", tolerance=[np.timedelta64(x) for x in tol_raw] + ), + np.array([0, -1, 1], dtype=np.intp), + ) + tol_bad = [ + pd.Timedelta("2 hour").to_timedelta64(), + pd.Timedelta("1 hour").to_timedelta64(), + "foo", + ] + with pytest.raises(ValueError, match="abbreviation w/o a number"): + idx.get_indexer(target, "nearest", tolerance=tol_bad) + with pytest.raises(ValueError, match="abbreviation w/o a number"): + idx.get_indexer(idx[[0]], method="nearest", tolerance="foo") + + +class TestMaybeCastSliceBound: + def test_maybe_cast_slice_bounds_empty(self): + # GH#14354 + empty_idx = date_range(freq="1H", periods=0, end="2015") + + right = empty_idx._maybe_cast_slice_bound("2015-01-02", "right", "loc") + exp = Timestamp("2015-01-02 23:59:59.999999999") + assert right == exp + + left = empty_idx._maybe_cast_slice_bound("2015-01-02", "left", "loc") + exp = Timestamp("2015-01-02 00:00:00") + assert left == exp + + def test_maybe_cast_slice_duplicate_monotonic(self): + # https://github.com/pandas-dev/pandas/issues/16515 + idx = DatetimeIndex(["2017", "2017"]) + result = idx._maybe_cast_slice_bound("2017-01-01", "left", "loc") + expected = Timestamp("2017-01-01") + assert result == expected + + class TestDatetimeIndex: @pytest.mark.parametrize( "null", [None, np.nan, np.datetime64("NaT"), pd.NaT, pd.NA] @@ -777,43 +862,3 @@ def test_get_value(self): result = dti.get_value(ser, key.to_datetime64()) assert result == 7 - - def test_get_indexer(self): - idx = pd.date_range("2000-01-01", periods=3) - exp = np.array([0, 1, 2], dtype=np.intp) - tm.assert_numpy_array_equal(idx.get_indexer(idx), exp) - - target = idx[0] + pd.to_timedelta(["-1 hour", "12 hours", "1 day 1 hour"]) - tm.assert_numpy_array_equal( - idx.get_indexer(target, "pad"), np.array([-1, 0, 1], dtype=np.intp) - ) - tm.assert_numpy_array_equal( - idx.get_indexer(target, "backfill"), np.array([0, 1, 2], dtype=np.intp) - ) - tm.assert_numpy_array_equal( - idx.get_indexer(target, "nearest"), np.array([0, 1, 1], dtype=np.intp) - ) - tm.assert_numpy_array_equal( - idx.get_indexer(target, "nearest", tolerance=pd.Timedelta("1 hour")), - np.array([0, -1, 1], dtype=np.intp), - ) - tol_raw = [ - pd.Timedelta("1 hour"), - pd.Timedelta("1 hour"), - pd.Timedelta("1 hour").to_timedelta64(), - ] - tm.assert_numpy_array_equal( - idx.get_indexer( - target, "nearest", tolerance=[np.timedelta64(x) for x in tol_raw] - ), - np.array([0, -1, 1], dtype=np.intp), - ) - tol_bad = [ - pd.Timedelta("2 hour").to_timedelta64(), - pd.Timedelta("1 hour").to_timedelta64(), - "foo", - ] - with pytest.raises(ValueError, match="abbreviation w/o a number"): - idx.get_indexer(target, "nearest", tolerance=tol_bad) - with pytest.raises(ValueError, match="abbreviation w/o a number"): - idx.get_indexer(idx[[0]], method="nearest", tolerance="foo") diff --git a/pandas/tests/indexes/datetimes/test_partial_slicing.py b/pandas/tests/indexes/datetimes/test_partial_slicing.py index 946d658e90132..ddde30d0f8fbf 100644 --- a/pandas/tests/indexes/datetimes/test_partial_slicing.py +++ b/pandas/tests/indexes/datetimes/test_partial_slicing.py @@ -21,28 +21,6 @@ class TestSlicing: - def test_dti_slicing(self): - dti = date_range(start="1/1/2005", end="12/1/2005", freq="M") - dti2 = dti[[1, 3, 5]] - - v1 = dti2[0] - v2 = dti2[1] - v3 = dti2[2] - - assert v1 == Timestamp("2/28/2005") - assert v2 == Timestamp("4/30/2005") - assert v3 == Timestamp("6/30/2005") - - # don't carry freq through irregular slicing - assert dti2.freq is None - - def test_slice_keeps_name(self): - # GH4226 - st = pd.Timestamp("2013-07-01 00:00:00", tz="America/Los_Angeles") - et = pd.Timestamp("2013-07-02 00:00:00", tz="America/Los_Angeles") - dr = pd.date_range(st, et, freq="H", name="timebucket") - assert dr[1:].name == dr.name - def test_slice_with_negative_step(self): ts = Series(np.arange(20), date_range("2014-01-01", periods=20, freq="MS")) SLC = pd.IndexSlice @@ -80,25 +58,6 @@ def test_slice_with_zero_step_raises(self): with pytest.raises(ValueError, match="slice step cannot be zero"): ts.loc[::0] - def test_slice_bounds_empty(self): - # GH#14354 - empty_idx = date_range(freq="1H", periods=0, end="2015") - - right = empty_idx._maybe_cast_slice_bound("2015-01-02", "right", "loc") - exp = Timestamp("2015-01-02 23:59:59.999999999") - assert right == exp - - left = empty_idx._maybe_cast_slice_bound("2015-01-02", "left", "loc") - exp = Timestamp("2015-01-02 00:00:00") - assert left == exp - - def test_slice_duplicate_monotonic(self): - # https://github.com/pandas-dev/pandas/issues/16515 - idx = pd.DatetimeIndex(["2017", "2017"]) - result = idx._maybe_cast_slice_bound("2017-01-01", "left", "loc") - expected = Timestamp("2017-01-01") - assert result == expected - def test_monotone_DTI_indexing_bug(self): # GH 19362 # Testing accessing the first element in a monotonic descending diff --git a/pandas/tests/indexes/period/test_indexing.py b/pandas/tests/indexes/period/test_indexing.py index 39688e5b92380..c4aaf6332ba15 100644 --- a/pandas/tests/indexes/period/test_indexing.py +++ b/pandas/tests/indexes/period/test_indexing.py @@ -31,6 +31,10 @@ def test_ellipsis(self): assert result.equals(idx) assert result is not idx + def test_getitem_slice_keeps_name(self): + idx = period_range("20010101", periods=10, freq="D", name="bob") + assert idx.name == idx[1:].name + def test_getitem(self): idx1 = period_range("2011-01-01", "2011-01-31", freq="D", name="idx") diff --git a/pandas/tests/indexes/period/test_partial_slicing.py b/pandas/tests/indexes/period/test_partial_slicing.py index c0597180184a6..ad9ee7bd2594d 100644 --- a/pandas/tests/indexes/period/test_partial_slicing.py +++ b/pandas/tests/indexes/period/test_partial_slicing.py @@ -40,10 +40,6 @@ def test_slice_with_zero_step_raises(self): with pytest.raises(ValueError, match="slice step cannot be zero"): ts.loc[::0] - def test_slice_keep_name(self): - idx = period_range("20010101", periods=10, freq="D", name="bob") - assert idx.name == idx[1:].name - def test_pindex_slice_index(self): pi = period_range(start="1/1/10", end="12/31/12", freq="M") s = Series(np.random.rand(len(pi)), index=pi) diff --git a/pandas/tests/indexes/timedeltas/test_indexing.py b/pandas/tests/indexes/timedeltas/test_indexing.py index 72d7763b549e7..8c39a9c40a69b 100644 --- a/pandas/tests/indexes/timedeltas/test_indexing.py +++ b/pandas/tests/indexes/timedeltas/test_indexing.py @@ -18,6 +18,11 @@ def test_ellipsis(self): assert result.equals(idx) assert result is not idx + def test_getitem_slice_keeps_name(self): + # GH#4226 + tdi = timedelta_range("1d", "5d", freq="H", name="timebucket") + assert tdi[1:].name == tdi.name + def test_getitem(self): idx1 = timedelta_range("1 day", "31 day", freq="D", name="idx") diff --git a/pandas/tests/indexes/timedeltas/test_partial_slicing.py b/pandas/tests/indexes/timedeltas/test_partial_slicing.py index 29e2c7dd20be0..a0ef953db3600 100644 --- a/pandas/tests/indexes/timedeltas/test_partial_slicing.py +++ b/pandas/tests/indexes/timedeltas/test_partial_slicing.py @@ -7,11 +7,6 @@ class TestSlicing: - def test_slice_keeps_name(self): - # GH4226 - dr = pd.timedelta_range("1d", "5d", freq="H", name="timebucket") - assert dr[1:].name == dr.name - def test_partial_slice(self): rng = timedelta_range("1 day 10:11:12", freq="h", periods=500) s = Series(np.arange(len(rng)), index=rng)