Skip to content

Commit 7959578

Browse files
authored
BUG: infer_string not inferring string dtype when NA is first value (#55655)
* BUG: infer_string not inferring string dtype when NA is first value * Update lib.pyx
1 parent e0a8443 commit 7959578

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

doc/source/whatsnew/v2.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Bug fixes
3535
- Fixed bug in :meth:`Series.str.extractall` for :class:`ArrowDtype` dtype being converted to object (:issue:`53846`)
3636
- Fixed bug where PDEP-6 warning about setting an item of an incompatible dtype was being shown when creating a new conditional column (:issue:`55025`)
3737
- Silence ``Period[B]`` warnings introduced by :issue:`53446` during normal plotting activity (:issue:`55138`)
38+
- Fixed bug in :class:`Series` constructor not inferring string dtype when ``NA`` is the first value and ``infer_string`` is set (:issue:` 55655`)
3839

3940
.. ---------------------------------------------------------------------------
4041
.. _whatsnew_212.other:

pandas/_libs/lib.pyx

+3
Original file line numberDiff line numberDiff line change
@@ -2665,6 +2665,9 @@ def maybe_convert_objects(ndarray[object] objects,
26652665
else:
26662666
seen.object_ = True
26672667
break
2668+
elif val is C_NA:
2669+
seen.object_ = True
2670+
continue
26682671
else:
26692672
seen.object_ = True
26702673
break

pandas/tests/series/test_constructors.py

+8
Original file line numberDiff line numberDiff line change
@@ -2131,6 +2131,14 @@ def test_series_constructor_infer_string_scalar(self):
21312131
tm.assert_series_equal(ser, expected)
21322132
assert ser.dtype.storage == "python"
21332133

2134+
def test_series_string_inference_na_first(self):
2135+
# GH#55655
2136+
pytest.importorskip("pyarrow")
2137+
expected = Series([pd.NA, "b"], dtype="string[pyarrow_numpy]")
2138+
with pd.option_context("future.infer_string", True):
2139+
result = Series([pd.NA, "b"])
2140+
tm.assert_series_equal(result, expected)
2141+
21342142

21352143
class TestSeriesConstructorIndexCoercion:
21362144
def test_series_constructor_datetimelike_index_coercion(self):

0 commit comments

Comments
 (0)