Python 熊猫数据帧键错误oop
此脚本的目的是读取csv文件 该文件包含外汇数据 该文件有7列Date、Time、Open、High、Low、Close和Volume,以及大约600k行 在删除日期和时间后,脚本必须进行一些日期时间计算,如月份和日期 然后利用TA-LIB库进行技术分析 代码如下:Python 熊猫数据帧键错误oop,python,pandas,oop,ta-lib,Python,Pandas,Oop,Ta Lib,此脚本的目的是读取csv文件 该文件包含外汇数据 该文件有7列Date、Time、Open、High、Low、Close和Volume,以及大约600k行 在删除日期和时间后,脚本必须进行一些日期时间计算,如月份和日期 然后利用TA-LIB库进行技术分析 代码如下: import pandas as pd import talib class Data: def __init__(self): self.df = pd.DataFrame() self
import pandas as pd
import talib
class Data:
def __init__(self):
self.df = pd.DataFrame()
self.names = ['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume']
self.open = self.df['Open'].astype(float)
self.high = self.df['High'].astype(float)
self.low = self.df['Low'].astype(float)
self.close = self.df['Close'].astype(float)
def file(self, file):
self.df = pd.read_csv(file, names=self.names,
parse_dates={'Release Date': ['Date', 'Time']})
return self.df
def date(self):
self.df['Release Date'] = pd.to_datetime(self.df['Release Date'])
def year(self):
self.df['year'] = pd.to_datetime(self.df['Release Date']).dt.year
def month(self):
self.df['year'] = pd.to_datetime(self.df['Release Date']).dt.month
def day(self):
self.df['year'] = pd.to_datetime(self.df['Release Date']).dt.day
def dema(self):
# DEMA - Double Exponential Moving Average
self.df['DEMA'] = talib.DEMA(self.close, timeperiod=30)
def ema(self):
# EMA - Exponential Moving Average
self.df['EMA'] = talib.EMA(self.close, timeperiod=30)
def HT_TRENDLINE(self):
# HT_TRENDLINE - Hilbert Transform - Instantaneous Trendline
self.df['HT_TRENDLINE '] = talib.HT_TRENDLINE(self.close)
def KAMA(self):
# KAMA - Kaufman Adaptive Moving Average
self.df['KAMA'] = talib.KAMA(self.close, timeperiod=30)
def ma(self):
# MA - Moving average
self.df['MA'] = talib.MA(self.close, timeperiod=30, matype=0)
def print(self):
return print(self.df.head())
x = Data()
x.file(r"D:\Projects\Project Forex\USDJPY.csv")
x.print()
以下是错误:
Traceback (most recent call last):
File "C:\Users\Sayed\miniconda3\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Open'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Sayed/PycharmProjects/project/Technical Analysis.py", line 55, in <module>
x = Data()
File "C:/Users/Sayed/PycharmProjects/project/Technical Analysis.py", line 9, in __init__
self.open = self.df['Open'].astype(float)
File "C:\Users\Sayed\miniconda3\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\Sayed\miniconda3\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Open'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Sayed\miniconda3\lib\site packages\pandas\core\index\base.py”,第2646行,在get\U loc中
返回发动机。获取位置(钥匙)
pandas.\u libs.index.IndexEngine.get\u loc中第111行的文件“pandas\\u libs\index.pyx”
pandas.\u libs.index.IndexEngine.get\u loc中的文件“pandas\\u libs\index.pyx”,第138行
pandas.\u libs.hashtable.PyObjectHashTable.get\u项中的文件“pandas\\u libs\hashtable\u class\u helper.pxi”,第1619行
pandas.\u libs.hashtable.PyObjectHashTable.get\u项中的文件“pandas\\u libs\hashtable\u class\u helper.pxi”,第1627行
KeyError:“打开”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“C:/Users/Sayed/PycharmProjects/project/Technical Analysis.py”,第55行,在
x=数据()
文件“C:/Users/Sayed/PycharmProjects/project/Technical Analysis.py”,第9行,在__
self.open=self.df['open'].astype(浮动)
文件“C:\Users\Sayed\miniconda3\lib\site packages\pandas\core\frame.py”,第2800行,在\uu getitem中__
indexer=self.columns.get_loc(键)
文件“C:\Users\Sayed\miniconda3\lib\site packages\pandas\core\index\base.py”,第2648行,在get\U loc中
返回self.\u引擎。获取\u loc(self.\u可能\u cast\u索引器(键))
pandas.\u libs.index.IndexEngine.get\u loc中第111行的文件“pandas\\u libs\index.pyx”
pandas.\u libs.index.IndexEngine.get\u loc中的文件“pandas\\u libs\index.pyx”,第138行
pandas.\u libs.hashtable.PyObjectHashTable.get\u项中的文件“pandas\\u libs\hashtable\u class\u helper.pxi”,第1619行
pandas.\u libs.hashtable.PyObjectHashTable.get\u项中的文件“pandas\\u libs\hashtable\u class\u helper.pxi”,第1627行
KeyError:“打开”
在\uuuu init\uuu
函数中,您正在初始化没有任何列的空数据帧。但在第1行之后,您正试图将数据帧的Open
列转换为float
def __init__(self):
self.df = pd.DataFrame() # No columns
self.names = ['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume']
self.open = self.df['Open'].astype(float) # ERROR: 'Open' column does not exist
self.high = self.df['High'].astype(float)
self.low = self.df['Low'].astype(float)
self.close = self.df['Close'].astype(float)
将init函数更改为此,它应该可以工作
def __init__(self):
self.names = ['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume']
self.df = pd.DataFrame(columns=self.names) # Empty dataframe with columns
self.open = self.df['Open'].astype(float) # Now 'Open' column exists
self.high = self.df['High'].astype(float)
self.low = self.df['Low'].astype(float)
self.close = self.df['Close'].astype(float)
错误提示没有
打开
列,请检查您的csv。有打开column@SayedGouda编辑您的问题以列出df.列的输出
。您的错误表明,Open
列不存在。也许有一个案例不同?pd.read\u csv(r“D:\Projects\Project Forex\USDJPY.csv”)。列返回什么?同样的错误问题中的错误在第x=Data()行上。您是否在建议的更改中遇到相同的错误?我试过运行它,它似乎在工作