2015-02-02 6 views
4

Python에서 두 날짜를 비교하려고합니다.두 날짜를 비교할 때 오류가 발생했습니다.

AttributeError: 'datetime.date' object has no attribute 'date'

사람이 말할 수 : 나는 유형() 검사를 실행할 때 나는 두 날짜에 대한 간단한 검사를 실행할 때, 모두 내가 다음과 같은 오류를 얻을 <type 'datetime.date'>

그럼에도로 표시 내가 여기서 잘못하고있는 것. 아래 코드 :

d_today = date.today() 
oldestDate = d_today - BDay(750) 
avgArray2 = [x for x in avgArray if x[0] >= oldestDate.date()] 

print type(oldestDate.date()) 
print type(avgArray[0][0]) 

그리고 출력 :에 따라 이미 datetime.date 두 개의 인스턴스가있는 경우

d_today = date.today() 
d_ref = d_today - BDay(66) 
lastDateData = dates[0] 

avgArray = [] 

while (d_ref >= lastDateData): 
    avg_data = [x for x in stockData_sorted if (x[0] >= d_ref and x[0] <= d_ref + BDay(22))] 
    avg_dates = [d[0] for d in avg_data] 
    avg_graphData = [d[1] for d in avg_data] 

    workingAvg = sum(avg_graphData)/len(avg_graphData) 
    avgArray.append((d_today,workingAvg)) 

    d_today = d_today - BDay(1) 
    d_ref = d_ref - BDay(1) 
+0

avgArray에서'print ([type (x [0]) '의 결과는 무엇입니까?)'? –

+0

다음은 결과입니다. - - 그래서 datetime 날짜로 변환하기 위해 ".date()"호출을 사용했습니다. (또는 그렇게 생각했습니다) – JDGD

+0

'avgArray'는 무엇으로 구성되어 있습니까? 이 배열이 만들어진 곳의 코드를 줄 수 있습니까? – armnotstrong

답변

3

일반 datetime 개체가 아닌 'pandas.tslib.Timestamp'이 있습니다. 에는 .date() 메소드가 없습니다.

avgArray2 = [x for x in avgArray 
       if datetime.date(x[0].year, x[0].month, x[0].day) >= oldestDate.date()] 
+0

도움 주셔서 감사합니다. date() 메소드가 없다는 것을 깨닫지 못했습니다. 지금이 오류가 발생합니다 : TypeError : 설명자 '날짜' 'datetime.datetime'개체가 필요하지만 'int'를 받았습니다. – JDGD

+0

마지막으로이 작업이 있습니다.잠시 전에 깨달았어야했던 간단한 실수 - date (x [0]. year, x [0] .month, x [0] .day)와 같은 함수를 호출해야했습니다. 도움에 다시 한번 감사드립니다. – JDGD

1

, 상기 "avgArray은"빌드 방법에 대한

<type 'datetime.date'> 
<type 'datetime.date'> 
avgArray2 = [x for x in avgArray if x[0].date() >= oldestDate.date()] 
AttributeError: 'datetime.date' object has no attribute 'date' 

전체 코드 출력 :

<type 'datetime.date'> 
<type 'datetime.date'> 

그때 오류가 각각에 대해 존재하지 않는 date 메서드를 호출하고 있습니다.이 메서드는 이미 날짜이며, 직접 비교합니다.

IOW는

if x[0].date() >= oldestDate.date() 

대신에 단지

if x[0] >= oldestDate 

는 잘 작동합니다.

추가 된 : 그것은 오해의 소지가 출력 x[0]에도 불구하고 것 같다되지datetime.date 오히려 팬더 타임 스탬프 (I 출력을 설명 할이 경우 드릴 수 없습니다). 이 경우,

datetime.date(x[0].year, x[0].month, x[0].day) >= oldestDate 

이 더 효과적 일 수 있습니다.

+0

그건 나에게 혼란 스럽다. 당신이 좋아하는 코드를 실행할 때 다음과 같은 오류가 발생합니다. avgArray2 = [x [0]> = oldestDate 인 경우 avgArray의 x에 대한 x : 파일 "tslib.pyx", 694 행, pandas.tslib._Timestamp .__ richcmp__ (pandas/tslib.c : 12256) TypeError : 'Timestamp'유형을 'date'유형과 비교할 수 없습니다. – JDGD

+0

A를 편집하여이 예외를 언급하십시오. –

관련 문제