Skip to content

Commit cdb45c6

Browse files
Backport PR pandas-dev#55655 on branch 2.1.x (BUG: infer_string not inferring string dtype when NA is first value) (pandas-dev#55666)
Backport PR pandas-dev#55655: BUG: infer_string not inferring string dtype when NA is first value Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 8f003de commit cdb45c6

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
@@ -2642,6 +2642,9 @@ def maybe_convert_objects(ndarray[object] objects,
26422642
else:
26432643
seen.object_ = True
26442644
break
2645+
elif val is C_NA:
2646+
seen.object_ = True
2647+
continue
26452648
else:
26462649
seen.object_ = True
26472650
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)