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

質問編集履歴

2

参考にしたリンク先の追加

2020/03/14 11:32

投稿

Sunset_Yuhi
Sunset_Yuhi

スコア15

title CHANGED
File without changes
body CHANGED
@@ -17,6 +17,7 @@
17
17
 
18
18
  警告文は、
19
19
  ```
20
+ SettingWithCopyWarning:
20
21
  A value is trying to be set on a copy of a slice from a DataFrame.
21
22
  Try using .loc[row_indexer,col_indexer] = value instead
22
23
 
@@ -26,6 +27,10 @@
26
27
  ### 試したこと
27
28
  警告文にあるリンク先の情報などを基に、①の行をdf_PL.loc[:,"決算期"] = df_PL.loc[:,"決算期"].str[-7:]とか、df_PL["決算期"] = df_PL["決算期"].copy().str[-7:]などと書いてみましたが、警告文は消えませんでした。
28
29
 
30
+ <参考リンク>
31
+ [Returning a view versus a copy - pandas](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy)
32
+ [pandas の SettingWithCopyWarning で苦労した話](https://qiita.com/HEM_SP/items/56cd62a1c000d342bd70)
33
+
29
34
  そこで、より単純なデータフレームを作って同じ処理をした所、こちらではSettingWithCopyWarningの警告は出ませんでした。株探のサイトからクローリングしたデータに何か問題があるのかと思いましたが、原因はよく分からないままです。
30
35
  ```Python
31
36
  #警告が出ないプログラム

1

プログラムを修正

2020/03/14 11:32

投稿

Sunset_Yuhi
Sunset_Yuhi

スコア15

title CHANGED
File without changes
body CHANGED
@@ -7,8 +7,9 @@
7
7
  import pandas as pd
8
8
 
9
9
  url = "https://kabutan.jp/stock/finance?code=7203"
10
- df_list = pd.read_html(url)
10
+ df_list = pd.read_html(url) #html内にあるテーブルをデータフレームとして取得
11
11
  df_PL = df_list[3].dropna()
12
+ #print(df_PL.dtypes)
12
13
 
13
14
  df_PL["決算期"] = df_PL["決算期"].str[-7:] #①年月だけ抽出
14
15
  print(df_PL)
@@ -35,7 +36,6 @@
35
36
  "U 2017.03",
36
37
  "U 2018.03",
37
38
  ]
38
-
39
39
  #print(df_PL2.dtypes)
40
40
 
41
41
  df_PL2["決算期"] = df_PL2["決算期"].str[-7:] #年月だけ抽出