Skip to content

TST/REF: collect indexing tests #39661

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 4 commits into from
Feb 8, 2021
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
8 changes: 5 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
)

if TYPE_CHECKING:
from pandas import IntervalIndex, MultiIndex, RangeIndex, Series
from pandas import CategoricalIndex, IntervalIndex, MultiIndex, RangeIndex, Series
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin


Expand Down Expand Up @@ -405,6 +405,7 @@ def _ensure_array(cls, data, dtype, copy: bool):
data = data.copy()
return data

@final
@classmethod
def _dtype_to_subclass(cls, dtype: DtypeObj):
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
Expand Down Expand Up @@ -1013,8 +1014,8 @@ def _format_data(self, name=None) -> str_t:
if self.inferred_type == "string":
is_justify = False
elif self.inferred_type == "categorical":
# error: "Index" has no attribute "categories"
if is_object_dtype(self.categories): # type: ignore[attr-defined]
self = cast("CategoricalIndex", self)
if is_object_dtype(self.categories):
is_justify = False

return format_object_summary(
Expand Down Expand Up @@ -1075,6 +1076,7 @@ def _format_with_header(
result = trim_front(format_array(values, None, justify="left"))
return header + result

@final
def to_native_types(self, slicer=None, **kwargs):
"""
Format specified values of `self` and return them.
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/sparse/test_arithmetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def test_mixed_array_float_int(self, kind, mix, all_arithmetic_functions, reques

if not np_version_under1p20:
if op in [operator.floordiv, ops.rfloordiv] and mix:
mark = pytest.mark.xfail(strict=True, reason="GH#38172")
mark = pytest.mark.xfail(reason="GH#38172")
request.node.add_marker(mark)

rdtype = "int64"
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/base/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,13 @@ def test_iter_box(self):
pd.DatetimeIndex(["2017", "2018"]),
np.ndarray,
"datetime64[ns]",
marks=[pytest.mark.xfail(reason="datetime _values", strict=True)],
marks=[pytest.mark.xfail(reason="datetime _values")],
),
pytest.param(
pd.TimedeltaIndex([10 ** 10]),
np.ndarray,
"m8[ns]",
marks=[pytest.mark.xfail(reason="timedelta _values", strict=True)],
marks=[pytest.mark.xfail(reason="timedelta _values")],
),
],
)
Expand Down
23 changes: 8 additions & 15 deletions pandas/tests/extension/test_boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,16 @@ def check_opname(self, s, op_name, other, exc=None):
# overwriting to indicate ops don't raise an error
super().check_opname(s, op_name, other, exc=None)

def _check_op(self, s, op, other, op_name, exc=NotImplementedError):
def _check_op(self, obj, op, other, op_name, exc=NotImplementedError):
if exc is None:
if op_name in self.implements:
msg = r"numpy boolean subtract"
with pytest.raises(TypeError, match=msg):
op(s, other)
op(obj, other)
return

result = op(s, other)
expected = s.combine(other, op)
result = op(obj, other)
expected = self._combine(obj, other, op)

if op_name in (
"__floordiv__",
Expand All @@ -130,27 +130,20 @@ def _check_op(self, s, op, other, op_name, exc=NotImplementedError):
elif op_name in ("__truediv__", "__rtruediv__"):
# combine with bools does not generate the correct result
# (numpy behaviour for div is to regard the bools as numeric)
expected = s.astype(float).combine(other, op).astype("Float64")
expected = self._combine(obj.astype(float), other, op)
expected = expected.astype("Float64")
if op_name == "__rpow__":
# for rpow, combine does not propagate NaN
expected[result.isna()] = np.nan
self.assert_series_equal(result, expected)
self.assert_equal(result, expected)
else:
with pytest.raises(exc):
op(s, other)
op(obj, other)

def _check_divmod_op(self, s, op, other, exc=None):
# override to not raise an error
super()._check_divmod_op(s, op, other, None)

def test_arith_frame_with_scalar(self, data, all_arithmetic_operators, request):
# frame & scalar
op_name = all_arithmetic_operators
if op_name not in self.implements:
mark = pytest.mark.xfail(reason="_reduce needs implementation")
request.node.add_marker(mark)
super().test_arith_frame_with_scalar(data, all_arithmetic_operators)


class TestComparisonOps(base.BaseComparisonOpsTests):
def check_opname(self, s, op_name, other, exc=None):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def _compare_other(self, s, data, op_name, other):


class TestPrinting(BaseSparseTests, base.BasePrintingTests):
@pytest.mark.xfail(reason="Different repr", strict=True)
@pytest.mark.xfail(reason="Different repr")
def test_array_repr(self, data, size):
super().test_array_repr(data, size)

Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/frame/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
CategoricalDtype,
CategoricalIndex,
DataFrame,
Index,
MultiIndex,
Series,
Timestamp,
Expand Down Expand Up @@ -174,3 +175,24 @@ def test_getitem_bool_mask_categorical_index(self):
df4[df4.index < 2]
with pytest.raises(TypeError, match=msg):
df4[df4.index > 1]


class TestGetitemSlice:
def test_getitem_slice_float64(self, frame_or_series):
values = np.arange(10.0, 50.0, 2)
index = Index(values)

start, end = values[[5, 15]]

data = np.random.randn(20, 3)
if frame_or_series is not DataFrame:
data = data[:, 0]

obj = frame_or_series(data, index=index)

result = obj[start:end]
expected = obj.iloc[5:16]
tm.assert_equal(result, expected)

result = obj.loc[start:end]
tm.assert_equal(result, expected)
1 change: 0 additions & 1 deletion pandas/tests/frame/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,6 @@ def test_replace_value_category_type(self):

@pytest.mark.xfail(
reason="category dtype gets changed to object type after replace, see #35268",
strict=True,
)
def test_replace_dict_category_type(self, input_category_df, expected_category_df):
"""
Expand Down
26 changes: 16 additions & 10 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,22 @@ def test_getitem_ndarray_3d(self, index, frame_or_series, indexer_sli):
idxr = indexer_sli(obj)
nd3 = np.random.randint(5, size=(2, 2, 2))

msg = "|".join(
[
r"Buffer has wrong number of dimensions \(expected 1, got 3\)",
"Cannot index with multidimensional key",
r"Wrong number of dimensions. values.ndim != ndim \[3 != 1\]",
"Index data must be 1-dimensional",
"positional indexers are out-of-bounds",
"Indexing a MultiIndex with a multidimensional key is not implemented",
]
)
msgs = []
if frame_or_series is Series and indexer_sli in [tm.setitem, tm.iloc]:
msgs.append(r"Wrong number of dimensions. values.ndim != ndim \[3 != 1\]")
if frame_or_series is Series or indexer_sli is tm.iloc:
msgs.append(r"Buffer has wrong number of dimensions \(expected 1, got 3\)")
if indexer_sli is tm.loc or (
frame_or_series is Series and indexer_sli is tm.setitem
):
msgs.append("Cannot index with multidimensional key")
if frame_or_series is DataFrame and indexer_sli is tm.setitem:
msgs.append("Index data must be 1-dimensional")
if isinstance(index, pd.IntervalIndex) and indexer_sli is tm.iloc:
msgs.append("Index data must be 1-dimensional")
if len(index) == 0 or isinstance(index, pd.MultiIndex):
msgs.append("positional indexers are out-of-bounds")
msg = "|".join(msgs)

potential_errors = (IndexError, ValueError, NotImplementedError)
with pytest.raises(potential_errors, match=msg):
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/series/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,24 @@ def test_getitem_slice_float_raises(self, datetime_series):
with pytest.raises(TypeError, match=msg.format(key=r"4\.5")):
datetime_series[4.5:10.0]

def test_getitem_slice_bug(self):
ser = Series(range(10), index=list(range(10)))
result = ser[-12:]
tm.assert_series_equal(result, ser)

result = ser[-7:]
tm.assert_series_equal(result, ser[3:])

result = ser[:-12]
tm.assert_series_equal(result, ser[:0])

def test_getitem_slice_integers(self):
ser = Series(np.random.randn(8), index=[2, 4, 6, 8, 10, 12, 14, 16])

result = ser[:4]
expected = Series(ser.values[:4], index=[2, 4, 6, 8])
tm.assert_series_equal(result, expected)


class TestSeriesGetitemListLike:
@pytest.mark.parametrize("box", [list, np.array, Index, pd.Series])
Expand Down
55 changes: 0 additions & 55 deletions pandas/tests/series/indexing/test_numeric.py

This file was deleted.

15 changes: 15 additions & 0 deletions pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ def test_setitem_slice_float_raises(self, datetime_series):
with pytest.raises(TypeError, match=msg.format(key=r"4\.5")):
datetime_series[4.5:10.0] = 0

def test_setitem_slice(self):
ser = Series(range(10), index=list(range(10)))
ser[-12:] = 0
assert (ser == 0).all()

ser[:-12] = 5
assert (ser == 0).all()

def test_setitem_slice_integers(self):
ser = Series(np.random.randn(8), index=[2, 4, 6, 8, 10, 12, 14, 16])

ser[:4] = 0
assert (ser[:4] == 0).all()
assert not (ser[4:] == 0).any()


class TestSetitemBooleanMask:
def test_setitem_boolean(self, string_series):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,6 @@ def test_attrs(self):
@skip_if_no("jinja2")
def test_inspect_getmembers(self):
# GH38782
ser = Series()
ser = Series(dtype=object)
with tm.assert_produces_warning(None):
inspect.getmembers(ser)
2 changes: 0 additions & 2 deletions pandas/tests/series/test_logical_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,13 @@ def test_reversed_xor_with_index_returns_index(self):
marks=pytest.mark.xfail(
reason="GH#22092 Index __and__ returns Index intersection",
raises=AssertionError,
strict=True,
),
),
pytest.param(
ops.ror_,
marks=pytest.mark.xfail(
reason="GH#22092 Index __or__ returns Index union",
raises=AssertionError,
strict=True,
),
),
],
Expand Down