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.
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.
y = [0.1234, 0.6789, 0.5678]
pandas
Seriess = pd.Series(data=y)
print(s.round(3))
returns
0 0.123
1 0.679
2 0.568
dtype: float64
print(s.astype(int))
returns
0 0
1 0
2 0
dtype: int64
pd.Series(data=y).round(3)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.