Skip to content

Commit ad9cbd9

Browse files
committed
validate result_type kwarg
1 parent 914e136 commit ad9cbd9

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

pandas/core/apply.py

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def __init__(self, obj, func, broadcast, raw, reduce, result_type,
3939
self.args = args or ()
4040
self.kwds = kwds or {}
4141

42+
if result_type not in [None, 'reduce', 'broadcast', 'expand']:
43+
raise ValueError("invalid value for result_type, must be one "
44+
"of {None, 'reduce', 'broadcast', 'expand'}")
45+
4246
if broadcast is not None:
4347
warnings.warn("The broadcast argument is deprecated and will "
4448
"be removed in a future version. You can specify "

pandas/io/formats/style.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ def _apply(self, func, axis=0, subset=None, **kwargs):
510510
data = self.data.loc[subset]
511511
if axis is not None:
512512
result = data.apply(func, axis=axis,
513-
result_type='infer', **kwargs)
513+
result_type='expand', **kwargs)
514514
result.columns = data.columns
515515
else:
516516
result = func(data, **kwargs)

pandas/tests/frame/test_apply.py

+12
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,18 @@ def test_result_type(self):
750750
expected.columns = columns
751751
assert_frame_equal(result, expected)
752752

753+
@pytest.mark.parametrize("result_type", ['foo', 1])
754+
def test_result_type_error(self, result_type):
755+
# allowed result_type
756+
df = DataFrame(
757+
np.tile(np.arange(3, dtype='int64'), 6).reshape(6, -1) + 1,
758+
columns=['A', 'B', 'C'])
759+
760+
with pytest.raises(ValueError):
761+
df.apply(lambda x: [1, 2, 3],
762+
axis=1,
763+
result_type=result_type)
764+
753765
@pytest.mark.parametrize(
754766
"box",
755767
[lambda x: list(x),

0 commit comments

Comments
 (0)