Skip to content

Commit 0971f55

Browse files
authored
BUG: Series(strings).astype("float64[pyarrow]") raising (#50430)
* BUG: Series(strings).astype("float64[pyarrow]") raising * gh refs * simplify
1 parent 4878dfe commit 0971f55

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ Conversion
830830
- Bug in :meth:`Series.convert_dtypes` not converting dtype to nullable dtype when :class:`Series` contains ``NA`` and has dtype ``object`` (:issue:`48791`)
831831
- Bug where any :class:`ExtensionDtype` subclass with ``kind="M"`` would be interpreted as a timezone type (:issue:`34986`)
832832
- Bug in :class:`.arrays.ArrowExtensionArray` that would raise ``NotImplementedError`` when passed a sequence of strings or binary (:issue:`49172`)
833+
- Bug in :meth:`Series.astype` raising ``pyarrow.ArrowInvalid`` when converting from a non-pyarrow string dtype to a pyarrow numeric type (:issue:`50430`)
833834
- Bug in :func:`to_datetime` was not respecting ``exact`` argument when ``format`` was an ISO8601 format (:issue:`12649`)
834835
- Bug in :meth:`TimedeltaArray.astype` raising ``TypeError`` when converting to a pyarrow duration type (:issue:`49795`)
835836
-

pandas/core/arrays/arrow/array.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,17 @@ def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy: bool = Fal
206206
Construct a new ExtensionArray from a sequence of scalars.
207207
"""
208208
pa_dtype = to_pyarrow_type(dtype)
209-
is_cls = isinstance(scalars, cls)
210-
if is_cls or isinstance(scalars, (pa.Array, pa.ChunkedArray)):
211-
if is_cls:
212-
scalars = scalars._data
213-
if pa_dtype:
214-
scalars = scalars.cast(pa_dtype)
215-
return cls(scalars)
216-
else:
217-
return cls(
218-
pa.chunked_array(pa.array(scalars, type=pa_dtype, from_pandas=True))
219-
)
209+
if isinstance(scalars, cls):
210+
scalars = scalars._data
211+
elif not isinstance(scalars, (pa.Array, pa.ChunkedArray)):
212+
try:
213+
scalars = pa.array(scalars, type=pa_dtype, from_pandas=True)
214+
except pa.ArrowInvalid:
215+
# GH50430: let pyarrow infer type, then cast
216+
scalars = pa.array(scalars, from_pandas=True)
217+
if pa_dtype:
218+
scalars = scalars.cast(pa_dtype)
219+
return cls(scalars)
220220

221221
@classmethod
222222
def _from_sequence_of_strings(

pandas/tests/extension/test_arrow.py

+8
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,14 @@ def test_astype_from_non_pyarrow(data):
14711471
tm.assert_extension_array_equal(result, data)
14721472

14731473

1474+
def test_astype_float_from_non_pyarrow_str():
1475+
# GH50430
1476+
ser = pd.Series(["1.0"])
1477+
result = ser.astype("float64[pyarrow]")
1478+
expected = pd.Series([1.0], dtype="float64[pyarrow]")
1479+
tm.assert_series_equal(result, expected)
1480+
1481+
14741482
def test_to_numpy_with_defaults(data):
14751483
# GH49973
14761484
result = data.to_numpy()

0 commit comments

Comments
 (0)