Skip to content

map_elements with return_dtype=pl.Float32 fails to cast from pl.Float64 #25732

@larsevj

Description

@larsevj

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 os
os.environ['POLARS_VERBOSE'] = '1'

import polars as pl

df = pl.DataFrame({"a": [1, 3, 4.0]}, schema={"a": pl.Float32})
df.with_columns(pl.col("a").map_elements(lambda x: x, return_dtype=pl.Float32))

Log output

Traceback (most recent call last):
  File "/Users/LEVJE/project/ert/example.py", line 7, in <module>
    df.with_columns(pl.col("a").map_elements(lambda x: x, return_dtype=pl.Float32))
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/LEVJE/project/ert/test_polars/lib/python3.14/site-packages/polars/dataframe/frame.py", line 10316, in with_columns
    .collect(optimizations=QueryOptFlags._eager())
     ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/LEVJE/project/ert/test_polars/lib/python3.14/site-packages/polars/_utils/deprecation.py", line 97, in wrapper
    return function(*args, **kwargs)
  File "/Users/LEVJE/project/ert/test_polars/lib/python3.14/site-packages/polars/lazyframe/opt_flags.py", line 328, in wrapper
    return function(*args, **kwargs)
  File "/Users/LEVJE/project/ert/test_polars/lib/python3.14/site-packages/polars/lazyframe/frame.py", line 2429, in collect
    return wrap_df(ldf.collect(engine, callback))
                   ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/Users/LEVJE/project/ert/test_polars/lib/python3.14/site-packages/polars/functions/lazy.py", line 1088, in __call__
    rv = self.function(slp, *args, **kwargs)
  File "/Users/LEVJE/project/ert/test_polars/lib/python3.14/site-packages/polars/expr/expr.py", line 4655, in _wrap
    return function(sl[0], *args, **kwargs)
  File "/Users/LEVJE/project/ert/test_polars/lib/python3.14/site-packages/polars/expr/expr.py", line 4879, in wrap_f
    return x.map_elements(
           ~~~~~~~~~~~~~~^
        function, return_dtype=return_dtype, skip_nulls=skip_nulls
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/LEVJE/project/ert/test_polars/lib/python3.14/site-packages/polars/series/series.py", line 5838, in map_elements
    self._s.map_elements(
    ~~~~~~~~~~~~~~~~~~~~^
        function, return_dtype=pl_return_dtype, skip_nulls=skip_nulls
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
polars.exceptions.SchemaError: unexpected value while building Series of type Float32; found value of type Float64: 1.0

Issue description

The following code used to work for polars version 1.35.2, but fails with polars 1.36.1. Reading the docs on map_elements:return_dtype:

return_dtype
Datatype of the output Series.

It is recommended to set this whenever possible. If this is None, it tries to infer the datatype by calling the function with dummy data and looking at the output.

it is not entirely clear to me if this is just intended to check that the output dtype match, or if it should convert in this instance from float64 to float32 as it used to do for polars < 1.36.

Expected behavior

Would ideally prefer for this to cast float64 to float32 as before, but if not then maybe the docs could be a bit clearer on the intended behaviour of the return_dtype parameter?

Installed versions

Details
--------Version info---------
Polars:              1.36.1
Index type:          UInt32
Platform:            macOS-26.1-arm64-arm-64bit-Mach-O
Python:              3.14.2 (main, Dec  5 2025, 16:49:16) [Clang 17.0.0 (clang-1700.4.4.1)]
Runtime:             rt32

----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

    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