Atif Hossain
Atif Hossain

Reputation: 9

ValueError: time data 'temp_month/temp_year/1' does not match format

I'm using Python 3.7.1

I'm having an issue with formatting dates. I have to print three value in the format of month/day/year.

I have taken year from a data frame and stored in temp_year and similarly for month and date by default will be 1. So I want to print in the format temp_month/1/temp_year.

temp_year=(input_df["Indexation Date"].dt.year).iloc[0]
temp_year=temp_year-1
temp_month=(input_df["Indexation Date"].dt.month).iloc[0]
temp_month=temp_month-rpi_lag_months

s="temp_month/temp_year/1"

index_start_date=datetime.strptime(s,"%m/%d/%Y")

I am expecting the output to be:

3/1/2012

However getting the error as:

Value Error: time data 'temp_month/temp_year/1' does not match format '%m/%d/%Y'

Upvotes: 0

Views: 45

Answers (1)

bumblebee
bumblebee

Reputation: 1841

You don't pass the string as it, as it does not contains any values. And your format doesn't match either.

Try changing this

s="temp_month/temp_year/1" 

to

s=f"{temp_month}/1/{temp_year}"

Upvotes: 1

Related Questions