Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to write a code for creating a bucket. since the column to be bucketed has a lot of decimal values and is a object datatype i converted the column into float type. After the type conversion and when running the bins the error occurs

What I have tried:

Here's my code

```
Python
import pandas as pd
import numpy as np
df=pd.read_excel(r'D:\Practice Files\Anirudh Exercise Skin India.xlsx')
#print(df)

#Conversion of str to int
df['Basepacksize'] = df['Basepacksize'].astype(str).astype(float)

#Pack Ranges/Sachets/Non-Sachets
bins= ['0','10','15','30','50','100']
df1=pd.cut(df['Basepacksize'], bins=bins)
print(df1)

```

Here's the error that occurs

Traceback (most recent call last):
  File "C:\Users\ani\PycharmProjects\pythonProject\main7.py", line 11, in <module>
    df1=pd.cut(df['Basepacksize'], bins)
  File "C:\Users\ani\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\reshape\tile.py", line 293, in cut
    fac, bins = _bins_to_cuts(
  File "C:\Users\ani\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\reshape\tile.py", line 444, in _bins_to_cuts
    labels = _format_labels(
  File "C:\Users\ani\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\reshape\tile.py", line 578, in _format_labels
    precision = _infer_precision(precision, bins)
  File "C:\Users\ani\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\reshape\tile.py", line 644, in _infer_precision
    levels = [_round_frac(b, precision) for b in bins]
  File "C:\Users\ani\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\reshape\tile.py", line 644, in <listcomp>    levels = [_round_frac(b, precision) for b in bins]
  File "C:\Users\ani\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\reshape\tile.py", line 628, in _round_frac
    if not np.isfinite(x) or x == 0:
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Process finished with exit code 1
Posted
Updated 11-Dec-22 22:49pm
v3
Comments
Richard MacCutchan 12-Dec-22 8:01am    
I just tried this and it worked fine. I can only assume that the problem is with your data, which we cannot see.
Anirudh Sudarsan 12-Dec-22 11:39am    
thanks for the solution. it worked

1 solution

This is not correct:
Python
bins= ['0','10','15','30','50','100']

The bins values are ranges from 0 to N, so it should be:
Python
bins= [0, 10, 15, 30, 50, 100]

giving the sets [0 - 10], [10 - 15], [15 - 30], [30 - 50], [50 - 100]

See Pandas.cut() method in Python - GeeksforGeeks[^] for sample code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900