Home > Blockchain >  Unwanted type conversion pandas apply (int64 --> float64)
Unwanted type conversion pandas apply (int64 --> float64)

Time:01-26

Why do pandas convert automatically int64 to float64?
I've checked out these questions:

but none of them are as simple as my case as far as I understood.
I am running the code on Jupyter lab.

>>> df.dtypes
cd_fndo      int64
dif        float64
dtype: object

so the types are int64 and float64. However applying the identity function results in type change:

>>> df.apply(lambda x: x, axis=1).dtypes
cd_fndo    float64
dif        float64
dtype: object

However, when considering only the first column, the type int64 remains the same:

>>> df.iloc[:, :1].apply(lambda x: x, axis=1).dtypes
cd_fndo    int64
dtype: object

Could someone please explain the causes of this type change?

CodePudding user response:

df.apply(lambda x: x, axis=1) means the lambda is mapped to each row. The row contains an int and a float hence it's converted to float to accommodate both elements.

In the second example, the row only contains 1 int so no conversion is needed.

  • Related