令人困惑的熊猫小组行为

时间:2015-08-30 23:43:58

标签: python pandas

运行后

import numpy as np 
randn = np.random.randn
from pandas import *

以下代码创建了一个面板:

## Create pseudo panel data ##
numindivs = 2 # Number of individuals
numwaves = 5 # Number of time periods (waves)

# Create panel object
wp = Panel(np.random.randint(12, size=(numindivs, numwaves, 4)), minor_axis=['background', 'lnH', 'F', 'T'])

# Generate random fixed background variable for each indiv: N(2,1)
wp.ix[:,:,'background'] = np.tile(randn(numindivs)+2,(numwaves,1))

# Generate lnF and lnT
wp.ix[:,:,'lnF'] = np.log(wp[:,:,'F']+1)
wp.ix[:,:,'lnT'] = np.log(wp[:,:,'T']+1)

然而,以下代码除了更改倒数第二部分中“背景”列的内容外,都是相同的,会产生错误(“ValueError:无法将形状(2)中的输入数组广播为形状(5) “):

## Create pseudo panel data ##
numindivs = 2 # Number of individuals
numwaves = 5 # Number of time periods (waves)

# Create panel object
wp = Panel(np.random.randint(12, size=(numindivs, numwaves, 4)), minor_axis=['background', 'lnH', 'F', 'T'])

# Generate lnF and lnT
wp.ix[:,:,'lnF'] = np.log(wp[:,:,'F']+1)
wp.ix[:,:,'lnT'] = np.log(wp[:,:,'T']+1)

这是非常奇怪的行为。有人可以解释发生了什么吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

这有点奇怪,可能与你分配时强制dtypes的方式有关。您可以报告问题here

看起来直接分配似乎没有问题但是:

 wp['lnF'] = np.log(wp[:,:,'F']+1)