Skip to content

BUG: df_empty.convert_dtypes() with pyarrow backend #50976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,17 @@ def to_pyarrow_type(
Convert dtype to a pyarrow type instance.
"""
if isinstance(dtype, ArrowDtype):
pa_dtype = dtype.pyarrow_dtype
return dtype.pyarrow_dtype
elif isinstance(dtype, pa.DataType):
pa_dtype = dtype
return dtype
elif dtype:
# Accepts python types too
pa_dtype = pa.from_numpy_dtype(dtype)
else:
pa_dtype = None
return pa_dtype
try:
# Accepts python types too
# Doesn't handle all numpy types
return pa.from_numpy_dtype(dtype)
except pa.ArrowNotImplementedError:
pass
return None


class ArrowExtensionArray(OpsMixin, ExtensionArray):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/methods/test_convert_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,11 @@ def test_pyarrow_dtype_backend_from_pandas_nullable(self):
}
)
tm.assert_frame_equal(result, expected)

def test_pyarrow_dtype_empty_object(self):
# GH 50970
pytest.importorskip("pyarrow")
expected = pd.DataFrame(columns=[0])
with pd.option_context("mode.dtype_backend", "pyarrow"):
result = expected.convert_dtypes()
tm.assert_frame_equal(result, expected)