How to calculate averages of datetime64[ns] numpy.ndarray?

Viewed 499

With the following data, I would like to show the mean and other averages:

time = ['2020-01-01T00:00:00.000000000' '2020-01-02T00:00:00.000000000'
 '2020-01-03T00:00:00.000000000' '2020-01-04T00:00:00.000000000'
 '2020-01-05T00:00:00.000000000' '2020-01-06T00:00:00.000000000'
 '2020-01-07T00:00:00.000000000' '2020-01-08T00:00:00.000000000'
 '2020-01-09T00:00:00.000000000' '2020-01-10T00:00:00.000000000'
 '2020-01-11T00:00:00.000000000' '2020-01-12T00:00:00.000000000'
 '2020-01-13T00:00:00.000000000' '2020-01-14T00:00:00.000000000'
 '2020-01-15T00:00:00.000000000' '2020-01-16T00:00:00.000000000'
 '2020-01-17T00:00:00.000000000' '2020-01-18T00:00:00.000000000'
 '2020-01-19T00:00:00.000000000' '2020-01-20T00:00:00.000000000'
 '2020-01-21T00:00:00.000000000' '2020-01-22T00:00:00.000000000'
 '2020-01-23T00:00:00.000000000' '2020-01-24T00:00:00.000000000'
 '2020-01-25T00:00:00.000000000' '2020-01-26T00:00:00.000000000'
 '2020-01-27T00:00:00.000000000' '2020-01-28T00:00:00.000000000'
 '2020-01-29T00:00:00.000000000' '2020-01-30T00:00:00.000000000'
 '2020-01-31T00:00:00.000000000']

print(np.mean(time)) has an error: TypeError: cannot perform reduce with flexible type

I think i may need to implement pandas / dataframe / slicing, however i am unsure how to do this.

2 Answers

First you need to add commas between the entries of your list. Then, a possible option is to use pandas:

import pandas as pd
import numpy as np

You can convert your sting list to a pandas datetime list.

time_pd = pd.to_datetime(time)

Then turn this into an integer list and perform all the calculations you want. For example, calculating the mean:

time_np = time_pd.astype(np.int64)

average_time_np = np.average(time_np)

average_time_pd = pd.to_datetime(average_time_np)
print(average_time_pd)

Which prints: 2020-01-16 00:00:00

There are certainly ways to cast the time strings directly to numpy without using pandas, but that's the solution that I could figure out without much more research.

Here is one approach based on converting back and forth between Unix time

dt = np.array(time, dtype='datetime64')
delta_sec = np.timedelta64(1, 's')
epoch = '1970-01-01T00:00:00'
epoch_sec = (dt - np.datetime64(epoch)) / delta_sec
epoch_sec_mean = np.mean(epoch_sec)
dt_mean = np.datetime64(epoch) + np.timedelta64(int(epoch_sec_mean), 's')
print(dt_mean)

Output

2020-01-16T00:00:00
Related