How can I calculate the time (number of days) between "events" in the Panda timeline? For example, if I have a short time series then I want to know every day in the series how many days have passed since last TRUE
event 2010 -01-01 false 2010-01-02 right 2010-01-03 false 2010-01-04 false 2010-01-05 true 2010-01-06 false
the way I 'I did this, it looks more than that, so I'm hoping for something more beautiful, obviously, it will work for loops running on lines, but I Ideally searching for a scalable solution. My current effort below:
DATE_RANGE = pd.date_range ('2010-01-01', '2010-01-06') DF = pd.DataFrame ([, false is true, False, false, false, false, index = DATE_RANGE, column = ['event']) event_dates = df.index [df ['event']] df2 = pd.DataFrame (event_dates, index = event_dates, column = ['Max_event_date']) DF = df.join (df2) df ['max_event_date'] = df ['max_event_date'] cummax (axis = 0, skipna = false) df ['days_since_event'] = df.index -. Df ['max_event_date'] event max_event_date days_since_event 2010-01-01 False NaT Barbershop 2010-01-02 True 2010-01-02 0 days 2010-01-03 False 2010-01-02 1 day 2010-01-04 Wrong 2008 -01-02 2 days 2010-01-05 Right 2010-01-05 0 Days 2010-01-06 False 2010-01-05 1 day
This answer continues to improve, and hoping that until someone comes up with 'python', I think this last update works best Does seem
previous = pd.to_datetime (np.nan) def passes (row): otherwise row.event: return row.name - and previous . : Global last last = row.name return row.name- previous df ['gone'] = df.apply (passed, axis = 1) df event 2010-01-01 false net 2010-01-02 it is true 0 days 2010 GUJARAT -01-03 False 1 day 2010-01-04 Incorrect 2 days 2010-01-05 True 0 days 2010-01-06 Wrong Day 1
:::::: :: :::::
leaving the last answer below, though they are optimal
::::::::::::
Many passes Instead of making it easier to think, just loop through indexed
df ['gone'] = df.index for i to 0 [1: ]: Df ['elapsed'] [i] = df ['elapsed'] [i-1] + 1
: ['event'] [i] :::::::::::: Assume that 'Trues' is an incident of your interest. The reason is that .diff () index trues.Elapsed = trues.Dates.diff Does not work on ()
No comments:
Post a Comment