teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

d

2019/02/06 06:15

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -41,4 +41,48 @@
41
41
  ma.plot()
42
42
  ```
43
43
 
44
- データはコメント欄にあった株価のデータを使っています。
44
+ データはコメント欄にあった株価のデータを使っています。
45
+
46
+ ## 追記
47
+
48
+ データは [こちら](https://query1.finance.yahoo.com/v7/finance/download/%5EN225?period1=1546755130&period2=1549433530&interval=1d&events=history&crumb=CXb32EA916t) の Download Data をクリックすると、ダウンロードされる CSV ファイルであっていますか?
49
+ 中身は以下のようです。
50
+
51
+ ```csv
52
+ Date,Open,High,Low,Close,Adj Close,Volume
53
+ 2019-01-07,19944.609375,20266.220703,19920.800781,20038.970703,20038.970703,81500
54
+ 2019-01-08,20224.669922,20347.919922,20106.359375,20204.039063,20204.039063,86400
55
+ 2019-01-09,20366.300781,20494.349609,20331.199219,20427.060547,20427.060547,72800
56
+ 2019-01-10,20270.880859,20345.919922,20101.929688,20163.800781,20163.800781,73700
57
+ 2019-01-11,20296.449219,20389.890625,20294.740234,20359.699219,20359.699219,77700
58
+ 2019-01-14,null,null,null,null,null,null
59
+ 2019-01-15,20264.820313,20571.279297,20204.429688,20555.289063,20555.289063,78300
60
+ 2019-01-16,20575.720703,20580.250000,20323.320313,20442.750000,20442.750000,69500
61
+ 2019-01-17,20544.230469,20571.750000,20342.460938,20402.269531,20402.269531,63600
62
+ 2019-01-18,20472.810547,20682.119141,20454.130859,20666.070313,20666.070313,64700
63
+ 2019-01-21,20848.380859,20892.679688,20678.259766,20719.330078,20719.330078,61200
64
+ 2019-01-22,20770.060547,20805.929688,20558.300781,20622.910156,20622.910156,57100
65
+ 2019-01-23,20453.439453,20686.289063,20438.220703,20593.720703,20593.720703,0
66
+ 2019-01-24,20506.240234,20620.720703,20467.589844,20574.630859,20574.630859,61400
67
+ 2019-01-25,20598.640625,20844.310547,20598.640625,20773.560547,20773.560547,68400
68
+ 2019-01-28,20746.289063,20759.480469,20624.550781,20649.000000,20649.000000,56800
69
+ 2019-01-29,20555.439453,20673.660156,20406.220703,20664.640625,20664.640625,63100
70
+ 2019-01-30,20701.619141,20706.269531,20527.529297,20556.539063,20556.539063,72100
71
+ 2019-01-31,20832.910156,20869.419922,20682.910156,20773.490234,20773.490234,75700
72
+ 2019-02-01,20797.029297,20929.630859,20741.980469,20788.390625,20788.390625,82200
73
+ 2019-02-04,20831.900391,20922.580078,20823.679688,20883.769531,20883.769531,66600
74
+ 2019-02-05,20960.470703,20981.230469,20823.179688,20844.449219,20844.449219,65500
75
+ 2019-02-06,20928.869141,20971.660156,20860.990234,20873.599609,20873.599609,0
76
+ ```
77
+
78
+ これに対して、
79
+
80
+ ```python
81
+ import pandas as pd
82
+
83
+ df = pd.read_csv('test.csv')
84
+ ma = df['Close'].rolling(window=3, min_periods=1).mean() # 移動平均
85
+ ma.plot()
86
+ ```
87
+
88
+ で動作確認していますが、質問者さんの環境ではエラーが出るということですか?

1

d

2019/02/06 06:15

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -21,4 +21,24 @@
21
21
  ma.plot()
22
22
  ```
23
23
 
24
- ![イメージ説明](be1990abbf50c73c6fbd92b493c44a14.png)
24
+ ![イメージ説明](be1990abbf50c73c6fbd92b493c44a14.png)
25
+
26
+ ## 追記 NaN を含むデータの移動平均について
27
+
28
+ [pandas.DataFrame.rolling](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rolling.html)
29
+
30
+ > Minimum number of observations in window required to have a value (otherwise result is NA).
31
+ > For a window that is specified by an offset, min_periods will default to 1. Otherwise, min_periods will default to the size of the window.
32
+
33
+ デフォルトでは window 内に1つでも NaN があると、その部分の平均は NaN になってしまいます。
34
+ min_periods を指定することで、window 内に最低でも min_periods 個の有効な値があれば、平均が計算されるようになります。
35
+
36
+ ```python
37
+ import pandas as pd
38
+
39
+ df = pd.read_csv('test.csv')
40
+ ma = df['Close'].rolling(window=3, min_periods=1).mean() # 移動平均
41
+ ma.plot()
42
+ ```
43
+
44
+ データはコメント欄にあった株価のデータを使っています。