Skip to content

ValueError when concatenating a length-one, reversed object #39401

Closed
@ahawryluk

Description

@ahawryluk

The following code should return a new Series with a single value, but raises a ValueError:

import pandas as pd
s = pd.Series([100])
pd.concat([s.iloc[::-1]])

The corresponding example with a DataFrame also raises a ValueError:

import pandas as pd
df = pd.DataFrame([100])
pd.concat([df.iloc[::-1]])

The DataFrame version can be reproduced with multiple columns, but any of the following modifications will prevent the bug:

  • create Series or DataFrame with len > 1
  • concatenate more than one object
  • remove the reversing operation .iloc[::-1]
  • pass ignore_index=True to pd.concat

This bug appeared as an edge case in a function that usually handles DataFrames with multiple rows.

Traceback for the Series example
C:\Anaconda3\lib\site-packages\pandas\core\reshape\concat.py in concat(objs, axis, join, ignore_index, keys, levels, names, verify_integrity, sort, copy)
    296     )
    297
--> 298     return op.get_result()
    299
    300

C:\Anaconda3\lib\site-packages\pandas\core\reshape\concat.py in get_result(self)
    483
    484                 res = concat_compat(arrs, axis=0)
--> 485                 result = cons(res, index=self.new_axes[0], name=name, dtype=res.dtype)
    486                 return result.__finalize__(self, method="concat")
    487

C:\Anaconda3\lib\site-packages\pandas\core\series.py in __init__(self, data, index, dtype, name, copy, fastpath)
    320                     if len(index) != len(data):
    321                         raise ValueError(
--> 322                             f"Length of passed values is {len(data)}, "
    323                             f"index implies {len(index)}."
    324                         )

ValueError: Length of passed values is 1, index implies 0.
Traceback for the DataFrame example
C:\Anaconda3\lib\site-packages\pandas\core\reshape\concat.py in concat(objs, axis, join, ignore_index, keys, levels, names, verify_integrity, sort, copy)
    296     )
    297
--> 298     return op.get_result()
    299
    300

C:\Anaconda3\lib\site-packages\pandas\core\reshape\concat.py in get_result(self)
    519
    520             new_data = concatenate_block_managers(
--> 521                 mgrs_indexers, self.new_axes, concat_axis=self.bm_axis, copy=self.copy
    522             )
    523             if not self.copy:

C:\Anaconda3\lib\site-packages\pandas\core\internals\concat.py in concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy)
     87         blocks.append(b)
     88
---> 89     return BlockManager(blocks, axes)
     90
     91

C:\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in __init__(self, blocks, axes, do_integrity_check)
    141
    142         if do_integrity_check:
--> 143             self._verify_integrity()
    144
    145         # Populate known_consolidate, blknos, and blklocs lazily

C:\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in _verify_integrity(self)
    321         for block in self.blocks:
    322             if block.shape[1:] != mgr_shape[1:]:
--> 323                 raise construction_error(tot_items, block.shape[1:], self.axes)
    324         if len(self.items) != tot_items:
    325             raise AssertionError(

ValueError: Shape of passed values is (1, 1), indices imply (0, 1)
pd.show_versions()
INSTALLED VERSIONS
------------------
commit           : 9d598a5e1eee26df95b3910e3f2934890d062caa
python           : 3.8.5.final.0
python-bits      : 64
OS               : Windows
OS-release       : 10
Version          : 10.0.17763
machine          : AMD64
processor        : Intel64 Family 6 Model 63 Stepping 2, GenuineIntel
byteorder        : little
LC_ALL           : None
LANG             : None
LOCALE           : English_Canada.1252

pandas           : 1.2.1
numpy            : 1.19.2
pytz             : 2020.5
dateutil         : 2.8.1
pip              : 20.3.3
setuptools       : 51.3.3.post20210118
Cython           : 0.29.21
pytest           : 6.2.1
hypothesis       : None
sphinx           : 3.4.3
blosc            : None
feather          : None
xlsxwriter       : 1.3.7
lxml.etree       : 4.6.2
html5lib         : 1.1
pymysql          : None
psycopg2         : None
jinja2           : 2.11.2
IPython          : 7.19.0
pandas_datareader: None
bs4              : 4.9.3
bottleneck       : 1.3.2
fsspec           : 0.8.3
fastparquet      : None
gcsfs            : None
matplotlib       : 3.3.2
numexpr          : 2.7.2
odfpy            : None
openpyxl         : 3.0.6
pandas_gbq       : None
pyarrow          : None
pyxlsb           : None
s3fs             : None
scipy            : 1.5.2
sqlalchemy       : 1.3.21
tables           : 3.6.1
tabulate         : None
xarray           : None
xlrd             : 2.0.1
xlwt             : 1.3.0
numba            : 0.51.2

Metadata

Metadata

Assignees

Labels

ReshapingConcat, Merge/Join, Stack/Unstack, Explode

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions