Check if pandas.DataFrame is equally spaced

Viewed 197

I have a pandas.DataFrame called df with

>>> type(df.index)
<class 'pandas.core.indexes.datetimes.DatetimeIndex'>

How can I assert that the index is equally spaced? I.e. I am looking for a boolean attribute of df or of df.index which is

  • True, if the distance between two consecutive indices is always the same, for example always 10 minutes
  • False, otherwise.

Is there an in-built attribute or function accomplishing this?

1 Answers

One way to do that, assuming the index is numeric:

import numpy as np
diff = np.diff(df.index.to_numpy())
Equaly_spaced = np.all(diff==diff[0])
Related