I think you can create the DataFrame directly with Decimal types and operate with the values
import pandas as pd
import numpy as np
from decimal import *
df = pd.DataFrame({
'DECIMAL_1': [Decimal('2342.2345234'), Decimal('564.5678'), Decimal('76867.8923892')],
'DECIMAL_2': [Decimal('67867.43534534323'), Decimal('67876.345345'), Decimal('234234.2345345')]
})
df['DECIMAL_3'] = df['DECIMAL_1'] + df['DECIMAL_2']
df.dtypes
The drawback could be that the columns dtype
is going to be object
and the performance will decrease, I am afraid. Anyway, I think that any operation with the Decimal
will require more computation than operating with floats.
Maybe the best solution is to have a copy of the DataFrame. One DF with floats and the other one with Decimal. If you need to make fast operations you can use the DF with floats, if you need to compare or assign new values to some cells with some specific precision you can use the DF created with Decimal.
Tell me what you think about my suggestions.
Note: I made my example with DataFrame, but a DataFrame is built with Series