Skip to content

Commit 377f741

Browse files
committed
Added unit tests to ensure that the keyword is correctly used
Signed-off-by: nbonnin <[email protected]>
1 parent ffdc947 commit 377f741

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

pandas/tests/generic/test_generic.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pandas import DataFrame, MultiIndex, Series, date_range
1010
import pandas.util.testing as tm
1111

12+
1213
# ----------------------------------------------------------------------
1314
# Generic types test cases
1415

@@ -265,7 +266,6 @@ def test_metadata_propagation(self):
265266

266267
# simple boolean
267268
for op in ["__eq__", "__le__", "__ge__"]:
268-
269269
# this is a name matching op
270270
v1 = getattr(o, op)(o)
271271
v2 = getattr(o, op)(o2)
@@ -948,3 +948,20 @@ def test_axis_classmethods(self, box):
948948
assert obj._get_axis_number(v) == box._get_axis_number(v)
949949
assert obj._get_axis_name(v) == box._get_axis_name(v)
950950
assert obj._get_block_manager_axis(v) == box._get_block_manager_axis(v)
951+
952+
# Tests that the ignore keyword causes the intended functionality
953+
def test_astype_ignores(self):
954+
df = pd.DataFrame({"a": [1, 2, 3]})
955+
df_validation = pd.DataFrame({"a": [1, 2, 3]})
956+
957+
# Tests that a KeyError is raised when raise is explicitly declared
958+
with pytest.raises(KeyError):
959+
df.astype({"b": str}, errors="raise")
960+
961+
# Tests that a KeyError is raised when no argument is declared
962+
with pytest.raises(KeyError):
963+
df.astype({"b": str})
964+
965+
# Tests that no KeyError is thrown if ignore is true. Ensures there is no change to the initial DataFrame
966+
df.astype({"b": str}, errors="ignore")
967+
assert df.equals(df_validation)

0 commit comments

Comments
 (0)