Evaluate datetime.date column with NoneType

1.6k views Asked by At

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.

1

There are 1 answers

0
Abhinav Gupta On

In the first f function, changing

if row['FFD'] < sixty:

to

if row['FFD'] and row['FFD'] < sixty:

solved OP's issue.

if row['FFD'] will evaluate to Trueif row['FFD'] contains anything else than NoneType, 0 or False. This is the Pythonic way to check for presence of None. Note that because of the short-circuit behaviour of logical operators, check for None should always be placed first in a compound condition. So if row['FFD'] and row['FFD'] < sixty: will work, but if row['FFD'] < sixty and if row['FFD'] won't.