Skip to content

Fix errors='ignore' being ignored in astype #30324 #30670

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

Closed
wants to merge 13 commits into from
Closed
4 changes: 2 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5447,7 +5447,7 @@ def astype(
"""
if is_dict_like(dtype):
if self.ndim == 1: # i.e. Series
if len(dtype) > 1 or self.name not in dtype:
if len(dtype) > 1 or self.name not in dtype and errors == "raise":
raise KeyError(
"Only the Series name can be used for "
"the key in Series dtype mappings."
Expand All @@ -5456,7 +5456,7 @@ def astype(
return self.astype(new_type, copy, errors)

for col_name in dtype.keys():
if col_name not in self:
if col_name not in self and errors == "raise":
raise KeyError(
"Only a column name can be used for the "
"key in a dtype mappings argument."
Expand Down
11 changes: 10 additions & 1 deletion pandas/tests/frame/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def test_astype_str_float(self):

@pytest.mark.parametrize("dtype_class", [dict, Series])
def test_astype_dict_like(self, dtype_class):
# GH7271 & GH16717
# GH7271 & GH16717 & GH30324
a = Series(date_range("2010-01-04", periods=5))
b = Series(range(5))
c = Series([0.0, 0.2, 0.4, 0.6, 0.8])
Expand Down Expand Up @@ -342,6 +342,15 @@ def test_astype_dict_like(self, dtype_class):
tm.assert_frame_equal(df, equiv)
tm.assert_frame_equal(df, original)

# GH 30324
# If errors=='ignore' than the resulting DataFrame
# should be the same as the original DataFrame
dt8 = dtype_class({"b": str, 2: str})
dt9 = dtype_class({"e": str})
df.astype(dt8, errors="ignore")
df.astype(dt9, errors="ignore")
tm.assert_frame_equal(df, original)

def test_astype_duplicate_col(self):
a1 = Series([1, 2, 3, 4, 5], name="a")
b = Series([0.1, 0.2, 0.4, 0.6, 0.8], name="b")
Expand Down