From 255db8e8e65ccb51497379dea12f318fa503da89 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 23 Dec 2016 18:42:33 -0800 Subject: [PATCH] BUG: applymap on empty DataFrame returns Series (#8222) fix whatsnew file Adjust whatsnew to API change and add test with data --- doc/source/whatsnew/v0.20.0.txt | 2 +- pandas/core/frame.py | 2 ++ pandas/tests/frame/test_apply.py | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 4799d2711231b..40bd8bc4154a6 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -237,6 +237,7 @@ Other API Changes - ``CParserError`` has been renamed to ``ParserError`` in ``pd.read_csv`` and will be removed in the future (:issue:`12665`) - ``SparseArray.cumsum()`` and ``SparseSeries.cumsum()`` will now always return ``SparseArray`` and ``SparseSeries`` respectively (:issue:`12855`) +- ``DataFrame.applymap()`` with an empty ``DataFrame`` will return a copy of the empty ``DataFrame`` instead of a ``Series`` (:issue:`8222`) .. _whatsnew_0200.deprecations: @@ -286,7 +287,6 @@ Bug Fixes - Bug in ``DataFrame.sort_values()`` when sorting by multiple columns where one column is of type ``int64`` and contains ``NaT`` (:issue:`14922`) - - Bug in ``pd.read_msgpack()`` in which ``Series`` categoricals were being improperly processed (:issue:`14901`) - Bug in ``Series.ffill()`` with mixed dtypes containing tz-aware datetimes. (:issue:`14956`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 7305df0f57736..ba1e08ecc482f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4288,6 +4288,8 @@ def applymap(self, func): # if we have a dtype == 'M8[ns]', provide boxed values def infer(x): + if x.empty: + return lib.map_infer(x, func) return lib.map_infer(x.asobject, func) return self.apply(infer) diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index 5cadb4dba577f..9e68b7e76d78f 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -405,6 +405,16 @@ def test_applymap(self): for f in ['datetime', 'timedelta']: self.assertEqual(result.loc[0, f], str(df.loc[0, f])) + # GH 8222 + empty_frames = [pd.DataFrame(), + pd.DataFrame(columns=list('ABC')), + pd.DataFrame(index=list('ABC')), + pd.DataFrame({'A': [], 'B': [], 'C': []})] + for frame in empty_frames: + for func in [round, lambda x: x]: + result = frame.applymap(func) + tm.assert_frame_equal(result, frame) + def test_applymap_box(self): # ufunc will not be boxed. Same test cases as the test_map_box df = pd.DataFrame({'a': [pd.Timestamp('2011-01-01'),