gpt4 book ai didi

python - 类型错误 : Type Tuple cannot be instantiated; use tuple() instead

转载 作者:行者123 更新时间:2023-12-03 16:07:44 25 4
gpt4 key购买 nike

我用以下代码编写了一个程序:

import pandas as pd
import numpy as np
from typing import Tuple

def split_data(self, df: pd.DataFrame, split_quantile: float) -> Tuple(pd.DataFrame, pd.DataFrame):
'''Split data sets into two parts - train and test data sets.'''
df = df.sort_values(by='datein').reset_index(drop=True)
quantile = int(np.quantile(df.index, split_quantile))
return (
df[df.index <= quantile].reset_index(drop=True),
df[df.index > quantile].reset_index(drop=True)
)

程序返回以下错误: TypeError: Type Tuple cannot be instantiated; use tuple() instead .我明白,我可以通过替换 Tuple(pd.DataFrame, pd.DataFrame) 来解决我的代码与 tuple() ,但是我丢失了信息的一部分,即我的元组将由两个 Pandas 数据框组成。

请您帮帮我,如何解决错误并同时不丢失信息?

最佳答案

使用方括号:

Tuple[pd.DataFrame, pd.DataFrame]

来自 docs :

Tuple type; Tuple[X, Y] is the type of a tuple of two items with the first item of type X and the second of type Y. The type of the empty tuple can be written as Tuple[()].

关于python - 类型错误 : Type Tuple cannot be instantiated; use tuple() instead,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58558412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com