0

I am trying to retrieve some stock data and calculates SMAs etc. Since the function rolling_mean has been deprecated in Pandas, I tried Series instead. However, I get an error message: ValueError: cannot copy sequence with size 6 to array axis with dimension 2165

Could anyone tell me what I do wrong?

Many thanks!

import numpy as np  # array operations
import pandas as pd  # time series management
from pandas_datareader import data as web  # data retrieval
import matplotlib.pyplot as plt  # standard plotting library
import seaborn as sns; sns.set()  # nicer plotting style

import datetime

start = datetime.datetime(2010, 1, 1)

end = datetime.datetime(2018, 4, 19)
f = web.DataReader('F', 'morningstar', start, end)
print(f.tail)

f['SMA50'] = pd.rolling_mean(f['Close'], window=50) #OLD

f['SMA50'] = pd.Series(f).rolling(window=50).mean() #NEW
Markus Knopfler
  • 617
  • 2
  • 7
  • 14
  • 1
    It is just `f['Close'].rolling(window=50).mean()` You don't need the series constructor. – ayhan Apr 20 '18 at 09:49
  • duh!! many thanks man! – Markus Knopfler Apr 20 '18 at 09:52
  • I am redirecting this to a more detailed answer since your issue is resolved. – ayhan Apr 20 '18 at 10:20
  • In case you are still around, can I pester you with a follow up question? I try to pull in multiple instruments but only the first works, getting NaNs for the rest.. Would appreciate if you still have a moment for me :) symbols = ['MSFT', 'YHOO', 'AMZN', 'GOOG'] # our symbols data = pd.DataFrame() # empty DataFrame for sym in symbols: data[sym] = web.DataReader(sym, data_source='morningstar')['Close'] print(data.tail) – Markus Knopfler Apr 21 '18 at 09:41
  • 1
    `web.DataReader` returns a MultiIndex DataFrame so your indices do not match when you iteratively assign a new column. Try `data[sym] = web.DataReader(sym, data_source='morningstar')['Close'].reset_index(level='Symbol', drop=True)` (This will get rid of the first level of the index, namely Symbol, since you already have that as a column). – ayhan Apr 21 '18 at 11:56
  • mate, youre a star! many many thanks. have a great weekend! – Markus Knopfler Apr 21 '18 at 12:43

0 Answers0