Pyd
Pyd

Reputation: 6159

How to find maximum gap of observations for the whole duration of the time series.using pandas

I have a dataframe like this,

date         value
2017-01-02  -0.665575
2017-01-02  2.850187
2017-01-03  6.035269
2017-01-03  -0.738031
2017-01-06  -0.330992

trying to find maximum gap of observations (maximum gap between the dates) for the whole duration of the time series.

I tried pandas.rolling window, I am not sure how to apply the method.

Please help.

Upvotes: 1

Views: 1128

Answers (1)

yatu
yatu

Reputation: 88275

You can use Series.diff, which will give you the amount of days between samples, and take the max:

df.date.diff().max()
# Timedelta('3 days 00:00:00')

If you want the amount of days:

df.date.diff().max().days
# 3

If the dates are not in order start with DataFrame.sort_values:

df.sort_values('date').date.diff().max()

Upvotes: 3

Related Questions