Skip to content

Allow accessing AxisProperties on classes #18196

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
Nov 10, 2017
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
9 changes: 8 additions & 1 deletion pandas/_libs/properties.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down