python-xarray

Concise way to filter data in xarray

旧时模样 提交于 2019-12-05 04:40:46
I need to apply a very simple 'match statement' to the values in an xarray array: Where the value > 0, make 2 Where the value == 0, make 0 Where the value is NaN , make NaN Here's my current solution. I'm using NaN s, .fillna , & type coercion in lieu of 2d indexing. valid = date_by_items.notnull() positive = date_by_items > 0 positive = positive * 2 result = positive.fillna(0.).where(valid) result This changes this: In [20]: date_by_items = xr.DataArray(np.asarray((list(range(3)) * 10)).reshape(6,5), dims=('date','item')) ...: date_by_items ...: Out[20]: <xarray.DataArray (date: 6, item: 5)>

add dimension to an xarray DataArray

。_饼干妹妹 提交于 2019-12-04 04:40:24
I need to add a dimension to a DataArray , filling the values across the new dimension. Here's the original array. a_size = 10 a_coords = np.linspace(0, 1, a_size) b_size = 5 b_coords = np.linspace(0, 1, b_size) # original 1-dimensional array x = xr.DataArray( np.random.random(a_size), coords=[('a', a coords)]) I guess I could create an empty DataArray with the new dimension and copy the existing data in. y = xr.DataArray( np.empty((b_size, a_size), coords=([('b', b_coords), ('a', a_coords)]) y[:] = x A better idea might be to be to use concat . It took me a while to figure out how to specify

Applying numpy.polyfit to xarray Dataset

纵饮孤独 提交于 2019-12-01 01:05:52
Does Xarray support numpy computation functions such as polyfit? Or is there an efficient way to apply such functions to datasets? Example: I want to calculate the slope of a line fitted to two variables (Temperature and Height), to calculate a lapse rate. I have a dataset (below) with these two variables with dimensions of (vertical, time, xgrid_0, ygrid_0). <xarray.Dataset> Dimensions: (PressLev: 7, time: 48, xgrid_0: 685, ygrid_0: 485) Coordinates: gridlat_0 (ygrid_0, xgrid_0) float32 44.6896 44.6956 44.7015 44.7075 ... gridlon_0 (ygrid_0, xgrid_0) float32 -129.906 -129.879 -129.851 ... *

Add 'constant' dimension to xarray Dataset

只愿长相守 提交于 2019-11-30 23:11:57
I have a series of monthly gridded datasets in CSV form. I want to read them, add a few dimensions, and then write to netcdf. I've had great experience using xarray (xray) in the past so thought I'd use if for this task. I can easily get them into a 2D DataArray with something like: data = np.ones((360,720)) lats = np.arange(-89.75, 90, 0.5) * -1 lngs = np.arange(-179.75, 180, 0.5) coords = {'lat': lats, 'lng':lngs} da = xr.DataArray(data, coords=coords) But when I try to add another dimension, which would convey information about time (all data is from the same year/month), things start to go

Add 'constant' dimension to xarray Dataset

女生的网名这么多〃 提交于 2019-11-30 17:54:48
问题 I have a series of monthly gridded datasets in CSV form. I want to read them, add a few dimensions, and then write to netcdf. I've had great experience using xarray (xray) in the past so thought I'd use if for this task. I can easily get them into a 2D DataArray with something like: data = np.ones((360,720)) lats = np.arange(-89.75, 90, 0.5) * -1 lngs = np.arange(-179.75, 180, 0.5) coords = {'lat': lats, 'lng':lngs} da = xr.DataArray(data, coords=coords) But when I try to add another

Xarray rolling mean with weights

回眸只為那壹抹淺笑 提交于 2019-11-30 10:36:56
When I do running / rolling mean with weights in numpy, I e.g. do something like this: data = np.random.random(100) # Example data... weights = np.array([1, 2, 1]) data_m = np.convolve(data, weights/float(np.sum(weights)), "same") And then replace data_m[0] and data_m[-1] with e.g. nans, depending on application. Something alike can be done with xarray. What I do (in this case) is xr.DataArray(data).rolling(dim_0=3, center=True).mean(dim="dim_0") But this corresponds to the weights weights = np.array([1, 1, 1]) in the numpy example. How would I apply other weights, when using xarray? The

Xarray rolling mean with weights

我是研究僧i 提交于 2019-11-29 15:51:05
问题 When I do running / rolling mean with weights in numpy, I e.g. do something like this: data = np.random.random(100) # Example data... weights = np.array([1, 2, 1]) data_m = np.convolve(data, weights/float(np.sum(weights)), "same") And then replace data_m[0] and data_m[-1] with e.g. nans, depending on application. Something alike can be done with xarray. What I do (in this case) is xr.DataArray(data).rolling(dim_0=3, center=True).mean(dim="dim_0") But this corresponds to the weights weights =

What is the pandas.Panel deprecation warning actually recommending?

青春壹個敷衍的年華 提交于 2019-11-29 07:58:00
问题 I have a package that uses pandas Panels to generate MultiIndex pandas DataFrames. However, whenever I use pandas.Panel, I get the following DeprecationError: DeprecationWarning: Panel is deprecated and will be removed in a future version. The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method. Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/. Pandas provides a .to_xarray()

python-xarray: open_mfdataset concat along two dimensions

喜夏-厌秋 提交于 2019-11-29 07:11:07
I have files which are made of 10 ensembles and 35 time files. One of these files looks like: >>> xr.open_dataset('ens1/CCSM4_ens1_07ic_19820701-19820731_NPac_Jul.nc') <xarray.Dataset> Dimensions: (ensemble: 1, latitude: 66, longitude: 191, time: 31) Coordinates: * ensemble (ensemble) int32 1 * latitude (latitude) float32 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ... * longitude (longitude) float32 100.0 101.0 102.0 103.0 104.0 105.0 106.0 ... * time (time) datetime64[ns] 1982-07-01 1982-07-02 1982-07-03 ... Data variables: u10m (time, latitude, longitude) float64 -1.471 -0.05933 -1.923 ...