Nov-06-2018, 06:41 PM
I am using Python 2.7 and I have data in a dataframe (df) and when I try rounding it does not consistently round, which appears to be due to the datatype being a float. My df has column value1 with values 0.121 to 0.130 I am testing on and I have used .round(2) and np.around(df['value'], decimals=2) but both round 0.125 to 0.12 and not 0.13. I attempted to use Decimal((df['value1'])).quantize(Decimal("0.01"), decimal.ROUND_HALF_UP).
However it caused an error to be raised "TypeError("Cannot convert %r to Decimal" % value)". I am also seeing TypeError : Cannot convert 0 0.121
Again, I feel I need to convert to decimal to correctly round, but nothing seems to work as expected for me. Any help is appreciated as this has frustrated me for the last couple days.
Thanks
However it caused an error to be raised "TypeError("Cannot convert %r to Decimal" % value)". I am also seeing TypeError : Cannot convert 0 0.121
Again, I feel I need to convert to decimal to correctly round, but nothing seems to work as expected for me. Any help is appreciated as this has frustrated me for the last couple days.
Thanks