mercredi 13 décembre 2017

Truth Value of a Pandas Series is not working as per expectation for Datetime Datatype

first of all I did not find answer to the Value Error for Datetime datatype. So this is not a duplicate PLEASE DO NOT MARK AS DUPLICATE.

For the below Data Frame I am trying a simple if condition to check for exame a date of 1st Jan 2017 while parsing through all the rows:

df.dtypes
Out[183]: 
Date       datetime64[ns]
Open              float64
Close             float64
Year                int64
Month               int64
WeekDay             int64
Day                 int64

If you see the data is very simple not too much of complications here:

print(df.head(2))
    Date    Open       Close  Year  Month   WeekDay  Day
0 2017-12-11  299.45  299.10  2017     12        0   11
1 2017-12-08  301.95  298.15  2017     12        4    8

But when I traverse the rows then it gives the Value Error:

lz=len(df)
for i in range(0,lz):
    if df['Year'][i]==2017 and df['Month']==1 and df['Day']==1:
        print("Found", df['Date'][i])
    else:
        print("Not Found")

Gives the Value Error below:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Any idea on how to correct this? A Thousand Thanks in advance for all your answers.

Aucun commentaire:

Enregistrer un commentaire