在日期框架中,我有 Date 列,现在想从 Date 中提取一个名为“status”的新列,如果 Date> CurrentDate (datetime.now()) 将状态更新为 Expired,如果 Date< CurrentDate 将状态更新为 SW Expires 并且季度日期中的年份(SW 到期 2018-Q4),如果 Date == CurrentDate 或“X”或“Y”:将状态更新为 Current else NA。说:CurrentDate=19-06-2019
Date Status
2019-06-28 SW Expires 2018-Q2
2020-01-20 SW Expires 2018-Q1
2018-07-18 SW Expired
X Current
Y Current
2019-06-20 Current
df['Qtr'] = pd.to_datetime(df['Date']).dt.quarter
df['Qtr']=pd.PeriodIndex(pd.to_datetime(df['Date']), freq='Q')
df['Qtr']=df['Qtr'].astype(str)
df['Qtr']="SW Expires"+' '+df['Qtr']
df['Status'] = df['Date'].apply (lambda x : 'SW Expired' if x >
CurrentDate else(Midrange_SWProduct['Qtr'] x < CurrentDate else('current'
Midrange_SWProduct['Qtr'] x == CurrentDate or 'X' or 'Y' else NA )))