I need to apply a very simple \'match statement\' to the values in an xarray array:
xarray now supports .where(condition, other)
, so this is now valid:
result = date_by_items.where(date_by_items > 0, 2)
If you are happy to load your data in-memory as a NumPy array, you can modify the DataArray values in place with NumPy:
date_by_items.values[date_by_items.values > 0] = 2
The cleanest way to handle this would be if xarray supported the other
argument to where
, but we haven't implemented that yet (hopefully soon -- the groundwork has been laid!). When that works, you'll be able to write date_by_items.where(date_by_items > 0, 2)
.
Either way, you'll need to do this twice to apply both your criteria.