I have a pandas column, created from a pd.read-sql query. There are null dates in the column and they are returned as NoneTypes that looks like the following.
FFD
2014-10-29
2015-06-03
None
2017-05-05
print(type(stores['FFD'][0]))
class datetime.date
print(type(stores['FFD'][2]))
class'NoneType'
I then try to run the following function:
sixty = now - timedelta(60)
def f(row):
if row['FFD'] < sixty:
val = 'SR'
return val
stores['JRSR'] = stores.apply(f, axis = 1)
This returns an error :
TypeError: ("'<' not supported between instances of 'NoneType' and 'datetime.date'", 'occurred at index 10')
I am able to convert the column to string, for comparison purposes, however I need this field to remain as a date field for downstream uses. My conversion code is:
stores['FFD'] = pd.to_datetime(stores['FFD'])
stores['FFD'] = stores['FFD'].dt.strftime("%Y-%m-%d")
How can I get my function to work without converting the column? Essentially I want my function to evaluate ONLY the datetime.date objects. I tried:
def(f)row:
if isinstance(row['FFD'], NoneType):
val = ""
elif row['FFD'] < sixty:
val = 'SR'
But that did not work as intended.
In the first
f
function, changingif row['FFD'] < sixty:
to
if row['FFD'] and row['FFD'] < sixty:
solved OP's issue.
if row['FFD']
will evaluate toTrue
ifrow['FFD']
contains anything else thanNoneType
,0
orFalse
. This is the Pythonic way to check for presence ofNone
. Note that because of the short-circuit behaviour of logical operators, check forNone
should always be placed first in a compound condition. Soif row['FFD'] and row['FFD'] < sixty:
will work, butif row['FFD'] < sixty and if row['FFD']
won't.