Skip to content

Pass the errors and kwargs arguments through to astype in the columns… #25888

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 12 commits into from
Mar 31, 2019
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Numeric
Conversion
^^^^^^^^^^

-
- Bug in :func:`DataFrame.astype()` when passing a dict of columns and types the `errors` parameter was ignored. (:issue:`25905`)
-
-

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5716,7 +5716,8 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
results = []
for col_name, col in self.iteritems():
if col_name in dtype:
results.append(col.astype(dtype[col_name], copy=copy))
results.append(col.astype(dtype=dtype[col_name], copy=copy,
errors=errors, **kwargs))
else:
results.append(results.append(col.copy() if copy else col))

Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/frame/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,20 @@ def test_arg_for_errors_in_astype(self):

df.astype(np.int8, errors='ignore')

def test_arg_for_errors_in_astype_dictlist(self):
# GH-25905
df = pd.DataFrame([
{'a': '1', 'b': '16.5%', 'c': 'test'},
{'a': '2.2', 'b': '15.3', 'c': 'another_test'}])
expected = pd.DataFrame([
{'a': 1.0, 'b': '16.5%', 'c': 'test'},
{'a': 2.2, 'b': '15.3', 'c': 'another_test'}])
type_dict = {'a': 'float64', 'b': 'float64', 'c': 'object'}

result = df.astype(dtype=type_dict, errors='ignore')

tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize('input_vals', [
([1, 2]),
(['1', '2']),
Expand Down