Skip to content

Commit de3a85c

Browse files
johnklehmjreback
authored andcommitted
Pass the errors and kwargs arguments through to astype in the columns… (#25888)
1 parent 819fc15 commit de3a85c

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ Numeric
299299
Conversion
300300
^^^^^^^^^^
301301

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

pandas/core/generic.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5715,7 +5715,8 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
57155715
results = []
57165716
for col_name, col in self.iteritems():
57175717
if col_name in dtype:
5718-
results.append(col.astype(dtype[col_name], copy=copy))
5718+
results.append(col.astype(dtype=dtype[col_name], copy=copy,
5719+
errors=errors, **kwargs))
57195720
else:
57205721
results.append(results.append(col.copy() if copy else col))
57215722

pandas/tests/frame/test_dtypes.py

+14
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,20 @@ def test_arg_for_errors_in_astype(self):
840840

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

843+
def test_arg_for_errors_in_astype_dictlist(self):
844+
# GH-25905
845+
df = pd.DataFrame([
846+
{'a': '1', 'b': '16.5%', 'c': 'test'},
847+
{'a': '2.2', 'b': '15.3', 'c': 'another_test'}])
848+
expected = pd.DataFrame([
849+
{'a': 1.0, 'b': '16.5%', 'c': 'test'},
850+
{'a': 2.2, 'b': '15.3', 'c': 'another_test'}])
851+
type_dict = {'a': 'float64', 'b': 'float64', 'c': 'object'}
852+
853+
result = df.astype(dtype=type_dict, errors='ignore')
854+
855+
tm.assert_frame_equal(result, expected)
856+
843857
@pytest.mark.parametrize('input_vals', [
844858
([1, 2]),
845859
(['1', '2']),

0 commit comments

Comments
 (0)