Skip to content

Commit

Permalink
find first non null to determine pandas object type (#3303)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed May 4, 2022
1 parent b796cf9 commit b19b6db
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion py-polars/polars/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def _pandas_series_to_arrow(
"""
dtype = values.dtype
if dtype == "object" and len(values) > 0:
if isinstance(values.values[0], str):
if isinstance(_get_first_non_none(values.values), str): # type: ignore
return pa.array(values, pa.large_utf8(), from_pandas=nan_to_none)

# array is null array, we set to a float64 array
Expand Down
10 changes: 10 additions & 0 deletions py-polars/tests/test_interop.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import typing
from datetime import datetime
from typing import Dict, Sequence, Type, Union

Expand Down Expand Up @@ -367,3 +368,12 @@ def test_from_pandas_ns_resolution() -> None:
columns=["date"],
)
assert pl.from_pandas(df)[0, 0] == datetime(2021, 1, 1, 1, 0, 1)


@typing.no_type_check
def test_pandas_string_none_conversion_3298() -> None:
data = {"col_1": ["a", "b", "c", "d"]}
data["col_1"][0] = None
df_pd = pd.DataFrame(data)
df_pl = pl.DataFrame(df_pd)
assert df_pl.to_series().to_list() == [None, "b", "c", "d"]

0 comments on commit b19b6db

Please sign in to comment.