Skip to content

Commit

Permalink
Allow appending datetime & boolean variables to zarr stores (#3504)
Browse files Browse the repository at this point in the history
* Allow appending datetime and boolean data variables to zarr stores.

* Run black and flake8

* Update error message
  • Loading branch information
amatsukawa authored and dcherian committed Nov 13, 2019
1 parent 7241aa1 commit 40588dc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ Bug fixes
- Fix :py:meth:`xarray.core.groupby.DataArrayGroupBy.reduce` and
:py:meth:`xarray.core.groupby.DatasetGroupBy.reduce` when reducing over multiple dimensions.
(:issue:`3402`). By `Deepak Cherian <https://github.com/dcherian/>`_
- Allow appending datetime and bool data variables to zarr stores.
(:issue:`3480`). By `Akihiro Matsukawa <https://github.com/amatsukawa/>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
7 changes: 5 additions & 2 deletions xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1234,15 +1234,18 @@ def _validate_datatypes_for_zarr_append(dataset):
def check_dtype(var):
if (
not np.issubdtype(var.dtype, np.number)
and not np.issubdtype(var.dtype, np.datetime64)
and not np.issubdtype(var.dtype, np.bool)
and not coding.strings.is_unicode_dtype(var.dtype)
and not var.dtype == object
):
# and not re.match('^bytes[1-9]+$', var.dtype.name)):
raise ValueError(
"Invalid dtype for data variable: {} "
"dtype must be a subtype of number, "
"a fixed sized string, a fixed size "
"unicode string or an object".format(var)
"datetime, bool, a fixed sized string, "
"a fixed size unicode string or an "
"object".format(var)
)

for k in dataset.data_vars.values():
Expand Down
14 changes: 14 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ def create_append_test_data(seed=None):
string_var = np.array(["ae", "bc", "df"], dtype=object)
string_var_to_append = np.array(["asdf", "asdfg"], dtype=object)
unicode_var = ["áó", "áó", "áó"]
datetime_var = np.array(
["2019-01-01", "2019-01-02", "2019-01-03"], dtype="datetime64[s]"
)
datetime_var_to_append = np.array(
["2019-01-04", "2019-01-05"], dtype="datetime64[s]"
)
bool_var = np.array([True, False, True], dtype=np.bool)
bool_var_to_append = np.array([False, True], dtype=np.bool)

ds = xr.Dataset(
data_vars={
Expand All @@ -102,6 +110,8 @@ def create_append_test_data(seed=None):
"unicode_var": xr.DataArray(
unicode_var, coords=[time1], dims=["time"]
).astype(np.unicode_),
"datetime_var": xr.DataArray(datetime_var, coords=[time1], dims=["time"]),
"bool_var": xr.DataArray(bool_var, coords=[time1], dims=["time"]),
}
)

Expand All @@ -118,6 +128,10 @@ def create_append_test_data(seed=None):
"unicode_var": xr.DataArray(
unicode_var[:nt2], coords=[time2], dims=["time"]
).astype(np.unicode_),
"datetime_var": xr.DataArray(
datetime_var_to_append, coords=[time2], dims=["time"]
),
"bool_var": xr.DataArray(bool_var_to_append, coords=[time2], dims=["time"]),
}
)

Expand Down

0 comments on commit 40588dc

Please sign in to comment.