Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:float()参数必须是字符串或数字,而不是';时间戳'';_Python_Pandas_Dataframe_Date - Fatal编程技术网

Python TypeError:float()参数必须是字符串或数字,而不是';时间戳'';

Python TypeError:float()参数必须是字符串或数字,而不是';时间戳'';,python,pandas,dataframe,date,Python,Pandas,Dataframe,Date,我正在尝试建立一个股票预测算法=,但我无法遵守我的日期。我想预测未来10%的股票。我遇到了很多错误,这就是我目前遇到的问题TypeError:float()参数必须是字符串或数字,而不是“Timestamp” df = pd.read_csv('TATASTEEL.NS (1).csv', parse_dates=['Date']) # import stock data df = df[['Date', 'Open', 'Adj Close', 'Low', 'High', 'Volume

我正在尝试建立一个股票预测算法=,但我无法遵守我的日期。我想预测未来10%的股票。我遇到了很多错误,这就是我目前遇到的问题
TypeError:float()参数必须是字符串或数字,而不是“Timestamp”

df = pd.read_csv('TATASTEEL.NS (1).csv', parse_dates=['Date'])  # import  stock data
df = df[['Date', 'Open', 'Adj Close', 'Low', 'High', 'Volume']]
df = df['Date'].map(lambda x: x.strftime("%d/%m/%Y"))
df['PCT_CHNG'] = (df['Adj Close'] - df['Open']) / df['Open'] * 100.0
df['HL_PCT'] = (df['High'] - df['Adj Close']) / df['Adj Close'] * 100.0
# df['Date'] = pd.to_datetime(df['Date'])  # helping pandas to make out that the date  column represents..date
df.sort_values(by='Date', inplace=True)
这显示了一些输出,但限制了我应用模型的其余部分,即:

forecast_col = 'Adj Close'
df.fillna(-999999, inplace=True)
forecast_out = int(math.ceil(0.1 * len(df)))
df['label'] = df[forecast_col].shift(-forecast_out)  # shifting down 10 spaces that's all
df.dropna(inplace=True)
df['PCT_CHNG_label'] = (df['Adj Close'] - df['label']) / df['Adj Close'] * 100.0

print(df.head())
print(df.tail())

X = np.array(df.drop(['label'], 1))  # I don't know why we dropped label and kept the rest
y = np.array(df['label'])  # I understand that we might need label as a parameter in this case
X = preprocessing.scale(X)
print(len(X), len(y))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

clf = LinearRegression()
clf.fit(X_train, y_train)
clf.score(X_test, y_test)
accuracy = clf.score(X_test, y_test)
print(accuracy)
完全回溯错误:

Traceback (most recent call last):
  File "F:/Yash Vyas/My Projects/Internship/TataSteel stock prediction.py", line 30, in <module>
    X = preprocessing.scale(X)
  File "F:\Yash Vyas\My Projects\Internship\venv\lib\site-packages\sklearn\utils\validation.py", line 72, in inner_f
    return f(**kwargs)
  File "F:\Yash Vyas\My Projects\Internship\venv\lib\site-packages\sklearn\preprocessing\_data.py", line 141, in scale
    X = check_array(X, accept_sparse='csc', copy=copy, ensure_2d=False,
  File "F:\Yash Vyas\My Projects\Internship\venv\lib\site-packages\sklearn\utils\validation.py", line 72, in inner_f
    return f(**kwargs)
  File "F:\Yash Vyas\My Projects\Internship\venv\lib\site-packages\sklearn\utils\validation.py", line 598, in check_array
    array = np.asarray(array, order=order, dtype=dtype)
  File "F:\Yash Vyas\My Projects\Internship\venv\lib\site-packages\numpy\core\_asarray.py", line 85, in asarray
    return array(a, dtype, copy=False, order=order)
TypeError: float() argument must be a string or a number, not 'Timestamp'
回溯(最近一次呼叫最后一次):
文件“F:/Yash Vyas/My Projects/Interporation/TataSteel stock prediction.py”,第30行,在
X=预处理。比例(X)
文件“F:\Yash Vyas\My Projects\Interporation\venv\lib\site packages\sklearn\utils\validation.py”,第72行,位于内部
返回f(**kwargs)
文件“F:\Yash Vyas\My Projects\Interporation\venv\lib\site packages\sklearn\preprocessing\\ U data.py”,第141行,按比例排列
X=检查数组(X,接受稀疏=csc',复制=复制,确保2d=假,
文件“F:\Yash Vyas\My Projects\Interporation\venv\lib\site packages\sklearn\utils\validation.py”,第72行,位于内部
返回f(**kwargs)
文件“F:\Yash Vyas\My Projects\Interporation\venv\lib\site packages\sklearn\utils\validation.py”,第598行,在check\u数组中
array=np.asarray(array,order=order,dtype=dtype)
文件“F:\Yash Vyas\My Projects\Interporation\venv\lib\site packages\numpy\core\\u asarray.py”,第85行,在asarray中
返回数组(a,数据类型,copy=False,order=order)
TypeError:float()参数必须是字符串或数字,而不是“Timestamp”

我认为您使用的“日期”是其中一个特性。因此,要么从X中删除日期,要么在除以X和y之前将日期作为索引

df = df.set_index('Date')
X = ...
y= ..

我想它现在应该可以工作了。否则,请完全粘贴错误,我可以帮助解决问题。

请添加完整的回溯。听起来好像字段名正在进入数据。很显然,你是在用float乘以日期,这显然不起作用,所以你必须以某种方式将该日期转换为一个数字!?@matiss是的。这就是我想要的o、 我该怎么做?@Barmar Traceback在功能行之后添加了set_索引行。收到错误:
Traceback(最近一次调用):文件“F:/Yash Vyas/My Projects/interporation/tatastel stock prediction.py”,第12行,在df=df.set_index('Date')文件中“F:\Yash Vyas\My Projects\interporation\venv\lib\site packages\pandas\core\frame.py”,第4550行,在set\u index raise KeyError(F“没有任何{missing}在列中”)KeyError:“['Date']没有一个在列中”
如果我在功能列表中保留'Date'列,我会得到以下信息:
KeyError:'Date'