我在Python 3.6和pandas' 0.20.1'中试验了一些标志对象。并发现pandas没有正确地路由我在持有枚举标志对象时启动的操作
=AND(VLOOKUP(1,A2:B31,2),SMALL(B2:B31,1))
pandas是否仍在追赶这些新的Python类型,而numpy似乎已经至少在import pandas
import enum
# Ok example with bools...
dfBool = pandas.DataFrame( { 'a':[True,True,False,False], 'b':[True,False,True,False]} )
dfBool['a'] | dfBool['b']
0 True
1 True
2 True
3 False
dtype: bool
# Broken example with enum.Flag...
flag = enum.Flag('flg' , ['a', 'b', 'c', 'd'] )
dfFlag = pandas.DataFrame( { 'c1':[flag.a, flag.b, flag.c, flag.d] , 'c2':[flag.d, flag.c, flag.b, flag.a]} )
dfFlag
c1 c2
0 flg.a flg.d
1 flg.b flg.c
2 flg.c flg.b
3 flg.d flg.a
dfFlag['c1'] | dfFlag['c2']
Traceback (most recent call last):
File "C:\Anaconda3\lib\site-packages\pandas\core\ops.py", line 883, in na_op
result = op(x, y)
TypeError: unsupported operand type(s) for |: 'flg' and 'bool'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda3\lib\site-packages\pandas\core\ops.py", line 924, in wrapper
return filler(self._constructor(na_op(self.values, other.values),
File "C:\Anaconda3\lib\site-packages\pandas\core\ops.py", line 894, in na_op
result = lib.vec_binop(x, y, op)
File "pandas\_libs\lib.pyx", line 894, in pandas._libs.lib.vec_binop (pandas\_libs\lib.c:15883)
File "pandas\_libs\lib.pyx", line 887, in pandas._libs.lib.vec_binop (pandas\_libs\lib.c:15755)
TypeError: unsupported operand type(s) for |: 'flg' and 'bool'
# Workaround with lambda to push the operation elementwise...
dfFlg[['c1','c2']].apply( lambda r : r[0] | r[1] , axis=1)
0 flg.d|a
1 flg.c|b
2 flg.c|b
3 flg.d|a
# Numpy workaround with values array
dfFlag['c1'].values | dfFlag['c2'].values
array([<flg.d|a: 9>, <flg.c|b: 6>, <flg.c|b: 6>, <flg.d|a: 9>], dtype=object)
之前是最新的,或者至少在某种程度上更强大地编码。