Skip to content

Commit 9b4d154

Browse files
committed
BUG: Panel.from_dict does not set dtype when specified
1 parent d03a22f commit 9b4d154

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

doc/source/whatsnew/v0.17.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Bug Fixes
6363
- Bug in ``Categorical`` repr with ``display.width`` of ``None`` in Python 3 (:issue:`10087`)
6464

6565

66+
- Bug where Panel.from_dict does not set dtype when specified (:issue:`10058`)
6667
- Bug in ``Timestamp``'s' ``microsecond``, ``quarter``, ``dayofyear``, ``week`` and ``daysinmonth`` properties return ``np.int`` type, not built-in ``int``. (:issue:`10050`)
6768
- Bug in ``NaT`` raises ``AttributeError`` when accessing to ``daysinmonth``, ``dayofweek`` properties. (:issue:`10096`)
6869

pandas/core/frame.py

+2
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,8 @@ def from_dict(cls, data, orient='columns', dtype=None):
662662
The "orientation" of the data. If the keys of the passed dict
663663
should be the columns of the resulting DataFrame, pass 'columns'
664664
(default). Otherwise if the keys should be rows, pass 'index'.
665+
dtype : dtype, default None
666+
Data type to force, otherwise infer
665667
666668
Returns
667669
-------

pandas/core/panel.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ def from_dict(cls, data, intersect=False, orient='items', dtype=None):
239239
(default). Otherwise if the columns of the values of the passed
240240
DataFrame objects should be the items (which in the case of
241241
mixed-dtype data you should do), instead pass 'minor'
242-
242+
dtype : dtype, default None
243+
Data type to force, otherwise infer
243244
244245
Returns
245246
-------
@@ -1383,6 +1384,7 @@ def _homogenize_dict(self, frames, intersect=True, dtype=None):
13831384
result[key] = None
13841385

13851386
axes_dict['data'] = result
1387+
axes_dict['dtype'] = dtype
13861388
return axes_dict
13871389

13881390
@staticmethod

pandas/tests/test_panel.py

+6
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,12 @@ def _check_dtype(panel, dtype):
962962
panel = Panel(np.random.randn(2,10,5),items=lrange(2),major_axis=lrange(10),minor_axis=lrange(5),dtype=dtype)
963963
_check_dtype(panel,dtype)
964964

965+
for dtype in ['float64', 'float32', 'int64', 'int32', 'object']:
966+
df1 = DataFrame(np.random.randn(2, 5), index=lrange(2), columns=lrange(5))
967+
df2 = DataFrame(np.random.randn(2, 5), index=lrange(2), columns=lrange(5))
968+
panel = Panel.from_dict({'a': df1, 'b': df2}, dtype=dtype)
969+
_check_dtype(panel, dtype)
970+
965971
def test_constructor_fails_with_not_3d_input(self):
966972
with tm.assertRaisesRegexp(ValueError,
967973
"The number of dimensions required is 3"):

0 commit comments

Comments
 (0)