Skip to content

pl.map_groups with return_dtype=pl.Object causes pyo3_runtime.PanicException: object dtype not supported in Series.iter #22814

@davorx

Description

@davorx

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

import polars as pl

class CustomObject:
    pass

df = pl.DataFrame({
    "id": [1, 2, 3, 4],
    "objects": [ CustomObject(), CustomObject(), CustomObject(), CustomObject() ],
})

df = df.group_by("id").agg(
    pl.map_groups([pl.col("objects")], lambda x: x[0], return_dtype=pl.Object).alias("first_object"))

print(df)

Log output

Issue description

Using pl.map_groups with return_dtype=pl.Object works with polars 1.27.1. However, using polars 1.28.1 or later, I get the following error:

$RUST_BACKTRACE=full POLARS_VERBOSE=1 python polars-bug-report.py
keys/aggregates are not partitionable: running default HASH AGGREGATION

thread '<unnamed>' panicked at crates/polars-core/src/series/iterator.rs:84:9:
object dtype not supported in Series.iter
stack backtrace:
   0:        0x138d27088 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::ha26fa7c0f8baf7a4
   1:        0x1356bbc80 - core::fmt::write::h8eff90b819ed25ac
   2:        0x138d251c4 - std::io::Write::write_fmt::hc3767759e8313866
   3:        0x138d26f28 - std::sys::backtrace::BacktraceLock::print::h5ab5bdfc8c7040c6
   4:        0x138d259d0 - std::panicking::default_hook::h2517e562906dc496
   5:        0x138d24fd0 - std::panicking::rust_panic_with_hook::hdf5ea4a7520fe9b8
   6:        0x138d55c60 - std::panicking::begin_panic_handler::{{closure}}::h7e667cf4bf22d5a9
   7:        0x138d55bf8 - std::sys::backtrace::__rust_end_short_backtrace::h6ac6505b1a15fed1
   8:        0x138d58520 - __rustc[e5679b3fd213e125]::rust_begin_unwind
   9:        0x138e8ae78 - core::panicking::panic_fmt::h0d9b963f0cbbd487
  10:        0x135de2160 - polars_core::series::iterator::<impl polars_core::series::Series>::iter::hd3b9b60a26bcea7d
  11:        0x135de19a4 - polars_core::fmt::<impl polars_core::series::Series>::fmt_list::ha53e1e28eda4e628
  12:        0x135de2e4c - polars_core::fmt::<impl core::fmt::Display for polars_core::datatypes::any_value::AnyValue>::fmt::h7db1bdbe0423a9c7
  13:        0x135e07f64 - polars_core::datatypes::any_value::AnyValue::str_value::h7ec314145cbb1bdd
  14:        0x1364ad8b8 - polars_core::fmt::<impl core::fmt::Display for polars_core::frame::DataFrame>::fmt::he2f1ccfbfc85ddd5
  15:        0x1356bbc80 - core::fmt::write::h8eff90b819ed25ac
  16:        0x13566df38 - alloc::fmt::format::format_inner::ha99af07c905693b7
  17:        0x137ceb2e4 - polars_python::dataframe::general::<impl polars_python::dataframe::PyDataFrame>::__pymethod_as_str__::hfbdc063217bfad4b
  18:        0x13822dd88 - pyo3::impl_::trampoline::trampoline::h1954118760425091
  19:        0x137c81904 - polars_python::dataframe::general::_::__INVENTORY::trampoline::h4f8bddb385110bbd
  20:        0x103313cb0 - _method_vectorcall_NOARGS
  21:        0x103462654 - __PyEval_EvalFrameDefault
  22:        0x10339a91c - _vectorcall_method
  23:        0x10339d97c - _slot_tp_str
  24:        0x103375710 - _PyObject_Str
  25:        0x103326f70 - _PyFile_WriteObject
  26:        0x103454aa0 - _builtin_print
  27:        0x1033708d4 - _cfunction_vectorcall_FASTCALL_KEYWORDS
  28:        0x103462654 - __PyEval_EvalFrameDefault
  29:        0x103456e64 - _PyEval_EvalCode
  30:        0x1034d7ed8 - _run_mod
  31:        0x1034d6f40 - _pyrun_file
  32:        0x1034d5e1c - __PyRun_SimpleFileObject
  33:        0x1034d59e0 - __PyRun_AnyFileObject
  34:        0x103505858 - _pymain_run_file_obj
  35:        0x1035055d0 - _pymain_run_file
  36:        0x10350489c - _Py_RunMain
  37:        0x103504f7c - _pymain_main
  38:        0x103505098 - _Py_BytesMain
Traceback (most recent call last):
  File "/project/polars-bug-report.py", line 11, in <module>
    print(df)
  File "/project/.venv/lib/python3.12/site-packages/polars/dataframe/frame.py", line 1199, in __str__
    return self._df.as_str()
           ^^^^^^^^^^^^^^^^^
pyo3_runtime.PanicException: object dtype not supported in Series.iter

Expected behavior

Using polars 1.27.1, I get the following output:

$ RUST_BACKTRACE=full POLARS_VERBOSE=1 python polars-bug-report.py
keys/aggregates are not partitionable: running default HASH AGGREGATION
shape: (4, 2)
┌─────┬─────────────────────────────────┐
│ id  ┆ first_object                    │
│ --- ┆ ---                             │
│ i64 ┆ object                          │
╞═════╪═════════════════════════════════╡
│ 1   ┆ <__main__.CustomObject object … │
│ 3   ┆ <__main__.CustomObject object … │
│ 2   ┆ <__main__.CustomObject object … │
│ 4   ┆ <__main__.CustomObject object … │
└─────┴─────────────────────────────────┘

Installed versions

--------Version info---------
Polars:              1.28.1
Index type:          UInt32
Platform:            macOS-15.4.1-arm64-arm-64bit
Python:              3.12.9 (v3.12.9:fdb81425a9a, Feb  4 2025, 12:21:36) [Clang 13.0.0 (clang-1300.0.29.30)]
LTS CPU:             False

----Optional dependencies----
Azure CLI            <not installed>
adbc_driver_manager  <not installed>
altair               <not installed>
azure.identity       <not installed>
boto3                <not installed>
cloudpickle          <not installed>
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               <not installed>
gevent               <not installed>
google.auth          <not installed>
great_tables         <not installed>
matplotlib           <not installed>
numpy                <not installed>
openpyxl             <not installed>
pandas               <not installed>
polars_cloud         <not installed>
pyarrow              <not installed>
pydantic             <not installed>
pyiceberg            <not installed>
sqlalchemy           <not installed>
torch                <not installed>
xlsx2csv             <not installed>
xlsxwriter           <not installed>

And my polars 1.27.1 version info:

--------Version info---------
Polars:              1.27.1
Index type:          UInt32
Platform:            macOS-15.4.1-arm64-arm-64bit
Python:              3.12.9 (v3.12.9:fdb81425a9a, Feb  4 2025, 12:21:36) [Clang 13.0.0 (clang-1300.0.29.30)]
LTS CPU:             False

----Optional dependencies----
Azure CLI            <not installed>
adbc_driver_manager  <not installed>
altair               <not installed>
azure.identity       <not installed>
boto3                <not installed>
cloudpickle          <not installed>
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               <not installed>
gevent               <not installed>
google.auth          <not installed>
great_tables         <not installed>
matplotlib           <not installed>
numpy                <not installed>
openpyxl             <not installed>
pandas               <not installed>
polars_cloud         <not installed>
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

    P-highPriority: highacceptedReady for implementationbugSomething isn't workingpythonRelated to Python Polars

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions