本文介绍了pandas.Panel弃用警告的实际建议是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用pandas Panels生成MultiIndex pandas DataFrames的程序包.但是,每当我使用pandas.Panel时,都会收到以下DeprecationError:

I have a package that uses pandas Panels to generate MultiIndex pandas DataFrames. However, whenever I use pandas.Panel, I get the following DeprecationError:

但是,我不明白这里的第一个建议实际上是为创建MultiIndex DataFrames建议的.如果要删除Panel,我将如何使用Panel.to_frame?

However, I can't understand what the first recommendation here is actually recommending in order to create MultiIndex DataFrames. If Panel is going to be removed, how am I going to be able to use Panel.to_frame?

为了澄清:我不是在问什么是弃用,也不是如何将我的面板转换为DataFrames.我要问的是,如果我在库中使用pandas.Panel然后使用pandas.Panel.to_frame从3D ndarrays创建MultiIndex DataFrames,而Panels将被弃用,那么制作那些没有使用面板API?

To clarify: I am not asking what deprecation is, or how to convert my Panels to DataFrames. What I am asking is, if I am using pandas.Panel and then pandas.Panel.to_frame in a library to create MultiIndex DataFrames from 3D ndarrays, and Panels are going to be deprecated, then what is the best option for making those DataFrames without using the Panel API?

例如,如果我正在执行以下操作,则将X作为形状为(N,J,K)的ndarray:

Eg, if I'm doing the following, with X as a ndarray with shape (N,J,K):

p = pd.Panel(X, items=item_names, major_axis=names0, minor_axis=names1)
df = p.to_frame()

尽管这是.

推荐答案

请考虑以下面板:

data = np.random.randint(1, 10, (5, 3, 2))
pnl = pd.Panel(
    data, 
    items=['item {}'.format(i) for i in range(1, 6)], 
    major_axis=[2015, 2016, 2017], 
    minor_axis=['US', 'UK']
)

如果将其转换为DataFrame,它将变为:

If you convert this to a DataFrame, this becomes:

             item 1  item 2  item 3  item 4  item 5
major minor                                        
2015  US          9       6       3       2       5
      UK          8       3       7       7       9
2016  US          7       7       8       7       5
      UK          9       1       9       9       1
2017  US          1       8       1       3       1
      UK          6       8       8       1       6

因此,它将长轴和短轴作为行MultiIndex,将项目作为列.形状已变成(6,5),原来是(5,3,2).由您决定在哪里使用MultiIndex,但是如果您想要完全相同的形状,则可以执行以下操作:

So it takes the major and minor axes as the row MultiIndex, and items as columns. The shape has become (6, 5) which was originally (5, 3, 2). It is up to you where to use the MultiIndex but if you want the exact same shape, you can do the following:

data = data.reshape(5, 6).T
df = pd.DataFrame(
    data=data,
    index=pd.MultiIndex.from_product([[2015, 2016, 2017], ['US', 'UK']]),
    columns=['item {}'.format(i) for i in range(1, 6)]
)

产生相同的DataFrame(如果要命名索引,请使用pd.MultiIndex.from_productnames参数):

which yields the same DataFrame (use the names parameter of pd.MultiIndex.from_product if you want to name your indices):

         item 1  item 2  item 3  item 4  item 5
2015 US       9       6       3       2       5
     UK       8       3       7       7       9
2016 US       7       7       8       7       5
     UK       9       1       9       9       1
2017 US       1       8       1       3       1
     UK       6       8       8       1       6

现在使用df['item 1'](可选地df['item 1'].unstack())而不是pnl['item1 1'];使用df.xs(2015)代替pnl.xs(2015),使用df.xs('US', level=1)代替pnl.xs('US', axis='minor').

Now instead of pnl['item1 1'], you use df['item 1'] (optionally df['item 1'].unstack()); instead of pnl.xs(2015) you use df.xs(2015) and instead of pnl.xs('US', axis='minor'), you use df.xs('US', level=1).

如您所见,这只是将您最初的3D numpy数组重塑为2D的问题.您可以在MultiIndex的帮助下添加其他(人工)维度.

As you see, this is just a matter of reshaping your initial 3D numpy array to 2D. You add the other (artificial) dimension with the help of MultiIndex.

这篇关于pandas.Panel弃用警告的实际建议是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:36