如何使用带有日期过滤器的IF else创建新列(然后替换为SAS)

时间:2018-12-18 10:32:25

标签: python pandas numpy datetime python-datetime

我想在Python dataFrame中创建一个新列,但是我的代码无法正常工作。

recession = 0
recovery = 0

if data['excl_flg'] == 0 and data['segment'] != 'ignore':
    if (data['vardate'].dt.to_period('M') - '2008-06-30'.dt.to_period('M')) < 0 & (data['vardate'].dt.to_period('M') - '31Mar2010'.dt.to_period('M') >= 0:
         recession = 1
    elif (data['vardate'].dt.to_period('M') - '31Mar2010'.dt.to_period('M')) < 0 and (data['vardate'].dt.to_period('M') - '30Sep2013'.dt.to_period('M')) >= 0:
        recovery = 1

enter image description here

这是我的数据,我正在尝试创建新的列衰退和恢复。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

尝试此操作..不使用if而是达到目的。

数据

   excl_flag        segment    vardate    obsdate
0          0         CC_620  2/29/2008  3/31/2008
1          1  CC_cur_It_660  2/28/2009  3/31/2009
2          0         ignore  2/28/2010  3/31/2010
3          0         CC_620  2/28/2011  3/31/2011
4          1         ignore  2/29/2012  3/31/2012
5          0         CC_620  2/28/2013  3/31/2013
6          1  CC_cur_It_660  2/28/2014  3/31/2014
7          0         CC_620  2/28/2015  3/31/2015
8          1  CC_cur_It_660  2/29/2016  3/31/2016
9          0         ignore  2/28/2017  3/31/2017

必要进口

from datetime import datetime
import pandas as pd

发现日期格式。

data['vardate'] = pd.to_datetime(data['vardate']).apply(lambda x: x.date())
data['obsdate'] = pd.to_datetime(data['obsdate']).apply(lambda x: x.date())

data['recession'] = 0
data['recovery'] = 0

实施条件

data[((data['excl_flag'] == 0) & (data['segment'] != 'ignore'))].loc[(data['vardate']- datetime.strptime('2008-06-30',"%Y-%m-%d").date()).apply(lambda x: x.days < 0 )  & ((data['vardate'] - datetime.strptime('31Mar2010',"%d%b%Y").date()).apply(lambda x: x.days >=0)), 'recession'] = 1


data[((data['excl_flag'] == 0) & (data['segment'] != 'ignore'))].loc[(data['vardate']- datetime.strptime('31Mar2010',"%d%b%Y").date()).apply(lambda x: x.days < 0 )  & ((data['vardate'] - datetime.strptime('30Sep2013',"%d%b%Y").date()).apply(lambda x: x.days >=0)), 'recovery'] = 1

print(data) 

您的结果为零,因为您指定的条件找不到任何接受者

   excl_flag        segment     vardate     obsdate  recession  recovery
0          0         CC_620  2008-02-29  2008-03-31          0         0
1          1  CC_cur_It_660  2009-02-28  2009-03-31          0         0
2          0         ignore  2010-02-28  2010-03-31          0         0
3          0         CC_620  2011-02-28  2011-03-31          0         0
4          1         ignore  2012-02-29  2012-03-31          0         0
5          0         CC_620  2013-02-28  2013-03-31          0         0
6          1  CC_cur_It_660  2014-02-28  2014-03-31          0         0
7          0         CC_620  2015-02-28  2015-03-31          0         0
8          1  CC_cur_It_660  2016-02-29  2016-03-31          0         0
9          0         ignore  2017-02-28  2017-03-31          0         0