Skip to content

Commit 1d93380

Browse files
committed
broadcast always returns original column names
1 parent 8a1837c commit 1d93380

File tree

3 files changed

+7
-18
lines changed

3 files changed

+7
-18
lines changed

pandas/core/apply.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ def apply_broadcast(self, target):
187187
# axis which we want to compare compliance
188188
result_compare = target.shape[0]
189189

190-
index = None
191190
for i, col in enumerate(target.columns):
192191
res = self.f(target[col])
193192
ares = np. asarray(res).ndim
@@ -201,19 +200,11 @@ def apply_broadcast(self, target):
201200
if result_compare != len(res):
202201
raise ValueError("cannot broadcast result")
203202

204-
# if we have a Series result, then then index
205-
# is our result
206-
if isinstance(res, ABCSeries):
207-
index = res.index
208-
209203
result_values[:, i] = res
210204

211-
# if we are returning a list-like
212-
# then preserve the original index
213-
if index is None:
214-
index = target.index
215-
216-
result = self.obj._constructor(result_values, index=index,
205+
# we *always* preserve the original index / columns
206+
result = self.obj._constructor(result_values,
207+
index=target.index,
217208
columns=target.columns)
218209
return result
219210

pandas/core/frame.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -4877,10 +4877,11 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None,
48774877
48784878
result_type : {'expand', 'reduce', 'broadcast, None}
48794879
These only act when axis=1 {columns}
4880-
* 'expand' : list-like results will be turned into columns
4880+
* 'expand' : list-like results will be turned into columns.
48814881
* 'reduce' : return a Series if possible rather than expanding
4882-
list-like results. This is the opposite to 'expand'
4883-
* 'broadcast' : scalar results will be broadcast to all columns
4882+
list-like results. This is the opposite to 'expand'.
4883+
* 'broadcast' : results will be broadcast to the original shape
4884+
of the frame, the original index & columns will be retained.
48844885
* None : list-like results will be returned as a list
48854886
in a single column. However if the apply function
48864887
returns a Series these are expanded to columns.

pandas/tests/frame/test_apply.py

-3
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,12 @@ def test_apply_broadcast(self):
171171
result_type='broadcast')
172172
tm.assert_frame_equal(result, df)
173173

174-
# columms come from the returned Series
175174
df = DataFrame(np.tile(np.arange(3), 6).reshape(6, -1) + 1,
176175
columns=list('ABC'))
177176
result = df.apply(lambda x: Series([1, 2, 3], index=list('abc')),
178177
axis=1,
179178
result_type='broadcast')
180179
expected = df.copy()
181-
expected.columns = list('abc')
182180
tm.assert_frame_equal(result, expected)
183181

184182
def test_apply_broadcast_error(self):
@@ -756,7 +754,6 @@ def test_result_type(self):
756754
axis=1,
757755
result_type='broadcast')
758756
expected = df.copy()
759-
expected.columns = columns
760757
assert_frame_equal(result, expected)
761758

762759
# series result

0 commit comments

Comments
 (0)