Skip to content

Commit 7faff8a

Browse files
committed
make much simpler
1 parent 5a9bc70 commit 7faff8a

File tree

6 files changed

+15
-57
lines changed

6 files changed

+15
-57
lines changed

pandas/core/common.py

+1-37
Original file line numberDiff line numberDiff line change
@@ -455,43 +455,7 @@ def _apply_if_callable(maybe_callable, obj, axis=None, **kwargs):
455455
"""
456456

457457
if callable(maybe_callable):
458-
459-
# we are allowing a user callable, which can return
460-
# a result based on the object itself, e.g. a scalar / list
461-
# of labels, or a boolean result from evaluating the labels
462-
# on the specified axis
463-
464-
def try_on_axis():
465-
labels = obj._get_axis(axis or 0)
466-
return labels.map(maybe_callable, **kwargs).values
467-
468-
# act on object
469-
try:
470-
result = maybe_callable(obj, **kwargs)
471-
472-
# if we have asked for a specific axis
473-
# then we must be 1d
474-
if axis is not None:
475-
if getattr(result, 'ndim', 1) != 1:
476-
raise ValueError
477-
478-
return result
479-
except KeyError as e:
480-
# this is potentially a legitimate error
481-
# if we cannot work on the labels
482-
# we want to preserve the original KeyError
483-
try:
484-
return try_on_axis()
485-
except: # no pragma
486-
raise e
487-
except: # no pragma
488-
pass
489-
490-
# act on the axis
491-
try:
492-
return try_on_axis()
493-
except AttributeError:
494-
pass
458+
return maybe_callable(obj, **kwargs)
495459

496460
return maybe_callable
497461

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2339,7 +2339,7 @@ def select(self, crit, axis=0):
23392339
"""
23402340
Return data corresponding to axis labels matching criteria
23412341
2342-
DEPRECATED: use .loc(axis=)[crit] to select via labels
2342+
DEPRECATED: use df.loc[labels.map(crit).values] to select via labels
23432343
23442344
Parameters
23452345
----------
@@ -2353,7 +2353,7 @@ def select(self, crit, axis=0):
23532353
"""
23542354
warnings.warn("select is deprecated and will be removed in a "
23552355
"future release. You can use "
2356-
".loc[crit] as a replacement",
2356+
".loc[labels.map(crit).values] as a replacement",
23572357
FutureWarning, stacklevel=2)
23582358

23592359
axis = self._get_axis_number(axis)

pandas/tests/frame/test_alter_axes.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,7 @@ def test_set_index_bug(self):
146146
xp = DataFrame({'val': [1, 2]},
147147
Index(['b', 'c'], name='key'))
148148

149-
df2 = df.loc(axis=0)[lambda indx: indx >= 1]
150-
rs = df2.set_index('key')
151-
assert_frame_equal(rs, xp)
152-
153-
df2 = df.loc[lambda indx: indx >= 1]
149+
df2 = df.loc[df.index.map(lambda indx: indx >= 1).values]
154150
rs = df2.set_index('key')
155151
assert_frame_equal(rs, xp)
156152

pandas/tests/frame/test_axis_select_reindex.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -812,27 +812,23 @@ def test_select(self):
812812
assert_frame_equal(result, expected, check_names=False)
813813

814814
# replacement
815-
result = self.tsframe.loc(axis=0)[f]
815+
f = lambda x: x.weekday == 2
816+
result = self.tsframe.loc(axis=0)[f(self.tsframe.index)]
816817
assert_frame_equal(result, expected_weekdays)
817818

818-
result = self.tsframe.loc[f]
819-
assert_frame_equal(result, expected_weekdays)
820-
821-
result = self.frame.loc(axis=1)[lambda x: x in ('B', 'D')]
819+
crit = lambda x: x in ['B', 'D']
820+
result = self.frame.loc(axis=1)[(self.frame.columns.map(crit)).values]
822821
expected = self.frame.reindex(columns=['B', 'D'])
823822
assert_frame_equal(result, expected, check_names=False)
824823

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

827+
crit = lambda x: x in ['bar', 'baz']
828828
with tm.assert_produces_warning(FutureWarning,
829829
check_stacklevel=False):
830-
831-
expected = df.select(lambda x: x in ['bar', 'baz'])
832-
result = df.loc[lambda x: x in ['bar', 'baz']]
833-
assert_frame_equal(result, expected, check_names=False)
834-
835-
result = df.loc(axis=0)[lambda x: x in ['bar', 'baz']]
830+
expected = df.select(crit)
831+
result = df.loc[df.index.map(crit).values]
836832
assert_frame_equal(result, expected, check_names=False)
837833

838834
def test_take(self):

pandas/tests/groupby/test_groupby.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3103,7 +3103,8 @@ def agg_before(hour, func, fix=False):
31033103
"""
31043104

31053105
def _func(data):
3106-
d = data.loc(axis=0)[lambda x: x.hour < 11].dropna()
3106+
d = data.loc[data.index.map(
3107+
lambda x: x.hour < 11).values].dropna()
31073108
if fix:
31083109
data[data.index[0]]
31093110
if len(d) == 0:

pandas/tests/test_multilevel.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,8 @@ def test_groupby_level_no_obs(self):
12391239
'f2', 's1'), ('f2', 's2'), ('f3', 's1'), ('f3', 's2')])
12401240
df = DataFrame(
12411241
[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]], columns=midx)
1242-
df1 = df.loc(axis=1)[lambda u: u[0] in ['f2', 'f3']]
1242+
df1 = df.loc(axis=1)[df.columns.map(
1243+
lambda u: u[0] in ['f2', 'f3']).values]
12431244

12441245
grouped = df1.groupby(axis=1, level=0)
12451246
result = grouped.sum()

0 commit comments

Comments
 (0)