Skip to content

Commit 8f20d74

Browse files
committed
BUG: applymap on empty DataFrame returns Series (pandas-dev#8222)
1 parent 6bea827 commit 8f20d74

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-17
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,10 @@ 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`)
291-
- Bug in ``Series.ffill()`` with mixed dtypes containing tz-aware datetimes. (:issue:`14956`)
292291

293292

294293

@@ -301,20 +300,5 @@ Bug Fixes
301300

302301
- Bug in ``Series.unique()`` in which unsigned 64-bit integers were causing overflow (:issue:`14721`)
303302
- Bug in ``pd.unique()`` in which unsigned 64-bit integers were causing overflow (:issue:`14915`)
304-
305-
306-
307-
308-
309-
310-
311-
312-
313-
314-
315-
316-
317-
318303
- Require at least 0.23 version of cython to avoid problems with character encodings (:issue:`14699`)
319304
- Bug in converting object elements of array-like objects to unsigned 64-bit integers (:issue:`4471`)
320-
- Bug in ``pd.pivot_table()`` where no error was raised when values argument was not in the columns (:issue:`14938`)

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)