Skip to content

Commit 72ba294

Browse files
committed
BUG: Panel.from_dict does not set dtype when specified
1 parent 3298528 commit 72ba294

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

doc/source/whatsnew/v0.17.0.txt

+1-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ Bug Fixes
6161
~~~~~~~~~
6262

6363
- Bug in ``Categorical`` repr with ``display.width`` of ``None`` in Python 3 (:issue:`10087`)
64-
65-
64+
- Bug where Panel.from_dict does not set dtype when specified (:issue:`10058`)
6665
- Bug in ``Timestamp``'s' ``microsecond``, ``quarter``, ``dayofyear``, ``week`` and ``daysinmonth`` properties return ``np.int`` type, not built-in ``int``. (:issue:`10050`)
6766
- Bug in ``NaT`` raises ``AttributeError`` when accessing to ``daysinmonth``, ``dayofweek`` properties. (:issue:`10096`)
6867

69-
70-

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
@@ -237,7 +237,8 @@ def from_dict(cls, data, intersect=False, orient='items', dtype=None):
237237
(default). Otherwise if the columns of the values of the passed
238238
DataFrame objects should be the items (which in the case of
239239
mixed-dtype data you should do), instead pass 'minor'
240-
240+
dtype : dtype, default None
241+
Data type to force, otherwise infer
241242
242243
Returns
243244
-------
@@ -1381,6 +1382,7 @@ def _homogenize_dict(self, frames, intersect=True, dtype=None):
13811382
result[key] = None
13821383

13831384
axes_dict['data'] = result
1385+
axes_dict['dtype'] = dtype
13841386
return axes_dict
13851387

13861388
@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)