Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
toobaz opened this issue Dec 4, 2017 · 1 comment · Fixed by #29339
Closed
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@toobaz
Copy link
Member

toobaz commented Dec 4, 2017

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

@jreback jreback added Bug Dtype Conversions Unexpected or buggy dtype conversions Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Dec 5, 2017
@jreback jreback added this to the 0.22.0 milestone Dec 5, 2017
@jreback jreback modified the milestones: 0.23.0, Next Major Release Apr 14, 2018
@jbrockmendel jbrockmendel added the Constructors Series/DataFrame/Index/pd.array Constructors label Jul 23, 2019
@mroeschke
Copy link
Member

Looks fixed on master. Could use a test

In [58]: pd.__version__
Out[58]: '0.26.0.dev0+565.g8c5941cd5'

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

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug Constructors Series/DataFrame/Index/pd.array Constructors Difficulty Intermediate Dtype Conversions Unexpected or buggy dtype conversions Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Oct 15, 2019
@jreback jreback modified the milestones: Contributions Welcome, 1.0 Nov 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
4 participants