I am discretizing my series for a learner. I really need the series to be in float, and I really need to avoid for loops.
How do I convert this series from float to int?
Here is my function that is currently failing:
def discretize_series(s,count,normalized=True):
def discretize(value,bucket_size):
return value % bucket_size
if normalized:
maximum = 1.0
else:
minimum = np.min(s)
s = s[:] - minimum
maximum = np.max(s)
bucket_size = maximum / float(count)
Here is the line that causes the function to fail:
s = int((s[:] - s[:] % bucket_size)/bucket_size)
The int() induces a casting error: I am unable to cast the pandas series as an int series.
return s
If I remove the int(), the function works, so I may just see if I can get it to work anyway.
To convert a column that includes a mixture of float and NaN values to int, first replace NaN values with zero on pandas DataFrame and then use astype() to convert. Use DataFrame. fillna() to replace the NaN values with integer value zero.
A float value can be converted to an int value no larger than the input by using the math. floor() function, whereas it can also be converted to an int value which is the smallest integer greater than the input using math.
Converting Floats to IntegersPython also has a built-in function to convert floats to integers: int() . In this case, 390.8 will be converted to 390 . When converting floats to integers with the int() function, Python cuts off the decimal and remaining numbers of a float to create an integer.
The regular python int
function only works for scalars. You should either use a numpy function to round the data, either
s = np.round((s - s % bucket_size) / bucket_size) #to round properly; or
s = np.fix((s - s % bucket_size) / bucket_size) #to round towards 0
and if you actually want to convert to an integer type, use
s = s.astype(int)
to cast your array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With