Skip to content

Commit e3a3a4a

Browse files
authored
ENH: Improve error mesage verbosity in string accessor (#59900)
1 parent e3e198f commit e3a3a4a

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

pandas/core/strings/accessor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ def _validate(data):
255255
inferred_dtype = lib.infer_dtype(values, skipna=True)
256256

257257
if inferred_dtype not in allowed_types:
258-
raise AttributeError("Can only use .str accessor with string values!")
258+
raise AttributeError(
259+
f"Can only use .str accessor with string values, not {inferred_dtype}"
260+
)
259261
return inferred_dtype
260262

261263
def __getitem__(self, key):

pandas/tests/series/accessors/test_str_accessor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ def test_str_attribute(self):
1515

1616
# str accessor only valid with string values
1717
ser = Series(range(5))
18-
with pytest.raises(AttributeError, match="only use .str accessor"):
18+
msg = "Can only use .str accessor with string values, not integer"
19+
with pytest.raises(AttributeError, match=msg):
1920
ser.str.repeat(2)
2021

2122
def test_str_accessor_updates_on_inplace(self):

0 commit comments

Comments
 (0)