Skip to content

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

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 2 commits into from
Oct 24, 2023
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 @@ -2665,6 +2665,9 @@ def maybe_convert_objects(ndarray[object] objects,
else:
seen.object_ = True
break
elif val is C_NA:
seen.object_ = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think missing a break after this? See it in all the other branches at least

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this is missing on purpose. We don't know which dtype we have with NA and infer-string set, so have to do another pass

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah OK. Does continue work here too? Assuming no difference with the current code setup, but continue would be more future-proof in case of a refactor, while also signaling intent

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sure

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