Open Erotemic opened 7 years ago
Seems to me simplest is just:
data.loc[flags, :] = False
Sorry, I missed that you didn't want to do this inplace. In that case the way to do it is with an apply
:
data.apply(lambda x: x & flags, axis=0)
That makes sense, I like apply
as a solution to this problem.
Is there any design-driven reason why methods like and_
, or_
, and xor
are not exposed as public methods of DataFrame's? Even though apply
is a nice way to do this, I think public method versions of these binary operations would be a useful improvement to pandas. Would a PR to add public versions of these methods be welcome?
Looks like these work now:
In [25]: pd.__version__
Out[25]: '0.24.0rc1+1.g33f91d8f9'
In [26]: data.mul(flags, axis=0)
Out[26]:
a b
0 True False
1 False False
2 False False
3 True True
In [27]: data.__and__(flags, axis=0)
Out[27]:
a b
0 True False
1 False False
2 False False
3 True True
Oh sorry, the request was for a DataFrame.and
, DataFrame.or
, etc...
Code Sample, a copy-pastable example if possible
Problem description
I have a use case where I have a data frame of where rows are samples and columns are sample properties. I also have a Series with associated flags that indicate which rows should have all values set to false.
At first I thought I could just the
&
operator, but I ran into a broadcasting problemValueError: operands could not be broadcast together with shapes (16,) (4,)
. So, I started to look for another way. I don't want to do this inplace sodata[flags] = False
wont work unless I use copy (but that's ugly).Instead I want to use an
and
function, but I couldn't find one (I'm still a bit new to the pandas API). So, I settled for using multiplication. However, I got a warning when I useddata.mul
. It told me to use&
which tipped me off that there was some way to call aand
function with the kwargsaxis=0
. I found that usingdata.__and__(flags, axis=0)
will work.I'm wondering why
data.mul
is exposed as a public version ofdata.__mul__
but why is there nodata.and_
version ofdata.__and__
?Expected Output
Output of
pd.show_versions()