xr.testing.assert_equal does not test for dtype
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
In #4622 @toddrjen points out that xr.testing.assert_equal does not test for the dtype, only for the value. Therefore the following does not raise an error:
import numpy as np
import xarray as xr
import pandas as pd
xr.testing.assert_equal(
xr.DataArray(np.array(1, dtype=int)), xr.DataArray(np.array(1, dtype=float))
)
xr.testing.assert_equal(
xr.DataArray(np.array(1, dtype=int)), xr.DataArray(np.array(1, dtype=object))
)
xr.testing.assert_equal(
xr.DataArray(np.array("a", dtype=str)), xr.DataArray(np.array("a", dtype=object))
)
This comes back to numpy, i.e. the following is True:
np.array(1, dtype=int) == np.array(1, dtype=float)
Depending on the situation one or the other is desirable or not. Thus, I would suggest to add a check_dtype argument to xr.testing.assert_equal and also to DataArray.equals (and Dataset and Variable and identical). I have not seen such an option in numpy, but pandas has it (e.g. pd.testing.assert_series_equal(left, right, check_dtype=True, ...) . I would not change __eq__.
- Thoughts?
- What should the default be? We could try
Truefirst and see how many failures this creates? - What to do with coords and indexes?
pd.testing.assert_series_equalhas acheck_index_typekeyword. Probably we needcheck_coords_typeas well? This makes the whole thing much more complicated... Also #4543
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing xr.testing.assert_equal and the equals and identical methods on DataArray, Dataset, and Variable, then reproduce the dtype examples from the issue. Determine the intended check_dtype default and how coordinate and index types should be handled; the work is done when the API behavior and those choices are consistently defined and verified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, pandas, python
- Domain
- testing-qa
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100