From 9b4d154b32f5f6436e0f204645eb610539ae9fe2 Mon Sep 17 00:00:00 2001 From: Mortada Mehyar Date: Tue, 5 May 2015 00:36:56 -0700 Subject: [PATCH] BUG: Panel.from_dict does not set dtype when specified --- doc/source/whatsnew/v0.17.0.txt | 1 + pandas/core/frame.py | 2 ++ pandas/core/panel.py | 4 +++- pandas/tests/test_panel.py | 6 ++++++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index 2a4a408643451..3d5a2697e0e84 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -63,6 +63,7 @@ Bug Fixes - Bug in ``Categorical`` repr with ``display.width`` of ``None`` in Python 3 (:issue:`10087`) +- Bug where Panel.from_dict does not set dtype when specified (:issue:`10058`) - Bug in ``Timestamp``'s' ``microsecond``, ``quarter``, ``dayofyear``, ``week`` and ``daysinmonth`` properties return ``np.int`` type, not built-in ``int``. (:issue:`10050`) - Bug in ``NaT`` raises ``AttributeError`` when accessing to ``daysinmonth``, ``dayofweek`` properties. (:issue:`10096`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 21cabdbe03951..ed01323eb9a27 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -662,6 +662,8 @@ def from_dict(cls, data, orient='columns', dtype=None): The "orientation" of the data. If the keys of the passed dict should be the columns of the resulting DataFrame, pass 'columns' (default). Otherwise if the keys should be rows, pass 'index'. + dtype : dtype, default None + Data type to force, otherwise infer Returns ------- diff --git a/pandas/core/panel.py b/pandas/core/panel.py index 778f6a22e558c..580510829baff 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -239,7 +239,8 @@ def from_dict(cls, data, intersect=False, orient='items', dtype=None): (default). Otherwise if the columns of the values of the passed DataFrame objects should be the items (which in the case of mixed-dtype data you should do), instead pass 'minor' - + dtype : dtype, default None + Data type to force, otherwise infer Returns ------- @@ -1383,6 +1384,7 @@ def _homogenize_dict(self, frames, intersect=True, dtype=None): result[key] = None axes_dict['data'] = result + axes_dict['dtype'] = dtype return axes_dict @staticmethod diff --git a/pandas/tests/test_panel.py b/pandas/tests/test_panel.py index 236bdc8a98ff4..d7d83887298b1 100644 --- a/pandas/tests/test_panel.py +++ b/pandas/tests/test_panel.py @@ -962,6 +962,12 @@ def _check_dtype(panel, dtype): panel = Panel(np.random.randn(2,10,5),items=lrange(2),major_axis=lrange(10),minor_axis=lrange(5),dtype=dtype) _check_dtype(panel,dtype) + for dtype in ['float64', 'float32', 'int64', 'int32', 'object']: + df1 = DataFrame(np.random.randn(2, 5), index=lrange(2), columns=lrange(5)) + df2 = DataFrame(np.random.randn(2, 5), index=lrange(2), columns=lrange(5)) + panel = Panel.from_dict({'a': df1, 'b': df2}, dtype=dtype) + _check_dtype(panel, dtype) + def test_constructor_fails_with_not_3d_input(self): with tm.assertRaisesRegexp(ValueError, "The number of dimensions required is 3"):