Skip to content

Commit 73cf1b4

Browse files
TST: Replace tm.all_index_generator with indices fixture (#32886)
1 parent 4195bfb commit 73cf1b4

File tree

6 files changed

+69
-63
lines changed

6 files changed

+69
-63
lines changed

pandas/_testing.py

-26
Original file line numberDiff line numberDiff line change
@@ -1683,32 +1683,6 @@ def _make_timeseries(start="2000-01-01", end="2000-12-31", freq="1D", seed=None)
16831683
return df
16841684

16851685

1686-
def all_index_generator(k=10):
1687-
"""
1688-
Generator which can be iterated over to get instances of all the various
1689-
index classes.
1690-
1691-
Parameters
1692-
----------
1693-
k: length of each of the index instances
1694-
"""
1695-
all_make_index_funcs = [
1696-
makeIntIndex,
1697-
makeFloatIndex,
1698-
makeStringIndex,
1699-
makeUnicodeIndex,
1700-
makeDateIndex,
1701-
makePeriodIndex,
1702-
makeTimedeltaIndex,
1703-
makeBoolIndex,
1704-
makeRangeIndex,
1705-
makeIntervalIndex,
1706-
makeCategoricalIndex,
1707-
]
1708-
for make_index_func in all_make_index_funcs:
1709-
yield make_index_func(k=k)
1710-
1711-
17121686
def index_subclass_makers_generator():
17131687
make_index_funcs = [
17141688
makeDateIndex,

pandas/tests/generic/test_frame.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import pandas.util._test_decorators as td
99

10+
from pandas.core.dtypes.generic import ABCMultiIndex
11+
1012
import pandas as pd
1113
from pandas import DataFrame, MultiIndex, Series, date_range
1214
import pandas._testing as tm
@@ -245,8 +247,12 @@ class TestToXArray:
245247
and LooseVersion(xarray.__version__) < LooseVersion("0.10.0"),
246248
reason="xarray >= 0.10.0 required",
247249
)
248-
@pytest.mark.parametrize("index", tm.all_index_generator(3))
249-
def test_to_xarray_index_types(self, index):
250+
def test_to_xarray_index_types(self, indices):
251+
if isinstance(indices, ABCMultiIndex):
252+
pytest.skip("MultiIndex is tested separately")
253+
if len(indices) == 0:
254+
pytest.skip("Test doesn't make sense for empty index")
255+
250256
from xarray import Dataset
251257

252258
df = DataFrame(
@@ -262,7 +268,7 @@ def test_to_xarray_index_types(self, index):
262268
}
263269
)
264270

265-
df.index = index
271+
df.index = indices[:3]
266272
df.index.name = "foo"
267273
df.columns.name = "bar"
268274
result = df.to_xarray()

pandas/tests/generic/test_generic.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,13 @@ def test_metadata_propagation(self):
249249
self.check_metadata(v1 & v2)
250250
self.check_metadata(v1 | v2)
251251

252-
@pytest.mark.parametrize("index", tm.all_index_generator(10))
253-
def test_head_tail(self, index):
252+
def test_head_tail(self, indices):
254253
# GH5370
255254

256-
o = self._construct(shape=10)
255+
o = self._construct(shape=len(indices))
257256

258257
axis = o._get_axis_name(0)
259-
setattr(o, axis, index)
258+
setattr(o, axis, indices)
260259

261260
o.head()
262261

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

274273
# neg index
275-
self._compare(o.head(-3), o.head(7))
276-
self._compare(o.tail(-3), o.tail(7))
274+
self._compare(o.head(-3), o.head(len(indices) - 3))
275+
self._compare(o.tail(-3), o.tail(len(indices) - 3))
277276

278277
def test_sample(self):
279278
# Fixes issue: 2419

pandas/tests/generic/test_series.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pandas import MultiIndex, Series, date_range
1111
import pandas._testing as tm
1212

13+
from ...core.dtypes.generic import ABCMultiIndex
1314
from .test_generic import Generic
1415

1516
try:
@@ -206,15 +207,17 @@ class TestToXArray:
206207
and LooseVersion(xarray.__version__) < LooseVersion("0.10.0"),
207208
reason="xarray >= 0.10.0 required",
208209
)
209-
@pytest.mark.parametrize("index", tm.all_index_generator(6))
210-
def test_to_xarray_index_types(self, index):
210+
def test_to_xarray_index_types(self, indices):
211+
if isinstance(indices, ABCMultiIndex):
212+
pytest.skip("MultiIndex is tested separately")
213+
211214
from xarray import DataArray
212215

213-
s = Series(range(6), index=index)
216+
s = Series(range(len(indices)), index=indices, dtype="object")
214217
s.index.name = "foo"
215218
result = s.to_xarray()
216219
repr(result)
217-
assert len(result) == 6
220+
assert len(result) == len(indices)
218221
assert len(result.coords) == 1
219222
tm.assert_almost_equal(list(result.coords.keys()), ["foo"])
220223
assert isinstance(result, DataArray)
@@ -223,17 +226,9 @@ def test_to_xarray_index_types(self, index):
223226
tm.assert_series_equal(result.to_series(), s, check_index_type=False)
224227

225228
@td.skip_if_no("xarray", min_version="0.7.0")
226-
def test_to_xarray(self):
229+
def test_to_xarray_multiindex(self):
227230
from xarray import DataArray
228231

