Skip to content

Commit e73e845

Browse files
mroeschkejreback
authored andcommitted
BUG: applymap on empty DataFrame returns Series (#8222) (#14977)
fix whatsnew file Adjust whatsnew to API change and add test with data
1 parent 78ba15f commit e73e845

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ Other API Changes
237237

238238
- ``CParserError`` has been renamed to ``ParserError`` in ``pd.read_csv`` and will be removed in the future (:issue:`12665`)
239239
- ``SparseArray.cumsum()`` and ``SparseSeries.cumsum()`` will now always return ``SparseArray`` and ``SparseSeries`` respectively (:issue:`12855`)
240+
- ``DataFrame.applymap()`` with an empty ``DataFrame`` will return a copy of the empty ``DataFrame`` instead of a ``Series`` (:issue:`8222`)
240241

241242
.. _whatsnew_0200.deprecations:
242243

@@ -286,7 +287,6 @@ Bug Fixes
286287
- Bug in ``DataFrame.sort_values()`` when sorting by multiple columns where one column is of type ``int64`` and contains ``NaT`` (:issue:`14922`)
287288

288289

289-
290290
- Bug in ``pd.read_msgpack()`` in which ``Series`` categoricals were being improperly processed (:issue:`14901`)
291291
- Bug in ``Series.ffill()`` with mixed dtypes containing tz-aware datetimes. (:issue:`14956`)
292292

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

+10
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,16 @@ 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+
pd.DataFrame({'A': [], 'B': [], 'C': []})]
413+
for frame in empty_frames:
414+
for func in [round, lambda x: x]:
415+
result = frame.applymap(func)
416+
tm.assert_frame_equal(result, frame)
417+
408418
def test_applymap_box(self):
409419
# ufunc will not be boxed. Same test cases as the test_map_box
410420
df = pd.DataFrame({'a': [pd.Timestamp('2011-01-01'),

0 commit comments

Comments
 (0)