Skip to content

Commit d234627

Browse files
committed
BUG: Respect errors="ignore" during extension astype
1 parent 92e4bd5 commit d234627

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

doc/source/whatsnew/v1.1.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Bug fixes
2929
- Bug in :class:`Series` constructor raising a ``TypeError`` when constructing sparse datetime64 dtypes (:issue:`35762`)
3030
- Bug in :meth:`DataFrame.apply` with ``result_type="reduce"`` returning with incorrect index (:issue:`35683`)
3131
- Bug in :meth:`DateTimeIndex.format` and :meth:`PeriodIndex.format` with ``name=True`` setting the first item to ``"None"`` where it should bw ``""`` (:issue:`35712`)
32+
- Bug in :meth:`Series.astype` and :meth:`DataFrame.astype` not respecting the ``errors`` argument when set to ``"ignore"`` for extension dtypes (:issue:`35471`)
3233
-
3334

3435
.. ---------------------------------------------------------------------------

pandas/core/internals/blocks.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,13 @@ def astype(self, dtype, copy: bool = False, errors: str = "raise"):
580580

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

pandas/tests/frame/methods/test_astype.py

Lines changed: 16 additions & 0 deletions
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,18 @@ 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+
"values",
572+
[
573+
Series(["x", "y", "z"], dtype="string"),
574+
Series(["x", "y", "z"], dtype="category"),
575+
Series(3 * [Timestamp("2020-01-01")]),
576+
Series(3 * [Interval(0, 1)]),
577+
],
578+
)
579+
def test_astype_ignores_errors_for_extension_dtypes(self, values):
580+
# https://github.com/pandas-dev/pandas/issues/35471
581+
expected = DataFrame(values)
582+
result = expected.astype(float, errors="ignore")
583+
tm.assert_frame_equal(result, expected)

pandas/tests/series/methods/test_astype.py

Lines changed: 18 additions & 1 deletion
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,18 @@ 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")]),
35+
Series(3 * [Interval(0, 1)]),
36+
],
37+
)
38+
def test_astype_ignores_errors_for_extension_dtypes(self, values):
39+
# https://github.com/pandas-dev/pandas/issues/35471
40+
expected = values
41+
result = expected.astype(float, errors="ignore")
42+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)