This scenario is a simplification of an ETL scenario involving multiple sets of data pulled from MySQL tables. I have a merged dataframe where one price column is type float64
and the other is type object
.
import pandas as pd
df = pd.DataFrame({
'price1': [0.066055],
'price2': ['0.066055'],
})
>>> df.dtypes
price1 float64
price2 object
dtype: object
When these two columns are converted to float64
, the column price1
is rounded incorrectly when rounded to 5 digits.
float64_df = df[price_cols].apply(lambda x: pd.to_numeric(x))
>>> float64_df.dtypes
price1 float64
price2 float64
dtype: object
>>> float64_df[price_cols].apply(lambda x: x.round(5))
price1 price2
0 0.06606 0.06605
However, when the columns are converted to float32
using downcast='float'
, the rounding works as expected.
float32_df = df[price_cols].apply(lambda x: pd.to_numeric(x, downcast='float'))
>>> float32_df.dtypes
price1 float32
price2 float32
dtype: object
>>> float32_df[price_cols].apply(lambda x: x.round(5))
price1 price2
0 0.06606 0.06606
Any ideas why the rounding doesn't work properly when both columns are of type float64
?