我有一个从pd.read-sql查询创建的pandas列。列中有空日期,它们返回为如下所示的NoneTypes。使用NoneType评估datetime.date列
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'
然后我尝试运行以下功能:
sixty = now - timedelta(60)
def f(row):
if row['FFD'] < sixty:
val = 'SR'
return val
stores['JRSR'] = stores.apply(f, axis = 1)
这会返回一个错误:
TypeError: ("'<' not supported between instances of 'NoneType' and 'datetime.date'", 'occurred at index 10')
我能够列转换为字符串,出于比较的目的,然而,我需要这个字段作为下游使用的日期字段。我的转换代码是:
stores['FFD'] = pd.to_datetime(stores['FFD'])
stores['FFD'] = stores['FFD'].dt.strftime("%Y-%m-%d")
如何让我的函数在不转换列的情况下工作?基本上我想我的函数只计算datetime.date对象。我试过了:
def(f)row:
if isinstance(row['FFD'], NoneType):
val = ""
elif row['FFD'] < sixty:
val = 'SR'
但是这并没有按照预期工作。
在你的第一个函数中,如果row ['FFD']
哇 - 多么简单。只需添加第一条建议并从中获得完美结果即可。如果行['FFD']和...做什么? –
哦,是的,谢谢! –