Filter Pandas Dataframe For Past X Days
I have a dataframe with a date column that I update daily. I'd like to create a copy of it with just the past 30 day's of data.  I tried the following syntax based on what I know a
Solution 1:
Try this:
import datetime
import pandas as pd 
df[df.the_date_column > datetime.datetime.now() - pd.to_timedelta("30day")]
Update: Edited as suggested by Josh.
Solution 2:
consider the df
today = pd.datetime.today().date()
begin = today - pd.offsets.Day(90)
tidx = pd.date_range(begin, today)
df = pd.DataFrame(dict(A=np.arange(len(tidx))), tidx)
you can slice the last 30 days like this
cut_off = today - pd.offsets.Day(29)
df[cut_off:]
             A
2016-09-23  61
2016-09-24  62
2016-09-25  63
2016-09-26  64
2016-09-27  65
2016-09-28  66
2016-09-29  67
2016-09-30  68
2016-10-01  69
2016-10-02  70
2016-10-03  71
2016-10-04  72
2016-10-05  73
2016-10-06  74
2016-10-07  75
2016-10-08  76
2016-10-09  77
2016-10-10  78
2016-10-11  79
2016-10-12  80
2016-10-13  81
2016-10-14  82
2016-10-15  83
2016-10-16  84
2016-10-17  85
2016-10-18  86
2016-10-19  87
2016-10-20  88
2016-10-21  89
2016-10-22  90
Post a Comment for "Filter Pandas Dataframe For Past X Days"