Mauro Gentile
Mauro Gentile

Reputation: 1511

Getting an error when rounding a series

I have a series of floats. It is the results of a data frame sum() operation. I need to round all its elements to integer but I get an error:

[in]:
A= mins.sum().iloc[1:]/60 
# this line works fine. The .iloc is to get rid of a text column.

[in]:
print(A)

[out]:
Min bad                     249.5
Min pr-ul                   967.57
intra com diff              178.05
Intra com diff 60           184.27
dtype: object

Now, if I try to round I get an error:

[in]:
A.round()

[out]:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-528-685b5302b717> in <module>()
  3 
  4 print(A)
  ----> 5 A.round()

 //anaconda/lib/python3.5/site-packages/pandas/core/series.py in round(self,decimals, *args, **kwargs)
   1303         """
   1304         nv.validate_round(args, kwargs)
   -> 1305         result = _values_from_object(self).round(decimals)
   1306         result = self._constructor(result, index=self.index).__finalize__(self)
   1307 

   AttributeError: 'float' object has no attribute 'rint'

Can anyone tell me why is that and ho I can fix it? I guess that the root of the problem is because the Series is "object" type. But what is this? It only contains float numbers!!! it is the results of a data frame summarization

thank you in advance for helping

Upvotes: 5

Views: 9692

Answers (1)

Jonathan Biemond
Jonathan Biemond

Reputation: 548

Cast the series to type float using .astype(float) as suggested by Mark Dickinson. In your case you should use A.astype(float).round()

I ran into the same error rounding a column in a data frame, casting to a float solved it for me. Thanks to Mark Dickinson.

Upvotes: 5

Related Questions