Skip to content

Fix type issues from pandas stubs #10128

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 7 commits into from
Mar 14, 2025
Merged
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
1 change: 1 addition & 0 deletions ci/requirements/environment.yml
Original file line number Diff line number Diff line change
@@ -56,6 +56,7 @@ dependencies:
- types-pytz
- types-PyYAML
- types-setuptools
- types-openpyxl
- typing_extensions
- zarr
- pip:
4 changes: 3 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
@@ -22,7 +22,9 @@ def pytest_runtest_setup(item):
pytest.skip(
"set --run-network-tests to run test requiring an internet connection"
)
if "mypy" in item.keywords and not item.config.getoption("--run-mypy"):
if any("mypy" in m.name for m in item.own_markers) and not item.config.getoption(
"--run-mypy"
):
pytest.skip("set --run-mypy option to run mypy tests")


1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -55,6 +55,7 @@ types = [
"types-pexpect",
"types-psutil",
"types-pycurl",
"types-openpyxl",
"types-python-dateutil",
"types-pytz",
"types-setuptools",
4 changes: 2 additions & 2 deletions xarray/core/coordinates.py
Original file line number Diff line number Diff line change
@@ -177,13 +177,13 @@ def to_index(self, ordered_dims: Sequence[Hashable] | None = None) -> pd.Index:

# compute the cartesian product
code_list += [
np.tile(np.repeat(code, repeat_counts[i]), tile_counts[i])
np.tile(np.repeat(code, repeat_counts[i]), tile_counts[i]).tolist()
for code in codes
]
level_list += levels
names += index.names

return pd.MultiIndex(level_list, code_list, names=names)
return pd.MultiIndex(levels=level_list, codes=code_list, names=names)


class Coordinates(AbstractCoordinates):
2 changes: 1 addition & 1 deletion xarray/core/groupby.py
Original file line number Diff line number Diff line change
@@ -527,7 +527,7 @@ def factorize(self) -> EncodedGroups:
_flatcodes = where(mask.data, -1, _flatcodes)

full_index = pd.MultiIndex.from_product(
(grouper.full_index.values for grouper in groupers),
list(grouper.full_index.values for grouper in groupers),
names=tuple(grouper.name for grouper in groupers),
)
# This will be unused when grouping by dask arrays, so skip..
9 changes: 6 additions & 3 deletions xarray/core/indexes.py
Original file line number Diff line number Diff line change
@@ -1046,9 +1046,11 @@ def stack(
*[lev.factorize() for lev in level_indexes], strict=True
)
labels_mesh = np.meshgrid(*split_labels, indexing="ij")
labels = [x.ravel() for x in labels_mesh]
labels = [x.ravel().tolist() for x in labels_mesh]

index = pd.MultiIndex(levels, labels, sortorder=0, names=variables.keys())
index = pd.MultiIndex(
levels=levels, codes=labels, sortorder=0, names=variables.keys()
)
level_coords_dtype = {k: var.dtype for k, var in variables.items()}

return cls(index, dim, level_coords_dtype=level_coords_dtype)
@@ -1120,7 +1122,8 @@ def from_variables_maybe_expand(
levels.append(cat.categories)
level_variables[name] = var

index = pd.MultiIndex(levels, codes, names=names)
codes_as_lists = [list(x) for x in codes]
index = pd.MultiIndex(levels=levels, codes=codes_as_lists, names=names)
level_coords_dtype = {k: var.dtype for k, var in level_variables.items()}
obj = cls(index, dim, level_coords_dtype=level_coords_dtype)
index_vars = obj.create_variables(level_variables)
4 changes: 2 additions & 2 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
@@ -2923,7 +2923,7 @@ def test_drop_indexes(self) -> None:
assert_identical(actual, ds)

# test index corrupted
midx = pd.MultiIndex.from_tuples([([1, 2]), ([3, 4])], names=["a", "b"])
midx = pd.MultiIndex.from_tuples([(1, 2), (3, 4)], names=["a", "b"])
midx_coords = Coordinates.from_pandas_multiindex(midx, "x")
ds = Dataset(coords=midx_coords)

@@ -3219,7 +3219,7 @@ def test_rename_dimension_coord_warnings(self) -> None:
ds.rename(x="x")

def test_rename_multiindex(self) -> None:
midx = pd.MultiIndex.from_tuples([([1, 2]), ([3, 4])], names=["a", "b"])
midx = pd.MultiIndex.from_tuples([(1, 2), (3, 4)], names=["a", "b"])
midx_coords = Coordinates.from_pandas_multiindex(midx, "x")
original = Dataset({}, midx_coords)


Unchanged files with check annotations Beta

if xp == np:
# numpy currently doesn't have a astype:
return data.astype(dtype, **kwargs)

Check warning on line 236 in xarray/core/duck_array_ops.py

GitHub Actions / macos-latest py3.10

invalid value encountered in cast

Check warning on line 236 in xarray/core/duck_array_ops.py

GitHub Actions / macos-latest py3.10

invalid value encountered in cast

Check warning on line 236 in xarray/core/duck_array_ops.py

GitHub Actions / ubuntu-latest py3.10

invalid value encountered in cast

Check warning on line 236 in xarray/core/duck_array_ops.py

GitHub Actions / ubuntu-latest py3.10

invalid value encountered in cast

Check warning on line 236 in xarray/core/duck_array_ops.py

GitHub Actions / windows-latest py3.10

invalid value encountered in cast

Check warning on line 236 in xarray/core/duck_array_ops.py

GitHub Actions / windows-latest py3.10

invalid value encountered in cast
return xp.astype(data, dtype, **kwargs)
# otherwise numpy unsigned ints will silently cast to the signed counterpart
fill_value = fill_value.item()
# passes if provided fill value fits in encoded on-disk type
new_fill = encoded_dtype.type(fill_value)

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).
except OverflowError:
encoded_kind_str = "signed" if encoded_dtype.kind == "i" else "unsigned"
warnings.warn(