TypeError: ‘Series’ objects are mutable, thus they cannot be hashed problemwith column

Your series contains other pd.Series objects. This is bad practice. In general, you should ensure your series is of a fixed type to enable you to perform manipulations without having to check for type explicitly.

Your error is due to pd.Series objects not being hashable. One workaround is to use a function to convert pd.Series objects to a hashable type such as tuple:

s = pd.Series(['one string', 'another string', pd.Series([1, 2, 3])])

def converter(x):
    if isinstance(x, pd.Series):
        return tuple(x.values)
    else:
        return x

res = s.apply(converter).unique()

print(res)

['one string' 'another string' (1, 2, 3)]

Leave a Comment

tech