Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Apply rolling window function over time dimension of 3D data
#1
import xarray as xr
import numpy as np
def get_grps(s, thresh=-1, Nmin=2):
    """
    Nmin : int > 0
        Min number of consecutive values below threshold.
    """
    s = pd.Series(s)
    m = np.logical_and.reduce([s.shift(-i).le(thresh) for i in range(Nmin)])
    if Nmin > 1:
        m = pd.Series(m, index=s.index).replace({False: np.NaN}).ffill(limit=Nmin-1).fillna(False)
    else:
        m = pd.Series(m, index=s.index)
 
    # Form consecutive groups
    gps = m.ne(m.shift(1)).cumsum().where(m)
 
    # Return None if no groups, else the aggregations
    if gps.isnull().all():
        return [0]
    else:
        d = s.groupby(gps).agg([list, sum, 'size']).reset_index(drop=True)
 
        data = len((d['sum']))  ### returning frequency
 
        data = np.array(data)
 
 
 
        return (data)
 
### create some dummy data::
 
spi = xr.DataArray(dims=("time", "lon", "lat"), data=np.random.randn(324, 180, 360))
 
### Apply xarray rolling window function::
 
spi.rolling(time=59).reduce(get_grps)
 
## then I get this error::
 
Traceback (most recent call last):
......
    raise Exception("Data must be 1-dimensional")
Exception: Data must be 1-dimensional
 
How can I modify this function to accept 3D data and then apply xarray rolling window properly
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  IDE for Finance TIME SERIES Data Trader2013 2 171 Jan-19-2020, 04:44 PM
Last Post: danielgoldfarb
  Filter value from DataFrame apply a function and save to xlsx zinho 1 396 Dec-22-2019, 03:54 PM
Last Post: zinho
  Apply function on different columns as defined DavidGG 6 991 Sep-04-2019, 03:35 PM
Last Post: vivekirti
  how to apply user defined function to Pandas DataFrame evelynow 3 1,396 Aug-20-2019, 11:35 PM
Last Post: scidam
  Grouping data based on rolling conditions kapilan15 0 363 Jun-05-2019, 01:07 PM
Last Post: kapilan15
  [pandas]How to liner fit time series data and get linear fit equation and r square Sri 5 888 Apr-04-2019, 12:00 PM
Last Post: Sri
  Pandas .rolling() with some calculations inside irmscher 5 1,407 Apr-04-2019, 11:55 AM
Last Post: scidam
  How to use pandas.rolling mean for 3D input array? Prv_Yadv 1 815 Mar-26-2019, 11:49 AM
Last Post: scidam
  Time Data does not match format AshBax 2 12,592 Nov-13-2018, 12:19 PM
Last Post: AshBax
  Calculating median value from time data series mkaru 1 797 Aug-22-2018, 08:41 AM
Last Post: Mekire

Forum Jump:


Users browsing this thread: 1 Guest(s)