Skip to content

Commit 7617ac2

Browse files
dsaxtonpull[bot]
authored andcommitted
BUG: Respect errors="ignore" during extension astype (#35979)
1 parent 9d923e8 commit 7617ac2

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

doc/source/whatsnew/v1.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Bug fixes
3434
- Bug in :meth:`DataFrame.eval` with ``object`` dtype column binary operations (:issue:`35794`)
3535
- Bug in :class:`Series` constructor raising a ``TypeError`` when constructing sparse datetime64 dtypes (:issue:`35762`)
3636
- Bug in :meth:`DataFrame.apply` with ``result_type="reduce"`` returning with incorrect index (:issue:`35683`)
37+
- Bug in :meth:`Series.astype` and :meth:`DataFrame.astype` not respecting the ``errors`` argument when set to ``"ignore"`` for extension dtypes (:issue:`35471`)
3738
- Bug in :meth:`DateTimeIndex.format` and :meth:`PeriodIndex.format` with ``name=True`` setting the first item to ``"None"`` where it should be ``""`` (:issue:`35712`)
3839
- Bug in :meth:`Float64Index.__contains__` incorrectly raising ``TypeError`` instead of returning ``False`` (:issue:`35788`)
3940
- Bug in :class:`Series` constructor incorrectly raising a ``TypeError`` when passed an ordered set (:issue:`36044`)

pandas/core/internals/blocks.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,13 @@ def astype(self, dtype, copy: bool = False, errors: str = "raise"):
581581

582582
# force the copy here
583583
if self.is_extension:
584-
# TODO: Should we try/except this astype?
585-
values = self.values.astype(dtype)
584+
try:
585+
values = self.values.astype(dtype)
586+
except (ValueError, TypeError):
587+
if errors == "ignore":
588+
values = self.values
589+
else:
590+
raise
586591
else:
587592
if issubclass(dtype.type, str):
588593

pandas/tests/frame/methods/test_astype.py

+22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
CategoricalDtype,
99
DataFrame,
1010
DatetimeTZDtype,
11+
Interval,
1112
IntervalDtype,
1213
NaT,
1314
Series,
@@ -565,3 +566,24 @@ def test_astype_empty_dtype_dict(self):
565566
result = df.astype(dict())
566567
tm.assert_frame_equal(result, df)
567568
assert result is not df
569+
570+
@pytest.mark.parametrize(
571+
"df",
572+
[
573+
DataFrame(Series(["x", "y", "z"], dtype="string")),
574+
DataFrame(Series(["x", "y", "z"], dtype="category")),
575+
DataFrame(Series(3 * [Timestamp("2020-01-01", tz="UTC")])),
576+
DataFrame(Series(3 * [Interval(0, 1)])),
577+
],
578+
)
579+
@pytest.mark.parametrize("errors", ["raise", "ignore"])
580+
def test_astype_ignores_errors_for_extension_dtypes(self, df, errors):
581+
# https://github.com/pandas-dev/pandas/issues/35471
582+
if errors == "ignore":
583+
expected = df
584+
result = df.astype(float, errors=errors)
585+
tm.assert_frame_equal(result, expected)
586+
else:
587+
msg = "(Cannot cast)|(could not convert)"
588+
with pytest.raises((ValueError, TypeError), match=msg):
589+
df.astype(float, errors=errors)

pandas/tests/series/methods/test_astype.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from pandas import Series, date_range
1+
import pytest
2+
3+
from pandas import Interval, Series, Timestamp, date_range
24
import pandas._testing as tm
35

46

@@ -23,3 +25,24 @@ def test_astype_dt64tz_to_str(self):
2325
dtype=object,
2426
)
2527
tm.assert_series_equal(result, expected)
28+
29+
@pytest.mark.parametrize(
30+
"values",
31+
[
32+
Series(["x", "y", "z"], dtype="string"),
33+
Series(["x", "y", "z"], dtype="category"),
34+
Series(3 * [Timestamp("2020-01-01", tz="UTC")]),
35+
Series(3 * [Interval(0, 1)]),
36+
],
37+
)
38+
@pytest.mark.parametrize("errors", ["raise", "ignore"])
39+
def test_astype_ignores_errors_for_extension_dtypes(self, values, errors):
40+
# https://github.com/pandas-dev/pandas/issues/35471
41+
if errors == "ignore":
42+
expected = values
43+
result = values.astype(float, errors="ignore")
44+
tm.assert_series_equal(result, expected)
45+
else:
46+
msg = "(Cannot cast)|(could not convert)"
47+
with pytest.raises((ValueError, TypeError), match=msg):
48+
values.astype(float, errors=errors)

0 commit comments

Comments
 (0)