Skip to content Skip to sidebar Skip to footer

Pandas Find Duration Between Dates Where A Condition Is Met?

I have a pandas DataFrame that looks like this: ╔═══╦════════════╦═════════════╗ ║ ║ VENDOR ID ║ DATE

Solution 1:

I get a bit different output:

df['DATE'] = pd.to_datetime(df['DATE'])
df['GAP'] = df.groupby('VENDOR ID')['DATE'].diff().dt.days
print (df)
   VENDOR ID       DATE   GAP
1         33 2018-01-12   NaN
2         33 2018-03-12  59.0
3         12 2018-01-08   NaN
4         12 2018-01-15   7.0
5         12 2018-01-23   8.0
6         33 2018-05-12  61.0
7         89 2018-01-12   NaN

Explanation:

  1. Convert column to_datetime
  2. Then groupby with diff
  3. Last convert timedeltass to days

Post a Comment for "Pandas Find Duration Between Dates Where A Condition Is Met?"