出现此错误是因为您试图关联的类别变量之间的类别不同。在下面的代码示例中,所有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