Python Forum
Function won't apply dynamically in timeseries
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function won't apply dynamically in timeseries
#1
Hello,

I have the following function:
def custom_hurst(timeseries): series = timeseries.iloc[-360:,0] max_window = len(series) min_window = 15 ndarray_likes = [np.ndarray] if "pandas.core.series" in sys.modules.keys(): ndarray_likes.append(pd.core.series.Series) if type(series) not in ndarray_likes: series = np.array(series) if "pandas.core.series" in sys.modules.keys() and type(series) == pd.core.series.Series: if series.isnull().values.any(): raise ValueError("Series contains NaNs") series = series.values elif np.isnan(np.min(series)): raise ValueError("Series contains NaNs") def to_inc(x): incs = x[1:] - x[:-1] return incs def to_pct(x): pcts = x[1:] / x[:-1] - 1. return pcts def RS_func(series): incs = to_pct(series) mean_inc = np.sum(incs) / len(incs) deviations = incs - mean_inc Z = np.cumsum(deviations) R = max(Z) - min(Z) S = np.std(incs, ddof=1) return R / S err = np.geterr() np.seterr(all='raise') max_window = max_window or len(series)-1 window_sizes = [15,30,45,90,180,360] RS = [] for w in window_sizes: rs = [] for start in range(0, len(series), w): if (start+w)>len(series): break _ = RS_func(series[start:start+w]) if _ != 0: rs.append(_) RS.append(np.mean(rs)) A = np.vstack([np.log10(window_sizes), np.ones(len(RS))]).T H, c = np.linalg.lstsq(A, np.log10(RS), rcond=-1)[0] np.seterr(**err) c = 10**c return H
but when I use it to produce a column of H values corresponding with the time series, it remains static and gives me the same data for each row.

df['Range'] = df.apply(lambda x: custom_hurst(df), axis=1)
Output:
Range Date 1983-03-30 29.40 0.672943 1983-03-31 29.29 0.672943 1983-04-04 29.44 0.672943 1983-04-05 29.71 0.672943 1983-04-06 29.90 0.672943 ... ... 2020-12-30 48.31 0.672943 2020-12-31 48.42 0.672943 2021-01-04 47.35 0.672943 2021-01-05 49.80 0.672943 2021-01-06 50.52 0.672943
how can I have it applied dynamically so that it incorporates the previous 360 values in a rolling fashion instead of just the last 360 in the df? I've tried rolling functions, for loops, changing the code, but nothing seems to work.

Any help would be appreciated
Thanks
Matt
Reply
#2
The core problem results from the function parameter and the lambda.

df['Range'] = df.apply(lambda x: custom_hurst(df), axis=1)
The function takes the full dataframe as the argument and the return is based upon that dataframe. When provided the same dataframe for each row, it produces the same output, which is good. However, nothing is happening with 'x'. To make this calculate for each row, you'll have to refactor the function so it will accept 'x' as the argument and operate upon that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to apply function lower() to the list? Toltimtixma 2 1,878 Feb-10-2023, 05:15 PM
Last Post: Toltimtixma
  IF statement to apply at each date illmattic 2 3,825 Apr-08-2021, 12:31 PM
Last Post: illmattic
  Possible to dynamically pass arguments to a function? grimm1111 2 3,577 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  How to apply VLookup formula jonzee 2 5,134 Jan-12-2020, 04:16 PM
Last Post: Clunk_Head
  Issue in .apply function fullstop 0 2,325 Dec-17-2019, 01:29 PM
Last Post: fullstop
  Converting Smiles to Fingerprint with pandas iterrows/apply-function: Jompie96 0 3,494 Jul-04-2019, 01:50 PM
Last Post: Jompie96
  Apply a function with pandas Jompie96 2 3,449 Jun-13-2019, 07:04 PM
Last Post: Jompie96
  How to combine timeseries data under one column? python_newbie09 1 2,975 Oct-20-2018, 06:51 PM
Last Post: mlieqo

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.