前提・実現したいこと
データセットの1日ごとの平均価格を集計した上で、日毎にグラフにプロットしようとしています。
データセットはcsv形式で読み込み、
#read csv import pandas as pd pd.set_option('display.max_columns', 8) df = pd.read_csv("sample.csv" , encoding='cp932', header=None)
今回使用するカラムはcsv読み込み時には、どちらもobject型になっています。
2017/1/1 1111 2017/1/1 2300 2017/1/1 9600 2017/1/2 1930 2017/1/2 1999 2017/1/2 8000
そのため、以下のようにデータ型変換を行い、
カラム0はdatetime64[ns]に、カラム1はint64に変更して作業しています。
import pandas as pd df[0] = pd.to_datetime(df[0]) df[1] = df[1].astype(int)
発生している問題・エラーメッセージ
日毎の平均値を出してグラフにプロットしようとしたところ、以下のエラーが発生しました。
修正方法がわかりません。
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-80-c2a1104544ac> in <module>() 1 #日別と平均値価格 ----> 2 daily = pd.DataFrame(df[13].resample("D").mean()) ~/.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages/pandas/core/generic.py in resample(self, rule, how, axis, fill_method, closed, label, convention, kind, loffset, limit, base, on, level) 4727 axis=axis, kind=kind, loffset=loffset, 4728 convention=convention, -> 4729 base=base, key=on, level=level) 4730 return _maybe_process_deprecations(r, 4731 how=how, ~/.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages/pandas/core/resample.py in resample(obj, kind, **kwds) 967 """ create a TimeGrouper and return our resampler """ 968 tg = TimeGrouper(**kwds) --> 969 return tg._get_resampler(obj, kind=kind) 970 971 ~/.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages/pandas/core/resample.py in _get_resampler(self, obj, kind) 1089 raise TypeError("Only valid with DatetimeIndex, " 1090 "TimedeltaIndex or PeriodIndex, " -> 1091 "but got an instance of %r" % type(ax).__name__) 1092 1093 def _get_grouper(self, obj): TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Int64Index'
該当のソースコード
python
1#日別と平均値価格 2daily = pd.DataFrame(df[1].resample("D").mean())
補足情報(FW/ツールのバージョンなど)
Python 3.6.0 :: Anaconda 4.3.0

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/08/13 04:00
2018/08/13 15:43
退会済みユーザー
2018/08/15 00:48