Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtracting timedelta from date - pandas

I'm trying to subtract delta times from dates, given a pandas series.:

date_current = hh.groupby('group').agg({'issue_date' : [np.min, np.max]})
date_current.issue_date.amax.head(5)

group
_101000000000_0.0   2017-01-03
_102000000000_1.0   2017-02-23
_102000000000_2.0   2017-03-20
_102000000000_3.0   2017-10-01
_103000000000_4.0   2017-01-24
Name: amax, dtype: datetime64[ns]

As can be seen, I'n already working with date times. However, when I try to perform the subtraction, I get an error:

import datetime
months = 4
datetime.timedelta(weeks=4*months)
date_before = date_current.values - datetime.timedelta(weeks=4*months)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-51-5a7f2a09bab6> in <module>()
      2 months = 4
      3 datetime.timedelta(weeks=4*months)
----> 4 date_before = date_current.values - datetime.timedelta(weeks=4*months)

TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('O')

What am I missing?

like image 439
pceccon Avatar asked Mar 10 '23 01:03

pceccon


2 Answers

For me works pandas Timedelta:

date_before = date_current.values - pd.Timedelta(weeks=4*months)
print (date_before)
['2016-09-13T00:00:00.000000000' '2016-11-03T00:00:00.000000000'
 '2016-11-28T00:00:00.000000000' '2017-06-11T00:00:00.000000000'
 '2016-10-04T00:00:00.000000000']

date_before = date_current - pd.Timedelta(weeks=4*months)
print (date_before)
group
_101000000000_0.0   2016-09-13
_102000000000_1.0   2016-11-03
_102000000000_2.0   2016-11-28
_102000000000_3.0   2017-06-11
_103000000000_4.0   2016-10-04
Name: amax, dtype: datetime64[ns]

print (type(date_before.iloc[0]))
<class 'pandas._libs.tslib.Timestamp'>

In my opinion problem is python timedelta is not converted to pandas Timedelta and it raise error.

But if need working with dates, first convert datetime to date for python date objects:

date_before = date_current.dt.date - datetime.timedelta(weeks=4*months)
print (date_before)
group
_101000000000_0.0    2016-09-13
_102000000000_1.0    2016-11-03
_102000000000_2.0    2016-11-28
_102000000000_3.0    2017-06-11
_103000000000_4.0    2016-10-04
Name: amax, dtype: object

print (type(date_before.iloc[0]))
<class 'datetime.date'>
like image 175
jezrael Avatar answered Mar 16 '23 16:03

jezrael


As jezrael pointed out, there is a pandas way, but you can also do this as a datetime using the .dt accessor:

df.dt.values - dt.timedelta(weeks=4 * months)

Test Code:

import datetime as dt
import pandas as pd

df = pd.Series([dt.datetime.now()])
print(df)

months = 4
print(df.values - pd.Timedelta(weeks=4*months))
print(df.dt.values - dt.timedelta(weeks=4 * months))

Results:

0   2017-05-23 05:36:53.300
dtype: datetime64[ns]

['2017-01-31T05:36:53.300000000']

DatetimeIndex(['2017-01-31 05:36:53.300000'], dtype='datetime64[ns]', freq=None)
like image 24
Stephen Rauch Avatar answered Mar 16 '23 17:03

Stephen Rauch