Skip to content

Commit a438f02

Browse files
committed
BUG: df_empty.convert_dtypes() with pyarrow backend
1 parent 74b13fa commit a438f02

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

pandas/core/arrays/arrow/array.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,17 @@ def to_pyarrow_type(
137137
Convert dtype to a pyarrow type instance.
138138
"""
139139
if isinstance(dtype, ArrowDtype):
140-
pa_dtype = dtype.pyarrow_dtype
140+
return dtype.pyarrow_dtype
141141
elif isinstance(dtype, pa.DataType):
142-
pa_dtype = dtype
142+
return dtype
143143
elif dtype:
144-
# Accepts python types too
145-
pa_dtype = pa.from_numpy_dtype(dtype)
146-
else:
147-
pa_dtype = None
148-
return pa_dtype
144+
try:
145+
# Accepts python types too
146+
# Doesn't handle all numpy types
147+
return pa.from_numpy_dtype(dtype)
148+
except pa.ArrowNotImplementedError:
149+
pass
150+
return None
149151

150152

151153
class ArrowExtensionArray(OpsMixin, ExtensionArray):

pandas/tests/frame/methods/test_convert_dtypes.py

+7
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,10 @@ def test_pyarrow_dtype_backend_from_pandas_nullable(self):
120120
}
121121
)
122122
tm.assert_frame_equal(result, expected)
123+
124+
def test_pyarrow_dtype_empty_object(self):
125+
# GH 50970
126+
expected = pd.DataFrame(columns=[0])
127+
with pd.option_context("mode.dtype_backend", "pyarrow"):
128+
result = expected.convert_dtypes()
129+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)