Skip to content

Commit f7169d4

Browse files
authored
fix: Support passing nw.Object to pandas-like (#2953)
Closes #2851
1 parent c22b582 commit f7169d4

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

narwhals/_arrow/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ def narwhals_to_native_dtype(dtype: IntoDType, version: Version) -> pa.DataType:
224224
return pa.time64("ns")
225225
if isinstance_or_issubclass(dtype, dtypes.Binary):
226226
return pa.binary()
227-
227+
if isinstance_or_issubclass(dtype, dtypes.Object):
228+
msg = f"Converting to {dtype} dtype is not supported for pyarrow."
229+
raise NotImplementedError(msg)
228230
msg = f"Unknown dtype: {dtype}" # pragma: no cover
229231
raise AssertionError(msg)
230232

narwhals/_pandas_like/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ def narwhals_to_native_dtype( # noqa: C901, PLR0912, PLR0915
571571
f"{implementation} and version {version}."
572572
)
573573
raise NotImplementedError(msg)
574+
if isinstance_or_issubclass(dtype, dtypes.Object):
575+
return "object"
574576
msg = f"Unknown dtype: {dtype}" # pragma: no cover
575577
raise AssertionError(msg)
576578

tests/frame/from_dict_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,19 @@ def test_alignment() -> None:
9292
).to_native()
9393
expected = pd.DataFrame({"a": [1, 2, 3], "b": [3, 2, 1]})
9494
pd.testing.assert_frame_equal(result, expected)
95+
96+
97+
def test_from_dict_object_2851(
98+
eager_backend: EagerAllowed, request: pytest.FixtureRequest
99+
) -> None:
100+
data = {"Var1": [3, "a"], "Var2": ["a", "b"]}
101+
schema = {"Var1": nw.Object(), "Var2": nw.String()}
102+
request.applymarker(
103+
pytest.mark.xfail(
104+
"pyarrow" in str(eager_backend),
105+
reason="Object DType not supported in pyarrow",
106+
raises=NotImplementedError,
107+
)
108+
)
109+
df = nw.DataFrame.from_dict(data, backend=eager_backend, schema=schema)
110+
assert df.schema == schema

0 commit comments

Comments
 (0)