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.
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) df['c1'] is a Series, while pd.Categorical(...) (and anything you assign it to) is a Categorical object.
arrays.categorical.Categoricalobject whileseries.Seriesobject