如何在滚动系列中使用qcut功能? 如果我这样做:
def my_func(values):
res = pd.qcut(values, 2)
return res
s = pd.Series([1, 2, -0.1, -8.45, 10, 11, 2, 3])
z = s.rolling(2).apply(my_func)
我获得:
TypeError: must be real number, not Categorical
因为qcut返回一个分类对象。
EDIT1: 我想输出如下:
z =
[(0.999, 1.5], (1.5, 2.0]]
[(0.95, 2.0], (-0.101, 0.95]]
[(-4.275, -0.1], (-8.451, -4.275]]
[(-8.451, 0.775], (0.775, 10.0]]
[(9.999, 10.5], (10.5, 11.0]]
[(6.5, 11.0], (1.999, 6.5]]
[(1.999, 2.5], (2.5, 3.0]]
答案 0 :(得分:0)
一种可能的黑客解决方案:
L = []
def my_func(values):
res = pd.qcut(values, 2)
#create lists
L.append(list(res))
#return some aggreagtion for working custom function
return values.sum()
s = pd.Series([1, 2, -0.1, -8.45, 10, 11, 2, 3])
z = s.rolling(2).apply(my_func)
print (z)
print (L)
[[Interval(0.999, 1.5, closed='right'),
Interval(1.5, 2.0, closed='right')],
[Interval(0.94999999999999996, 2.0, closed='right'),
Interval(-0.10100000000000001, 0.94999999999999996, closed='right')],
[Interval(-4.2750000000000004, -0.10000000000000001, closed='right'),
Interval(-8.4509999999999987, -4.2750000000000004, closed='right')],
[Interval(-8.4509999999999987, 0.77500000000000002, closed='right'),
Interval(0.77500000000000002, 10.0, closed='right')],
[Interval(9.9990000000000006, 10.5, closed='right'),
Interval(10.5, 11.0, closed='right')],
[Interval(6.5, 11.0, closed='right'),
Interval(1.9990000000000001, 6.5, closed='right')],
[Interval(1.9990000000000001, 2.5, closed='right'),
Interval(2.5, 3.0, closed='right')]]
答案 1 :(得分:0)
这项工作可以吗?
def my_func(values):
res = pd.qcut(values, 2, labels=False)[-1]
return res
s = pd.Series([1, 2, -0.1, -8.45, 10, 11, 2, 3])
z = s.rolling(2).apply(my_func)
z
result:
0 NaN
1 1.0
2 0.0
3 0.0
4 1.0
5 1.0
6 0.0
7 1.0
dtype: float64
只需阅读您的首选输出。我一读就错过了。