Skip to content

Commit

Permalink
Backport PR #55655 on branch 2.1.x (BUG: infer_string not inferring s…
Browse files Browse the repository at this point in the history
…tring dtype when NA is first value) (#55666)

Backport PR #55655: BUG: infer_string not inferring string dtype when NA is first value

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl committed Oct 24, 2023
1 parent 8f003de commit cdb45c6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
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

0 comments on commit cdb45c6

Please sign in to comment.