Skip to content

Feature/pandas coords#719

Closed
RobbieKiwi wants to merge 5 commits into
PyPSA:masterfrom
RobbieKiwi:feature/pandas-coords
Closed

Feature/pandas coords#719
RobbieKiwi wants to merge 5 commits into
PyPSA:masterfrom
RobbieKiwi:feature/pandas-coords

Conversation

@RobbieKiwi
Copy link
Copy Markdown
Contributor

Closes #709

Changes proposed in this Pull Request

Fix the bug with pandas coords not being broadcast

Checklist

  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release_notes.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

@FBumann
Copy link
Copy Markdown
Collaborator

FBumann commented May 21, 2026

@RobbieKiwi many ci fails. We might only fix this in #717

@FBumann
Copy link
Copy Markdown
Collaborator

FBumann commented May 23, 2026

@RobbieKiwi Thanks for the PR.

I would like to rewrite some of it to be more efficient:

def _sanitize_pandas(arr: pd.Series | pd.DataFrame) -> DataArray | None:
    names = list(arr.index.names)
    if isinstance(arr, pd.DataFrame):
        names += list(arr.columns.names)
    if any(n is None for n in names):
        return None

    if isinstance(arr, pd.DataFrame):
        arr = arr.stack(list(range(arr.columns.nlevels)), future_stack=True)

    return arr.to_xarray()

Also, we need to take care that the dim order stays deterministic (see #719).
Id like to merge #710 first, then adjust it here so pandas gets transposed properly

@FBumann
Copy link
Copy Markdown
Collaborator

FBumann commented May 23, 2026

@RobbieKiwi Due to the close coupling with #710 i decided thats its easiest to combine both into a single PR.
I've ported your tests and made some small changes to your code. If you want to I'd love to get your review on it.

@FBumann FBumann closed this May 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add_variables: pandas Series/DataFrame bounds with missing dimensions silently drop the dimension

2 participants