BUG/API: Accessors like .cat raise AttributeError when invalid by shoyer · Pull Request #9617 · pandas-dev/pandas (original) (raw)
This really seems a like a gray area to me as far as standard Python exceptions go:
- AttributeError: Raised when an attribute reference (see Attribute references) or assignment fails. (When an object does not support attribute references or attribute assignments at all, TypeError is raised.)
- TypeError: Raised when an operation or function is applied to an object of inappropriate type.
(Note that the only case I could find for the parenthetical remark about TypeError is when attempting to set an attribute of the built-in object. Series pretty clearly does not fall in this category.)
To me, the error from Series([1, 2, 3]).cat is closer to "an attribute reference fails" than "an operation or function is applied to an object of inappropriate type" (is attribute lookup an operation or function?), but really I can see a case being made either way.
What tips me in the direction of AttributeError is that it plays much more nicely with hasattr and getattr, which exist and will be used like getattr(s, 'cat') whether we encourage it or not. Duck typing is a pretty standard way to do things in Python, and we've had at least two reports from categorical users of using these methods. Plus, it enables a nice safe way to check for Categorical dtype (unlike s.dtype == 'category').
@JanSchulz - we jumped through some hoops to ensure that Series.cat works and Series().cat should not (see #9322). I agree that ideally s.<tab> should not show cat for non-categorical arrays. I think we discussed this in #9322 and decided against it only because the value seemed marginal and implementation would be a little messy (involving some hacks to Series.__dir__). But we can revisit that if you think it's important.