Skip to content

Commit 81c7b66

Browse files
committed
BUG: applymap on empty DataFrame returns Series (#8222)
fix whatsnew file
1 parent 6bea827 commit 81c7b66

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ Bug Fixes
284284
- Bug in ``DataFrame(..).apply(to_numeric)`` when values are of type decimal.Decimal. (:issue:`14827`)
285285
- Bug in ``describe()`` when passing a numpy array which does not contain the median to the ``percentiles`` keyword argument (:issue:`14908`)
286286
- Bug in ``DataFrame.sort_values()`` when sorting by multiple columns where one column is of type ``int64`` and contains ``NaT`` (:issue:`14922`)
287-
287+
- Bug in ``applymap()`` returning a ``Series`` with an empty ``DataFrame`` (:issue:`8222`)
288288

289289

290290
- Bug in ``pd.read_msgpack()`` in which ``Series`` categoricals were being improperly processed (:issue:`14901`)

pandas/core/frame.py

+2
Original file line numberDiff line numberDiff line change
@@ -4288,6 +4288,8 @@ def applymap(self, func):
42884288

42894289
# if we have a dtype == 'M8[ns]', provide boxed values
42904290
def infer(x):
4291+
if x.empty:
4292+
return lib.map_infer(x, func)
42914293
return lib.map_infer(x.asobject, func)
42924294

42934295
return self.apply(infer)

pandas/tests/frame/test_apply.py

+9
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,15 @@ def test_applymap(self):
405405
for f in ['datetime', 'timedelta']:
406406
self.assertEqual(result.loc[0, f], str(df.loc[0, f]))
407407

408+
# GH 8222
409+
empty_frames = [pd.DataFrame(),
410+
pd.DataFrame(columns=list('ABC')),
411+
pd.DataFrame(index=list('ABC'))]
412+
for frame in empty_frames:
413+
for func in [round, lambda x: x]:
414+
result = frame.applymap(func)
415+
tm.assert_frame_equal(result, frame)
416+
408417
def test_applymap_box(self):
409418
# ufunc will not be boxed. Same test cases as the test_map_box
410419
df = pd.DataFrame({'a': [pd.Timestamp('2011-01-01'),

0 commit comments

Comments
 (0)