william007
william007

Reputation: 18535

invalid on specified as date, must be a column (if DataFrame) or None

The following line gives me error: invalid on specified as date, must be a column (if DataFrame) or None

How to resolve it?

test_data=pd.DataFrame({'date':['20190606','20190610','20190708','20190805','20190909','20190930'],'number':[3,5,4,1,0,0],\
                       'avg_last_2m':[None,3,4,4,1,0.5]})
test_data['date']=pd.to_datetime(test_data['date'])
print(test_data.rolling('90d', on='date')['number'].agg({'sum','count'}))

Upvotes: 1

Views: 2307

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150735

Surprisingly, @Andy's answer was correct, it's likely a bug. Although passing a dictionary to agg is deprecated.

You can also fix it by:

test_data.set_index('date').rolling('90d')['number'].agg({'sum','count'})

output:

            count   sum
date                   
2019-06-06    1.0   3.0
2019-06-10    2.0   8.0
2019-07-08    3.0  12.0
2019-08-05    4.0  13.0
2019-09-09    3.0   5.0
2019-09-30    4.0   5.0

Upvotes: 2

Ian
Ian

Reputation: 3898

I think the issue is in your agg. Try this:

test_data.rolling('90d', on='date')['number'].agg({'sum':sum,'count':'count'})

Output:

    sum     count
0   3.0     1.0
1   8.0     2.0
2   12.0    3.0
3   13.0    4.0
4   5.0     3.0
5   5.0     4.0

Upvotes: 2

Related Questions