1

I have a datestring in thew following format which is month/day/year and then time

print(df):
Date
6/06/20 4:41pm
6/06/20 5:41pm

I am trying to convert using pd.to_datetime and have used the following:

df['Date'] =  pd.to_datetime(df['Date'], format='%m%d%Y:%H:%M.%f')

but I cant match the format. Does anyone know the format for this particular sting? thank you very much!

1
  • Change the format to %m/%d/%y %H:%M.%p strftime.org Commented Jun 6, 2020 at 3:46

2 Answers 2

1

Data

df=pd.DataFrame({'Date':['6/06/20 4:41pm','6/06/20 5:41pm']})

df['Date']=pd.to_datetime(df['Date'])

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much. as a follow up question. If I wanted to filter only rows for today how would I do that? I have tried today = date.today() and tomorrow = today + timedelta(days=1) and then df= df[[df['Date']<tomorrow]] but getting an TypeError: Invalid comparison between dtype=datetime64[ns] and date error
Based on your latest comment. If wanted to select todays date df.assign(Dates=pd.to_datetime(df['Date']).dt.date)==pd.Timestamp('today')#Boolean selection df[df.assign(Dates=pd.to_datetime(df['Date']).dt.date)==pd.Timestamp('today')].dropna()#DataFrame
0

you are missing part of the format

df['Date'] =  pd.to_datetime(df['Date'], format='%m/%d/%Y %H:%M.%f')

3 Comments

Thanks for the answer although seems to be still not picking it up. There is a space between the year and hour does that matter?
Yes, it should be the exact same
thanks for your help. I still couldnt get it working for some reason but appreaciate you help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.