Can I use strftime without using strptime before?

I was planning to convert the format of the date column into another format. The initial format was like “12/18/2001 16:35”, and I’d like to convert it into “December, 2001” . I wanted to convert this column into datetime objects first and then convert the format. But there was an error: TypeError: must be str, not datetime.datetime
Here is my code:

date_format = "%m/%d/%y %H:%M"
for row in potus:
    start_date = row[2]
    start_date = dt.datetime.strptime(start_date, date_format)
    start_date = start_date.strftime("%B, %Y")

Here is the answer:

for row in potus:
    month_dt = row[2]
    month_str = month_dt.strftime("%B, %Y")

I’m not sure if it’s because the previous screen has converted the column into datetime format or I don’t need to do so. Thank you!

Hi @stellayou1126,
You get an error because you have already converted your data to “datetime” object in the previous screen, but in general, before using “strftime”, you must have “datetime” object.

1 Like