0

I am new to Pandas. I have the following pandas dataframe which contains the following values :

index print_statement      timestamp 
0     echo "I AM HAPPY2" 2018-11-12 08:01:00       
1     echo "I AM HAPPY3" 2018-11-12 08:01:00       
2     echo "I AM HAPPY1" 2018-11-12 08:01:00       
3     echo "I AM HAPPY4" 2018-12-12 08:02:00      
4     echo "I AM HAPPY5" 2018-12-13 08:02:00  

I want to compare the df as: - let's say I have a time_argument which is datetime.datetime(2018, 12, 12, 5, 1). I want to store the result in another dataframe where timestamp > time_argument.

I tried using the following approach:

for index, row in df.iterrows():
     date_store = row['time_to_expire']
     if date_store.to_pydatetime() >= ii:
         df_final = row

But I am not getting the desired answer.

Hope I am clear with the question.

0

1 Answer 1

5

You can do this:

First convert timestamp column into Pandas datetime:

In [2346]: df.timestamp = pd.to_datetime(df.timestamp)
In [2347]: df
Out[2347]: 
      print_statement           timestamp
0  echo "I AM HAPPY2" 2018-11-12 08:01:00
1  echo "I AM HAPPY3" 2018-11-12 08:01:00
2  echo "I AM HAPPY1" 2018-11-12 08:01:00
3  echo "I AM HAPPY4" 2018-12-12 08:02:00
4  echo "I AM HAPPY5" 2018-12-13 08:02:00

In [2348]: time_argument = datetime.datetime(2018, 12, 12, 5, 1)

In [2350]: result = df[df.timestamp > time_argument]
Out[2350]: result
      print_statement           timestamp
3  echo "I AM HAPPY4" 2018-12-12 08:02:00
4  echo "I AM HAPPY5" 2018-12-13 08:02:00
Sign up to request clarification or add additional context in comments.

5 Comments

@jezrael Got it. Edited my answer also. Thanks.
@MayankPorwal But if I pass time_argument = datetime.datetime(2018, 12, 11, 4, 59) . In that case it is showing me the same result as of datetime.datetime(2018, 12, 12, 5, 1)` . Anything I am missing here ?
@AniketMaithani Yes, it should produce the same result. As, the new time_argument is basically = 2018-12-11 04:59. So, you are searching for rows > 11th Dec 2018, which is basically the last 2 rows of your dataframe.
Thanks for the clarification @MayankPorwal. In case I want to compare the date as well as time in that case ?
@AniketMaithani The code compares date with time already. Just make sure to give correct value to the time_argument variable. It will do what you want.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.