cannot concatenate unaligned mixed dimensional NDFrame objects
Package:
pandas
30911

Exception Class:
ValueError
Raise code
in objs:
ndim = obj.ndim
if ndim == max_ndim:
pass
elif ndim != max_ndim - 1:
raise ValueError(
"cannot concatenate unaligned mixed "
"dimensional NDFrame objects"
)
else:
name = getattr(obj, "name", None)
if ignore_index or name is None:
Links to the raise (1)
https://github.com/pandas-dev/pandas/blob/b3e335254f46a526ee3ce9bb757eac4011d9d1fe/pandas/core/reshape/concat.py#L437Ways to fix
pd.concat concatenates pandas objects along a particular axis with optional set logic along the other axes.
It's possible to concatenate Series or DataFrame each other. Depend on their dimension, the operation can be done. But when trying to concatenate mixed dimensional NDFrame object, it becomes an error. Concatenate of the sequential dimensional object is allowed.
Like
import pandas as pd
df1 = pd.DataFrame([['a', 1], ['b', 2]],
columns=['letter', 'number'])
df2 = pd.DataFrame([['c', 3], ['d', 4]],
columns=['letter', 'number'])
conci = pd.concat([df1,df2],axis=1)
print(conci)
But if you want to try to concatenate 1D
and 3D
DataFrame, that will cause an error.
elif ndim != max_ndim - 1:
raise ValueError(
"cannot concatenate unaligned mixed "
"dimensional NDFrame objects"
)
When max_ndim
(maximum dimension in list of DataFrame or Series) is 3 and ndim
(current object which is iterated) is 1, the result will be 1!=2 i.e return to True and will be thrown an error.
Add a possible fix
Please authorize to post fix