我有一个函数,如果你向它传递一些数据标识符,它将返回该数据集的 pandas 数据框。我刚刚添加了一个新参数,当该参数为 True 时,会返回第二个数据帧,其中包含有关数据集的元数据。因此,该函数可以返回一个或两个数据集,具体取决于参数。但我发现当元数据标志为 False 但函数调用要求两个返回值时,如果数据集只有两列,它将以字符串形式返回数据集列名。
res1, res2 = get_dataframe(dataset_id='123', include_metadata = False) #123 has 2 columns; this call should return 1 dataframe
type(res1)
>>> <class 'str'>
res1
>>> Anonymizing bipartite graph data...
type(res2)
>>> <class 'str'>
res2
>>> The union-split algorithm...
我想要的是让这段代码抛出错误,就像它对具有 3 列或更多数据列的数据集所做的那样(1 列数据集是不可能的)。
res1, res2 = get_dataframe(dataset_id='abc', include_metadata = False) #abc has 6 columns; this call should return 1 dataframe
ValueError: too many values to unpack (expected 2)
如果我使用两列数据集正确调用,它会返回 1 或 2 个数据帧。
res = get_dataframe(dataset_id='123', include_metadata = False) #123 has 2 columns; this call should return 1 dataframe
type(res)
>>> <class 'pandas.core.frame.DataFrame'>
res.shape
>>> (5, 2)
res1, res2 = get_dataframe(dataset_id='123', include_metadata = True) #123 has 2 columns; this call should return 2 dataframes
type(res1)
>>> <class 'pandas.core.frame.DataFrame'>
res1.shape
>>> (5, 2)
type(res2)
>>> <class 'pandas.core.frame.DataFrame'>
res2.shape
>>> (8, 2)
所以我的问题是,为什么上面的第一个例子没有抛出错误而是返回字符串?预先感谢您的任何反馈!