-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from 2 commits
f65492e
1c9a2bb
737d590
5e09cc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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") | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potentially this could benefit from using the MultiIndex cases from |
||
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( | ||
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually i think we always have a > 0.7.0 xarary? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
[ | ||
|
@@ -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)) | ||
|
||
|
@@ -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): | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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", | ||
[ | ||
|
@@ -109,17 +106,17 @@ 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 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 | ||
): | ||
|
@@ -131,6 +128,14 @@ def test_setitem_ndarray_3d(self, index, obj, idxr, idxr_id): | |
err = ValueError | ||
msg = r"Buffer has wrong number of dimensions \(expected 1, got 3\)|" | ||
|
||
if ( | ||
(len(indices) == 0) | ||
and (idxr_id == "iloc") | ||
and isinstance(obj, pd.DataFrame) | ||
): | ||
# TODO: Seems to be bugged | ||
pytest.xfail("This doesn't raise") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wasn't sure what to do here. Is this ok or should this be fixed? I added an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you split this to a separate test for now (and just xfail the entire test; we can come back later and fix), but rather not add this code. also create an issue (and reference it here). |
||
|
||
with pytest.raises(err, match=msg): | ||
idxr[nd3] = 0 | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.