Skip to content

TST: Replace tm.all_index_generator with indices fixture #32886

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
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
26 changes: 0 additions & 26 deletions pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1683,32 +1683,6 @@ def _make_timeseries(start="2000-01-01", end="2000-12-31", freq="1D", seed=None)
return df


def all_index_generator(k=10):
"""
Generator which can be iterated over to get instances of all the various
index classes.

Parameters
----------
k: length of each of the index instances
"""
all_make_index_funcs = [
makeIntIndex,
makeFloatIndex,
makeStringIndex,
makeUnicodeIndex,
makeDateIndex,
makePeriodIndex,
makeTimedeltaIndex,
makeBoolIndex,
makeRangeIndex,
makeIntervalIndex,
makeCategoricalIndex,
]
for make_index_func in all_make_index_funcs:
yield make_index_func(k=k)


def index_subclass_makers_generator():
make_index_funcs = [
makeDateIndex,
Expand Down
12 changes: 9 additions & 3 deletions pandas/tests/generic/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import pandas.util._test_decorators as td

from pandas.core.dtypes.generic import ABCMultiIndex

import pandas as pd
from pandas import DataFrame, MultiIndex, Series, date_range
import pandas._testing as tm
Expand Down Expand Up @@ -245,8 +247,12 @@ class TestToXArray:
and LooseVersion(xarray.__version__) < LooseVersion("0.10.0"),
reason="xarray >= 0.10.0 required",
)
@pytest.mark.parametrize("index", tm.all_index_generator(3))
def test_to_xarray_index_types(self, index):
def test_to_xarray_index_types(self, indices):
if isinstance(indices, ABCMultiIndex):
pytest.skip("MultiIndex is tested separately")
if len(indices) == 0:
pytest.skip("Test doesn't make sense for empty index")

from xarray import Dataset

df = DataFrame(
Expand All @@ -262,7 +268,7 @@ def test_to_xarray_index_types(self, index):
}
)

df.index = index
df.index = indices[:3]
df.index.name = "foo"
df.columns.name = "bar"
result = df.to_xarray()
Expand Down
11 changes: 5 additions & 6 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,13 @@ def test_metadata_propagation(self):
self.check_metadata(v1 & v2)
self.check_metadata(v1 | v2)

@pytest.mark.parametrize("index", tm.all_index_generator(10))
def test_head_tail(self, index):
def test_head_tail(self, indices):
# GH5370

o = self._construct(shape=10)
o = self._construct(shape=len(indices))

axis = o._get_axis_name(0)
setattr(o, axis, index)
setattr(o, axis, indices)

o.head()

Expand All @@ -272,8 +271,8 @@ def test_head_tail(self, index):
self._compare(o.tail(len(o) + 1), o)

# neg index
self._compare(o.head(-3), o.head(7))
self._compare(o.tail(-3), o.tail(7))
self._compare(o.head(-3), o.head(len(indices) - 3))
self._compare(o.tail(-3), o.tail(len(indices) - 3))

def test_sample(self):
# Fixes issue: 2419
Expand Down
33 changes: 20 additions & 13 deletions pandas/tests/generic/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pandas import MultiIndex, Series, date_range
import pandas._testing as tm

from ...core.dtypes.generic import ABCMultiIndex
from .test_generic import Generic

try:
Expand Down Expand Up @@ -223,15 +224,17 @@ class TestToXArray:
and LooseVersion(xarray.__version__) < LooseVersion("0.10.0"),
reason="xarray >= 0.10.0 required",
)
@pytest.mark.parametrize("index", tm.all_index_generator(6))
def test_to_xarray_index_types(self, index):
def test_to_xarray_index_types(self, indices):
if isinstance(indices, ABCMultiIndex):
pytest.skip("MultiIndex is tested separately")

from xarray import DataArray

s = Series(range(6), index=index)
s = Series(range(len(indices)), index=indices, dtype="object")
s.index.name = "foo"
result = s.to_xarray()
repr(result)
assert len(result) == 6
assert len(result) == len(indices)
assert len(result.coords) == 1
tm.assert_almost_equal(list(result.coords.keys()), ["foo"])
assert isinstance(result, DataArray)
Expand All @@ -240,17 +243,9 @@ def test_to_xarray_index_types(self, index):
tm.assert_series_equal(result.to_series(), s, check_index_type=False)

