How can I create a pandas dataframe column with dtype bool (or int for that matter) with support for Nan/missing values?
When I try like this:
d = {'one' : np.ma.MaskedArray([True, False, True, True], mask = [0,0,1,0]),
'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print (df.dtypes)
print (df)
column one
is implicitly converted to object. Likewise similar for ints
:
d = {'one' : np.ma.MaskedArray([1,3,2,1], mask = [0,0,1,0]),
'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print (df.dtypes)
print (df)
one
is here implicitly converted to float64
, and I'd prefer if I stayed in int
domain and not handle floating point arithmetic with its idiosyncrasies (always have tolerance when comparing, rounding errors, etc.)
one_na
to record that. – Birr