Home:ALL Converter>PYTHON Numpy where time condition

PYTHON Numpy where time condition

Ask Time:2017-04-25T07:42:57         Author:Filippo

Json Formatter

I have the following target: I need to compare two date columns in the same table and create a 3rd column based on the result of the comparison. I do not know how to compare dates in a np.where statement.

This is my current code:

now = datetime.datetime.now() #set the date to compare
delta = datetime.timedelta(days=7) #set delta
time_delta = now+delta #now+7 days

And here is the np.where statement:

DB['s_date'] = np.where((DB['Start Date']<=time_delta | DB['Start Date'] = (None,"")),DB['Start Date'],RW['date'])

There is an OR condition to take into account the possibility that Start Date column might be empty

Author:Filippo,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43599318/python-numpy-where-time-condition
walker_4 :

Would lambda apply work for you Filippo? It looks at a series row-wise, then applies a function of your choice to every value of the row. Whatever is returned in the function will fill up the series with the values it returns.\n\ndef compare(date):\n if date <= time_delta or date == None:\n #return something\n else:\n #return something else\nDB['s_date'] = DB.apply(lambda x: compare(x))\n\n\nEDIT: This will work as well (thanks EyuelDK)\n\nDB['s_date'] = DB.apply(compare)\n",
2017-04-25T00:43:49
yy