滚动平均值,在数据框中返回 nan pandas python
Rolling mean, returning nan in dataframe pandas python
所以我一直在尝试计算移动平均线,输出时由于某种原因它给了我 NaN
import pandas as pd
import pandas_datareader.data as web
import numpy as np
currentTime = datetime.now().date()
f = web.DataReader('goog', 'yahoo', start=2014-1-1, end=currentTime)
ma_50 = pd.rolling_mean(f, window=50)
print(ma_50)
输出只是给我日期,我想要的值似乎是 NaN
,如下所示:
2012-01-03 NaN NaN NaN NaN NaN
您想要 50 天的滚动平均值,因此前 49 天将没有 data.Try 较小数量的平均值可以看到:
import pandas as pd
import pandas_datareader.data as web
import datetime
import numpy as np
currentTime = datetime.datetime.now().date()
df = web.DataReader('goog','yahoo', start='2016-5-1',end='2016-5-15')
ma_3 = pd.rolling_mean(df, window=3)
print(ma_3)
输出:
Open High Low Close Volume \
Date
2016-05-02 NaN NaN NaN NaN NaN
2016-05-03 NaN NaN NaN NaN NaN
2016-05-04 694.996663 699.410014 690.670003 695.423340 1.621233e+06
2016-05-05 695.019999 699.970011 692.243327 696.496663 1.632333e+06
2016-05-06 695.523336 704.643331 694.278992 702.750000 1.730700e+06
2016-05-09 702.693339 710.963338 701.275655 708.483337 1.670633e+06
2016-05-10 709.043335 718.023336 707.942322 715.733337 1.632533e+06
2016-05-11 717.386658 722.230001 712.839986 717.123332 1.586100e+06
2016-05-12 719.073324 722.409993 712.506653 717.259990 1.536867e+06
2016-05-13 717.466655 720.130656 710.353333 713.143331 1.451600e+06
Adj Close
Date
2016-05-02 NaN
2016-05-03 NaN
2016-05-04 695.423340
2016-05-05 696.496663
2016-05-06 702.750000
2016-05-09 708.483337
2016-05-10 715.733337
2016-05-11 717.123332
2016-05-12 717.259990
2016-05-13 713.143331
所以我一直在尝试计算移动平均线,输出时由于某种原因它给了我 NaN
import pandas as pd
import pandas_datareader.data as web
import numpy as np
currentTime = datetime.now().date()
f = web.DataReader('goog', 'yahoo', start=2014-1-1, end=currentTime)
ma_50 = pd.rolling_mean(f, window=50)
print(ma_50)
输出只是给我日期,我想要的值似乎是 NaN
,如下所示:
2012-01-03 NaN NaN NaN NaN NaN
您想要 50 天的滚动平均值,因此前 49 天将没有 data.Try 较小数量的平均值可以看到:
import pandas as pd
import pandas_datareader.data as web
import datetime
import numpy as np
currentTime = datetime.datetime.now().date()
df = web.DataReader('goog','yahoo', start='2016-5-1',end='2016-5-15')
ma_3 = pd.rolling_mean(df, window=3)
print(ma_3)
输出:
Open High Low Close Volume \
Date
2016-05-02 NaN NaN NaN NaN NaN
2016-05-03 NaN NaN NaN NaN NaN
2016-05-04 694.996663 699.410014 690.670003 695.423340 1.621233e+06
2016-05-05 695.019999 699.970011 692.243327 696.496663 1.632333e+06
2016-05-06 695.523336 704.643331 694.278992 702.750000 1.730700e+06
2016-05-09 702.693339 710.963338 701.275655 708.483337 1.670633e+06
2016-05-10 709.043335 718.023336 707.942322 715.733337 1.632533e+06
2016-05-11 717.386658 722.230001 712.839986 717.123332 1.586100e+06
2016-05-12 719.073324 722.409993 712.506653 717.259990 1.536867e+06
2016-05-13 717.466655 720.130656 710.353333 713.143331 1.451600e+06
Adj Close
Date
2016-05-02 NaN
2016-05-03 NaN
2016-05-04 695.423340
2016-05-05 696.496663
2016-05-06 702.750000
2016-05-09 708.483337
2016-05-10 715.733337
2016-05-11 717.123332
2016-05-12 717.259990
2016-05-13 713.143331