I have this dataframe:
avg date high low qty
0 16.92 2013-05-27 00:00:00 19.00 1.22 71151.00
1 14.84 2013-05-30 00:00:00 19.00 1.22 42939.00
2 9.19 2013-06-02 00:00:00 17.20 1.23 5607.00
3 23.63 2013-06-05 00:00:00 5000.00 1.22 5850.00
4 13.82 2013-06-10 00:00:00 19.36 1.22 5644.00
5 17.76 2013-06-15 00:00:00 24.00 2.02 16969.00
Each row is an observation of avg, high, low, and qty that was created on the specified date.
I'm trying to compute an exponential moving weighted average with a span of 60 days:
df["emwa"] = pandas.ewma(df["avg"],span=60,freq="D")
But I get
TypeError: Only valid with DatetimeIndex or PeriodIndex
Okay, so maybe I need to add a DateTimeIndex to my DataFrame when it's constructed. Let me change my constructor call from
df = pandas.DataFrame(records) #records is just a list of dictionaries
to
rng = pandas.date_range(firstDate,lastDate, freq='D')
df = pandas.DataFrame(records,index=rng)
But now I get
ValueError: Shape of passed values is (5,), indices imply (5, 1641601)
Any suggestions for how to compute my EMWA?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…