Skip to content

Accept pandas DataFrames directly in the Python API - #187

Merged
hechth merged 2 commits into
mainfrom
copilot/improve-api-accept-dataframes
Oct 20, 2026
Merged

hechth merged 2 commits into
mainfrom
copilot/improve-api-accept-dataframes

Conversation

Copilot AI commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

The Python API could only load data from files, which forced users who already hold a DataFrame to write it to disk first. This PR lets Application take a DataFrame directly and optionally rename its columns via a mapping, so users don't have to change their own frames.

  • Application
    • load_dataframe(df, column_mapping=None) loads a DataFrame as the working data.
    • get_dataframe(column_mapping=None) returns the current data as a DataFrame. It raises TypeError if the loaded data isn't DataFrame-backed (e.g. spectra loaded from MSP).
  • DataFrame data class
    • load_dataframe and get_dataframe hold the logic. Both work on a copy, so the caller's frame is never mutated.
    • load_dataframe raises TypeError for non-DataFrame input.
    • column_mapping is {source_name: target_name}. Columns not in the mapping are kept as they are.
  • Tests: tests/test_DataFrame_api.py covers load with mapping, no mutation of the input, and the reverse rename.
app = Application()
app.load_dataframe(df, {"Name": "compound_name"})
await app.annotate_spectra(converters=["PubChem"])
out = app.get_dataframe({"compound_name": "Name"})  # back to the user's own column names

Not covered:

  • There is no validation that the required columns are present.
  • The README, docs and CHANGELOG are not updated for the new methods.

Co-authored-by: hechth <12066490+hechth@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve Python API to accept DataFrames for direct use Accept pandas DataFrames directly in the Python API Oct 20, 2026
Copilot AI requested a review from hechth October 20, 2026 10:06
@hechth
hechth marked this pull request as ready for review October 20, 2026 13:20
@hechth
hechth merged commit d8d8a56 into main Oct 20, 2026
4 checks passed
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.

Improve Python API to accept DataFrames for direct use

2 participants