Skip to content

Commit bd2e92b

Browse files
pietrustwesm
authored andcommitted
(1) add a get() method like dicts have, (2) throw KeyError, not Exception, when a column isn't found
1 parent 3625933 commit bd2e92b

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

pandas/core/frame.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ def _getitem_array(self, key):
10741074
indexer = self.columns.get_indexer(key)
10751075
mask = indexer == -1
10761076
if mask.any():
1077-
raise Exception("No column(s) named: %s" % str(key[mask]))
1077+
raise KeyError("No column(s) named: %s" % str(key[mask]))
10781078
return self.reindex(columns=key)
10791079

10801080
def _slice(self, slobj, axis=0):
@@ -1215,6 +1215,13 @@ def pop(self, item):
12151215
"""
12161216
return NDFrame.pop(self, item)
12171217

1218+
def get(self, column, default=None):
1219+
try:
1220+
return self[column]
1221+
except KeyError:
1222+
return default
1223+
1224+
12181225
# to support old APIs
12191226
@property
12201227
def _series(self):

0 commit comments

Comments
 (0)