Skip to content

Fix merge_features crash on empty DataFrame input - #45

Merged
kheal merged 4 commits into
masterfrom
copilot/fix-merge-features-empty-df
Mar 11, 2026
Merged

Fix merge_features crash on empty DataFrame input#45
kheal merged 4 commits into
masterfrom
copilot/fix-merge-features-empty-df

Conversation

Copilot AI commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

merge_features raised ValueError: zero-size array to reduction operation maximum which has no identity when passed an empty DataFrame, because it unconditionally called values.max() before checking whether the array had any elements.

Changes

  • deimos/alignment.py: Added an early-return guard in merge_features — if the input DataFrame is empty, return it immediately before any distance computation:
    if len(features.index) == 0:
        return features
  • tests/test_alignment.py: Added test_merge_features_pass_none and test_merge_features_empty to cover the None and empty-DataFrame paths.

Example

import pandas as pd
import deimos

empty_df = pd.DataFrame(columns=['mz', 'intensity'])

# Previously raised ValueError; now returns the empty DataFrame
result = deimos.alignment.merge_features(empty_df, dims=['mz'], tol=[5e-6], relative=[True])
assert len(result) == 0
Original prompt

This section details on the original issue you should resolve

<issue_title>Issue: merge_features fails with empty DataFrame</issue_title>
<issue_description>## Description

The deimos.alignment.merge_features() function raises a ValueError when called with an empty DataFrame, even when the DataFrame has the correct column structure.

Steps to Reproduce

import pandas as pd
from deimos.alignment import merge_features

# Create empty DataFrame with correct structure
empty_df = pd.DataFrame(columns=['mz', 'intensity'])

# This raises ValueError
result = merge_features(
    empty_df,
    dims=['mz'],
    tol=[5e-6],
    relative=[True]
)

Expected Behavior

The function should handle empty DataFrames gracefully and return an empty DataFrame with the same structure, or raise a more informative error message about the input being empty.

Actual Behavior

ValueError: zero-size array to reduction operation maximum which has no identity

The error occurs in deimos/alignment.py:411 when attempting to calculate max_tol = tol[i] * values.max() on an empty array.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: kheal <10502759+kheal@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix merge_features to handle empty DataFrame Fix merge_features crash on empty DataFrame input Mar 10, 2026
@kheal
kheal marked this pull request as ready for review March 10, 2026 22:13
@kheal
kheal merged commit 5478645 into master Mar 11, 2026
10 checks passed
@kheal
kheal deleted the copilot/fix-merge-features-empty-df branch March 16, 2026 18:25
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.

Issue: merge_features fails with empty DataFrame

2 participants