0

I have a snippet of the dataframe here

  TIME                Value1 Value2
0 2014-10-02 12:45:03   5      6
1 2014-10-02 12:45:05   6      7
2 2014-10-02 12:45:08   3      5
3 2014-10-02 12:45:09   7      4
.....................   ...    ...
45 2014-11-03 00:51:09   7     8

Now, I would like to get all the dataframe rows between 2014-10-02 and 2014-11-02 without knowing the exact time to the second. I tried this method as shown below, but I am getting 0 rows.

start_date = pd.to_datetime('2014-10-02 00:00:01')
end_date = pd.to_datetime('2014-11-02 00:00:01')
df.loc[(df['TIME'] > start_date) & (df['TIME'] < end_date)]

But when I put in an exact datetime value like '2014-10-02 12:45:03' for the start date and end date, I get the output. The data has millions of rows and I could not possibly find out the exact time to the second for the start date and end date. I just need to get rows between two dates.

0

1 Answer 1

1

You can try boolean masking:

df.loc[(df['TIME'].dt.date > start_date.date()) & (df['TIME'].dt.date< end_date,date())]

OR

You can also use boolean masking and between() method:

df[df['TIME'].dt.date.between(start_date.date(),end_date.date())]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the solution and sorry for the late reply. I solved this by setting the time as index which suited my work much better. Anyway thank you for this solution.

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.