Skip to content

bandpower() returns integer stage indices instead of string labels when include is passed as strings #250

@bkowshik

Description

@bkowshik

When calling bandpower() with a Hypnogram object and string include labels, the returned DataFrame has integer stage codes in the index instead of the original string labels. This causes bandpower.xs("N3") to raise a KeyError.

bandpower = yasa.bandpower(raw, hypno=hyp, include=["N2", "N3", "REM"])

bandpower.index.get_level_values("Stage").unique()
# Index([2, 3, 4], dtype='int64')  ← integers, not strings

bandpower.xs("N3")["Delta"]
# KeyError: 'N3'

This also breaks the example in the quickstart tutorial which uses bandpower.xs("N3").

Root cause: In spectral.py, string include labels are converted to integers for internal processing (line 178) but never mapped back to strings when building the output DataFrame.

Expected behavior: When include is passed as strings, the output DataFrame index should use those same string labels so that bandpower.xs("N3") works as documented.

Metadata

Metadata

Assignees

Labels

bug 💥Something isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions