0
s = pd.Series(np.nan, index=[49,48,47,46,45, 1, 2, 3, 4, 5])

output of s is

49   NaN
48   NaN
47   NaN
46   NaN
45   NaN
1    NaN
2    NaN
3    NaN
4    NaN
5    NaN

s.loc[[False,True]]

it gives output as-:

48   NaN

.loc Access a group of rows and columns by label(s), I have given list of false and true and it is also not equal to length of axis being sliced. My doubt is if we gave list of boolean array to loc it slice the dataframe/series with position instead of label?

2
  • 3
    It is not clear what you are wanting to return here. Commented Dec 9, 2019 at 17:10
  • 1
    s.loc[[False,True]] does not (or at least, should not) return what you stated Commented Dec 9, 2019 at 17:43

1 Answer 1

1

I certainly get an error when I am running this:

import pandas as pd
import numpy as np
s = pd.Series(np.nan, index=[49,48,47,46,45, 1, 2, 3, 4, 5])
s.loc[[False,True]]

the error (as expected) is:

IndexError: Item wrong length 2 instead of 10.

Maybe your problem is specific to a certain version of pandas? Maybe an old one? I used pandas version 0.25.3

Sign up to request clarification or add additional context in comments.

1 Comment

That used to work but it was a bug which was fixed: github.com/pandas-dev/pandas/pull/26911 What OP is doing is essentially equivalent to s.loc[[False, True] + [False] * (len(s) - 2)] which in fact returns only the second element in the series, and I'm not sure what else they were expecting here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.