Skip to content

Commit

Permalink
Always include at least one category in random test data (pydata#9436)
Browse files Browse the repository at this point in the history
* Always include at least one category in random test data

Otherwise, creating test data can fail randomly, depending on how the
random number generator is seeded.

(Note: we would ideally avoid using a random number generator or fix the
random seed so test data is entirely deterministic, but that's a bigger
refactor...)

* use RandomState object
  • Loading branch information
shoyer authored and hollymandel committed Sep 23, 2024
1 parent 2d14d03 commit fa67b78
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions xarray/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,16 +324,16 @@ def create_test_data(
obj["var4"] = (
"dim1",
pd.Categorical(
np.random.choice(
list(string.ascii_lowercase[: np.random.randint(5)]),
rs.choice(
list(string.ascii_lowercase[: rs.randint(1, 5)]),
size=dim_sizes[0],
)
),
)
if dim_sizes == _DEFAULT_TEST_DIM_SIZES:
numbers_values = np.array([0, 1, 2, 0, 0, 1, 1, 2, 2, 3], dtype="int64")
else:
numbers_values = np.random.randint(0, 3, _dims["dim3"], dtype="int64")
numbers_values = rs.randint(0, 3, _dims["dim3"], dtype="int64")
obj.coords["numbers"] = ("dim3", numbers_values)
obj.encoding = {"foo": "bar"}
assert_writeable(obj)
Expand Down

0 comments on commit fa67b78

Please sign in to comment.