Skip to content Skip to sidebar Skip to footer

Comparing Dataframe Datetime Column Value In Python?

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

Solution 1:

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

Post a Comment for "Comparing Dataframe Datetime Column Value In Python?"