Skip to content

Commit 1b887ec

Browse files
authored
update astype keyerror msg (#44487)
1 parent a327ad1 commit 1b887ec

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

pandas/core/generic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5837,7 +5837,8 @@ def astype(
58375837
if col_name not in self:
58385838
raise KeyError(
58395839
"Only a column name can be used for the "
5840-
"key in a dtype mappings argument."
5840+
"key in a dtype mappings argument. "
5841+
f"'{col_name}' not found in columns."
58415842
)
58425843

58435844
# GH#44417 cast to Series so we can use .iat below, which will be

pandas/tests/frame/methods/test_astype.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,13 @@ def test_astype_dict_like(self, dtype_class):
222222
# in the keys of the dtype dict
223223
dt4 = dtype_class({"b": str, 2: str})
224224
dt5 = dtype_class({"e": str})
225-
msg = "Only a column name can be used for the key in a dtype mappings argument"
226-
with pytest.raises(KeyError, match=msg):
225+
msg_frame = (
226+
"Only a column name can be used for the key in a dtype mappings argument. "
227+
"'{}' not found in columns."
228+
)
229+
with pytest.raises(KeyError, match=msg_frame.format(2)):
227230
df.astype(dt4)
228-
with pytest.raises(KeyError, match=msg):
231+
with pytest.raises(KeyError, match=msg_frame.format("e")):
229232
df.astype(dt5)
230233
tm.assert_frame_equal(df, original)
231234

0 commit comments

Comments
 (0)