Skip to content

Initializing Series with dict, list of lists values and explicit dtype raises ValueError #18625

Closed
@toobaz

Description

@toobaz

Code Sample, a copy-pastable example if possible

In [2]: pd.Series([[[2], [3], [4]]], index=['a'], dtype='object')
Out[2]: 
a    [[2], [3], [4]]
dtype: object

In [3]: pd.Series({'a' : [[2], [3], [4]]}, index=['a'])
Out[3]: 
a    [[2], [3], [4]]
dtype: object

In [4]: pd.Series({'a' : [[2], [3], [4]]}, index=['a'], dtype='object')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-03acef2498da> in <module>()
----> 1 pd.Series({'a' : [[2], [3], [4]]}, index=['a'], dtype='object')

/home/nobackup/repo/pandas/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
    198                 data = data._data
    199             elif isinstance(data, dict):
--> 200                 data, index = self._init_dict(data, index, dtype)
    201                 dtype = None
    202                 copy = False

/home/nobackup/repo/pandas/pandas/core/series.py in _init_dict(self, data, index, dtype)
    275 
    276         # Input is now list-like, so rely on "standard" construction:
--> 277         s = Series(values, index=keys, dtype=dtype)
    278 
    279         # Now we just make sure the order is respected, if any

/home/nobackup/repo/pandas/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
    241                                        raise_cast_failure=True)
    242 
--> 243                 data = SingleBlockManager(data, index, fastpath=True)
    244 
    245         generic.NDFrame.__init__(self, data, fastpath=True)

/home/nobackup/repo/pandas/pandas/core/internals.py in __init__(self, block, axis, do_integrity_check, fastpath)
   4426         if not isinstance(block, Block):
   4427             block = make_block(block, placement=slice(0, len(axis)), ndim=1,
-> 4428                                fastpath=True)
   4429 
   4430         self.blocks = [block]

/home/nobackup/repo/pandas/pandas/core/internals.py in make_block(values, placement, klass, ndim, dtype, fastpath)
   2962                      placement=placement, dtype=dtype)
   2963 
-> 2964     return klass(values, ndim=ndim, fastpath=fastpath, placement=placement)
   2965 
   2966 # TODO: flexible with index=None and/or items=None

/home/nobackup/repo/pandas/pandas/core/internals.py in __init__(self, values, ndim, fastpath, placement, **kwargs)
   2086 
   2087         super(ObjectBlock, self).__init__(values, ndim=ndim, fastpath=fastpath,
-> 2088                                           placement=placement, **kwargs)
   2089 
   2090     @property

/home/nobackup/repo/pandas/pandas/core/internals.py in __init__(self, values, placement, ndim, fastpath)
    110             ndim = values.ndim
    111         elif values.ndim != ndim:
--> 112             raise ValueError('Wrong number of dimensions')
    113         self.ndim = ndim
    114 

ValueError: Wrong number of dimensions

Problem description

Yes, this is a weird use case, but

  • the first and second examples, which are very similar, work
  • the fix is easy (will push a PR)

Expected Output

Out[2]:

Output of pd.show_versions()

INSTALLED VERSIONS

commit: 6e56195
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.9.0-4-amd64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: it_IT.UTF-8
LOCALE: it_IT.UTF-8

pandas: 0.22.0.dev0+275.g6e56195fc
pytest: 3.2.3
pip: 9.0.1
setuptools: 36.7.0
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: 1.2.0dev
tables: 3.3.0
numexpr: 2.6.1
feather: 0.3.1
matplotlib: 2.0.0
openpyxl: 2.3.0
xlrd: 1.0.0
xlwt: 1.3.0
xlsxwriter: 0.9.6
lxml: 4.1.1
bs4: 4.5.3
html5lib: 0.999999999
sqlalchemy: 1.0.15
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: 0.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions