-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
don't use issubdtype
to check for integer dtypes in polyval
#7619
Conversation
for more information, see https://pre-commit.ci
issubdtype
to check for integer dtypes in polyval
this happens with any code that calls In [19]: import xarray as xr
...:
...: x = xr.DataArray(np.arange(10), dims=["x"], name="x")
...: a = xr.DataArray(3 + 4 * x, dims=["x"], coords={"x": x})
...: out = a.polyfit(dim="x", deg=1, full=True)
...: out
Out[19]:
<xarray.Dataset>
Dimensions: (degree: 2)
Coordinates:
* degree (degree) int64 1 0
Data variables:
x_matrix_rank int64 2
x_singular_values (degree) float64 1.358 0.3963
polyfit_coefficients (degree) float64 4.0 3.0
polyfit_residuals float64 4.522e-28
In [20]: xr.polyval(coord=x, coeffs=out.assign_coords(degree=out.degree.astype("int64")))
Out[20]:
<xarray.Dataset>
Dimensions: (x: 10)
Dimensions without coordinates: x
Data variables:
x_matrix_rank (x) int64 2 4 6 8 10 12 14 16 18 20
x_singular_values (x) float64 0.3963 1.754 3.111 ... 9.899 11.26 12.61
polyfit_coefficients (x) float64 3.0 7.0 11.0 15.0 ... 27.0 31.0 35.0 39.0
polyfit_residuals (x) float64 4.522e-28 9.044e-28 ... 4.07e-27 4.522e-27
In [21]: xr.polyval(coord=x, coeffs=out.assign_coords(degree=out.degree.astype("int32")))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[21], line 1
----> 1 xr.polyval(coord=x, coeffs=out.assign_coords(degree=out.degree.astype("int32")))
File .../xarray/core/computation.py:1972, in polyval(coord, coeffs, degree_dim)
1968 raise ValueError(
1969 f"Dimension `{degree_dim}` should be a coordinate variable with labels."
1970 )
1971 if not np.issubdtype(coeffs[degree_dim].dtype, int):
-> 1972 raise ValueError(
1973 f"Dimension `{degree_dim}` should be of integer dtype. Received {coeffs[degree_dim].dtype} instead."
1974 )
1975 max_deg = coeffs[degree_dim].max().item()
1976 coeffs = coeffs.reindex(
1977 {degree_dim: np.arange(max_deg + 1)}, fill_value=0, copy=False
1978 )
ValueError: Dimension `degree` should be of integer dtype. Received int32 instead. |
this looks good to me, but we'd need some tests to make sure we don't reintroduce this issue. I'd probably extend the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you could add a test for this, that would be great :)
@Ravenin7 do you need some guidance on how to add a test for this? Happy to help if so. It would be great to get this fix merged soon because a number of other people have encountered the same bug! |
* main: (40 commits) Faq pull request (According to pull request pydata#7604 & issue pydata#1285 (pydata#7638) add timeouts for tests (pydata#7657) Pull Request Labeler - Undo workaround sync-labels bug (pydata#7667) [pre-commit.ci] pre-commit autoupdate (pydata#7651) Allow all integer dtypes in `polyval` (pydata#7619) [skip-ci] dev whats-new (pydata#7660) Redo whats-new for 2023.03.0 (pydata#7659) Set copy=False when calling pd.Series (pydata#7642) Pin pandas < 2 (pydata#7650) Whats-new for release 2023.03.0 (pydata#7643) Bump pypa/gh-action-pypi-publish from 1.7.1 to 1.8.1 (pydata#7648) Use more descriptive link texts (pydata#7625) Fix missing 'dim' argument in _get_nan_block_lengths (pydata#7598) Fix `pcolormesh` with str coords (pydata#7612) [skip-ci] Fix groupby binary ops benchmarks (pydata#7603) Remove incomplete sentence in IO docs (pydata#7631) Allow indexing unindexed dimensions using dask arrays (pydata#5873) Bump pypa/gh-action-pypi-publish from 1.6.4 to 1.7.1 (pydata#7618) [pre-commit.ci] pre-commit autoupdate (pydata#7620) add a test for scatter colorbar extend (pydata#7616) ...
* upstream/main: (716 commits) Faq pull request (According to pull request pydata#7604 & issue pydata#1285 (pydata#7638) add timeouts for tests (pydata#7657) Pull Request Labeler - Undo workaround sync-labels bug (pydata#7667) [pre-commit.ci] pre-commit autoupdate (pydata#7651) Allow all integer dtypes in `polyval` (pydata#7619) [skip-ci] dev whats-new (pydata#7660) Redo whats-new for 2023.03.0 (pydata#7659) Set copy=False when calling pd.Series (pydata#7642) Pin pandas < 2 (pydata#7650) Whats-new for release 2023.03.0 (pydata#7643) Bump pypa/gh-action-pypi-publish from 1.7.1 to 1.8.1 (pydata#7648) Use more descriptive link texts (pydata#7625) Fix missing 'dim' argument in _get_nan_block_lengths (pydata#7598) Fix `pcolormesh` with str coords (pydata#7612) [skip-ci] Fix groupby binary ops benchmarks (pydata#7603) Remove incomplete sentence in IO docs (pydata#7631) Allow indexing unindexed dimensions using dask arrays (pydata#5873) Bump pypa/gh-action-pypi-publish from 1.6.4 to 1.7.1 (pydata#7618) [pre-commit.ci] pre-commit autoupdate (pydata#7620) add a test for scatter colorbar extend (pydata#7616) ...
Why
While running the "make" command for building the documentation, I had the following error :
(I am on Windows 11 and using Anaconda Prompt as my CLI)
In the file computation.py(present in xarray/core), the program raises an error if the function
np.issubdtype()
returns false. Here, it wantedcoeffs[degree_dim]
to be ofint
data type (windows defaultint
data type isint32
) , but it was ofint64
datatype, and as the documentation ofnp.issubdtype()
states here : "Similar types of different sizes are not subdtypes of each other" so the function returned false and the program raised an error.To fix this error, I have changed the following :
What
I replaced the
np.issubdtype
call withobj.dtype.kind not in "i"
to check ifcoeffs[degree_dim]
is a general signed integer data type.