-1

How to convert the month number from a dataframe to month name?

Had try:

df['month'] = pd.to_datetime(df['Settlement_Date']).dt.month

x= df['month']


datetime_object = datetime.strptime(x, "%m")

month_name = datetime.strftime("%b")

print("Short name: ",month_name)

However, getting error message:

TypeError: strptime() argument 1 must be str, not Series

beginofwork
  • 129
  • 8
  • Does this answer your question? [Convert column of date objects in Pandas DataFrame to strings](https://stackoverflow.com/questions/19738169/convert-column-of-date-objects-in-pandas-dataframe-to-strings) – sushanth Aug 12 '22 at 04:08

1 Answers1

0

You could use a custom hashmap which maps month numbers to names:

d = {1:'January', 2:'February', 3:'March', 4:'April', 5:'May', 6:'June', 7:'July', 8:'August', 9:'September', 10:'October', 11:'November', 12:'December'}
df["month"] = df.apply(lambda row : d[row["Settlement_Date"]], axis=1)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360