Keeping Large Decimal to multiply with Float Pandas

50 views Asked by At

I have large decimal values that I need to multiply with floats. Converting the decimal values to floats doesn't retain all the decimals that I need for calculations. How can I multiply the decimal values with floats while retaining the precision of the decimal?

The large decimal values (adj column here) are the product of an np.select function so they can't be read in as floats.

Minimally reproducible sample code:

from decimal import getcontext, Decimal
import pandas as pd

adjustment = ["0.235340860", "0.158615282", "0.117625053"]
ltrs = ["a", "b", "c"]
df = pd.DataFrame(
    zip(adjustment, ltrs), columns=["adj", "ltrs"])

df['adj'] = df['adj'].apply(lambda x: Decimal(x))
df['adj'] = df['adj'].apply(lambda x: x.quantize(Decimal('1.000000000')))

df['factor'] = round((df['adj']/(1-1.5*df['adj'])),12)
0

There are 0 answers