Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert pandas float series to int

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.

like image 466
Chris Avatar asked Dec 07 '15 23:12

Chris


People also ask

How do you convert all float columns to int in pandas?

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.

Can you convert a float to an int?

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.

Can you convert float to integer in Python?

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.


1 Answers

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.

like image 126