0

I have a pandas Series:

freq_features = num_occurrences/num_nodes
print(type(freq_features))
print(freq_features)

<class 'pandas.core.series.Series'>
PAT    0.386667
NUR    0.360000
MED    0.146667
ADM    0.106667

I round each value to 2 decimal places:

freq_features_rounded = freq_features.round(2)
print type(freq_features_rounded)
print freq_features_rounded

<class 'pandas.core.series.Series'>
PAT    0.39
NUR    0.36
MED    0.15
ADM    0.11
dtype: float64

But when I use the to_dict() command, I get the non-rounded values (except for the ADM key):

freq_features_dict = freq_features_rounded.to_dict()
print freq_features_dict
{'NUR': 0.35999999999999999, 'ADM': 0.11, 'PAT': 0.39000000000000001, 'MED': 0.14999999999999999}

Why is this happening, and how can I get my rounded values into the dict() object as values?

StatsSorceress
  • 3,019
  • 7
  • 41
  • 82
  • 4
    They are rounded, what you are see is float point error and is unavoidable. This is 'working as expected'. – gbtimmon Dec 05 '17 at 17:19
  • A workaround is to turn that in % : int(0.39*100) has always 2 digits. – B. M. Dec 05 '17 at 17:27
  • @gbtimmon: Not exactly unavoidable. It looks like Python's `float` type would remove the excess precision, as would a `str` version of `numpy.float64`, but the `repr` of `numpy.float64` doesn't try to minimize the representation. Printing a `dict` always prints the `repr` of its values though, so in this case, you see the excess precision. Under the hood, the imprecision still exists (`0.36` doesn't really exist in IEEE 64 bit floating point, it's just a short-hand for `0.3599999999...`), but you don't need to see it. – ShadowRanger Dec 05 '17 at 17:29

0 Answers0