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

質問編集履歴

3

再編集

2021/05/05 04:03

投稿

y_masa.teratail
y_masa.teratail

スコア13

title CHANGED
File without changes
body CHANGED
@@ -60,7 +60,10 @@
60
60
  int64
61
61
  ```
62
62
  やはりAの列でobjectが表示されてしまいます。
63
+ (無料ダウンロードのデータだと効率よく使用できないようになっている、、?
63
64
 
65
+ ---
66
+
64
67
  ```python
65
68
  #pandasとnumpyをインポート
66
69
  import pandas as pd

2

私的頂いた内容に沿っての修正

2021/05/05 04:03

投稿

y_masa.teratail
y_masa.teratail

スコア13

title CHANGED
File without changes
body CHANGED
@@ -18,8 +18,50 @@
18
18
  [参考ページ](https://qiita.com/kazama1209/items/98f63b624f3987ba3322)
19
19
  [csvファイル参照](http://www.histdata.com)
20
20
 
21
+ ---
22
+ 以下、ppaulさんに回答頂いた内容について実践してみた内容となります。
21
23
 
24
+ どこかに数字ではないデータが入っている可能性があるということで、
25
+ ppaulさんに私的頂いたdtypeにてデータ型の確認を行ったところ、
26
+ 以下のような結果が得られました。
27
+
22
28
  ```python
29
+ print(dataM1['Open'].dtype)
30
+ print(dataM1['High'].dtype)
31
+ print(dataM1['Low'].dtype)
32
+ print(dataM1['Close'].dtype)
33
+ print(dataM1['A'].dtype)
34
+ print(dataM1['f'].dtype)
35
+
36
+ float64
37
+ float64
38
+ float64
39
+ float64
40
+ object
41
+ int64
42
+ ```
43
+ この結果より、Aの列に数字でないデータが入っている可能性があるということで、
44
+ ヒストリカルデータを別のサイトから取得し、同様に実行してみました。
45
+ [参照データ](https://www.axiory.com/jp/how-to-install/mt4-historical-data)
46
+ 以下dtypeでの実行結果です。
47
+ ```python
48
+ print(dataM1['Open'].dtype)
49
+ print(dataM1['High'].dtype)
50
+ print(dataM1['Low'].dtype)
51
+ print(dataM1['Close'].dtype)
52
+ print(dataM1['A'].dtype)
53
+ print(dataM1['f'].dtype)
54
+
55
+ float64
56
+ float64
57
+ float64
58
+ float64
59
+ object
60
+ int64
61
+ ```
62
+ やはりAの列でobjectが表示されてしまいます。
63
+
64
+ ```python
23
65
  #pandasとnumpyをインポート
24
66
  import pandas as pd
25
67
  import numpy as np

1

CSVファイルを参照したURLのリンクと、エラー内容の全文を掲載させて頂きました。(エラー内容長文となってしまい、申し訳ありません。)よろしくお願い致します。

2021/05/05 03:55

投稿

y_masa.teratail
y_masa.teratail

スコア13

title CHANGED
File without changes
body CHANGED
@@ -16,6 +16,7 @@
16
16
  よろしくお願いいたします。
17
17
 
18
18
  [参考ページ](https://qiita.com/kazama1209/items/98f63b624f3987ba3322)
19
+ [csvファイル参照](http://www.histdata.com)
19
20
 
20
21
 
21
22
  ```python
@@ -50,4 +51,104 @@
50
51
  10
51
52
  ---> 11 ohlc_1h = make_mtf_ohlc(dataM1, '1H')
52
53
  12 ohlc_1h
54
+
55
+ <ipython-input-4-5bc21324f74e> in make_mtf_ohlc(dataM1, tf)
56
+ 1 def make_mtf_ohlc(dataM1, tf):
57
+ ----> 2 x = dataM1.resample(tf).ohlc()
58
+ 3 O = x['Open']['open']
59
+ 4 H = x['High']['high']
60
+ 5 L = x['Low']['low']
61
+
62
+ ~/anaconda3/lib/python3.7/site-packages/pandas/core/resample.py in f(self, _method, *args, **kwargs)
63
+ 863 def f(self, _method=method, *args, **kwargs):
64
+ 864 nv.validate_resampler_func(_method, args, kwargs)
65
+ --> 865 return self._downsample(_method)
66
+ 866 f.__doc__ = getattr(GroupBy, method).__doc__
67
+ 867 setattr(Resampler, method, f)
68
+
69
+ ~/anaconda3/lib/python3.7/site-packages/pandas/core/resample.py in _downsample(self, how, **kwargs)
70
+ 1018 # we want to call the actual grouper method here
71
+ 1019 result = obj.groupby(
72
+ -> 1020 self.grouper, axis=self.axis).aggregate(how, **kwargs)
73
+ 1021
74
+ 1022 result = self._apply_loffset(result)
75
+
76
+ ~/anaconda3/lib/python3.7/site-packages/pandas/core/groupby/generic.py in aggregate(self, arg, *args, **kwargs)
77
+ 1313 @Appender(_shared_docs['aggregate'])
78
+ 1314 def aggregate(self, arg, *args, **kwargs):
79
+ -> 1315 return super(DataFrameGroupBy, self).aggregate(arg, *args, **kwargs)
80
+ 1316
81
+ 1317 agg = aggregate
82
+
83
+ ~/anaconda3/lib/python3.7/site-packages/pandas/core/groupby/generic.py in aggregate(self, arg, *args, **kwargs)
84
+ 184
85
+ 185 _level = kwargs.pop('_level', None)
86
+ --> 186 result, how = self._aggregate(arg, _level=_level, *args, **kwargs)
87
+ 187 if how is None:
88
+ 188 return result
89
+
90
+ ~/anaconda3/lib/python3.7/site-packages/pandas/core/base.py in _aggregate(self, arg, *args, **kwargs)
91
+ 354 if isinstance(arg, compat.string_types):
92
+ 355 return self._try_aggregate_string_function(arg, *args,
93
+ --> 356 **kwargs), None
94
+ 357
95
+ 358 if isinstance(arg, dict):
96
+
97
+ ~/anaconda3/lib/python3.7/site-packages/pandas/core/base.py in _try_aggregate_string_function(self, arg, *args, **kwargs)
98
+ 310 if f is not None:
99
+ 311 if callable(f):
100
+ --> 312 return f(*args, **kwargs)
101
+ 313
102
+ 314 # people may try to aggregate on a non-callable attribute
103
+
104
+ ~/anaconda3/lib/python3.7/site-packages/pandas/core/groupby/groupby.py in ohlc(self)
105
+ 1317
106
+ 1318 return self._apply_to_column_groupbys(
107
+ -> 1319 lambda x: x._cython_agg_general('ohlc'))
108
+ 1320
109
+ 1321 @Appender(DataFrame.describe.__doc__)
110
+
111
+ ~/anaconda3/lib/python3.7/site-packages/pandas/core/groupby/generic.py in _apply_to_column_groupbys(self, func)
112
+ 1490 (func(col_groupby) for _, col_groupby
113
+ 1491 in self._iterate_column_groupbys()),
114
+ -> 1492 keys=self._selected_obj.columns, axis=1)
115
+ 1493
116
+ 1494 def _fill(self, direction, limit=None):
117
+
118
+ ~/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/concat.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, sort, copy)
119
+ 226 keys=keys, levels=levels, names=names,
120
+ 227 verify_integrity=verify_integrity,
121
+ --> 228 copy=copy, sort=sort)
122
+ 229 return op.get_result()
123
+ 230
124
+
125
+ ~/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/concat.py in __init__(self, objs, axis, join, join_axes, keys, levels, names, ignore_index, verify_integrity, copy, sort)
126
+ 257 objs = [objs[k] for k in keys]
127
+ 258 else:
128
+ --> 259 objs = list(objs)
129
+ 260
130
+ 261 if len(objs) == 0:
131
+
132
+ ~/anaconda3/lib/python3.7/site-packages/pandas/core/groupby/generic.py in <genexpr>(.0)
133
+ 1488 from pandas.core.reshape.concat import concat
134
+ 1489 return concat(
135
+ -> 1490 (func(col_groupby) for _, col_groupby
136
+ 1491 in self._iterate_column_groupbys()),
137
+ 1492 keys=self._selected_obj.columns, axis=1)
138
+
139
+ ~/anaconda3/lib/python3.7/site-packages/pandas/core/groupby/groupby.py in <lambda>(x)
140
+ 1317
141
+ 1318 return self._apply_to_column_groupbys(
142
+ -> 1319 lambda x: x._cython_agg_general('ohlc'))
143
+ 1320
144
+ 1321 @Appender(DataFrame.describe.__doc__)
145
+
146
+ ~/anaconda3/lib/python3.7/site-packages/pandas/core/groupby/groupby.py in _cython_agg_general(self, how, alt, numeric_only, min_count)
147
+ 836
148
+ 837 if len(output) == 0:
149
+ --> 838 raise DataError('No numeric types to aggregate')
150
+ 839
151
+ 840 return self._wrap_aggregated_output(output, names)
152
+
153
+ DataError: No numeric types to aggregate
53
154
  ```