Reputation: 896
I've got a dataframe of all 505 stock from the SP500. I want to get the weekly values for each Sunday by using the asfreq function. Let's say I'm only using it for two columns of AAPL and MSFT stock for just 20 trading days.
AAPL MSFT
Date
2012-04-05 82.099293 27.453578
2012-04-09 82.429673 27.087763
2012-04-10 81.420407 26.539039
2012-04-11 81.130193 26.434521
2012-04-12 80.685799 26.983243
2012-04-13 78.413325 26.835175
2012-04-16 75.161385 27.070343
2012-04-17 78.992457 27.383899
2012-04-18 78.816259 27.122601
2012-04-19 76.108461 27.009374
2012-04-20 74.235032 28.237466
2012-04-23 74.069192 27.976170
2012-04-24 72.589626 27.801974
2012-04-25 79.031329 28.045851
2012-04-26 78.733339 27.967462
2012-04-27 78.124412 27.854232
2012-04-30 75.660185 27.889073
2012-05-01 75.420503 27.880361
2012-05-02 75.919303 27.697454
2012-05-03 75.380337 27.662615
I try on the dataframe the asfreq function.
df2 = df.asfreq("W-SUN", method="pad")
It generates this huge error that says at the bottom:
TypeError: Cannot compare type 'Timestamp' with type 'str'
I also tried method = "ffill", same error. The asfreq("W-SUN") gave me only NaN values, and asfreq("W-MON", method="pad") also gave me the same error "W-SUN".
I've used the asfreq function before, and it worked perfectly, but now it doesn't. What gives? Any help would be very much appreciated.
Upvotes: 2
Views: 1513
Reputation: 394071
asfreq
works with DatetimeIndex
, the error is telling you that it's still str
so you need to convert the dtype
using to_datetime
:
df.index = pd.to_datetime(df.index)
then the call to asfreq
should work as expected
Upvotes: 1