Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a categorical column of month names with incomplete months

Tags:

python

pandas

I have a dataframe with a 'Date' column. I want to turn it into a categorical column that includes all months Jan to Dec. However, my column may not have all months represented.

Consider the dataframe df

df = pd.DataFrame(dict(Date=pd.date_range('2011-03-31', periods=4, freq='Q')))

df

        Date
0 2011-03-31
1 2011-06-30
2 2011-09-30
3 2011-12-31

I've tried

df.Date.dt.strftime('%b').astype('category')

0    Mar
1    Jun
2    Sep
3    Dec
Name: Date, dtype: category
Categories (4, object): [Dec, Jun, Mar, Sep]

You can see that only the four months in my column are represented as categories. How do I get to

0    Mar
1    Jun
2    Sep
3    Dec
Name: Date, dtype: category
Categories (12, object): [Jan, Feb, Mar, Apr, ..., Sep, Oct, Nov, Dec]
like image 640
piRSquared Avatar asked Dec 04 '25 07:12

piRSquared


1 Answers

You could use pd.Categorical and set the categories manually with the categories parameter:

cat = pd.date_range('2011-01-1', periods=12, freq='M').strftime('%b')
out = pd.Categorical(df.Date.dt.strftime('%b'), categories=cat)
out

[Mar, Jun, Sep, Dec]
Categories (12, object): [Jan, Feb, Mar, Apr, ..., Sep, Oct, Nov, Dec]
like image 113
cs95 Avatar answered Dec 05 '25 22:12

cs95