|
31 | 31 | from pandas.core.indexing import _NDFrameIndexer, _maybe_droplevels
|
32 | 32 | from pandas.core.internals import (BlockManager, make_block, form_blocks,
|
33 | 33 | IntBlock)
|
34 |
| -from pandas.core.series import Series, _radd_compat |
| 34 | +from pandas.core.series import Series, _radd_compat, _dtype_from_scalar |
35 | 35 | from pandas.compat.scipy import scoreatpercentile as _quantile
|
36 | 36 | from pandas.util import py3compat
|
37 | 37 | from pandas.util.terminal import get_terminal_size
|
@@ -417,7 +417,24 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
|
417 | 417 | mgr = self._init_ndarray(data, index, columns, dtype=dtype,
|
418 | 418 | copy=copy)
|
419 | 419 | else:
|
420 |
| - raise PandasError('DataFrame constructor not properly called!') |
| 420 | + try: |
| 421 | + arr = np.array(data, dtype=dtype, copy=copy) |
| 422 | + except (ValueError, TypeError): |
| 423 | + raise PandasError('DataFrame constructor called with ' |
| 424 | + 'incompatible data and dtype') |
| 425 | + |
| 426 | + if arr.ndim == 0 and index is not None and columns is not None: |
| 427 | + if isinstance(data, basestring) and dtype is None: |
| 428 | + dtype = np.object_ |
| 429 | + if dtype is None: |
| 430 | + data, dtype = _dtype_from_scalar(data) |
| 431 | + |
| 432 | + values = np.empty((len(index), len(columns)), dtype=dtype) |
| 433 | + values.fill(data) |
| 434 | + mgr = self._init_ndarray(values, index, columns, dtype=dtype, |
| 435 | + copy=False) |
| 436 | + else: |
| 437 | + raise PandasError('DataFrame constructor not properly called!') |
421 | 438 |
|
422 | 439 | NDFrame.__init__(self, mgr)
|
423 | 440 |
|
|
0 commit comments