pydata / pydata/xarray

xr.testing.assert_equal does not test for dtype

Open
#4,727 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-testing
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 True first and see how many failures this creates?
  • What to do with coords and indexes? pd.testing.assert_series_equal has a check_index_type keyword. Probably we need check_coords_type as well? This makes the whole thing much more complicated... Also #4543

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.