I've got a DataFrame with enforced data types, which are quite important to my application:
df = (pd.DataFrame([(1, 1, 1000),
(1, 2, 2000)],
columns=['id', 'fk', 'value'])
.astype({'id': pd.Int32Dtype(),
'fk': pd.Int32Dtype(),
'value': pd.Float32Dtype()})
df.dtypes.to_dict()
correctly yields:
{'id': Int32Dtype(), 'fk': Int32Dtype(), 'value': Float32Dtype()}
However, when I pick one row using .iloc, Pandas suddently casts everything into float -- presumably because it turns it into a Series which wants a data type:
df.iloc[0].dtypes
yields:
Float64Dtype()
That causes downstream problems, as I need the data in the correct types. How can I pull out a single row while maintaining the correct types?