質問編集履歴

3

誤字修正

2021/09/03 20:44

投稿

yutakao
yutakao

スコア10

test CHANGED
File without changes
test CHANGED
@@ -258,7 +258,7 @@
258
258
 
259
259
  dummy=list(tickers.tickers.values())[**1**].balance_sheet.T["Total Stockholder Equity"]
260
260
 
261
- とダミー作成用に作成使用するインデックスを変えると。
261
+ とダミー作成用に使用するインデックスを変えると。
262
262
 
263
263
  1332.T 1333.T 1414.T 1605.T ^N225
264
264
 

2

初期エラー対策後、次の類似コードに同対応した際に理由不明の意図せぬ結果が返ってくる。

2021/09/03 20:44

投稿

yutakao
yutakao

スコア10

test CHANGED
File without changes
test CHANGED
@@ -175,3 +175,131 @@
175
175
 
176
176
 
177
177
  分かる方、ご教示いただきたくお願いいたします。
178
+
179
+
180
+
181
+ 追記
182
+
183
+ 上記のエラーはmeg_様からのご教示により対応できましたが…
184
+
185
+ 次の同様のコードにて動作不備が発生しました。
186
+
187
+
188
+
189
+ ```ここに言語を入力
190
+
191
+ equity = [] # 自己資本
192
+
193
+
194
+
195
+ dummy=list(tickers.tickers.values())[0].balance_sheet.T["Total Stockholder Equity"]
196
+
197
+ dummy[:] = np.nan
198
+
199
+
200
+
201
+ for i in tickers.tickers:
202
+
203
+ try:
204
+
205
+ equity.append(tickers.tickers[i].balance_sheet.T["Total Stockholder Equity"])
206
+
207
+ except:
208
+
209
+ equity.append(dummy) # エラー発生時はダミーを入れる
210
+
211
+
212
+
213
+ equity = pd.DataFrame(equity).T # DataFrame化
214
+
215
+ equity.columns = stocks # カラム名の設定
216
+
217
+
218
+
219
+ print(equity)
220
+
221
+ ```
222
+
223
+ 上記の実行結果(結果1)は
224
+
225
+ 1332.T 1333.T 1414.T 1605.T ^N225
226
+
227
+
228
+
229
+ 2018-03-31 NaN 1.154590e+11 NaN 2.916679e+12 NaN
230
+
231
+ 2018-06-30 NaN NaN 7.409500e+10 NaN NaN
232
+
233
+ 2019-03-31 NaN 1.253530e+11 NaN 3.006479e+12 NaN
234
+
235
+ 2019-06-30 NaN NaN 7.806600e+10 NaN NaN
236
+
237
+ 2019-12-31 NaN NaN NaN 3.040774e+12 NaN
238
+
239
+ 2020-03-31 NaN 1.326280e+11 NaN NaN NaN
240
+
241
+ 2020-06-30 NaN NaN 8.359900e+10 NaN NaN
242
+
243
+ 2020-12-31 NaN NaN NaN 2.736540e+12 NaN
244
+
245
+ 2021-03-31 NaN 1.428330e+11 NaN NaN NaN
246
+
247
+ 2021-06-30 NaN NaN 9.089500e+10 NaN NaN
248
+
249
+
250
+
251
+ となります。…が、1332.Tはエラーではないので1333.T,1414.T,1605.Tと同様にデータが取得できるはずなのです。
252
+
253
+ ちなみに上記実行後に上記コードの
254
+
255
+ dummy=list(tickers.tickers.values())[**0**].balance_sheet.T["Total Stockholder Equity"]
256
+
257
+
258
+
259
+ dummy=list(tickers.tickers.values())[**1**].balance_sheet.T["Total Stockholder Equity"]
260
+
261
+ とダミー作成用に作成使用するインデックスを変えると。
262
+
263
+ 1332.T 1333.T 1414.T 1605.T ^N225
264
+
265
+
266
+
267
+ 2018-03-31 NaN NaN NaN 2.916679e+12 NaN
268
+
269
+ 2018-06-30 NaN NaN 7.409500e+10 NaN NaN
270
+
271
+ 2019-03-31 NaN NaN NaN 3.006479e+12 NaN
272
+
273
+ 2019-06-30 NaN NaN 7.806600e+10 NaN NaN
274
+
275
+ 2019-12-31 NaN NaN NaN 3.040774e+12 NaN
276
+
277
+ 2020-03-31 NaN NaN NaN NaN NaN
278
+
279
+ 2020-06-30 NaN NaN 8.359900e+10 NaN NaN
280
+
281
+ 2020-12-31 NaN NaN NaN 2.736540e+12 NaN
282
+
283
+ 2021-03-31 NaN NaN NaN NaN NaN
284
+
285
+ 2021-06-30 NaN NaN 9.089500e+10 NaN NaN
286
+
287
+
288
+
289
+ と1332.Tはエラーのままで追加で1333.Tまでエラー扱いになります。
290
+
291
+ (同様にインデックスを2,3と変えれば1414.Tもエラー扱い。1605.Tもエラー扱いになっていきます。)
292
+
293
+
294
+
295
+ ちなみに、tikersの中身を再度入れなおすと元の結果1に戻ります。
296
+
297
+
298
+
299
+ 根本的にダミー作成のためにリストを作成して(適当な)該当tickersの.balance_sheet.T["Total Stockholder Equity"]を引っ張るだけで
300
+
301
+ for i in tickers.tickers:以降の.balance_sheet.T["Total Stockholder Equity"]に影響を与える意味が分かりません。。。。
302
+
303
+
304
+
305
+ 理由と対策わかられる方、ご教示の程宜しくお願い致します。

