Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions novem/table/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ def _pd_ix_lookup(self) -> str:
col_positions = enhance_positions(col_positions, cor)
col_positions = [x for x in col_positions if x >= 0 and x <= width]

# Return empty string if there are no valid rows/columns and no explicit override (Issue #55)
if (not row_positions and not self.i) or (not col_positions and not self.c):
return ""

# Create a comma-separated list of row and column positions
row_str = ",".join(map(str, row_positions))
col_str = ",".join(map(str, col_positions))
Expand Down
42 changes: 42 additions & 0 deletions tests/test_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,45 @@ def test_2d_selector():
df = df.transpose()
format = S(df.iloc[:, :], ",.1%", r=df).get_selector_string()
assert format == "1,2,3 1,2,3,4"


def test_empty_selector():
"""
Test that a selector with no valid rows returns an empty string.
Issue #55: able-utils: the novem selector should return "" (empty string)
if the selector has no rows
"""
data = {
"A": [1, 2, 3],
"B": [4, 5, 6],
"C": [7, 8, 9],
}
df = pd.DataFrame(data)

# Filter that produces no rows
empty_df = df[df["A"] > 10]
assert len(empty_df) == 0 # Sanity check

# Selector should return empty string when no valid rows
sel = S(empty_df, "test", r=df)
result = sel.get_selector_string()
assert result == ""

# Should also return empty string when c is set but no rows
sel_with_c = S(empty_df, "test", r=df, c=":")
assert sel_with_c.get_selector_string() == ""

# But should honor explicit i override
sel_with_i = S(empty_df, "test", r=df, i=":")
result_with_i = sel_with_i.get_selector_string()
assert result_with_i == ": 1,2,3"

# Same logic applies to columns - empty columns without c override returns ""
empty_cols_df = df.loc[:, []] # No columns
sel_empty_cols = S(empty_cols_df, "test", r=df)
assert sel_empty_cols.get_selector_string() == ""

# But should honor explicit c override
sel_with_c_override = S(empty_cols_df, "test", r=df, c=":")
result_with_c = sel_with_c_override.get_selector_string()
assert result_with_c == "1,2,3 :"