Home > Enterprise >  Pandas, convert long name date to short form
Pandas, convert long name date to short form

Time:09-28

I have an input of a date and want to convert it to its short form. Ie. August 2021 to Aug 2021

monthyear = input("Enter Month and Year: ")

How can I convert monthyear to its abbreviated version in pandas?

What i've tried:

smalldate = monthyear.strftime("%b %y")

getting AttributeError: 'str' object has no attribute 'strftime'

Also tried:

smalldate = dt.datetime.strftime(monthyear, "%b %y")

with error TypeError: descriptor 'strftime' for 'datetime.date' objects doesn't apply to a 'str' object

CodePudding user response:

You should parse the string input to a datetime before using strftime.

Try:

monthyear = input("Enter Month and Year: ")
smalldate = pd.to_datetime(monthyear, format="%B %Y").strftime("%b %Y")
  • Related