229-
s = Series([], dtype=object)
230-
s.index.name = "foo"
231-
result = s.to_xarray()
232-
assert len(result) == 0
233-
assert len(result.coords) == 1
234-
tm.assert_almost_equal(list(result.coords.keys()), ["foo"])
235-
assert isinstance(result, DataArray)
236-
237232
s = Series(range(6))
238233
s.index.name = "foo"
239234
s.index = pd.MultiIndex.from_product(
@@ -244,3 +239,15 @@ def test_to_xarray(self):
244239
tm.assert_almost_equal(list(result.coords.keys()), ["one", "two"])
245240
assert isinstance(result, DataArray)
246241
tm.assert_series_equal(result.to_series(), s)
242+
243+
@td.skip_if_no("xarray", min_version="0.7.0")
244+
def test_to_xarray(self):
245+
from xarray import DataArray
246+
247+
s = Series([], dtype=object)
248+
s.index.name = "foo"
249+
result = s.to_xarray()
250+
assert len(result) == 0
251+
assert len(result.coords) == 1
252+
tm.assert_almost_equal(list(result.coords.keys()), ["foo"])
253+
assert isinstance(result, DataArray)

pandas/tests/indexing/test_indexing.py

+28-12
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ def test_setitem_ndarray_1d(self):
5252
with pytest.raises(ValueError):
5353
df[2:5] = np.arange(1, 4) * 1j
5454

55-
@pytest.mark.parametrize(
56-
"index", tm.all_index_generator(5), ids=lambda x: type(x).__name__
57-
)
5855
@pytest.mark.parametrize(
5956
"obj",
6057
[
@@ -71,9 +68,9 @@ def test_setitem_ndarray_1d(self):
7168
(lambda x: x.iloc, "iloc"),
7269
],
7370
)
74-
def test_getitem_ndarray_3d(self, index, obj, idxr, idxr_id):
71+
def test_getitem_ndarray_3d(self, indices, obj, idxr, idxr_id):
7572
# GH 25567
76-
obj = obj(index)
73+
obj = obj(indices)
7774
idxr = idxr(obj)
7875
nd3 = np.random.randint(5, size=(2, 2, 2))
7976

@@ -83,16 +80,16 @@ def test_getitem_ndarray_3d(self, index, obj, idxr, idxr_id):
8380
"Cannot index with multidimensional key",
8481
r"Wrong number of dimensions. values.ndim != ndim \[3 != 1\]",
8582
"Index data must be 1-dimensional",
83+
"positional indexers are out-of-bounds",
84+
"Indexing a MultiIndex with a multidimensional key is not implemented",
8685
]
8786
)
8887

89-
with pytest.raises(ValueError, match=msg):
88+
potential_errors = (IndexError, ValueError, NotImplementedError)
89+
with pytest.raises(potential_errors, match=msg):
9090
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
9191
idxr[nd3]
9292

93-
@pytest.mark.parametrize(
94-
"index", tm.all_index_generator(5), ids=lambda x: type(x).__name__
95-
)
9693
@pytest.mark.parametrize(
9794
"obj",
9895
[
@@ -109,17 +106,25 @@ def test_getitem_ndarray_3d(self, index, obj, idxr, idxr_id):
109106
(lambda x: x.iloc, "iloc"),
110107
],
111108
)
112-
def test_setitem_ndarray_3d(self, index, obj, idxr, idxr_id):
109+
def test_setitem_ndarray_3d(self, indices, obj, idxr, idxr_id):
113110
# GH 25567
114-
obj = obj(index)
111+
obj = obj(indices)
115112
idxr = idxr(obj)
116113
nd3 = np.random.randint(5, size=(2, 2, 2))
117114

115+
if (
116+
(len(indices) == 0)
117+
and (idxr_id == "iloc")
118+
and isinstance(obj, pd.DataFrame)
119+
):
120+
# gh-32896
121+
pytest.skip("This is currently failing. There's an xfailed test below.")
122+
118123
if idxr_id == "iloc":
119124
err = ValueError
120125
msg = f"Cannot set values with ndim > {obj.ndim}"
121126
elif (
122-
isinstance(index, pd.IntervalIndex)
127+
isinstance(indices, pd.IntervalIndex)
123128
and idxr_id == "setitem"
124129
and obj.ndim == 1
125130
):
@@ -134,6 +139,17 @@ def test_setitem_ndarray_3d(self, index, obj, idxr, idxr_id):
134139
with pytest.raises(err, match=msg):
135140
idxr[nd3] = 0
136141

142+
@pytest.mark.xfail(reason="gh-32896")
143+
def test_setitem_ndarray_3d_does_not_fail_for_iloc_empty_dataframe(self):
144+
# when fixing this, please remove the pytest.skip in test_setitem_ndarray_3d
145+
i = Index([])
146+
obj = DataFrame(np.random.randn(len(i), len(i)), index=i, columns=i)
147+
nd3 = np.random.randint(5, size=(2, 2, 2))
148+
149+
msg = f"Cannot set values with ndim > {obj.ndim}"
150+
with pytest.raises(ValueError, match=msg):
151+
obj.iloc[nd3] = 0
152+
137153
def test_inf_upcast(self):
138154
# GH 16957
139155
# We should be able to use np.inf as a key

pandas/tests/series/test_apply.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import numpy as np
55
import pytest
66

7+
from pandas.core.dtypes.generic import ABCMultiIndex
8+
79
import pandas as pd
810
from pandas import DataFrame, Index, Series, isna
911
import pandas._testing as tm
@@ -514,9 +516,11 @@ def test_map(self, datetime_series):
514516
exp = Series([np.nan, "B", "C", "D"])
515517
tm.assert_series_equal(a.map(c), exp)
516518

517-
@pytest.mark.parametrize("index", tm.all_index_generator(10))
518-
def test_map_empty(self, index):
519-
s = Series(index)
519+
def test_map_empty(self, indices):
520+
if isinstance(indices, ABCMultiIndex):
521+
pytest.skip("Initializing a Series from a MultiIndex is not supported")
522+
523+
s = Series(indices)
520524
result = s.map({})
521525

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

0 commit comments

Comments
 (0)