@td.skip_if_no("xarray", min_version="0.7.0")
def test_to_xarray(self):
def test_to_xarray_multiindex(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially this could benefit from using the MultiIndex cases from indices. I didn't want to include it in this PR though

from xarray import DataArray

s = Series([], dtype=object)
s.index.name = "foo"
result = s.to_xarray()
assert len(result) == 0
assert len(result.coords) == 1
tm.assert_almost_equal(list(result.coords.keys()), ["foo"])
assert isinstance(result, DataArray)

s = Series(range(6))
s.index.name = "foo"
s.index = pd.MultiIndex.from_product(
Expand All @@ -261,3 +256,15 @@ def test_to_xarray(self):
tm.assert_almost_equal(list(result.coords.keys()), ["one", "two"])
assert isinstance(result, DataArray)
tm.assert_series_equal(result.to_series(), s)

@td.skip_if_no("xarray", min_version="0.7.0")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @jbrockmendel your test moving PR for xrray might need rebasing after this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i think we always have a > 0.7.0 xarary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can check that as a follow-up. There are more spots where this condition is used

def test_to_xarray(self):
from xarray import DataArray

s = Series([], dtype=object)
s.index.name = "foo"
result = s.to_xarray()
assert len(result) == 0
assert len(result.coords) == 1
tm.assert_almost_equal(list(result.coords.keys()), ["foo"])
assert isinstance(result, DataArray)
40 changes: 28 additions & 12 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ def test_setitem_ndarray_1d(self):
with pytest.raises(ValueError):
df[2:5] = np.arange(1, 4) * 1j

@pytest.mark.parametrize(
"index", tm.all_index_generator(5), ids=lambda x: type(x).__name__
)
@pytest.mark.parametrize(
"obj",
[
Expand All @@ -71,9 +68,9 @@ def test_setitem_ndarray_1d(self):
(lambda x: x.iloc, "iloc"),
],
)
def test_getitem_ndarray_3d(self, index, obj, idxr, idxr_id):
def test_getitem_ndarray_3d(self, indices, obj, idxr, idxr_id):
# GH 25567
obj = obj(index)
obj = obj(indices)
idxr = idxr(obj)
nd3 = np.random.randint(5, size=(2, 2, 2))

Expand All @@ -83,16 +80,16 @@ def test_getitem_ndarray_3d(self, index, obj, idxr, idxr_id):
"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",
]
)

with pytest.raises(ValueError, match=msg):
potential_errors = (IndexError, ValueError, NotImplementedError)
with pytest.raises(potential_errors, match=msg):
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
idxr[nd3]

@pytest.mark.parametrize(
"index", tm.all_index_generator(5), ids=lambda x: type(x).__name__
)
@pytest.mark.parametrize(
"obj",
[
Expand All @@ -109,17 +106,25 @@ def test_getitem_ndarray_3d(self, index, obj, idxr, idxr_id):
(lambda x: x.iloc, "iloc"),
],
)
def test_setitem_ndarray_3d(self, index, obj, idxr, idxr_id):
def test_setitem_ndarray_3d(self, indices, obj, idxr, idxr_id):
# GH 25567
obj = obj(index)
obj = obj(indices)
idxr = idxr(obj)
nd3 = np.random.randint(5, size=(2, 2, 2))

if (
(len(indices) == 0)
and (idxr_id == "iloc")
and isinstance(obj, pd.DataFrame)
):
# gh-32896
pytest.skip("This is currently failing. There's an xfailed test below.")

if idxr_id == "iloc":
err = ValueError
msg = f"Cannot set values with ndim > {obj.ndim}"
elif (
isinstance(index, pd.IntervalIndex)
isinstance(indices, pd.IntervalIndex)
and idxr_id == "setitem"
and obj.ndim == 1
):
Expand All @@ -134,6 +139,17 @@ def test_setitem_ndarray_3d(self, index, obj, idxr, idxr_id):
with pytest.raises(err, match=msg):
idxr[nd3] = 0

@pytest.mark.xfail(reason="gh-32896")
def test_setitem_ndarray_3d_does_not_fail_for_iloc_empty_dataframe(self):
# when fixing this, please remove the pytest.skip in test_setitem_ndarray_3d
i = Index([])
obj = DataFrame(np.random.randn(len(i), len(i)), index=i, columns=i)
nd3 = np.random.randint(5, size=(2, 2, 2))

msg = f"Cannot set values with ndim > {obj.ndim}"
with pytest.raises(ValueError, match=msg):
obj.iloc[nd3] = 0

def test_inf_upcast(self):
# GH 16957
# We should be able to use np.inf as a key
Expand Down
10 changes: 7 additions & 3 deletions pandas/tests/series/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import numpy as np
import pytest

from pandas.core.dtypes.generic import ABCMultiIndex

import pandas as pd
from pandas import DataFrame, Index, Series, isna
import pandas._testing as tm
Expand Down Expand Up @@ -514,9 +516,11 @@ def test_map(self, datetime_series):
exp = Series([np.nan, "B", "C", "D"])
tm.assert_series_equal(a.map(c), exp)

@pytest.mark.parametrize("index", tm.all_index_generator(10))
def test_map_empty(self, index):
s = Series(index)
def test_map_empty(self, indices):
if isinstance(indices, ABCMultiIndex):
pytest.skip("Initializing a Series from a MultiIndex is not supported")

s = Series(indices)
result = s.map({})

expected = pd.Series(np.nan, index=s.index)
Expand Down