我对我在下面的代码测试数据中遇到的问题以及解释感到有点困惑。
test = {"Med to Ind Date": ['', '', 1402531200000000000, '', 1402876800000000000],
"Med to Ind Indicator": ['', '', 'Y', '', 'Y']}
test = pd.DataFrame(test)
date_fields = ["Med to Ind Date"]
test.loc[:, date_fields] = test.loc[:, date_fields].apply(pd.to_datetime)
因此,当您运行上面的代码时,您将看到所有空白时间字段都映射到NaT。哪个没问题,但它正在打断我的代码:
if "Med to Ind Indicator" in test.columns:
test["Med to Ind Indicator"] = np.where(test["Med to Ind Date"] != '', "Yes", '')
上面的代码查看Med to Ind Date字段,如果它不是空白,则将Med列显示为Ind Indicator为Yes。 我的工作是试图用""替换pd.NaT。哪个工作,但它反过来解除我的date_time转换,并将其返回到原始形式。你们能推荐一个替代方案吗?另外,大熊猫究竟是如何看待NaT场的呢?
答案 0 :(得分:2)
使用isnull()
(或notnull()
)来测试NaT
:
np.where(test["Med to Ind Date"].isnull(), '', "Yes")
test
的结果输出:
Med to Ind Date Med to Ind Indicator
0 None
1 None
2 1402531200000000000 Yes
3 None
4 1402876800000000000 Yes