1

エラー追記

2021/09/03 20:42

投稿

yutakao
yutakao

スコア10

test CHANGED
File without changes
test CHANGED
@@ -60,6 +60,108 @@
60
60
 
61
61
 
62
62
 
63
+ エラー内容追記
64
+
65
+ ```ここに言語を入力
66
+
67
+ KeyError Traceback (most recent call last)
68
+
69
+ ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
70
+
71
+ 3080 try:
72
+
73
+ -> 3081 return self._engine.get_loc(casted_key)
74
+
75
+ 3082 except KeyError as err:
76
+
77
+
78
+
79
+ pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
80
+
81
+
82
+
83
+ pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
84
+
85
+
86
+
87
+ pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
88
+
89
+
90
+
91
+ pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
92
+
93
+
94
+
95
+ KeyError: 'Net Income'
96
+
97
+
98
+
99
+ The above exception was the direct cause of the following exception:
100
+
101
+
102
+
103
+ KeyError Traceback (most recent call last)
104
+
105
+ <ipython-input-76-21ebd2ac4cd6> in <module>
106
+
107
+ 9 try:
108
+
109
+ ---> 10 earnings.append(tickers.tickers[i].financials.T["Net Income"])
110
+
111
+ 11 except:
112
+
113
+
114
+
115
+ ~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
116
+
117
+ 3023 return self._getitem_multilevel(key)
118
+
119
+ -> 3024 indexer = self.columns.get_loc(key)
120
+
121
+ 3025 if is_integer(indexer):
122
+
123
+
124
+
125
+ ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
126
+
127
+ 3082 except KeyError as err:
128
+
129
+ -> 3083 raise KeyError(key) from err
130
+
131
+ 3084
132
+
133
+
134
+
135
+ KeyError: 'Net Income'
136
+
137
+
138
+
139
+ During handling of the above exception, another exception occurred:
140
+
141
+
142
+
143
+ NameError Traceback (most recent call last)
144
+
145
+ <ipython-input-76-21ebd2ac4cd6> in <module>
146
+
147
+ 10 earnings.append(tickers.tickers[i].financials.T["Net Income"])
148
+
149
+ 11 except:
150
+
151
+ ---> 12 earnings.append(dummy) # エラー発生時はダミーを入れる
152
+
153
+ 13
154
+
155
+ 14
156
+
157
+
158
+
159
+ NameError: name 'dummy' is not defined
160
+
161
+
162
+
163
+ ```
164
+
63
165
 
64
166
 
65
167
  > dummy = tickers.tickers[0].financials.T["Net Income"]