Python Forum
Pandas .rolling() with some calculations inside
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas .rolling() with some calculations inside
#1
Hey guys

So in short, my Pandas dataframe is something like this:

https://www.dropbox.com/s/ja6kn0f55599xul/test.csv?dl=1

I want to pull out something like this:

a = df.index - df.index.where(df.candle == df.candle -3)[0]
df['test'] = df.bid.rolling(int(a)).mean()
So basically, I want to look back X amount of periods with .rolling(), but I don't know exactly how many therefore I need to calculate it based on df.candle values. I want to look 3 candles back to be more precise and calculate the index difference to use inside .rolling()

I though something like the above piece of code would work, but no, it's giving me errors. I also tried a few other approaches to no avail so end up a bit lost.. The full csv dataframe is here: https://www.dropbox.com/s/9p3kt338bj20yd...8.csv?dl=1
Any suggestions or help will be very much appreciated, thanks! :)
Reply
#2
Anyone? I actually can't get an answer for this on Stack Overflow and few other places for a week now. Is it that difficult?
Reply
#3
What error do you get? I don't know pandas, but I can at least try to poke it and see what happens.
Reply
#4
Are you telling about applying a rolling window of variable width? If so, Pandas doesn't support such windows, except ones based on datetime-like columns. You can try to 1) write all logic in a for loop, e.g. usingdf.iterrows(); 2) Use Cython, df.values is numpy array, Cython supports numpy arrays; (that would be most efficient) 3) make some magic and turn you condition on selecting window size into datetime-like column and apply pandas rolling window (I am not sure if this possible);
Reply
#5
It's interesting how pandas with myriads of features sometimes doesn't have solutions for simple problems. No one can answer this simple question on the internet. I've been trying to find a work around for this for a couple of weeks already, the whole process is so unintuitive and bulky that makes me cringe. Brrr! I think the only elegant solution would be to go and learn Cython!?
Reply
#6
Cython supports numpy arrays and has examples how to work with such array efficiently. You wouldn't have problems with implementing your algorithm, I think. (If you don't need to do the same work repeatedly, you probably can implement and run a long running for-loop in Python, and it probably ends on your (1GB) dataset faster than two weeks or time spent for learning Cython :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Evaluate Calculations yrstruly 0 1,005 Jun-17-2023, 06:51 PM
Last Post: yrstruly
  pandas df inside a df question mbaker_wv 4 1,180 Dec-25-2022, 01:11 AM
Last Post: mbaker_wv
  Rolling window and apply code JunkBoy 6 1,865 Jul-23-2022, 07:00 PM
Last Post: JunkBoy
  Hurst Exponent in Rolling Basis illmattic 1 3,848 Jan-06-2021, 09:49 PM
Last Post: illmattic
  Calculating Beta over Rolling Periods illmattic 2 5,373 Sep-27-2020, 11:27 PM
Last Post: Larz60+
  What to use with simple calculations Milfredo 6 2,656 Sep-24-2020, 06:42 AM
Last Post: Milfredo
  Apply rolling window function over time dimension of 3D data Staph 0 2,183 Jan-01-2020, 08:31 AM
Last Post: Staph
  huge and weird values after applying some calculations karlito 2 2,156 Dec-13-2019, 08:32 AM
Last Post: karlito
  Grouping data based on rolling conditions kapilan15 0 1,951 Jun-05-2019, 01:07 PM
Last Post: kapilan15
  How to use pandas.rolling mean for 3D input array? Prv_Yadv 1 3,846 Mar-26-2019, 11:49 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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