Skip to content

Backport PR #55655 on branch 2.1.x (BUG: infer_string not inferring string dtype when NA is first value) #55666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Bug fixes
- Fixed bug in :meth:`Series.str.extractall` for :class:`ArrowDtype` dtype being converted to object (:issue:`53846`)
- 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`)
- Silence ``Period[B]`` warnings introduced by :issue:`53446` during normal plotting activity (:issue:`55138`)
- Fixed bug in :class:`Series` constructor not inferring string dtype when ``NA`` is the first value and ``infer_string`` is set (:issue:` 55655`)

.. ---------------------------------------------------------------------------
.. _whatsnew_212.other:
Expand Down
3 changes: 3 additions & 0 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2642,6 +2642,9 @@ def maybe_convert_objects(ndarray[object] objects,
else:
seen.object_ = True
break
elif val is C_NA:
seen.object_ = True
continue
else:
seen.object_ = True
break
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2131,6 +2131,14 @@ def test_series_constructor_infer_string_scalar(self):
tm.assert_series_equal(ser, expected)
assert ser.dtype.storage == "python"

def test_series_string_inference_na_first(self):
# GH#55655
pytest.importorskip("pyarrow")
expected = Series([pd.NA, "b"], dtype="string[pyarrow_numpy]")
with pd.option_context("future.infer_string", True):
result = Series([pd.NA, "b"])
tm.assert_series_equal(result, expected)


class TestSeriesConstructorIndexCoercion:
def test_series_constructor_datetimelike_index_coercion(self):
Expand Down