Skip to content

Commit ef55e60

Browse files
committed
Merge pull request #6126 from jreback/apply_issue
BUG: DataFrame.apply when using mixed datelike reductions (GH6125)
2 parents 1112cb7 + b3b1d3e commit ef55e60

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

doc/source/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ Bug Fixes
161161
are a slice (e.g. next to each other) (:issue:`6120`)
162162
- Bug in propogating _ref_locs during construction of a DataFrame with dups
163163
index/columns (:issue:`6121`)
164+
- Bug in ``DataFrame.apply`` when using mixed datelike reductions (:issue:`6125`)
165+
164166

165167
pandas 0.13.0
166168
-------------

pandas/core/frame.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3341,10 +3341,16 @@ def _apply_raw(self, func, axis):
33413341

33423342
def _apply_standard(self, func, axis, ignore_failures=False, reduce=True):
33433343

3344+
# skip if we are mixed datelike and trying reduce across axes
3345+
# GH6125
3346+
if reduce and axis==1 and self._is_mixed_type and self._is_datelike_mixed_type:
3347+
reduce=False
3348+
33443349
# try to reduce first (by default)
33453350
# this only matters if the reduction in values is of different dtype
33463351
# e.g. if we want to apply to a SparseFrame, then can't directly reduce
33473352
if reduce:
3353+
33483354
try:
33493355

33503356
# the is the fast-path

pandas/tests/test_frame.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9215,6 +9215,28 @@ def transform2(row):
92159215
self.assertEqual(e.args[1], 'occurred at index 4')
92169216
self.assertEqual(e.args[0], "'float' object has no attribute 'startswith'")
92179217

9218+
def test_apply_bug(self):
9219+
9220+
# GH 6125
9221+
import datetime
9222+
positions = pd.DataFrame([[1, 'ABC0', 50], [1, 'YUM0', 20],
9223+
[1, 'DEF0', 20], [2, 'ABC1', 50],
9224+
[2, 'YUM1', 20], [2, 'DEF1', 20]],
9225+
columns=['a', 'market', 'position'])
9226+
def f(r):
9227+
return r['market']
9228+
expected = positions.apply(f, axis=1)
9229+
9230+
positions = DataFrame([[datetime.datetime(2013, 1, 1), 'ABC0', 50],
9231+
[datetime.datetime(2013, 1, 2), 'YUM0', 20],
9232+
[datetime.datetime(2013, 1, 3), 'DEF0', 20],
9233+
[datetime.datetime(2013, 1, 4), 'ABC1', 50],
9234+
[datetime.datetime(2013, 1, 5), 'YUM1', 20],
9235+
[datetime.datetime(2013, 1, 6), 'DEF1', 20]],
9236+
columns=['a', 'market', 'position'])
9237+
result = positions.apply(f, axis=1)
9238+
assert_series_equal(result,expected)
9239+
92189240
def test_swapaxes(self):
92199241
df = DataFrame(np.random.randn(10, 5))
92209242
assert_frame_equal(df.T, df.swapaxes(0, 1))

0 commit comments

Comments
 (0)