いくつかの.CVSファイルを解析し、いくつかの列のすべてのデータを取得して、データをデータフレーム(dfsと呼ばれる)に配置するコードがあります。現在、2つの日付の間のみのdfs内のすべてのデータフィールドを返そうとしています。
私はコマンドを使おうとしています:
return dfs[(dfs['date'] >= startDate) & (dfs['date'] <= endDate)]
ただし、次のエラーが発生します。
KeyError: 'date'
誰かが私が間違ったことを教えてもらえますか?私のコードについては、以下を参照してください。
def getTimeseriesData(path,column_num,startDate,endDate):
colNames = ['date']
dfs = []
allfiles = glob.glob(os.path.join(path, "*.csv"))
for fname in allfiles:
name = os.path.splitext(fname)[0]
name = os.path.split(name)[1]
colNames.append(name)
df = pd.read_csv(fname, header=None, usecols=[0, column_num,4,5],
parse_dates=[0], dayfirst=True,
index_col=[0], names=['date', name+'_LAST',name+'_VOLUME',name+'_MKTCAP'])
df = df.groupby(level=0).agg('mean')
dfs.append(df)
dfs = pd.concat(dfs, axis=1)
return dfs[(dfs['date'] >= startDate) & (dfs['date'] <= endDate)] #<<--I think this is the problem
dfsのヘッド(2つの日付の間(たとえば2001-01-03と2001-01-05の間)のデータを返したい)は次のようになります。
BBG.XLON.BTA.S_LAST BBG.XLON.BTA.S_VOLUME BBG.XLON.BTA.S_MKTCAP \
date
2001-01-02 572 26605510 37494.60
2001-01-03 560 24715470 36708.00
2001-01-04 613 52781855 40182.15
2001-01-05 630 56600152 41296.50
2001-01-08 633 41014402 41493.15
BBG.XLON.VOD.S_LAST BBG.XLON.VOD.S_VOLUME BBG.XLON.VOD.S_MKTCAP
date
2001-01-02 NaN NaN NaN
2001-01-03 225.00 444328736 145216.0020
2001-01-04 239.00 488568000 154251.6643
2001-01-05 242.25 237936704 156349.2288
2001-01-08 227.75 658059776 146990.8642
これdate
は、列名ではなく、インデックスの名前です。
変化する:
return dfs[(dfs['date'] >= startDate) & (dfs['date'] <= endDate)]
に:
return dfs[(dfs.index >= startDate) & (dfs.index <= endDate)]
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加