Search code examples
pythonpandasdatetimeseries

Extract month name from datetime column or index


i have a df

Name Date
A    2021-04-21
B    2021-03-21
C    2021-02-23
D    2021-03-22

and the dtype of Date is object

i want another column as

Name Date         Month
A    2021-04-21   April
B    2021-03-21   March
C    2021-02-23   February
D    2021-03-22   January

Tried with

df['month'] = pd.DatetimeIndex(df['Date']).month

this is giving the number of the month but not the name.


Solution

  • Use this DatetimeIndex.month_name

    df['month'] = pd.DatetimeIndex(df['Date']).month_name()