Skip to content

Commit

Permalink
ENH: attach name to Series on axis=1 in DataFrame.apply, #983
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Apr 10, 2012
1 parent 91802b1 commit 5721a0c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3095,7 +3095,7 @@ def _apply_standard(self, func, axis, ignore_failures=False):
elif axis == 1:
res_index = self.index
res_columns = self.columns
series_gen = ((i, Series(v, self.columns))
series_gen = ((i, Series(v, self.columns, name=i))
for i, v in izip(self.index, self.values))

results = {}
Expand Down Expand Up @@ -3125,7 +3125,8 @@ def _apply_standard(self, func, axis, ignore_failures=False):
else:
index = None

result = self._constructor(data=results, index=index)
result = self._constructor(data=results, index=index,
columns=res_index)

if axis == 1:
result = result.T
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3904,6 +3904,22 @@ def test_apply_attach_name(self):
expected = Series(self.frame.index, index=self.frame.index)
assert_series_equal(result, expected)

# non-reductions
result = self.frame.apply(lambda x: np.repeat(x.name, len(x)))
expected = DataFrame(np.tile(self.frame.columns,
(len(self.frame.index), 1)),
index=self.frame.index,
columns=self.frame.columns)
assert_frame_equal(result, expected)

result = self.frame.apply(lambda x: np.repeat(x.name, len(x)),
axis=1)
expected = DataFrame(np.tile(self.frame.index,
(len(self.frame.columns), 1)).T,
index=self.frame.index,
columns=self.frame.columns)
assert_frame_equal(result, expected)

def test_applymap(self):
applied = self.frame.applymap(lambda x: x * 2)
assert_frame_equal(applied, self.frame * 2)
Expand Down

0 comments on commit 5721a0c

Please sign in to comment.