1

I have a Series with a multiindex of companies and filing periods. I want to index this series with a separate boolean series of companies that pass some filter, returning all companies and filing periods that match the list of companies provided.

Here is example code: First create a series with a multiindex.

arrays = [
    ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
    ["one", "two", "one", "two", "one", "two", "one", "two"],
]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
s = pd.Series(np.random.randn(8), index=index)

first  second
bar    one      -0.107011
       two       1.170427
baz    one      -0.567174
       two      -0.902847
foo    one      -0.986784
       two       0.972722
qux    one       0.714136
       two       0.044249
dtype: float64

Then create the boolean series (in my case generated from a groupby object with a filter for companies with multiple filing periods):

test = pd.Series([True, False, True, False], index=['bar', 'baz', 'foo', 'qux'], name='first')
bar     True
baz    False
foo     True
qux    False
Name: first, dtype: bool

I want to slice the Series to only return rows that match company bar and foo and omit the rest.

s.loc[test]
>>>IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).

Can you help?

1 Answer 1

1

Try:

>>> s[test[test].index]
first  second
bar    one      -0.188867
       two       0.025214
foo    one       0.693238
       two       2.569597
dtype: float64
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.