Skip to content

Commit

Permalink
make much simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback committed Oct 1, 2017
1 parent 5a9bc70 commit 7faff8a
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 57 deletions.
38 changes: 1 addition & 37 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,43 +455,7 @@ def _apply_if_callable(maybe_callable, obj, axis=None, **kwargs):
"""

if callable(maybe_callable):

# we are allowing a user callable, which can return
# a result based on the object itself, e.g. a scalar / list
# of labels, or a boolean result from evaluating the labels
# on the specified axis

def try_on_axis():
labels = obj._get_axis(axis or 0)
return labels.map(maybe_callable, **kwargs).values

# act on object
try:
result = maybe_callable(obj, **kwargs)

# if we have asked for a specific axis
# then we must be 1d
if axis is not None:
if getattr(result, 'ndim', 1) != 1:
raise ValueError

return result
except KeyError as e:
# this is potentially a legitimate error
# if we cannot work on the labels
# we want to preserve the original KeyError
try:
return try_on_axis()
except: # no pragma
raise e
except: # no pragma
pass

# act on the axis
try:
return try_on_axis()
except AttributeError:
pass
return maybe_callable(obj, **kwargs)

return maybe_callable

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2339,7 +2339,7 @@ def select(self, crit, axis=0):
"""
Return data corresponding to axis labels matching criteria
DEPRECATED: use .loc(axis=)[crit] to select via labels
DEPRECATED: use df.loc[labels.map(crit).values] to select via labels
Parameters
----------
Expand All @@ -2353,7 +2353,7 @@ def select(self, crit, axis=0):
"""
warnings.warn("select is deprecated and will be removed in a "
"future release. You can use "
".loc[crit] as a replacement",
".loc[labels.map(crit).values] as a replacement",
FutureWarning, stacklevel=2)

axis = self._get_axis_number(axis)
Expand Down
6 changes: 1 addition & 5 deletions pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,7 @@ def test_set_index_bug(self):
xp = DataFrame({'val': [1, 2]},
Index(['b', 'c'], name='key'))

df2 = df.loc(axis=0)[lambda indx: indx >= 1]
rs = df2.set_index('key')
assert_frame_equal(rs, xp)

df2 = df.loc[lambda indx: indx >= 1]
df2 = df.loc[df.index.map(lambda indx: indx >= 1).values]
rs = df2.set_index('key')
assert_frame_equal(rs, xp)

Expand Down
18 changes: 7 additions & 11 deletions pandas/tests/frame/test_axis_select_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,27 +812,23 @@ def test_select(self):
assert_frame_equal(result, expected, check_names=False)

# replacement
result = self.tsframe.loc(axis=0)[f]
f = lambda x: x.weekday == 2
result = self.tsframe.loc(axis=0)[f(self.tsframe.index)]
assert_frame_equal(result, expected_weekdays)

result = self.tsframe.loc[f]
assert_frame_equal(result, expected_weekdays)

result = self.frame.loc(axis=1)[lambda x: x in ('B', 'D')]
crit = lambda x: x in ['B', 'D']
result = self.frame.loc(axis=1)[(self.frame.columns.map(crit)).values]
expected = self.frame.reindex(columns=['B', 'D'])
assert_frame_equal(result, expected, check_names=False)

# doc example
df = DataFrame({'A': [1, 2, 3]}, index=['foo', 'bar', 'baz'])

crit = lambda x: x in ['bar', 'baz']
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):

expected = df.select(lambda x: x in ['bar', 'baz'])
result = df.loc[lambda x: x in ['bar', 'baz']]
assert_frame_equal(result, expected, check_names=False)

result = df.loc(axis=0)[lambda x: x in ['bar', 'baz']]
expected = df.select(crit)
result = df.loc[df.index.map(crit).values]
assert_frame_equal(result, expected, check_names=False)

def test_take(self):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3103,7 +3103,8 @@ def agg_before(hour, func, fix=False):
"""

def _func(data):
d = data.loc(axis=0)[lambda x: x.hour < 11].dropna()
d = data.loc[data.index.map(
lambda x: x.hour < 11).values].dropna()
if fix:
data[data.index[0]]
if len(d) == 0:
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,8 @@ def test_groupby_level_no_obs(self):
'f2', 's1'), ('f2', 's2'), ('f3', 's1'), ('f3', 's2')])
df = DataFrame(
[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]], columns=midx)
df1 = df.loc(axis=1)[lambda u: u[0] in ['f2', 'f3']]
df1 = df.loc(axis=1)[df.columns.map(
lambda u: u[0] in ['f2', 'f3']).values]

grouped = df1.groupby(axis=1, level=0)
result = grouped.sum()
Expand Down

0 comments on commit 7faff8a

Please sign in to comment.