0

I have a simple(and long) Panda DF with Datetime Index and Prices, I am trying to create a new Column ['ewm_12'] which pick 11 previous rows every 120 minutes appends current price and calculates the ewm

I am trying to do it in a vectorized fashion as the DF is long, using the following code:

dftemp['ewm_12'] = dftemp.loc[dftemp.index::120][-11:].append(dftemp.loc[(dftemp.index)])[dfs].ewm(min_periods=12,span=12, adjust = True).mean()[-1:][0]

TypeError: Cannot convert input [DatetimeIndex(['2018-01-01 22:00:00+00:00', '2018-01-01 22:01:00+00:00','2019-01-18 22:00:00+00:00'],dtype='datetime64[ns, Europe/London]', length=394561, freq=None)] of type to Timestamp

This seem very strange as if i pick just one row dftemp.index it returns a Timestamp but when I ask it to iterate over the whole df.index it says it cannot convert the Datetimeindex (which is a collection of Timestamps), I can do it with a for loop but will take several minutes and I am sure there must be a way if someone knows pls help

Mcavalca
  • 15
  • 3
  • 2018-01-01 22:00:00+00:00 1.72756 2018-01-01 22:01:00+00:00 1.72774 2018-01-01 22:02:00+00:00 1.72768 2018-01-01 22:03:00+00:00 1.72769 – Mcavalca Jan 23 '19 at 18:17
  • I've got closer to the solution the version below parses across the same ewm on the whole column, still unclear why Python is not iteracting over the df.index **dftemp['ewm_12'] = dftemp.loc[(dftemp.index < dftemp.loc[dftemp.index,'time'])][::120][-11:].append(dftemp.loc[(dftemp.index)])[dfs].ewm(min_periods=12,span=12, adjust = True).mean()[-1:][0]** – Mcavalca Jan 24 '19 at 19:06

0 Answers0