Skip to content

Commit d7c9d5a

Browse files
committed
Update the test data to relfect #25905. Add an explicit check for the absense of the exception we'd normally throw. Assert the resulting dataframe against a reference dataframe. Move the test case to test_dtypes.
1 parent 11e5d42 commit d7c9d5a

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

pandas/tests/frame/common.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from contextlib import contextmanager
2+
from typing import Optional, Union
3+
14
import numpy as np
25

36
from pandas.util._decorators import cache_readonly
@@ -139,3 +142,30 @@ def _check_mixed_int(df, dtype=None):
139142
assert(df.dtypes['C'] == dtypes['C'])
140143
if dtypes.get('D'):
141144
assert(df.dtypes['D'] == dtypes['D'])
145+
146+
147+
@contextmanager
148+
def not_raises(expected_exception: Union[Exception, ValueError],
149+
msg: Optional[str] = None) -> None:
150+
"""Explicitly checks that a type of exception is not raised inside a
151+
with context.
152+
153+
References:
154+
SO: how-to-use-pytest-to-check-that-error-is-not-raised
155+
156+
Parameters
157+
----------
158+
expected_exception: that is verified not to be raised.
159+
msg: if given the message to verify in addition to the exception type.
160+
"""
161+
try:
162+
yield
163+
except expected_exception as e:
164+
if not msg:
165+
raise AssertionError(
166+
f"Raised exception {repr(e)} when it should not!")
167+
168+
elif hasattr(e, 'message') and e.message == msg:
169+
raise AssertionError(
170+
f"Raised exception {repr(e)} with message {e.message} " +
171+
"when it should not!")

pandas/tests/frame/test_dtypes.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Categorical, DataFrame, Series, Timedelta, Timestamp,
1616
_np_version_under1p14, compat, concat, date_range, option_context)
1717
from pandas.core.arrays import integer_array
18-
from pandas.tests.frame.common import TestData
18+
from pandas.tests.frame.common import TestData, not_raises
1919
import pandas.util.testing as tm
2020
from pandas.util.testing import (
2121
assert_frame_equal, assert_series_equal, makeCustomDataframe as mkdf)
@@ -850,6 +850,24 @@ def test_arg_for_errors_in_astype(self):
850850

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

853+
def test_arg_for_errors_in_astype_dictlist(self):
854+
data_df = pd.DataFrame([{'col_a': '1', 'col_b': '16.5%',
855+
'col_c': 'test'},
856+
{'col_a': '2.2', 'col_b': '15.3',
857+
'col_c': 'another_test'}
858+
])
859+
reference_df = pd.DataFrame([{'col_a': '1', 'col_b': '16.5%',
860+
'col_c': 'test'},
861+
{'col_a': '2.2', 'col_b': '15.3',
862+
'col_c': 'another_test'}
863+
], dtype='float64')
864+
type_dict = {'col_a': 'float64', 'col_b': 'float64', 'col_c': 'object'}
865+
866+
with not_raises(ValueError):
867+
df_astype = data_df.astype(dtype=type_dict, errors='ignore')
868+
869+
tm.assert_frame_equal(reference_df, df_astype)
870+
853871
@pytest.mark.parametrize('input_vals', [
854872
([1, 2]),
855873
(['1', '2']),

pandas/tests/generic/test_generic.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,6 @@ def test_downcast(self):
202202
expected = o.astype(np.int64)
203203
self._compare(result, expected)
204204

205-
def test_multiple_astype_casts(self):
206-
df = DataFrame({'A': [1, 2, 'z'], 'B': [3, 4, 'z']})
207-
df.astype({'A': int,
208-
'B': 'datetime64[ns]'},
209-
errors='ignore')
210-
211205
def test_constructor_compound_dtypes(self):
212206
# see gh-5191
213207
# Compound dtypes should raise NotImplementedError.

0 commit comments

Comments
 (0)