Vikash Yadav
Vikash Yadav

Reputation: 813

AttributeError: 'Categorical' object has no attribute 'cat'

enter image description here

In python dataframe while getting category codes after assigning a column to variable(y=df.column) is giving attribute error.

enter image description here.

While same is working fine if we directoly pass df.column to Categorical function.

enter image description here

Upvotes: 1

Views: 11075

Answers (1)

cs95
cs95

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

Related Questions