Skip to content

TST/DOC: Fix tests and docs for .cat raising AttributeError if invalid #9629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/source/categorical.rst
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,14 @@ Dtype comparisons work:
dtype == np.str_
np.str_ == dtype

To check if a Series contains Categorical data, with pandas 0.16 or later, use
``hasattr(s, 'cat')``:

.. ipython:: python
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a :okexcept: option to the directive here (so the doc building does not issue a warning for the failure)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasattr catches the error, so I don't think that's necessary?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, yes, sorry, I was looking too quickly, thought you were giving an example of that accessing the attribute raises, but it is hasattr, not getattr ..


hasattr(Series(['a'], dtype='category'), 'cat')
hasattr(Series(['a']), 'cat')

Using `numpy` functions on a `Series` of type ``category`` should not work as `Categoricals`
are not numeric data (even in the case that ``.categories`` is numeric).

Expand Down
9 changes: 5 additions & 4 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ class NDFrame(PandasObject):
copy : boolean, default False
"""
_internal_names = ['_data', '_cacher', '_item_cache', '_cache',
'is_copy', 'dt', 'cat', 'str', '_subtyp', '_index',
'is_copy', '_subtyp', '_index',
'_default_kind', '_default_fill_value',
'__array_struct__','__array_interface__']
_internal_names_set = set(_internal_names)
_accessors = frozenset([])
_metadata = []
is_copy = None

Expand Down Expand Up @@ -1957,9 +1958,9 @@ def __getattr__(self, name):
# Note: obj.x will always call obj.__getattribute__('x') prior to
# calling obj.__getattr__('x').

if name in self._internal_names_set:
return object.__getattribute__(self, name)
elif name in self._metadata:
if (name in self._internal_names_set
or name in self._metadata
or name in self._accessors):
return object.__getattribute__(self, name)
else:
if name in self._info_axis:
Expand Down
1 change: 1 addition & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
Copy input data
"""
_metadata = ['name']
_accessors = frozenset(['dt', 'cat', 'str'])
_allow_index_ops = True

def __init__(self, data=None, index=None, dtype=None, name=None,
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,13 +1354,13 @@ def test_sequence_like(self):
def test_series_delegations(self):

# invalid accessor
self.assertRaises(TypeError, lambda : Series([1,2,3]).cat)
tm.assertRaisesRegexp(TypeError,
self.assertRaises(AttributeError, lambda : Series([1,2,3]).cat)
tm.assertRaisesRegexp(AttributeError,
r"Can only use .cat accessor with a 'category' dtype",
lambda : Series([1,2,3]).cat)
self.assertRaises(TypeError, lambda : Series(['a','b','c']).cat)
self.assertRaises(TypeError, lambda : Series(np.arange(5.)).cat)
self.assertRaises(TypeError, lambda : Series([Timestamp('20130101')]).cat)
self.assertRaises(AttributeError, lambda : Series(['a','b','c']).cat)
self.assertRaises(AttributeError, lambda : Series(np.arange(5.)).cat)
self.assertRaises(AttributeError, lambda : Series([Timestamp('20130101')]).cat)

# Series should delegate calls to '.categories', '.codes', '.ordered' and the
# methods '.set_categories()' 'drop_unused_categories()' to the categorical
Expand Down