Search code examples
pythonpandasdatetimedatetimeindex

How to find missing days or hours that breaks continuity in Datetime index?


Many thanks in advance for helping a python newbie like me !

I have a DataFrame containing daily or hourly prices for a particular crypto.

I was just wondering if there is an easy way to check if there is any missing day or hour (depending on the chosen granularity) that would break a perfectly constant timedelta (between 2 dates) in the index?

Here an example of an other "due diligence" check I am doing. I am just making sure that the temporal order is respected:

 # Check timestamp order:
        i = 0
        for i in range(0,len(df.TS)-1):

            if df.TS[i] > df.TS[i+1]:
                print('Timestamp does not respect time direction, please check df.')
                break
            else:
            i += 1

Perhaps there is surely a better way to do this but I didn't find any build in function for both of these checks I would like to do.

Many thanks again and best regards,

Pierre


Solution

  • If df.TS is where you store your datetime data, then you can do this (example for daily data, change freq accordingly):

    pd.date_range(start = df.TS.min(), end = df.TS.max(), freq = 'D').difference(df.TS)
    

    This will return the difference between a complete range and your datetime series.