2017-07-06 140 views
1

目標:對於在接下來的9個月內具有日期的特定列中的單元格,將新列添加到Master,選擇「Yes」,日期即將到來,或者「No」,因爲日期不是接下來。使用熊貓,添加timedelta到主數據框中的新行?

我是熊貓新手,所以任何幫助都會很棒。我有一個數據框稱爲「Master」,因爲我將所有內容添加到數據框中,然後導出到Excel電子表格。使用下面的代碼,我爲日期創建了一個新的數據框,並試圖說「如果單元格處於日期狀態,請在新列中輸入」是「。

我懷疑我的問題是,我試圖把熊貓當作數組來處理,但是它實際上做的是在創建一個新的數據框時創建全新的索引並且不保留它如何映射到主。我的理解是否正確?如果是這樣,我怎樣才能以更優雅的方式實現我的目標?

def mess_with_time(Master): 
    now = datetime.datetime.now() 
    nine_months_in_the_future = str(datetime.datetime.today() + relativedelta(months=+8)) 
    just_future_month=int(nine_months_in_the_future[5:7]) 
    just_future_year=int(nine_months_in_the_future[0:4]) 

    dates = Master[(Master['Contract End Date'] > '{}-{}-1'.format(now.year,now.month)) & (Master['Contract End Date'] <= '{}-{}-20'.format(just_future_year, just_future_month))] 
Master['Contract End date coming up?'] = np.where(dates, 'Yes', 'No') #this breaks with 
the error 'Length of values does not match length of index' 

def Create_Master(file): 
    df = pd.ExcelFile('{}.xlsx'.format(file)) 

    Master = df.parse('Main').fillna(0) 

回答

0

我試試您的解決方案改寫爲大熊貓:

首先是可能使用DateOffset然後between創建布爾面膜:

now = pd.datetime.now() 
months9 = now + pd.offsets.DateOffset(months=8) 
print (months9) 
2018-03-06 15:07:52.850238 

rng = pd.date_range('2017-04-03', periods=10, freq='3M') 
Master = pd.DataFrame({'Contract End Date': rng, 'a': range(10)}) 

dates_mask = Master['Contract End Date'].between(now, months9) 
Master['Contract End date coming up?'] = np.where(dates_mask, 'Yes', 'No') 
print (Master) 
    Contract End Date a Contract End date coming up? 
0  2017-04-30 0       No 
1  2017-07-31 1       Yes 
2  2017-10-31 2       Yes 
3  2018-01-31 3       Yes 
4  2018-04-30 4       No 
5  2018-07-31 5       No 
6  2018-10-31 6       No 
7  2019-01-31 7       No 
8  2019-04-30 8       No 
9  2019-07-31 9       No 

看來你需要改變boolean indexingMaster[boolean_condition]boolean condition只:

dates = Master[(Master['Contract End Date'] > '{}-{}-1'.format(now.year,now.month)) & (Master['Contract End Date'] <= '{}-{}-20'.format(just_future_year, just_future_month))] 

到:

dates_mask = (Master['Contract End Date'] > '{}-{}-1'.format(now.year,now.month)) & (Master['Contract End Date'] <= '{}-{}-20'.format(just_future_year, just_future_month)) 

Master['Contract End date coming up?'] = np.where(dates_mask, 'Yes', 'No')