diff --git a/py-polars/polars/_utils/construction/series.py b/py-polars/polars/_utils/construction/series.py index 4a4d1d2b41c0..211f58af0898 100644 --- a/py-polars/polars/_utils/construction/series.py +++ b/py-polars/polars/_utils/construction/series.py @@ -404,6 +404,7 @@ def to_series_chunk(values: list[Any], dtype: PolarsDataType | None) -> Series: def pandas_to_pyseries( name: str, values: pd.Series[Any] | pd.Index[Any] | pd.DatetimeIndex, + dtype: PolarsDataType | None = None, *, nan_to_null: bool = True, ) -> PySeries: @@ -411,7 +412,7 @@ def pandas_to_pyseries( if not name and values.name is not None: name = str(values.name) if is_simple_numpy_backed_pandas_series(values): - return pl.Series(name, values.to_numpy(), nan_to_null=nan_to_null)._s + return pl.Series(name, values.to_numpy(), dtype=dtype, nan_to_null=nan_to_null)._s if not _PYARROW_AVAILABLE: msg = ( "pyarrow is required for converting a pandas series to Polars, " diff --git a/py-polars/polars/series/series.py b/py-polars/polars/series/series.py index 3f4873613173..a32d0e81eab4 100644 --- a/py-polars/polars/series/series.py +++ b/py-polars/polars/series/series.py @@ -346,7 +346,7 @@ def __init__( elif _check_for_pandas(values) and isinstance( values, (pd.Series, pd.Index, pd.DatetimeIndex) ): - self._s = pandas_to_pyseries(name, values) + self._s = pandas_to_pyseries(name, values, dtype=dtype) elif _is_generator(values): self._s = iterable_to_pyseries(name, values, dtype=dtype, strict=strict) diff --git a/py-polars/tests/unit/series/test_series.py b/py-polars/tests/unit/series/test_series.py index db8f77cfe91a..9f3b8ec0d77c 100644 --- a/py-polars/tests/unit/series/test_series.py +++ b/py-polars/tests/unit/series/test_series.py @@ -2337,3 +2337,7 @@ def test_search_sorted( multiple_s = s.search_sorted(multiple) assert_series_equal(multiple_s, pl.Series(multiple_expected, dtype=pl.UInt32)) + +def test_series_from_pandas_with_dtype()->None: + s = pl.Series('foo', pd.Series([1,2,3]), pl.Float32) + assert_series_equal(s, pl.Series('foo', [1,2,3], dtype=pl.Float32))