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]
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With