Skip to content

Commit 8b5d431

Browse files
jbrockmendelyehoshuadimarsky
authored andcommitted
BUG: accept strings in FloatingArray._from_sequence (pandas-dev#45424)
1 parent 8b34e9c commit 8b5d431

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ Conversion
200200
- Bug in constructing a :class:`Series` from a float-containing list or a floating-dtype ndarray-like (e.g. ``dask.Array``) and an integer dtype raising instead of casting like we would with an ``np.ndarray`` (:issue:`40110`)
201201
- Bug in :meth:`Float64Index.astype` to unsigned integer dtype incorrectly casting to ``np.int64`` dtype (:issue:`45309`)
202202
- Bug in :meth:`Series.astype` and :meth:`DataFrame.astype` from floating dtype to unsigned integer dtype failing to raise in the presence of negative values (:issue:`45151`)
203+
- Bug in :func:`array` with ``FloatingDtype`` and values containing float-castable strings incorrectly raising (:issue:`45424`)
203204
-
204205

205206
Strings

pandas/core/arrays/floating.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
is_float_dtype,
1515
is_integer_dtype,
1616
is_object_dtype,
17+
is_string_dtype,
1718
)
1819
from pandas.core.dtypes.dtypes import register_extension_dtype
1920

@@ -112,7 +113,7 @@ def coerce_to_array(
112113
return values, mask
113114

114115
values = np.array(values, copy=copy)
115-
if is_object_dtype(values.dtype):
116+
if is_object_dtype(values.dtype) or is_string_dtype(values.dtype):
116117
inferred_type = lib.infer_dtype(values, skipna=True)
117118
if inferred_type == "empty":
118119
pass

pandas/tests/arrays/categorical/test_astype.py

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ def test_astype_str_int_categories_to_nullable_int(self):
2323
expected = array(codes, dtype="Int64")
2424
tm.assert_extension_array_equal(res, expected)
2525

26+
def test_astype_str_int_categories_to_nullable_float(self):
27+
# GH#39616
28+
dtype = CategoricalDtype([str(i / 2) for i in range(5)])
29+
codes = np.random.randint(5, size=20)
30+
arr = Categorical.from_codes(codes, dtype=dtype)
31+
32+
res = arr.astype("Float64")
33+
expected = array(codes, dtype="Float64") / 2
34+
tm.assert_extension_array_equal(res, expected)
35+
2636
@pytest.mark.parametrize("ordered", [True, False])
2737
def test_astype(self, ordered):
2838
# string

pandas/tests/arrays/floating/test_construction.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ def test_to_array_mixed_integer_float():
112112
"values",
113113
[
114114
["foo", "bar"],
115-
["1", "2"],
116115
"foo",
117116
1,
118117
1.0,
@@ -132,12 +131,25 @@ def test_to_array_error(values):
132131
"values must be a 1D list-like",
133132
"Cannot pass scalar",
134133
r"float\(\) argument must be a string or a (real )?number, not 'dict'",
134+
"could not convert string to float: 'foo'",
135135
]
136136
)
137137
with pytest.raises((TypeError, ValueError), match=msg):
138138
pd.array(values, dtype="Float64")
139139

140140

141+
@pytest.mark.parametrize("values", [["1", "2", None], ["1.5", "2", None]])
142+
def test_construct_from_float_strings(values):
143+
# see also test_to_integer_array_str
144+
expected = pd.array([float(values[0]), 2, None], dtype="Float64")
145+
146+
res = pd.array(values, dtype="Float64")
147+
tm.assert_extension_array_equal(res, expected)
148+
149+
res = FloatingArray._from_sequence(values)
150+
tm.assert_extension_array_equal(res, expected)
151+
152+
141153
def test_to_array_inferred_dtype():
142154
# if values has dtype -> respect it
143155
result = pd.array(np.array([1, 2], dtype="float32"))

0 commit comments

Comments
 (0)