Home > Mobile >  Calculations with datetime index of Pandas dataframe
Calculations with datetime index of Pandas dataframe

Time:07-08

I have a Pandas dataframe. The index of the dataframe is a datetime column namely "time". How is it possible to call certain information like min() or max values of this index datetime column? or how do I do a timedelta calculation?

The dataframe example:

                      Temperature   Humidity        Height
 time                                                                       
 2022-03-17 12:52:27   281.278408  74.551713    602.406738  
 2022-03-17 12:52:28   281.278408  74.551713    602.406738  
 2022-03-17 12:52:29   281.278408  74.551713    602.406738  
 2022-03-17 12:52:30   281.278408  74.551713    602.406738  
 2022-03-17 12:52:31   281.278408  74.551713    602.406738  
 ...                          ...        ...           ...  
 2022-03-17 14:28:07   220.382478   0.929040  26529.819649  
 2022-03-17 14:28:08   220.355416   0.933099  26534.843300  
 2022-03-17 14:28:09   220.327658   0.937137  26539.820942   
 2022-03-17 14:28:10   220.300062   0.941082  26544.776129   
 2022-03-17 14:28:11   220.272778   0.944972  26549.735739 

What I principally aim at (just for illustration):

starttime = str(timedelta(seconds=int(df.time.min())))  " UTC")
endtime = str(timedelta(seconds=int(df.time.max())))  " UTC")
duration = endtime - starttime

Resulting (of course) in a AttributeError: 'DataFrame' object has no attribute 'time'.

And using

starttime = str(timedelta(seconds=int(df.index.min())))  " UTC")

results in a TypeError: int() argument must be a string, a bytes-like object or a number, not 'Timestamp'

Any hint for me?

Thanks.

CodePudding user response:

Use:

duration = (pd.Timedelta(str(df_1hz.index.time.max()))-
            pd.Timedelta(str(df_1hz.index.time.min())))
print (duration)
0 days 01:35:44

If there is same datetimes:

def f(x):
    ts = x.total_seconds()
    hours, remainder = divmod(ts, 3600)
    minutes, seconds = divmod(remainder, 60)
    return ('{:02d}:{:02d}:{:02d} UTC').format(int(hours), int(minutes), int(seconds))

duration = f(df_1hz.index.max() - df_1hz.index.min())
print (duration)
01:35:44 UTC
  • Related