From be356a94bec69f9f610ffb8f04025a710efdcf32 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Thu, 9 Nov 2017 11:48:28 -0600 Subject: [PATCH] Allow accessing AxisProperties on classes --- pandas/_libs/properties.pyx | 9 ++++++++- pandas/tests/frame/test_api.py | 5 +++++ pandas/tests/series/test_api.py | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/properties.pyx b/pandas/_libs/properties.pyx index 374da8067eedd..4beb24f07c21c 100644 --- a/pandas/_libs/properties.pyx +++ b/pandas/_libs/properties.pyx @@ -63,7 +63,14 @@ cdef class AxisProperty(object): self.axis = axis def __get__(self, obj, type): - cdef list axes = obj._data.axes + cdef: + list axes + + if obj is None: + # Only instances have _data, not classes + return None + else: + axes = obj._data.axes return axes[self.axis] def __set__(self, obj, value): diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index be6d81c63ae1e..c50aa858a15b5 100644 --- a/pandas/tests/frame/test_api.py +++ b/pandas/tests/frame/test_api.py @@ -306,6 +306,11 @@ def test_axis_aliases(self): result = f.sum(axis='columns') assert_series_equal(result, expected) + def test_class_axis(self): + # https://github.com/pandas-dev/pandas/issues/18147 + DataFrame.index # no exception! + DataFrame.columns # no exception! + def test_more_asMatrix(self): values = self.mixed_frame.as_matrix() assert values.shape[1] == len(self.mixed_frame.columns) diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index 6b950be15ca46..c1e4189283928 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -334,6 +334,10 @@ def test_axis_alias(self): assert s._get_axis_number('rows') == 0 assert s._get_axis_name('rows') == 'index' + def test_class_axis(self): + # https://github.com/pandas-dev/pandas/issues/18147 + Series.index # no exception! + def test_numpy_unique(self): # it works! np.unique(self.ts)