Python 熊猫会忽略reindex_轴中的copy关键字吗?

Python 熊猫会忽略reindex_轴中的copy关键字吗?,python,pandas,Python,Pandas,快速演示: import numpy as np import pandas as pd from itertools import product np.random.seed(1) team_names = ['Yankees', 'Mets', 'Dodgers'] jersey_numbers = [35, 71, 84] game_numbers = [1, 2] observer_names = ['Bill', 'John', 'Ralph'] observation_type

快速演示:

import numpy as np
import pandas as pd
from itertools import product

np.random.seed(1)

team_names = ['Yankees', 'Mets', 'Dodgers']
jersey_numbers = [35, 71, 84]
game_numbers = [1, 2]
observer_names = ['Bill', 'John', 'Ralph']
observation_types = ['Speed', 'Strength']

row_indices_1 = list(product(team_names, jersey_numbers, game_numbers, observer_names, observation_types))
observation_values_1 = np.random.randn(len(row_indices_1))

tns_1, jns_1, gns_1, ons_1, ots_1 = zip(*row_indices_1)

data_1 = pd.DataFrame({
                        'team': tns_1, 
                        'jersey': jns_1, 
                        'game': gns_1, 
                        'observer': ons_1, 
                        'obstype': ots_1, 
                        'value': observation_values_1
                    })

data_1 = data_1.set_index(['team', 'jersey', 'game', 'observer', 'obstype'])
data_1 = data_1.unstack(['observer', 'obstype'])
data_1.columns = data_1.columns.droplevel(0)

row_indices_2 = list(product(team_names, jersey_numbers))
observation_values_2 = np.random.randn(len(row_indices_2))

tns_2, jns_2, = zip(*row_indices_2)

data_2 = pd.DataFrame({'team': tns_2, 'jersey': jns_2, 'value': observation_values_2})
data_2 = data_2.set_index(['team', 'jersey'])
数据_1是一个很大的“ol”东西,行上有一个3级多索引,列上有一个2级多索引。data_2是一个“lil”的家伙,行上有一个2级多索引,数据只有一列

奇怪的行为如下:

data_1.reindex_axis(sorted(data_1.index, reverse=True), axis=0, copy=False)
这将返回行顺序翻转的数据_1。伟大的但我希望它会保持先翻洋基队,最后翻道奇队,当我随后显示数据时。但这是不对的。数据_1本身似乎不受上述命令的影响

但是,在数据_2上调用时,行为是不同的。电话:

data_2.reindex_axis(sorted(data_2.index, reverse=True), axis=0, copy=False)
根据需要显示数据_2的翻转版本。随后对data_2的调用显示,先是洋基队,最后是道奇队。与数据_1的情况完全相反


这是怎么回事?为什么索引翻转只适用于数据2而不适用于数据1?

熊猫的哪个版本?在0.12上,两个数据帧都不会被修改:除非新索引与当前索引相等且copy=false,否则将生成一个新对象。对于我来说,其中一个帧似乎实际上遵循copy=False,而另一个没有。您能为您的代码提供输出吗?对于类似data_2.reindex_axissorteddata_2.index的内容,reverse=True,axis=0,copy=False[:3]以及对于数据_2[:3]在重新索引之前和之后。对我来说,数据_2没有改变,正如预期的那样。数据_2具有正确的行为。您的数据_1得到了什么?您的数据_2已按相反顺序排列。并且在不设置copy=false的情况下调用数据_1 reindex_轴