2016-02-11 75 views
0

我定義我的代碼我的日期窗口這樣錯誤佔位符日期

import pandas as pd 
##### 
end = datetime.datetime.now() 
end = end.date() 
start = end - days * pd.tseries.offsets.BDay() 
start = start.date() 

並使用它像這樣

print('After analysing from %s to %s' %(datetime.date(start), datetime.date(end))) 
print('Duration: %1.1f Years ~ %d workdays - Extent: %d strategies and %d tickers' '\n' 
'The following matrix of tickers and strategies show highest potential:' %(years, days, len(strategies), len(stocklist)) 
) 

但我得到一個錯誤

print('After analysing from %s to %s' %(datetime.date(start), datetime.date(end))) 

TypeError: an integer is required (got type datetime.date) 

有誰知道我如何正確使用日期?謝謝,我在這裏的搜索沒有返回任何我能理解的東西。

+0

你能不能只用'%(起點,終點)'因爲這些已經是'datetime'對象了? – xnx

回答

1

你的問題是在試圖創建一個datetime對象從datetime對象(datetime.date預計接收整數年,月和日的參數)。由於datetime對象知道如何表現自己作爲字符串反正,你可以使用:

...'from %s ...' % start 

等。例如,

In [183]: end = datetime.datetime.now() 

In [184]: end 
Out[184]: datetime.datetime(2016, 2, 11, 14, 56, 32, 77665) 

In [185]: str(end) 
Out[185]: '2016-02-11 14:56:32.077665' 

In [186]: end.date() 
Out[186]: datetime.date(2016, 2, 11) 

In [188]: '%s' % end.date() 
Out[188]: '2016-02-11' 

或者,(如果你還沒有提取的日期只有部分來自end),你可以使用strftime

In [189]: '%s' % end.strftime('%Y-%m-%d') 
Out[189]: '2016-02-11' 
+0

嗯,在使用這個答案我得到一個醜陋的污點與日期和時間戳。我正在尋找日期。 – Excaliburst

+0

你確定你將'end'傳遞給你的字符串_after_' end = end.date()'嗎?看我的編輯。 – xnx