I have a very simple set of code that I'm trying to generate a result off of a timedelta.
currentdate = datetime.datetime.now()
currentdate = currentdate.strftime("%m/%d/%Y")
##I need this first bit for another portion of my code, so it's a little messy.
now = datetime.date.today()
now = datetime.datetime.strptime(currentdate,'%m/%d/%Y').date()
now = pd.to_datetime(now)
delta1 = timedelta(days=30)
delta2 = timedelta(days=59)
delta3 = timedelta(days=180)
outdated = timedelta(days=1)
Line_Down = pd.to_datetime(fulldf['Line_Down'])
After this, I have each column set to tell me whether the "Line_Down" column is a certain amount of days away from the current date, generating a TRUE or FALSE value. I've stripped the time from the dates to make things more readable.
dateout = Line_Down - now
fulldf['Priority1'] = (delta1 >= dateout)
fulldf['Priority2'] = (delta2 >= dateout)
fulldf['Priority3'] = (delta3 >= dateout)
This is the code I have to check for TRUE/FALSE which obviously doesn't work.
fulldf['Priority1'] = fulldf['Priority1'].apply(lambda x: str(x))
if (fulldf['Priority1']) == 'FALSE':
fulldf['Priority'] = '1'
The 'Priority' column just sets a number depending on how far away the "Line_Down" date is from the current date. However Python doesn't recognize "Priority1-3" as a boolean, so I can't parse it with any statement. It states (As I'm sure you've expected):
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
edit: I understand that it's trying to process the entire column at once, which creates the ambiguity. How do I get it to look at each row individually, to resolve the ambiguity?
How can I convert this to a string or boolean to process it? The raw TRUE/FALSE values are worthless to me, but that's all I seem to be able to get from a TimeDelta calculation.
You could use numpy.where
function to perform the operation you're trying implement.
Here's how you could use it in the context of your problem:
import numpy as np
fulldf['Priority'] = np.where(fulldf['Priority1']==False, 1, 0)
# ^-------------------------^ ^-^ ^-^
# | | |
# | | +-- [Optional] Add the value
# | | you want to set when
# | | the condition is False
# | |
# | +-- Add the value you
# | want to set when
# | the condition is True
# |
# +-- Set the condition you want to
# check.
print(fulldf)
# Prints:
#
# Line_Down Priority1 Priority2 Priority3 Priority
# 0 2023-01-06 True True True 0
# 1 2023-01-07 True True True 0
# 2 2023-01-08 True True True 0
Line_Down | Priority1 | Priority2 | Priority3 | Priority |
---|---|---|---|---|
2023-01-06 00:00:00 | True | True | True | 0 |
2023-01-07 00:00:00 | True | True | True | 0 |
2023-01-08 00:00:00 | True | True | True | 0 |
The np.where
function works by taking in a condition and two values that represent the values to set when the condition is true or false.
The advantage of using this function is that it works well with pandas' data frames and series.
The function is also efficient, since it is vectorized, meaning that it can work on an entire array at once.