Data type of pandas column changes to object when it's passed to a function via apply?
It appears to be due to an optimization in DataFrame._apply_standard
. The "fast path" in the code of that method creates an output Series whose dtype is the dtype of df.values
, which in your case is object
since the DataFrame is of mixed type. If you pass reduce=False
to your apply
call, the result is correct:
>>> df.apply(lambda col: col.dtype, reduce=False)
floatcol float64
stringcol object
dtype: object
(I must say that it is not clear to me how this behavior of reduce
jibes with the documentation.)