bradyrx / esmtools

:octopus: a toolbox for Earth System Model analysis :octopus:
https://esmtools.readthedocs.io/en/latest/
MIT License
27 stars 4 forks source link

`temporal.to_annual()` returns zeros where there were NaNs #75

Closed bradyrx closed 4 years ago

bradyrx commented 4 years ago

Currently the temporal.to_annual() replaces any NaNs in a DataArray with zeros after converting. My current workaround is to replace the original NaNs afterwards with a simple .where() but that shouldn't be the standard.

import numpy as np
import xarray as xr
import esmtools as et

np.random.seed(999)
space = np.arange(5)
time = xr.cftime_range('1990-01', '1992-01', freq='MS', calendar='noleap')
da = xr.DataArray(np.random.rand(len(time), len(space)), dims=["time", "space"],
                  coords=[time, space])
da[:, 0:1] = np.nan
print(da)
>>> <xarray.DataArray (time: 25, space: 5)>
array([[       nan, 0.5275223 , 0.11911147, 0.63968144, 0.09092526],
       [       nan, 0.42738095, 0.55438581, 0.62812652, 0.69739294],
       [       nan, 0.13189035, 0.34277045, 0.20155961, 0.70732423],
       [       nan, 0.90925004, 0.40516066, 0.76043547, 0.47375838],
       [       nan, 0.75129249, 0.09708994, 0.41235779, 0.28163896],
       [       nan, 0.87110921, 0.08124512, 0.55793117, 0.54753428],
       [       nan, 0.97326881, 0.2862761 , 0.5082575 , 0.14795074],
       [       nan, 0.84082001, 0.0037532 , 0.78262101, 0.83347772],
       [       nan, 0.97260166, 0.83282304, 0.06581761, 0.40379256],
       [       nan, 0.50750135, 0.97787696, 0.81899021, 0.18754124],
       [       nan, 0.68261077, 0.99909815, 0.48263116, 0.73059268],
       [       nan, 0.26139168, 0.16107376, 0.69850315, 0.89950917],
       [       nan, 0.31244902, 0.95412616, 0.7242641 , 0.02091039],
       [       nan, 0.58165923, 0.9545687 , 0.74233195, 0.19750339],
       [       nan, 0.85836332, 0.44904621, 0.82365038, 0.99726878],
       [       nan, 0.5890016 , 0.42402702, 0.89548786, 0.44437266],
       [       nan, 0.66019353, 0.30244304, 0.02295771, 0.83766937],
       [       nan, 0.37552193, 0.18172362, 0.83135182, 0.18487429],
       [       nan, 0.69644561, 0.60566253, 0.49600661, 0.70888438],
       [       nan, 0.65267488, 0.62297362, 0.83609334, 0.3572364 ],
       [       nan, 0.06336003, 0.86713576, 0.55147501, 0.8895871 ],
       [       nan, 0.20783523, 0.48823397, 0.59087233, 0.87087296],
       [       nan, 0.95453522, 0.17335265, 0.106837  , 0.6271594 ],
       [       nan, 0.65200627, 0.68657226, 0.47579499, 0.66581477],
       [       nan, 0.18154475, 0.70021069, 0.44733626, 0.52452094]])
Coordinates:
  * time     (time) object 1990-01-01 00:00:00 ... 1992-01-01 00:00:00
  * space    (space) int64 0 1 2 3 4

da = et.temporal.to_annual(da)
print(da)
>>> <xarray.DataArray (year: 3, space: 5)>
array([[0.        , 0.654351  , 0.4019154 , 0.54661122, 0.49807322],
       [0.        , 0.55067952, 0.55752298, 0.59013334, 0.57021443],
       [0.        , 0.18154475, 0.70021069, 0.44733626, 0.52452094]])
Coordinates:
  * space    (space) int64 0 1 2 3 4
  * year     (year) int64 1990 1991 1992