before OP takes down my question, please note that I have viewed posts: pandas python - round() not behaving correctly
round() doesn't seem to be rounding properly
and they don't answer my question.
My question is similar on those, as you may have known that python does not round half values correctly (in a standard way), i.e.:
2.5 -> 2
2.25 -> 2.2
whereas we know that:
2.5 should be ->3
2.25 should be -> 2.3 etc...
now I have a Pandas Series object say below:
index | Another header |
---|---|
1 | 2.25 |
2 | 3.55 |
3 | 4.44 |
4 | 5.56 |
5 | 6.23 |
6 | 7.45 |
I currently have the code Series.apply(lambda x: round(x, 1))
and have created this function:
def correctRound(x: float, n: int):
target_precision = "1." + "0" * n
rounded_x = float(Decimal(x).quantize(Decimal(target_precision), dwRound))
if n == 0:
rounded_x = int(rounded_x)
return rounded_x
however this could not be worked on Series, how could I correctly round a DataFrame or Series object?
Thanks a lot for the help