Reputation: 977
I have tried to write the following code but I get the error message: "ValueError: Cannot shift with no freq." I have no idea of how to fix it? I tried to google on the error message but couldn't find any case similar to mine.
df is a python pandas dataframe for which I want to create new columns showing the daily change. The code is shown below. How can I fix the code to avoid the value error?
for column_names in df:
df[column_names+'%-daily'] =df[column_names].pct_change(freq=1).fillna(0)
Upvotes: 1
Views: 7027
Reputation: 63333
To resolve the error, instead of df.index.shift()
, I had to use pd.Series(df.index).shift()
.
Overall, I used it as below:
df['index_diff'] = (df.index - pd.Series(df.index).shift()).values
Upvotes: 0
Reputation: 977
The problem was that I had date as index. Since only weekdays were shown delta became incorrect. When I changed to period.
for column_names in list(df.columns.values):
df[column_names+'%-daily'] =df[column_names].pct_change(periods=1).fillna(0)
Upvotes: 1