RAY
RAY

Reputation: 7100

pandas.Panel(data=list_of_dfs) broken?

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

print(np.__version__)
print(pd.__version__)

d1 = pd.DataFrame({'Name': [1, 1, 1, 1, 1],'number': [1, 1, 1, 1, 1]})
d2 = pd.DataFrame({'Name': [1, 1, 1, 1, 1], 'number': [1, 1, 1, 1, 1]}) 

x=[d1,d2]
pd.Panel(x, items=[1,2])

Error:

<python dir>\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
    472 
    473     """
--> 474     return array(a, dtype, copy=False, order=order)
    475 
    476 def asanyarray(a, dtype=None, order=None):

ValueError: cannot copy sequence with size 5 to array axis with dimension 2

I think it is related to the following post, but I'm not sure what the best workaround is: ValueError: cannot copy sequence with size 5 to array axis with dimension 2

Upvotes: 1

Views: 111

Answers (1)

Hei
Hei

Reputation: 70

According to the doc, the Panel constructor only takes ndarray or dict of DataFrame. You may want to convert the list of DataFrame into dict first.

import numpy as np
import pandas as pd

print(np.__version__)
print(pd.__version__)

d1 = pd.DataFrame({'Name': [1, 1, 1, 1, 1],'number': [1, 1, 1, 1, 1]})
d2 = pd.DataFrame({'Name': [1, 1, 1, 1, 1], 'number': [1, 1, 1, 1, 1]})

x = dict(enumerate([d1,d2], 1))
pd.Panel(x)

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Panel.html#pandas.Panel

Upvotes: 3

Related Questions