Skip to content

ENH: Add errors='coerce' to DataFrame.astype / Series.astype (GH#48781)#65851

Open
berasaikat wants to merge 4 commits into
pandas-dev:mainfrom
berasaikat:issue#48781
Open

ENH: Add errors='coerce' to DataFrame.astype / Series.astype (GH#48781)#65851
berasaikat wants to merge 4 commits into
pandas-dev:mainfrom
berasaikat:issue#48781

Conversation

@berasaikat

@berasaikat berasaikat commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Closes #48781


Add errors='coerce' as a option. Unconvertible values are replaced with the dtype's NA sentinel instead of raising:

df = pd.DataFrame({"a": [1, 2, "ERR"], "b": [3.0, "bad", 5.0]})

df.astype(float, errors="coerce")
#      a    b
# 0  1.0  3.0
# 1  2.0  NaN
# 2  NaN  5.0

df.astype("Float64", errors="coerce")
#       a     b
# 0   1.0   3.0
# 1   2.0  <NA>
# 2  <NA>   5.0

df.astype({"a": "Int64", "b": "datetime64[us]"}, errors="coerce")
# per-column dict form also supported

Files changed

  • pandas/_typing.py — add IgnoreRaiseCoerce
  • pandas/core/dtypes/astype.pyastype_array_safe coerce branch + _coerce_to_na helper
  • pandas/core/generic.py — widen errors type + docstring + examples
  • pandas/core/internals/blocks.py — widen errors type
  • pandas/tests/frame/methods/test_astype.pyTestAstypeCoerce
  • pandas/tests/series/methods/test_astype.pyTestSeriesAstypeCoerce
  • doc/source/whatsnew/v3.1.0.rst — enhancement entry

Checklist

  • Tests added in TestAstypeCoerce (DataFrame) and TestSeriesAstypeCoerce (Series) covering: float coerce, nullable Float64/Int64, datetime, dict-dtype, all-valid parity with errors='raise', and invalid errors= kwarg
  • Whatsnew entry added
  • Docstring updated with new errors option description and examples

@berasaikat berasaikat requested a review from Dr-Irv as a code owner June 10, 2026 19:15

@Dr-Irv Dr-Irv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I only reviewed pandas/_typing.py

Comment thread pandas/_typing.py Outdated

# Shared by functions such as drop and astype
IgnoreRaise: TypeAlias = Literal["ignore", "raise"]
IgnoreRaiseCoerce: TypeAlias = Literal["ignore", "raise", "coerce"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
IgnoreRaiseCoerce: TypeAlias = Literal["ignore", "raise", "coerce"]
IgnoreRaiseCoerce: TypeAlias = IgnoreRaise | Literal["coerce"]

@Dr-Irv Dr-Irv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I approve the changes to pandas/_typing.py. Others will need to approve the rest of this.

@jbrockmendel

Copy link
Copy Markdown
Member

Possible xref #45588. Can you see if this helps accomplish that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: add errors='coerce' to DataFrame.astype

3 participants