How to use pandas aggregate Datetime object to int

Viewed 1338

I have a DataFrame structure like that

         id   time                   number
0        1    1970-01-01 00:00:00    1
1        2    1970-01-02 00:00:00    2
2        1    1970-01-03 00:00:00    2

I want the groupby id and aggregate the time which have pd.Datetime dtype to be int representing the time delta and I have the code below:

def interval(a):
    return (np.max(a) - np.min(a)).days

_df = df.groupby(['id'], as_index=False).agg(
        {
            "number": numpy.sum,
            "time": interval,
        }
    )

The column time has origin dtype pd.Datetime however the aggregated data is int which results the data in time column of _df are converted from int to pd.Datetime like 1970-01-01 00:00:00.000000000

Could you tell me how to get the correct result which the time column of aggregated dataframe is int

1 Answers
Related