Python Forum
Apply rolling window function over time dimension of 3D data
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help: Conversion of Electricity Data into Time Series Data SmallGuy 3 1,112 Oct-04-2023, 03:31 PM
Last Post: deanhystad
  Rolling window and apply code JunkBoy 6 1,782 Jul-23-2022, 07:00 PM
Last Post: JunkBoy
  apply(pd.Series) until no more array mikisDeWitte 1 2,711 Apr-17-2021, 08:45 PM
Last Post: Caprone
  Hurst Exponent in Rolling Basis illmattic 1 3,774 Jan-06-2021, 09:49 PM
Last Post: illmattic
  Calculating Beta over Rolling Periods illmattic 2 5,207 Sep-27-2020, 11:27 PM
Last Post: Larz60+
  how to handling time series data file with Python? aupres 4 2,888 Aug-10-2020, 12:40 PM
Last Post: MattKahn13
  HELP- DATA FRAME INTO TIME SERIES- BASIC bntayfur 0 1,718 Jul-11-2020, 09:04 PM
Last Post: bntayfur
  How can I convert time-series data in rows into column srvmig 0 2,020 Apr-11-2020, 05:40 AM
Last Post: srvmig
  IDE for Finance TIME SERIES Data Trader2013 2 60,093 Jan-19-2020, 04:44 PM
Last Post: danielgoldfarb
  Filter value from DataFrame apply a function and save to xlsx zinho 1 1,995 Dec-22-2019, 03:54 PM
Last Post: zinho

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020