pola-rs/polars · error · InvalidOperationError
passing a list to `search_sorted` is ambiguous; use…
Error message
passing a list to `search_sorted` is ambiguous; use `Series.search_sorted(pl.Series([...]), ...)` or `Series.search_sorted(pl.lit(...), ...)`
What it means
Series.search_sorted disallows raw Python lists as the element argument because list semantics are ambiguous (a list of values vs a single element). Polars raises InvalidOperationError telling you to wrap the list explicitly in a Series or a literal expression.
Solutions
- Wrap the list: srs.search_sorted(pl.Series([1, 2, 3]))
- Use an expression: srs.search_sorted(pl.lit([1, 2, 3]))
- Pass a numpy array (np.array([1,2,3])), which is also accepted
Example fix
// before srs.search_sorted([10, 20]) // after srs.search_sorted(pl.Series([10, 20]))
Defensive patterns
Strategy: type-guard
Validate before calling
assert not isinstance(element, list), 'wrap list in pl.Series or pl.lit for search_sorted'
Type guard
def search_sorted_safe(srs: pl.Series, element):
if isinstance(element, list):
element = pl.Series(element)
return srs.search_sorted(element) Try / catch
try:
out = srs.search_sorted(element)
except pl.exceptions.InvalidOperationError:
out = srs.search_sorted(pl.Series(element)) Prevention
- Never pass raw lists to search_sorted
- Wrap lookup keys in pl.Series or use np.ndarray
- Add helper wrappers in shared code to normalize arguments
When it happens
Trigger: srs.search_sorted([1, 2, 3]) — passing a list as element instead of pl.Series([1,2,3]) or pl.lit(...).
Common situations: Directly porting code from other libraries where lists are accepted; quick interactive lookups with a list of lookup keys.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- `pivot` needs either `index or `values` needs to be…
- {0}
- a more recent version of `fastexcel` is required for…
- a more recent version of `fastexcel` is required for…
- a more recent version of `fastexcel` is required for…
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/fec39b6b4180fb3f.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/series/series.py:4114
Series: 'set' [u32]
[
0
3
5
]
>>> # To search for a list of values in a series, of lists, use pl.lit():
>>> list_s = pl.Series("lists", [[0, 1], [0, 2], [1, 4]])
>>> list_s.search_sorted(pl.lit([0, 2]), "left")
shape: (1,)
Series: 'lists' [u32]
[
1
]
"""
df = F.select(F.lit(self).search_sorted(element, side, descending=descending))
if isinstance(element, list):
msg = "passing a list to `search_sorted` is ambiguous; use `Series.search_sorted(pl.Series([...]), ...)` or `Series.search_sorted(pl.lit(...), ...)`"
raise InvalidOperationError(msg)
elif isinstance(element, (Series, pl.Expr)):
return df.to_series()
elif _check_for_numpy(element) and isinstance(element, np.ndarray):
return df.to_series()
else:
return df.item()
def unique(self, *, maintain_order: bool = False) -> Series:
"""
Get unique elements in series.
`null` is considered to be a unique value for the purposes of this operation.
Parameters
----------
maintain_order
Maintain order of data. This requires more work.
View on GitHub (pinned to fe841f959e)