0

Initial "ImportDate" datatype Initial Pandas Dataframe interested in "ImportDate

Problem statement -

I want to extract the data where "ImportDate" last till "1-1-2019". For eg - start_date to 1-1-2019. I tried converting "object" into "datetime64[ns] and wrote the code as

df[df['ImportDate'].between(4/26/2018, 1/1/2019)]

But resulted in an error while extracting the data:

"'>=' not supported between instances of 'str' and 'float"

Can anyone help me how to deal with my problem statement?

4
  • 1
    df[df['ImportDate'].between(start_date,end_date)] , take a look here Commented Jul 25, 2019 at 6:04
  • I tried that but it showed an error - "<class 'int'> type object 1993" @anky_91 Commented Jul 25, 2019 at 6:06
  • ok. Please take the time to read this post on how to provide a great pandas example as well as how to provide a minimal, complete, and verifiable example and revise your question accordingly Commented Jul 25, 2019 at 6:07
  • @anky_91 I'm sorry for the inconvenience. I have reframed my question and also uploaded a photo of a dataframe I'm using. I hope it helps . Commented Jul 25, 2019 at 6:18

2 Answers 2

1

My guess is that your input in the between function are not dates. You should try to convert them :

df[df['ImportDate'].between(pd.to_datetime("4/26/2018"), pd.to_datetime("1/1/2019"))]

Or directly create date objects : datetime.date(2019,1,1) (do not forget to import datetime).

As stated, it would be easier to check if you can provide a piece of data.

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

Comments

0

Is the column you say is a datetime really datetime? Fro mthe error you posted, it looks like it is not. Please check once more with df.dtypes. if its is not a datetime object, then convert it to datetime with for example df['ImportDate']= pd.to_datetime(df['ImportDate'],format='%d/%m/%y') (you will have to tweak parameters to suit your data). Then you can do df[df['ImportDate'].between(start_date,end_date)]

2 Comments

thank you. I tried your code but still shows the error. So now I/m starting my problem from the scratch. I have re framed my problem statement again. Can you please check it out and help me with it
according to the photo you uploaded, the type of "ImportDate" is "object". To convert it to datetime, you should use df['ImportDate']= pd.to_datetime(df['ImportDate'],unit=s,format='%d/%m/%y') (you will have to tweak parameters to suit your data).

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.