Pandas 无法添加关系,因为数据类型不';功能工具不匹配
尝试在Featuretools中添加两个实体之间的关系时出现以下错误Pandas 无法添加关系,因为数据类型不';功能工具不匹配,pandas,categorical-data,featuretools,Pandas,Categorical Data,Featuretools,尝试在Featuretools中添加两个实体之间的关系时出现以下错误 Unable to add relationship because ID in metadata is Pandas `dtype category` and ID in transactions is Pandas `dtype category` 请注意,系列不一定是相同的。类别代码出现此错误是因为您试图关联的类别变量之间的类别不同。在下面的代码示例中,所有3个系列都是分类,但只有s和s2具有相同的数据类型 impor
Unable to add relationship because ID in metadata is Pandas `dtype category` and ID in transactions is Pandas `dtype category`
请注意,系列不一定是相同的。类别代码出现此错误是因为您试图关联的类别变量之间的类别不同。在下面的代码示例中,所有3个系列都是分类,但只有
s
和s2
具有相同的数据类型
import pandas as pd
from pandas.api.types import is_dtype_equal
s = pd.Series(["a","b","a"], dtype="category")
s2 = pd.Series(["b","b","a"], dtype="category")
s3 = pd.Series(["a","b","c"], dtype="category")
is_dtype_equal(s.dtype, s2.dtype) # this is True
is_dtype_equal(s.dtype, s3.dtype) # this is False
要解决此问题,您需要在将数据帧加载到Featuretools之前更新数据帧,以确保Pandas类别具有相同的值类别值。你是怎么做到的
如果s
缺少s3
new_s = s.astype(s3.dtype)
is_dtype_equal(new_s.dtype, s3.dtype) # this is True
如果两个系列都缺少另一个系列中的类别,我们必须使两个类别合并
s4 = pd.Series(["b","c"], dtype="category")
categories = set(s.dtype.categories + s4.dtype.categories) # make union of categories
new_s = s.astype("category", categories=categories)
new_s4 = s4.astype("category", categories=categories)
is_dtype_equal(new_s.dtype, new_s4.dtype) # this is True
出现此错误是因为您试图关联的分类变量之间的类别不同。在下面的代码示例中,所有3个系列都是分类,但只有
s
和s2
具有相同的数据类型
import pandas as pd
from pandas.api.types import is_dtype_equal
s = pd.Series(["a","b","a"], dtype="category")
s2 = pd.Series(["b","b","a"], dtype="category")
s3 = pd.Series(["a","b","c"], dtype="category")
is_dtype_equal(s.dtype, s2.dtype) # this is True
is_dtype_equal(s.dtype, s3.dtype) # this is False
要解决此问题,您需要在将数据帧加载到Featuretools之前更新数据帧,以确保Pandas类别具有相同的值类别值。你是怎么做到的
如果s
缺少s3
new_s = s.astype(s3.dtype)
is_dtype_equal(new_s.dtype, s3.dtype) # this is True
如果两个系列都缺少另一个系列中的类别,我们必须使两个类别合并
s4 = pd.Series(["b","c"], dtype="category")
categories = set(s.dtype.categories + s4.dtype.categories) # make union of categories
new_s = s.astype("category", categories=categories)
new_s4 = s4.astype("category", categories=categories)
is_dtype_equal(new_s.dtype, new_s4.dtype) # this is True