Cofeinnie Bonda Cofeinnie Bonda - 3 months ago 8
Python Question

pd.DataFrame() fails when combining 2 lists

I have 2 lists like below while my lists actually have 3000 elements:

a=[numpy.datetime64('2004-04-12T08:00:00.000000000+0800'),numpy.datetime64('2004-04-12T08:00:00.000000000+0800'),numpy.datetime64('2004-04-12T08:00:00.000000000+0800')]
b=[1,2,3]


When I tried to combine them to form a dataframe with 2 columns:
c=pd.DataFrame([pd.Series(a), pd.Series(b)])

It says:
TypeError: invalid type promotion


How can I solve this problem?

Answer

Try:

c = pd.DataFrame(data={'a':a,'b':b})

You're getting the error because you are trying to create a pandas dataframe by putting two lists together into one column, while the data types of the elements in those lists don't match. Basically, you're trying to put dates and integers into the same column.

I hope this helps.