I'm trying to make an if/elif/else statement that checks a date in a column dataframe (date_df), the day of the week and a another date in a variable that checks if it's friday (friday). I've tried all diferent ways to do this if/elif statement and the closest i got to make it work was using numpy to do the comparison between dates, but i getting stuck in a syntax error in the elif part.
Can someone please, help me where i'm doing something wrong?
friday = (date.today() - timedelta(days=3)).strftime('%Y%m%d' ' 00:00:00')
cond1 = np.logical_and((date.today().weekday() != 0), (date_df == yesterday)) #date_df it's a value in a df column
cond2 = np.logical_and((date.today().weekday() == 0), (date_df == friday ))
if cond1.bool() == True:
print('database updated')
elif cond2.bool():
print('database updated')
else:
print('database not updated')
Error returned:
elif cond2.bool()==True:
File "\<stdin\>", line 1
elif cond2.bool()==True:
^
SyntaxError: invalid syntax\`
CodePudding user response:
The syntax error is being caused by the elif statement because the bool() method is being called on a numpy array instead of a single value. The bool() method returns a TypeError when called on a numpy array.
Instead of using the bool() method, you can use the any() or all() method to check if any or all of the values in the numpy array are True, respectively.
Here's an example of how you can update your code to fix the syntax error:
friday = (date.today() - timedelta(days=3)).strftime('%Y%m%d' ' 00:00:00')
cond1 = np.logical_and((date.today().weekday() != 0), (date_df == yesterday))
cond2 = np.logical_and((date.today().weekday() == 0), (date_df == friday ))
if cond1.any():
print('database updated')
elif cond2.any():
print('database updated')
else:
print('database not updated')
Alternatively, you can use the .iloc[] indexing to extract the value of the numpy array and check it with the if statement.
friday = (date.today() - timedelta(days=3)).strftime('%Y%m%d' ' 00:00:00')
cond1 = np.logical_and((date.today().weekday() != 0), (date_df == yesterday))
cond2 = np.logical_and((date.today().weekday() == 0), (date_df == friday ))
if cond1.iloc[0]:
print('database updated')
elif cond2.iloc[0]:
print('database updated')
else:
print('database not updated')
CodePudding user response:
The problem seems to be in the condition cond2. In the first np.logical_and the first parameter is checking if the day of the week is 0, which corresponds to Monday, however, in the second parameter you are checking if the date in the column date_df is the same as the variable friday, which you previously defined as 3 days before today. The second parameter should be comparing with the current day (friday) and not 3 days before. The condition cond2 should look like this:
cond2 = np.logical_and((date.today().weekday() == 4), (date_df == date.today().strftime('%Y%m%d' ' 00:00:00')))
Also, you should compare the bool value of the condition using .all() instead of .bool() because the conditions return a Pandas Series and not a single boolean value.
if cond1.all() == True:
Also, you should be careful with the timedelta(days=3), that is not 3 days before today, but 3 days before the current day.
friday = (date.today() - timedelta(days=3)).strftime('%Y%m%d' ' 00:00:00')
should be:
friday = (date.today() - timedelta(days=4)).strftime('%Y%m%d' ' 00:00:00')
This will ensure that you are comparing the right date to friday.