Skip to content

Add get_attr behavior for Panel, similar to DataFrame #554

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

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 28 additions & 0 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# pylint: disable=E1103,W0231,W0212,W0621

import operator
import sys
import numpy as np

from pandas.core.common import (PandasError, _mut_exclusive,
Expand Down Expand Up @@ -509,6 +510,14 @@ def set_value(self, item, major, minor, value):
def _box_item_values(self, key, values):
return DataFrame(values, index=self.major_axis, columns=self.minor_axis)

def __getattr__(self, name):
"""After regular attribute access, try looking up the name of an item.
This allows simpler access to items for interactive use."""
if name in self.items:
return self[name]
raise AttributeError("'%s' object has no attribute '%s'" %
(type(self).__name__, name))

def _slice(self, slobj, axis=0):
new_data = self._data.get_slice(slobj, axis=axis)
return self._constructor(new_data)
Expand Down Expand Up @@ -1269,3 +1278,22 @@ def _get_distinct_indexes(indexes):

def _monotonic(arr):
return not (arr[1:] < arr[:-1]).any()

def install_ipython_completers(): # pragma: no cover
"""Register the Panel type with IPython's tab completion machinery, so
that it knows about accessing column names as attributes."""
from IPython.utils.generics import complete_object

@complete_object.when_type(Panel)
def complete_dataframe(obj, prev_completions):
return prev_completions + [c for c in obj.items \
if isinstance(c, basestring) and py3compat.isidentifier(c)]

# Importing IPython brings in about 200 modules, so we want to avoid it unless
# we're in IPython (when those modules are loaded anyway).
if "IPython" in sys.modules: # pragma: no cover
try:
install_ipython_completers()
except Exception:
pass

3 changes: 3 additions & 0 deletions pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,9 @@ def test_rename(self):
renamed_nocopy['foo'] = 3.
self.assert_((self.panel['ItemA'].values == 3).all())

def test_get_attr(self):
assert_frame_equal(self.panel['ItemA'], self.panel.ItemA)

class TestLongPanel(unittest.TestCase):
"""
LongPanel no longer exists, but...
Expand Down