1

I am looking for an equivalent to df.any() and df.all() that would work on series that are not in a dataframe. I would rather not use the | and & operators, because i want to chain the result with other methods in a more readable way.

For example, I have two series of booleans of same length- s1, s2. I would like to be able to do something like this:

pd.any(s1, s2)

That would return the same result as this:

s1 | s2

I also found a way to do do "and" comparison:

s1.add(s2)

The above statements returns the same result as:

s1 & s2

It seems to me like there might be a more explicit way to do an "and" operation.

Thanks!

Kuzenbo
  • 229
  • 4
  • 9

1 Answers1

2

Use numpy.logical_and or numpy.logical_or:

np.logical_and(s1, s2)

Or:

np.logical_or(s1, s2)

If there is multiple Series add reduce:

np.logical_and.reduce([s1, s2, s3])

Last if need new Series use constructor:

pd.Series(np.logical_and(s1, s2), index=s1.index)

Notice:

Those methods ignore the series index, so the series must be sorted before passing them to the method for same indices in both.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Thanks. I tested it and it works as I wanted! I would just like to note that according to my tests those methods ignore the series index, so the series must be sorted before passing them to the method – Kuzenbo Feb 13 '19 at 08:07