从timedelta python

时间:2017-04-06 21:26:08

标签: python python-2.7 pandas

我有一个如下所示的DataFrame:

raw_data = {'Series_Date':['2017-03-10','2017-03-13','2017-03-14','2017-03-15'],'SeriesDate':['2017-03-10','2017-03-13','2017-03-14','2017-03-15']}
import pandas as pd
df = pd.DataFrame(raw_data,columns=['Series_Date','SeriesDate'])
df['SeriesDate'] = pd.to_datetime(df['SeriesDate'])
df['StartDate'] = df['SeriesDate'] - timedelta(days=10)
df['EndDate'] = df['SeriesDate'] - timedelta(days=3)
print df

当我尝试这样做时:

df['DateDifference'] = ((df.StartDate- df.EndDate) / 2).dt.ceil('D')

我收到以下错误:

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

知道如何克服这个问题吗?

1 个答案:

答案 0 :(得分:0)

你可以这样做:

import pandas as pd
from  dateutil import relativedelta
raw_data = {'Series_Date':
                ['2017-03-10','2017-03-13','2017-03-14','2017-03-15'],
            'SeriesDate':['2017-03-10','2017-03-13','2017-03-14','2017-03-15']}

df = pd.DataFrame(raw_data,columns=['Series_Date','SeriesDate'])
df['SeriesDate'] = pd.to_datetime(df['SeriesDate'])

move_days = (-10)
df['StartDate'] = (df['SeriesDate']
                   .apply(lambda x: 
                            x + relativedelta.relativedelta(days=move_days)))

movedays= (-3)
df['EndDate'] = (df['SeriesDate']
                 .apply(lambda x: 
                           x + relativedelta.relativedelta(days=move_days)))
print(df)

给出:

   Series_Date SeriesDate  StartDate    EndDate
0  2017-03-10   2017-03-10 2017-02-28 2017-02-28
1  2017-03-13   2017-03-13 2017-03-03 2017-03-03
2  2017-03-14   2017-03-14 2017-03-04 2017-03-04
3  2017-03-15   2017-03-15 2017-03-05 2017-03-05

获取日期差异,

df['DateDiff'] = (df['EndDate'] - df['StartDate'])
df['DateDiffDays'] = df['DateDiff'].dt.days
print(df)

给出

   Series_Date SeriesDate  StartDate    EndDate  DateDiff  DateDiffDays
0  2017-03-10  2017-03-10 2017-02-28 2017-03-07    7 days             7
1  2017-03-13  2017-03-13 2017-03-03 2017-03-10    7 days             7
2  2017-03-14  2017-03-14 2017-03-04 2017-03-11    7 days             7
3  2017-03-15  2017-03-15 2017-03-05 2017-03-12    7 days             7