Code Sample, a copy-pastable example if possible
In [1]: from collections import OrderedDict
In [2]: import pandas as pd
In [3]: pd.__version__
Out[3]: u'0.21.0'
# the following works as expected:
In [4]: df1 = pd.DataFrame([[1, 2, 3]], columns=['a', 'b', 'c'])
In [5]: df2 = pd.DataFrame(columns=['a', 'b', 'c'])
In [6]: pd.concat([df1, df2])
Out[6]:
a b c
0 1 2 3
# however, the following seems like it does an identical thing but throws an error:
In [7]: od3 = OrderedDict([('a', [1]), ('b', [2]), ('c', [3])])
In [8]: od4 = OrderedDict([('a', []), ('b', []), ('c', [])])
In [9]: df3 = pd.DataFrame(od3)
In [10]: df4 = pd.DataFrame(od4)
In [11]: pd.concat([df3, df4])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-11-ac4fae34c928> in <module>()
----> 1 pd.concat([df3, df4])
#...
ValueError: Shape of passed values is (3, 1), indices imply (3, 0)
Rewriting it in copy/pastable form...
This works:
df1 = pd.DataFrame([[1, 2, 3]], columns=['a', 'b', 'c'])
df2 = pd.DataFrame(columns=['a', 'b', 'c'])
pd.concat([df1, df2])
And this does not:
od3 = OrderedDict([('a', [1]), ('b', [2]), ('c', [3])])
od4 = OrderedDict([('a', []), ('b', []), ('c', [])])
df3 = pd.DataFrame(od3)
df4 = pd.DataFrame(od4)
pd.concat([df3, df4])
Problem description
This is also described here.
In the above code, df1
equals df3
, and df2
and df4
are both empty dataframes with the same column names (although, strangely, df2
and df4
aren't equal according to df2.equals(df4)
) but pd.concat([df1, df2])
works while pd.concat([df3, df4])
results in ValueError
. This did not happen in previous versions of Pandas, but when I upgraded to 0.21.0, it started happening.
Oddly, as the stackoverflow link notes, using the drop_duplicates()
method on either df3
or df4
(or both) results in the concat()
working, even though neither of them contains any duplicates.
Expected Output
The expected output of pd.concat([df3, df4])
is
a b c
0 1 2 3
Output of pd.show_versions()
Comment From: jreback
this was just fixed in #18191 in master
Comment From: jreback
duplicated #18178 and #18187