Skip to content

Commit 7531038

Browse files
takluyverwesm
authored andcommitted
Add simple attribute access to DataFrame columns.
1 parent b5435f3 commit 7531038

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

pandas/core/frame.py

+8
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,14 @@ def _getitem_single(self, key):
861861
res = Series(values, index=self.index, name=key)
862862
self._series_cache[key] = res
863863
return res
864+
865+
def __getattr__(self, name):
866+
"""After regular attribute access, try looking up the name of a column.
867+
This allows simpler access to columns for interactive use."""
868+
if name in self.columns:
869+
return self[name]
870+
raise AttributeError("'%s' object has no attribute '%s'" % \
871+
(type(self).__name__, name))
864872

865873
def __setitem__(self, key, value):
866874
# support boolean setting with DataFrame input, e.g.

pandas/tests/test_frame.py

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ def test_getitem_boolean(self):
6767

6868
subframe_obj = self.tsframe[indexer_obj]
6969
assert_frame_equal(subframe_obj, subframe)
70+
71+
def test_getattr(self):
72+
tm.assert_series_equal(self.frame.A, self.frame['A'])
73+
self.assertRaises(AttributeError, getattr, self.frame, 'NONEXISTENT_NAME')
7074

7175
def test_setitem(self):
7276
# not sure what else to do here

0 commit comments

Comments
 (0)