Concise way to filter data in xarray

前端 未结 2 870
眼角桃花
眼角桃花 2021-02-13 22:59

I need to apply a very simple \'match statement\' to the values in an xarray array:

  1. Where the value > 0, make 2
  2. Where the value == 0, make 0
  3. Wher
相关标签:
2条回答
  • 2021-02-13 23:16

    xarray now supports .where(condition, other), so this is now valid:

    result = date_by_items.where(date_by_items > 0, 2)
    
    0 讨论(0)
  • 2021-02-13 23:30

    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.

    0 讨论(0)
提交回复
热议问题