polars icon indicating copy to clipboard operation
polars copied to clipboard

.search_sorted(...) broken on Series with dtype pl.Date

Open davidia opened this issue 10 months ago • 3 comments

Checks

  • [X] I have checked that this issue has not already been reported.
  • [X] I have confirmed this bug exists on the latest version of Polars.

Reproducible example

import polars as pl
import datetime as dt
d1 = dt.date.today()
s = pl.Series([d1])
s.search_sorted(d1)

Log output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/david/mambaforge/envs/plbug/lib/python3.11/site-packages/polars/series/series.py", line 3605, in search_sorted
    element = Series(element)
              ^^^^^^^^^^^^^^^
  File "/home/david/mambaforge/envs/plbug/lib/python3.11/site-packages/polars/series/series.py", line 368, in __init__
    raise TypeError(msg)
TypeError: Series constructor called with unsupported type 'date' for the `values` parameter

Issue description

search_sorted doesn't work on Series of dtype pl.Date

Expected behavior

Should return 0

Installed versions

--------Version info---------
Polars:               0.20.21
Index type:           UInt32
Platform:             Linux-6.5.0-28-generic-x86_64-with-glibc2.38
Python:               3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:36:13) [GCC 12.3.0]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          <not installed>
connectorx:           <not installed>
deltalake:            <not installed>
fastexcel:            <not installed>
fsspec:               <not installed>
gevent:               <not installed>
hvplot:               <not installed>
matplotlib:           <not installed>
nest_asyncio:         1.6.0
numpy:                1.26.4
openpyxl:             <not installed>
pandas:               <not installed>
pyarrow:              <not installed>
pydantic:             <not installed>
pyiceberg:            <not installed>
pyxlsb:               <not installed>
sqlalchemy:           <not installed>
xlsx2csv:             <not installed>
xlsxwriter:           <not installed>


davidia avatar Apr 26 '24 13:04 davidia

The error message from your example is for the Series constructor, not search_sorted().

aut0clave avatar Apr 26 '24 15:04 aut0clave

The Series in my example constructs just fine. Somewhere in search_sorted another Series is constructed and that's the one that fails.

image

davidia avatar Apr 26 '24 16:04 davidia

It seems it ends up calling pl.Series(d1) in this case (line 3605) which is causing the error.

https://github.com/pola-rs/polars/blob/61a6c196b568a96c84377cd2b2048d887bd0db2a/py-polars/polars/series/series.py#L3603-L3605

Whereas it would want to instead perform the operation inside the isinstance check?

>>> pl.select(pl.lit(s).search_sorted(d1)).item()
0

cmdlineluser avatar Apr 26 '24 17:04 cmdlineluser