equanimity
equanimity

Reputation: 2533

How to Filter Out Today's Date in Pandas Dataframe

I have the following data frame:

Company     Date                 Value
ABC         08/21/16 00:00:00    500
ABC         08/22/16 00:00:00    600
ABC         08/23/16 00:00:00    650
ABC         08/24/16 00:00:00    625
ABC         08/25/16 00:00:00    675
ABC         08/26/16 00:00:00    680

If we assume that 26-August-2016 is today's date, then I would like to create a new data frame that effectively excludes the data in the 08/26/16 row.

EDIT: Here's my code to do so:

today = time.strftime("%m/%d/%Y")
df.Date = df.Date <> today

Unfortunately, I see an error message indicating:

'Series' object has no attribute 'Date'

Any idea how to resolve this?

Thanks!

SOLUTION:

today = time.strftime("%Y-%m-%d")

df = df.loc[(df.Date < today)]

Upvotes: 3

Views: 9036

Answers (1)

Nickil Maveli
Nickil Maveli

Reputation: 29711

I think you want to filter the dates that appear earlier than your specified date, though the title seems misleading. You must convert the Date column which are of dtype object to datetime64.

In [22]: df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')

In [23]: today = datetime.strptime('08/26/16', '%m/%d/%y')

In [24]: today
Out[24]: datetime.datetime(2016, 8, 26, 0, 0)

In [25]: df = df.loc[(df['Date'] < today)]

In [26]: df
Out[26]: 
  Company       Date  Value
0     ABC 2016-08-21    500
1     ABC 2016-08-22    600
2     ABC 2016-08-23    650
3     ABC 2016-08-24    625
4     ABC 2016-08-25    675

Upvotes: 6

Related Questions