Skip to content

REF: misplaced tests in test_partial_slicing files #33349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 85 additions & 40 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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")
41 changes: 0 additions & 41 deletions pandas/tests/indexes/datetimes/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/indexes/period/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 0 additions & 4 deletions pandas/tests/indexes/period/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/indexes/timedeltas/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
5 changes: 0 additions & 5 deletions pandas/tests/indexes/timedeltas/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down