Skip to content

Commit 324ac85

Browse files
TomAugspurgerjorisvandenbossche
authored andcommitted
Allow accessing AxisProperties on classes (#18196)
1 parent ca737ac commit 324ac85

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

pandas/_libs/properties.pyx

+8-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,14 @@ cdef class AxisProperty(object):
6363
self.axis = axis
6464

6565
def __get__(self, obj, type):
66-
cdef list axes = obj._data.axes
66+
cdef:
67+
list axes
68+
69+
if obj is None:
70+
# Only instances have _data, not classes
71+
return None
72+
else:
73+
axes = obj._data.axes
6774
return axes[self.axis]
6875

6976
def __set__(self, obj, value):

pandas/tests/frame/test_api.py

+5
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ def test_axis_aliases(self):
306306
result = f.sum(axis='columns')
307307
assert_series_equal(result, expected)
308308

309+
def test_class_axis(self):
310+
# https://github.com/pandas-dev/pandas/issues/18147
311+
DataFrame.index # no exception!
312+
DataFrame.columns # no exception!
313+
309314
def test_more_asMatrix(self):
310315
values = self.mixed_frame.as_matrix()
311316
assert values.shape[1] == len(self.mixed_frame.columns)

pandas/tests/series/test_api.py

+4
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ def test_axis_alias(self):
334334
assert s._get_axis_number('rows') == 0
335335
assert s._get_axis_name('rows') == 'index'
336336

337+
def test_class_axis(self):
338+
# https://github.com/pandas-dev/pandas/issues/18147
339+
Series.index # no exception!
340+
337341
def test_numpy_unique(self):
338342
# it works!
339343
np.unique(self.ts)

0 commit comments

Comments
 (0)