Skip to content

Commit 40588dc

Browse files
amatsukawadcherian
authored andcommitted
Allow appending datetime & boolean variables to zarr stores (#3504)
* Allow appending datetime and boolean data variables to zarr stores. * Run black and flake8 * Update error message
1 parent 7241aa1 commit 40588dc

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ Bug fixes
9494
- Fix :py:meth:`xarray.core.groupby.DataArrayGroupBy.reduce` and
9595
:py:meth:`xarray.core.groupby.DatasetGroupBy.reduce` when reducing over multiple dimensions.
9696
(:issue:`3402`). By `Deepak Cherian <https://github.com/dcherian/>`_
97+
- Allow appending datetime and bool data variables to zarr stores.
98+
(:issue:`3480`). By `Akihiro Matsukawa <https://github.com/amatsukawa/>`_.
9799

98100
Documentation
99101
~~~~~~~~~~~~~

xarray/backends/api.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,15 +1234,18 @@ def _validate_datatypes_for_zarr_append(dataset):
12341234
def check_dtype(var):
12351235
if (
12361236
not np.issubdtype(var.dtype, np.number)
1237+
and not np.issubdtype(var.dtype, np.datetime64)
1238+
and not np.issubdtype(var.dtype, np.bool)
12371239
and not coding.strings.is_unicode_dtype(var.dtype)
12381240
and not var.dtype == object
12391241
):
12401242
# and not re.match('^bytes[1-9]+$', var.dtype.name)):
12411243
raise ValueError(
12421244
"Invalid dtype for data variable: {} "
12431245
"dtype must be a subtype of number, "
1244-
"a fixed sized string, a fixed size "
1245-
"unicode string or an object".format(var)
1246+
"datetime, bool, a fixed sized string, "
1247+
"a fixed size unicode string or an "
1248+
"object".format(var)
12461249
)
12471250

12481251
for k in dataset.data_vars.values():

xarray/tests/test_dataset.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ def create_append_test_data(seed=None):
9090
string_var = np.array(["ae", "bc", "df"], dtype=object)
9191
string_var_to_append = np.array(["asdf", "asdfg"], dtype=object)
9292
unicode_var = ["áó", "áó", "áó"]
93+
datetime_var = np.array(
94+
["2019-01-01", "2019-01-02", "2019-01-03"], dtype="datetime64[s]"
95+
)
96+
datetime_var_to_append = np.array(
97+
["2019-01-04", "2019-01-05"], dtype="datetime64[s]"
98+
)
99+
bool_var = np.array([True, False, True], dtype=np.bool)
100+
bool_var_to_append = np.array([False, True], dtype=np.bool)
93101

94102
ds = xr.Dataset(
95103
data_vars={
@@ -102,6 +110,8 @@ def create_append_test_data(seed=None):
102110
"unicode_var": xr.DataArray(
103111
unicode_var, coords=[time1], dims=["time"]
104112
).astype(np.unicode_),
113+
"datetime_var": xr.DataArray(datetime_var, coords=[time1], dims=["time"]),
114+
"bool_var": xr.DataArray(bool_var, coords=[time1], dims=["time"]),
105115
}
106116
)
107117

@@ -118,6 +128,10 @@ def create_append_test_data(seed=None):
118128
"unicode_var": xr.DataArray(
119129
unicode_var[:nt2], coords=[time2], dims=["time"]
120130
).astype(np.unicode_),
131+
"datetime_var": xr.DataArray(
132+
datetime_var_to_append, coords=[time2], dims=["time"]
133+
),
134+
"bool_var": xr.DataArray(bool_var_to_append, coords=[time2], dims=["time"]),
121135
}
122136
)
123137

0 commit comments

Comments
 (0)