Reputation: 813
In python dataframe while getting category codes after assigning a column to variable(y=df.column
) is giving attribute error.
While same is working fine if we directoly pass df.column
to Categorical
function.
Upvotes: 1
Views: 11075
Reputation: 403130
The .cat
attribute is a categorical accessor associated with categorical
dtype Series:
s = pd.Series(['a', 'b', 'a']).astype('category')
s
0 a
1 b
2 a
dtype: category
Categories (2, object): [a, b]
s.cat.codes
0 0
1 1
2 0
dtype: int8
OTOH, pd.Category
returns a pandas.core.arrays.categorical.Categorical
object, which has these attributes defined on the object directly:
pd.Categorical(['a', 'b', 'c'])
# [a, b, c]
pd.Categorical(['a', 'b', 'c']) .codes
# array([0, 1, 2], dtype=int8)
Upvotes: 4