从日期减去timedelta - 熊猫

时间:2017-05-23 12:25:55

标签: python pandas datetime

考虑到熊猫系列,我试图从日期中减去delta时间。:

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]

可以看出,我已经在处理日期时间了。但是,当我尝试执行减法时,我收到错误:

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')

我错过了什么?

2 个答案:

答案 0 :(得分:4)

对我来说,作品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'>

在我看来,问题python timedelta未转换为pandas Timedelta,而且会引发错误。

但如果需要使用date,请先将datetime转换为date以获取python date个对象:

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'>

答案 1 :(得分:1)

作为jezrael pointed out,有一种大熊猫的方式,但你也可以使用.dt accessor作为日期时间:

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

测试代码:

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))

<强>结果:

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)