Reputation: 327
I'm a bit confused on an issue I have been having with my code test data below, as well as explanation.
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)
So when you run the above code you will see that all the blank time fields are mapped to NaT. Which is fine, however it is interrupting my code below:
if "Med to Ind Indicator" in test.columns:
test["Med to Ind Indicator"] = np.where(test["Med to Ind Date"] != '', "Yes", '')
The code above looks at the Med to Ind Date field, and if it isn't blank it maps the column Med to Ind Indicator to Yes. The work around I had was trying to replace pd.NaT with "" which worked, but it in turn undid my date_time transformation, and returned it to the original form. Can you guys recommend an alternative? Also, how exactly does pandas view the NaT field?
Upvotes: 1
Views: 43
Reputation: 109576
Use isnull()
(or notnull()
) to test for NaT
:
np.where(test["Med to Ind Date"].isnull(), '', "Yes")
Resulting output of test
:
Med to Ind Date Med to Ind Indicator
0 None
1 None
2 1402531200000000000 Yes
3 None
4 1402876800000000000 Yes
Upvotes: 2