Skip to content

Segmentation fault with to_numpy with Numpy 2 and datetime column #17380

@lesteve

Description

@lesteve

Checks

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

Reproducible example

from sklearn.datasets import fetch_openml

co2 = fetch_openml(data_id=41187, as_frame=True)
import polars as pl

co2_data = pl.DataFrame(co2.frame[["year", "month", "day", "co2"]]).select(
    pl.date("year", "month", "day"), "co2"
)
co2_data["date"].to_numpy()

Log output

The gdb back-trace:

Thread 1 "python" received signal SIGSEGV, Segmentation fault.
0x00007fffad715f3c in numpy::datetime::TypeDescriptors::from_unit ()
   from /home/lesteve/micromamba/envs/del312/lib/python3.12/site-packages/polars/polars.abi3.so
(gdb) bt
#0  0x00007fffad715f3c in numpy::datetime::TypeDescriptors::from_unit ()
   from /home/lesteve/micromamba/envs/del312/lib/python3.12/site-packages/polars/polars.abi3.so
#1  0x00007fffacbf8854 in <alloc::vec::Vec<T> as numpy::convert::IntoPyArray>::into_pyarray_bound ()
   from /home/lesteve/micromamba/envs/del312/lib/python3.12/site-packages/polars/polars.abi3.so
#2  0x00007fffad209daf in polars::interop::numpy::to_numpy_series::series_to_numpy_with_copy ()
   from /home/lesteve/micromamba/envs/del312/lib/python3.12/site-packages/polars/polars.abi3.so
#3  0x00007fffad204fac in polars::interop::numpy::to_numpy_series::series_to_numpy ()
   from /home/lesteve/micromamba/envs/del312/lib/python3.12/site-packages/polars/polars.abi3.so
#4  0x00007fffad4a41e5 in polars::interop::numpy::to_numpy_series::<impl polars::series::PySeries>::__pymethod_to_numpy__ ()
   from /home/lesteve/micromamba/envs/del312/lib/python3.12/site-packages/polars/polars.abi3.so
#5  0x00007fffacb342a1 in pyo3::impl_::trampoline::trampoline ()
   from /home/lesteve/micromamba/envs/del312/lib/python3.12/site-packages/polars/polars.abi3.so
#6  0x00007fffad4a4511 in polars::interop::numpy::to_numpy_series::_::__INVENTORY::trampoline ()
   from /home/lesteve/micromamba/envs/del312/lib/python3.12/site-packages/polars/polars.abi3.so
#7  0x00005555557836a1 in method_vectorcall_VARARGS_KEYWORDS (
    func=0x7fffb0b12840, args=0x7ffff7f9c120, nargsf=<optimized out>, 
    kwnames=<optimized out>)
    at /usr/local/src/conda/python-3.12.4/Objects/descrobject.c:365
#8  0x0000555555775c9f in _PyObject_VectorcallTstate (
    kwnames=0x7fffaaecf880, nargsf=9223372036854775809, 
    args=0x7ffff7f9c120, callable=0x7fffb0b12840, 
    tstate=0x555555bf2078 <_PyRuntime+459704>)
    at /usr/local/src/conda/python-3.12.4/Include/internal/pycore_call.h:92
#9  PyObject_Vectorcall (callable=0x7fffb0b12840, args=0x7ffff7f9c120, 
    nargsf=9223372036854775809, kwnames=0x7fffaaecf880)
    at /usr/local/src/conda/python-3.12.4/Objects/call.c:325
#10 0x0000555555667f20 in _PyEval_EvalFrameDefault (
    tstate=<optimized out>, frame=0x7ffff7f9c0a0, 
    throwflag=<optimized out>) at Python/bytecodes.c:2714
#11 0x000055555581b6fe in PyEval_EvalCode (co=co@entry=0x7ffff7be41d0, 
    globals=globals@entry=0x7ffff7ca1e40, 
    locals=locals@entry=0x7ffff7ca1e40)
    at /usr/local/src/conda/python-3.12.4/Python/ceval.c:578
#12 0x000055555583f7ba in run_eval_code_obj (
    tstate=tstate@entry=0x555555bf2078 <_PyRuntime+459704>, 
    co=co@entry=0x7ffff7be41d0, globals=globals@entry=0x7ffff7ca1e40, 
    locals=locals@entry=0x7ffff7ca1e40)
    at /usr/local/src/conda/python-3.12.4/Python/pythonrun.c:1722
#13 0x000055555583aa9b in run_mod (mod=mod@entry=0x555555cbde88, 
    filename=filename@entry=0x7ffff7c5a830, 
    globals=globals@entry=0x7ffff7ca1e40, 
    locals=locals@entry=0x7ffff7ca1e40, 
    flags=flags@entry=0x7fffffffb3c0, arena=arena@entry=0x7ffff7bc3c50)
    at /usr/local/src/conda/python-3.12.4/Python/pythonrun.c:1743
#14 0x00005555558537b0 in pyrun_file (fp=fp@entry=0x555555bf5590, 
    filename=filename@entry=0x7ffff7c5a830, start=start@entry=257, 
    globals=globals@entry=0x7ffff7ca1e40, 
    locals=locals@entry=0x7ffff7ca1e40, closeit=closeit@entry=1, 
    flags=0x7fffffffb3c0)

Issue description

Seen in scikit-learn examples when upgrading to numpy 2 see scikit-learn/scikit-learn#29388.

Created a smaller reproducer that still needs scikit-learn. I am sure you could craft a reproducer without scikit-learn.

To create the environment run the snippet:

conda create -n del312 python=3.12 -y
pip install polars numpy pandas scikit-learn

Installing numpy<2 fixes the issue pip install 'numpy<2'

Expected behavior

No error

Installed versions

Details
--------Version info---------
Polars:               1.0.0
Index type:           UInt32
Platform:             Linux-6.9.5-arch1-1-x86_64-with-glibc2.39
Python:               3.12.4 | packaged by conda-forge | (main, Jun 17 2024, 10:23:07) [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>
great_tables:         <not installed>
hvplot:               <not installed>
matplotlib:           <not installed>
nest_asyncio:         <not installed>
numpy:                2.0.0
openpyxl:             <not installed>
pandas:               2.2.2
pyarrow:              <not installed>
pydantic:             <not installed>
pyiceberg:            <not installed>
sqlalchemy:           <not installed>
torch:                <not installed>
xlsx2csv:             <not installed>
xlsxwriter:           <not installed>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds triageAwaiting prioritization by a maintainerpythonRelated to Python Polars

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions