angu
@angu

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

iteritems is deprecated and will be removed in a future version. Use .items instead. for col_name, dtype in df.dtypes.iteritems():というエラーメッセージについて

解決したいこと

pythonで株価可視化アプリ作成のさいに出てきたエラーメッセージです
直訳すると、iteritems は非推奨であり、将来のバージョンで削除される予定です。代わりに .items を使用してください。とのことですが、よくわかりません、どのように対処すればよろしいでしょうか。

col_name の場合、df.dtypes.iteritems() の dtype:

発生している問題・エラー

iteritems is deprecated and will be removed in a future version. Use .items instead.   for col_name, dtype in df.dtypes.iteritems():
!pip3 install yfinance

import pandas as pd
import matplotlib.pyplot as plt
import yfinance as yf

%matplotlib inline

aapl = yf.Ticker('AAPL')

days = 20
hist = aapl.history(period=f'{days}d')
hist

hist.reset_index()

hist_msft = yf.Ticker('MSFT').history(period=f'{days}d')
hist_msft.head()

pd.concat([hist, hist_msft], axis=1).head()

hist.head(3)


hist.index

hist.index = hist.index.strftime('%d %B %Y')

hist = hist[['Close']]
hist.columns = ['aaple']
hist.head()

hist = hist.T
hist

hist.index.name = 'Name'

    days = 20
    hist = yf.Ticker('META').history(period = f"{days}d")
    print(hist.index)
    hist.index = hist.index.strftime("%d %B %Y")

def get_data(days, tickers):
    df = pd.DataFrame()
    for company in tickers.keys():
    # company = 'facebook'
        tkr = yf.Ticker(tickers[company])
        hist = tkr.history(period=f'{days}d')
        hist.index = hist.index.strftime('%d %B %Y')
        hist = hist[['Close']]
        hist.columns = [company]
        hist = hist.T
        hist.index.name = 'Name'
        df = pd.concat([df, hist])
    return df

days = 20
tickers = {
'apple': 'AAPL',
'facebook': 'META',
'google': 'GOOGL',
'microsoft': 'MSFT',
'netflix': 'NFLX',
'amazon': 'AMZN'
}

df = get_data(days, tickers)
df

aapl = yf.Ticker('AAPL')

aapl.info

aapl.actions.head()

aapl.dividends.plot()

!pip3 install altair

import altair as alt

df

companies = ['apple','facebook']
data = df.loc[companies]
data

data.sort_index()

data = data.T.reset_index()
data.head()

ata = pd.melt(data, id_vars=['Date']).rename(
    columns={'value': 'Stock Prices(USD)'}
)
data

data = pd.melt(data, id_vars=['Date']).rename(
    columns={'value': 'Stock Prices(USD)'}
)
data

ymin, ymax = 250, 300

chart = (
    alt.Chart(data)
    .mark_line(opacity=0.8, clip=True)
    .encode(
        x="Date:T",
        y=alt.Y("Stock Prices(USD):Q", stack=None, scale=alt.Scale(domain=[ymin, ymax])),
        color='Name:N'
    )
)


chart


スクリーンショット 2022-09-24 2.38.35.png

自分で試したこと
スクリーンショット 2022-09-25 2.49.44.png

追記
@PondVillege

おかげさまでアプリを完成させることができました!ありがとうございます!!!スクリーンショット 2022-09-25 5.08.59.png

2

1Answer

Pandasのバージョン1.5.0未満であればまだ普通に使えますが,最新の1.5.0では非推奨になりました.

内容を抜粋すると

Deprecated DataFrame.iteritems(), Series.iteritems(), HDFStore.iteritems() in favor of DataFrame.items(), Series.items(), HDFStore.items() (GH45321)

の箇所で明言されています.

このバージョンはつい5日前のリリースだったのでaltairの更新が追いつかず,非推奨になったコードでライブラリが記述されていること原因です.

今回は ErrorではなくWarning なのでコードも問題なく動いている,といった状態ですので,無視しても大丈夫です.

どうしても気になるのなら,Warningメッセージで指摘されている

/Users/your_id/.pyenv/versions/3.10.4/lib/python3.10/site-packages/altair/utils/core.py:317: FutureWarning: iteritems is deprecated and will be removed in a future version. Use .items instead.
 for col_name, dtype in df.dtypes.iteritems():

のファイルの該当箇所を変更して自力でaltairをアップデートすれば良いと思います.

-   for col_name, dtype in df.dtypes.iteritems():
+   for col_name, dtype in df.dtypes.items():

もしくはPandas自体を1つ前のバージョンにダウングレードする,もしくはaltiarのアップデートを待つ,ぐらいの対処しかありません.

2Like

Comments

  1. @angu

    Questioner


    @angu

    2022年09月25日に投稿
    ご回答ありがとうございます!大変初歩的な質問で恐縮なのですが、メッセージをどこで変更すればいいかわかりません、また上の一枚目の画像のように株価のチャートが出力されいません、、解答ではErrorではなくWarning なのでコードも問題なく動いているとのことですが、なぜチャートは出力されないのでしょうか。

    お忙しいところ大変申し訳ございませんが、よろしければご回答お待ちしております。

  2. 言葉足らずで申し訳ありません,Warningで示されているファイル
    /Users/your_id/.pyenv/versions/3.10.4/lib/python3.10/site-packages/altair/utils/core.py
    の317行目が,変更すべき場所に該当します.テキストエディタ等でこのcore.pyを開いて,上のように変更するとWarningが消えるはずです.

    チャートが出力されていないというより,設定したymin, ymaxが250~300であるのに対して,実際の表示したいデータは140~170に存在するので,グラフ上に見えていないというだけだと思います.
  3. @angu

    Questioner

    @PondVillege様

    何度も質問に回答いただきありがとうございます!
    プログラミングを途中まで挫折しかけていた自分に手を差し伸べて下さって本当に感謝しかありません、おかげさまで、アプリを完成させることができました!
  4. 完成まで応援できて良かったです,完成おめでとうございます!
    これからも色んなエラーに出会うと思いますが,数をこなして1人で解決できるよう,期待しております

Your answer might help someone💌