Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas/datetime/total seconds : numpy.timedelta64' object has no attribute 'total_seconds'

I have a data frame. I converted two of my date columns to datetime format. And I want to calculate the difference in minutes. But I get the following error.

from datetime import datetime
df['A'] = df['A'].apply(lambda t: datetime.strptime(t, '%Y-%m-%d %H:%M:%S'))
df['B'] = df['B'].apply(lambda t: datetime.strptime(t, '%Y-%m-%d %H:%M:%S'))

df['C'] = ((df['B']-df['A']).apply(lambda x:x.total_seconds()/60.))

I get this error:

AttributeError: 'numpy.timedelta64' object has no attribute 'total_seconds'

Any help would be appreciated.

EDIT: Small dataset works fine:

df = pd.DataFrame({'A':['2015-09-01 00:02:34', '2015-09-02 00:02:34'],'B': ['2015-09-02 00:02:34', '2015-09-03 00:02:34']})
df['A'] = df['A'].apply(lambda t: datetime.strptime(t, '%Y-%m-%d %H:%M:%S'))
df['B'] = df['B'].apply(lambda t: datetime.strptime(t, '%Y-%m-%d %H:%M:%S'))
df['C'] = ((df['B']-df['A']).apply(lambda x:x.total_seconds()/60.))
df
                    A                   B       C
0 2015-09-01 00:02:34 2015-09-02 00:02:34  1440.0
1 2015-09-02 00:02:34 2015-09-03 00:02:34  1440.0

For my original big dataset, If I only select the first two rows of each column and do the same apply function, I would get the same error.

like image 602
Hamid K Avatar asked Nov 25 '25 07:11

Hamid K


1 Answers

It seems I need to do this:

df['C'] = (df['B'] - df['A'])/ np.timedelta64(1, 's')
like image 81
Hamid K Avatar answered Nov 26 '25 22:11

Hamid K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!