Skip to content

BUG: groupby.nth after selection #53518

Closed
@rhshadrach

Description

@rhshadrach
df = DataFrame(
    {
        "a": [1, 1, 2],
        "b": [np.nan, 4, 5],
        "c": [6, 7, 8],
    }
)
gb = df.groupby("a")[["b"]]
result = gb.nth(0, dropna="any")

raises ValueError: Item wrong length 3 instead of 2. If you remove the null value, column selection is no longer reflected in the result

df = DataFrame(
    {
        "a": [1, 1, 2],
        "b": [3, 4, 5],
        "c": [6, 7, 8],
    }
)
gb = df.groupby("a")[["b"]]
result = gb.nth(0, dropna="any")
print(result)
#    a  b  c
# 0  1  3  6
# 2  2  5  8

Replacing dropna=None in the above gives the expected

   b
0  3
2  5

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions