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.

Aniket Maithani
  • 845
  • 1
  • 10
  • 24

1 Answers1

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
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
  • 1
    @jezrael Got it. Edited my answer also. Thanks. – Mayank Porwal Dec 11 '18 at 14:40
  • @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 ? – Aniket Maithani Dec 11 '18 at 14:43
  • @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. – Mayank Porwal Dec 11 '18 at 14:54
  • Thanks for the clarification @MayankPorwal. In case I want to compare the date as well as time in that case ? – Aniket Maithani Dec 11 '18 at 14:58
  • @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. – Mayank Porwal Dec 11 '18 at 15:00