Skip to content

Commit 70086c5

Browse files
authored
REF: misplaced tests in test_partial_slicing files (#33349)
1 parent b195a67 commit 70086c5

File tree

6 files changed

+94
-90
lines changed

6 files changed

+94
-90
lines changed

pandas/tests/indexes/datetimes/test_indexing.py

+85-40
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ def test_ellipsis(self):
2525
assert result.equals(idx)
2626
assert result is not idx
2727

28+
def test_getitem_slice_keeps_name(self):
29+
# GH4226
30+
st = pd.Timestamp("2013-07-01 00:00:00", tz="America/Los_Angeles")
31+
et = pd.Timestamp("2013-07-02 00:00:00", tz="America/Los_Angeles")
32+
dr = pd.date_range(st, et, freq="H", name="timebucket")
33+
assert dr[1:].name == dr.name
34+
2835
def test_getitem(self):
2936
idx1 = pd.date_range("2011-01-01", "2011-01-31", freq="D", name="idx")
3037
idx2 = pd.date_range(
@@ -119,6 +126,21 @@ def test_dti_custom_getitem_matplotlib_hackaround(self):
119126
expected = rng.values[:, None]
120127
tm.assert_numpy_array_equal(values, expected)
121128

129+
def test_getitem_int_list(self):
130+
dti = date_range(start="1/1/2005", end="12/1/2005", freq="M")
131+
dti2 = dti[[1, 3, 5]]
132+
133+
v1 = dti2[0]
134+
v2 = dti2[1]
135+
v3 = dti2[2]
136+
137+
assert v1 == Timestamp("2/28/2005")
138+
assert v2 == Timestamp("4/30/2005")
139+
assert v3 == Timestamp("6/30/2005")
140+
141+
# getitem with non-slice drops freq
142+
assert dti2.freq is None
143+
122144

123145
class TestWhere:
124146
def test_where_doesnt_retain_freq(self):
@@ -483,6 +505,69 @@ def test_dti_contains_with_duplicates(self):
483505
assert d in ix
484506

485507

508+
class TestGetIndexer:
509+
def test_get_indexer(self):
510+
idx = pd.date_range("2000-01-01", periods=3)
511+
exp = np.array([0, 1, 2], dtype=np.intp)
512+
tm.assert_numpy_array_equal(idx.get_indexer(idx), exp)
513+
514+
target = idx[0] + pd.to_timedelta(["-1 hour", "12 hours", "1 day 1 hour"])
515+
tm.assert_numpy_array_equal(
516+
idx.get_indexer(target, "pad"), np.array([-1, 0, 1], dtype=np.intp)
517+
)
518+
tm.assert_numpy_array_equal(
519+
idx.get_indexer(target, "backfill"), np.array([0, 1, 2], dtype=np.intp)
520+
)
521+
tm.assert_numpy_array_equal(
522+
idx.get_indexer(target, "nearest"), np.array([0, 1, 1], dtype=np.intp)
523+
)
524+
tm.assert_numpy_array_equal(
525+
idx.get_indexer(target, "nearest", tolerance=pd.Timedelta("1 hour")),
526+
np.array([0, -1, 1], dtype=np.intp),
527+
)
528+
tol_raw = [
529+
pd.Timedelta("1 hour"),
530+
pd.Timedelta("1 hour"),
531+
pd.Timedelta("1 hour").to_timedelta64(),
532+
]
533+
tm.assert_numpy_array_equal(
534+
idx.get_indexer(
535+
target, "nearest", tolerance=[np.timedelta64(x) for x in tol_raw]
536+
),
537+
np.array([0, -1, 1], dtype=np.intp),
538+
)
539+
tol_bad = [
540+
pd.Timedelta("2 hour").to_timedelta64(),
541+
pd.Timedelta("1 hour").to_timedelta64(),
542+
"foo",
543+
]
544+
with pytest.raises(ValueError, match="abbreviation w/o a number"):
545+
idx.get_indexer(target, "nearest", tolerance=tol_bad)
546+
with pytest.raises(ValueError, match="abbreviation w/o a number"):
547+
idx.get_indexer(idx[[0]], method="nearest", tolerance="foo")
548+
549+
550+
class TestMaybeCastSliceBound:
551+
def test_maybe_cast_slice_bounds_empty(self):
552+
# GH#14354
553+
empty_idx = date_range(freq="1H", periods=0, end="2015")
554+
555+
right = empty_idx._maybe_cast_slice_bound("2015-01-02", "right", "loc")
556+
exp = Timestamp("2015-01-02 23:59:59.999999999")
557+
assert right == exp
558+
559+
left = empty_idx._maybe_cast_slice_bound("2015-01-02", "left", "loc")
560+
exp = Timestamp("2015-01-02 00:00:00")
561+
assert left == exp
562+
563+
def test_maybe_cast_slice_duplicate_monotonic(self):
564+
# https://github.com/pandas-dev/pandas/issues/16515
565+
idx = DatetimeIndex(["2017", "2017"])
566+
result = idx._maybe_cast_slice_bound("2017-01-01", "left", "loc")
567+
expected = Timestamp("2017-01-01")
568+
assert result == expected
569+
570+
486571
class TestDatetimeIndex:
487572
@pytest.mark.parametrize(
488573
"null", [None, np.nan, np.datetime64("NaT"), pd.NaT, pd.NA]
@@ -777,43 +862,3 @@ def test_get_value(self):
777862

778863
result = dti.get_value(ser, key.to_datetime64())
779864
assert result == 7
780-
781-
def test_get_indexer(self):
782-
idx = pd.date_range("2000-01-01", periods=3)
783-
exp = np.array([0, 1, 2], dtype=np.intp)
784-
tm.assert_numpy_array_equal(idx.get_indexer(idx), exp)
785-
786-
target = idx[0] + pd.to_timedelta(["-1 hour", "12 hours", "1 day 1 hour"])
787-
tm.assert_numpy_array_equal(
788-
idx.get_indexer(target, "pad"), np.array([-1, 0, 1], dtype=np.intp)
789-
)
790-
tm.assert_numpy_array_equal(
791-
idx.get_indexer(target, "backfill"), np.array([0, 1, 2], dtype=np.intp)
792-
)
793-
tm.assert_numpy_array_equal(
794-
idx.get_indexer(target, "nearest"), np.array([0, 1, 1], dtype=np.intp)
795-
)
796-
tm.assert_numpy_array_equal(
797-
idx.get_indexer(target, "nearest", tolerance=pd.Timedelta("1 hour")),
798-
np.array([0, -1, 1], dtype=np.intp),
799-
)
800-
tol_raw = [
801-
pd.Timedelta("1 hour"),
802-
pd.Timedelta("1 hour"),
803-
pd.Timedelta("1 hour").to_timedelta64(),
804-
]
805-
tm.assert_numpy_array_equal(
806-
idx.get_indexer(
807-
target, "nearest", tolerance=[np.timedelta64(x) for x in tol_raw]
808-
),
809-
np.array([0, -1, 1], dtype=np.intp),
810-
)
811-
tol_bad = [
812-
pd.Timedelta("2 hour").to_timedelta64(),
813-
pd.Timedelta("1 hour").to_timedelta64(),
814-
"foo",
815-
]
816-
with pytest.raises(ValueError, match="abbreviation w/o a number"):
817-
idx.get_indexer(target, "nearest", tolerance=tol_bad)
818-
with pytest.raises(ValueError, match="abbreviation w/o a number"):
819-
idx.get_indexer(idx[[0]], method="nearest", tolerance="foo")

pandas/tests/indexes/datetimes/test_partial_slicing.py

-41
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,6 @@
2121

2222

2323
class TestSlicing:
24-
def test_dti_slicing(self):
25-
dti = date_range(start="1/1/2005", end="12/1/2005", freq="M")
26-
dti2 = dti[[1, 3, 5]]
27-
28-
v1 = dti2[0]
29-
v2 = dti2[1]
30-
v3 = dti2[2]
31-
32-
assert v1 == Timestamp("2/28/2005")
33-
assert v2 == Timestamp("4/30/2005")
34-
assert v3 == Timestamp("6/30/2005")
35-
36-
# don't carry freq through irregular slicing
37-
assert dti2.freq is None
38-
39-
def test_slice_keeps_name(self):
40-
# GH4226
41-
st = pd.Timestamp("2013-07-01 00:00:00", tz="America/Los_Angeles")
42-
et = pd.Timestamp("2013-07-02 00:00:00", tz="America/Los_Angeles")
43-
dr = pd.date_range(st, et, freq="H", name="timebucket")
44-
assert dr[1:].name == dr.name
45-
4624
def test_slice_with_negative_step(self):
4725
ts = Series(np.arange(20), date_range("2014-01-01", periods=20, freq="MS"))
4826
SLC = pd.IndexSlice
@@ -80,25 +58,6 @@ def test_slice_with_zero_step_raises(self):
8058
with pytest.raises(ValueError, match="slice step cannot be zero"):
8159
ts.loc[::0]
8260

83-
def test_slice_bounds_empty(self):
84-
# GH#14354
85-
empty_idx = date_range(freq="1H", periods=0, end="2015")
86-
87-
right = empty_idx._maybe_cast_slice_bound("2015-01-02", "right", "loc")
88-
exp = Timestamp("2015-01-02 23:59:59.999999999")
89-
assert right == exp
90-
91-
left = empty_idx._maybe_cast_slice_bound("2015-01-02", "left", "loc")
92-
exp = Timestamp("2015-01-02 00:00:00")
93-
assert left == exp
94-
95-
def test_slice_duplicate_monotonic(self):
96-
# https://github.com/pandas-dev/pandas/issues/16515
97-
idx = pd.DatetimeIndex(["2017", "2017"])
98-
result = idx._maybe_cast_slice_bound("2017-01-01", "left", "loc")
99-
expected = Timestamp("2017-01-01")
100-
assert result == expected
101-
10261
def test_monotone_DTI_indexing_bug(self):
10362
# GH 19362
10463
# Testing accessing the first element in a monotonic descending

pandas/tests/indexes/period/test_indexing.py

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def test_ellipsis(self):
3131
assert result.equals(idx)
3232
assert result is not idx
3333

34+
def test_getitem_slice_keeps_name(self):
35+
idx = period_range("20010101", periods=10, freq="D", name="bob")
36+
assert idx.name == idx[1:].name
37+
3438
def test_getitem(self):
3539
idx1 = period_range("2011-01-01", "2011-01-31", freq="D", name="idx")
3640

pandas/tests/indexes/period/test_partial_slicing.py

-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ def test_slice_with_zero_step_raises(self):
4040
with pytest.raises(ValueError, match="slice step cannot be zero"):
4141
ts.loc[::0]
4242

43-
def test_slice_keep_name(self):
44-
idx = period_range("20010101", periods=10, freq="D", name="bob")
45-
assert idx.name == idx[1:].name
46-
4743
def test_pindex_slice_index(self):
4844
pi = period_range(start="1/1/10", end="12/31/12", freq="M")
4945
s = Series(np.random.rand(len(pi)), index=pi)

pandas/tests/indexes/timedeltas/test_indexing.py

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ def test_ellipsis(self):
1818
assert result.equals(idx)
1919
assert result is not idx
2020

21+
def test_getitem_slice_keeps_name(self):
22+
# GH#4226
23+
tdi = timedelta_range("1d", "5d", freq="H", name="timebucket")
24+
assert tdi[1:].name == tdi.name
25+
2126
def test_getitem(self):
2227
idx1 = timedelta_range("1 day", "31 day", freq="D", name="idx")
2328

pandas/tests/indexes/timedeltas/test_partial_slicing.py

-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77

88

99
class TestSlicing:
10-
def test_slice_keeps_name(self):
11-
# GH4226
12-
dr = pd.timedelta_range("1d", "5d", freq="H", name="timebucket")
13-
assert dr[1:].name == dr.name
14-
1510
def test_partial_slice(self):
1611
rng = timedelta_range("1 day 10:11:12", freq="h", periods=500)
1712
s = Series(np.arange(len(rng)), index=rng)

0 commit comments

Comments
 (0)