GVKun编程网logo

为什么我不能在熊猫函数中应用shift?(熊猫绘画为什么添加不了图层)

3

如果您想了解为什么我不能在熊猫函数中应用shift?和熊猫绘画为什么添加不了图层的知识,那么本篇文章将是您的不二之选。我们将深入剖析为什么我不能在熊猫函数中应用shift?的各个方面,并为您解答熊猫绘

如果您想了解为什么我不能在熊猫函数中应用shift?熊猫绘画为什么添加不了图层的知识,那么本篇文章将是您的不二之选。我们将深入剖析为什么我不能在熊猫函数中应用shift?的各个方面,并为您解答熊猫绘画为什么添加不了图层的疑在这篇文章中,我们将为您介绍为什么我不能在熊猫函数中应用shift?的相关知识,同时也会详细的解释熊猫绘画为什么添加不了图层的运用方法,并给出实际的案例分析,希望能帮助到您!

本文目录一览:

为什么我不能在熊猫函数中应用shift?(熊猫绘画为什么添加不了图层)

为什么我不能在熊猫函数中应用shift?(熊猫绘画为什么添加不了图层)

我试图建立一个使用.shift()的函数,但它给了我一个错误。考虑一下:

In [40]:data={''level1'':[20,19,20,21,25,29,30,31,30,29,31],      ''level2'': [10,10,20,20,20,10,10,20,20,10,10]}index= pd.date_range(''12/1/2014'', periods=11)frame=DataFrame(data, index=index)frameOut[40]:            level1 level22014-12-01  20  102014-12-02  19  102014-12-03  20  202014-12-04  21  202014-12-05  25  202014-12-06  29  102014-12-07  30  102014-12-08  31  202014-12-09  30  202014-12-10  29  102014-12-11  31  10

正常功能可以正常工作。为了演示我使用直接和函数方法两次计算相同的结果:

In [63]:frame[''horizontaladd1'']=frame[''level1'']+frame[''level2'']#worksdef horizontaladd(x):    test=x[''level1'']+x[''level2'']    return testframe[''horizontaladd2'']=frame.apply(horizontaladd, axis=1)frameOut[63]:            level1 level2 horizontaladd1 horizontaladd22014-12-01  20  10  30  302014-12-02  19  10  29  292014-12-03  20  20  40  402014-12-04  21  20  41  412014-12-05  25  20  45  452014-12-06  29  10  39  392014-12-07  30  10  40  402014-12-08  31  20  51  512014-12-09  30  20  50  502014-12-10  29  10  39  392014-12-11  31  10  41  41

但是当直接应用shift可以工作时,在一个函数中它不起作用:

frame[''verticaladd1'']=frame[''level1'']+frame[''level1''].shift(1)#worksdef verticaladd(x):    test=x[''level1'']+x[''level1''].shift(1)    return testframe.apply(verticaladd)#error

结果是

KeyError: (''level1'', u''occurred at index level1'')

我还尝试将单个列应用于我认为更有意义的一列,但是没有运气:

def verticaladd2(x):    test=x-x.shift(1)    return testframe[''level1''].map(verticaladd2)#error, also with apply

错误:

AttributeError: ''numpy.int64'' object has no attribute ''shift''

为什么不直接打电话接班?我需要将其嵌入到一个函数中以沿轴1同时计算多个列。请参阅相关问题布尔逻辑的含糊真值

答案1

小编典典

尝试将框架传递给函数,而不是使用apply(我不确定为什么apply行不通,即使按列也不知道):

def f(x):    x.level1     return x.level1 + x.level1.shift(1)f(frame)

返回:

2014-12-01   NaN2014-12-02    392014-12-03    392014-12-04    412014-12-05    462014-12-06    542014-12-07    592014-12-08    612014-12-09    612014-12-10    592014-12-11    60Freq: D, Name: level1, dtype: float64

bash – 为什么我不能在shell中定义一个空函数?

bash – 为什么我不能在shell中定义一个空函数?

我在学习bash.我不小心遇到了空函数的语法错误.

#!/bin/bash
# script name : empty_function.sh
function empty_func() {
}

bash empty_function.sh

empty_function.sh: line 3: Syntax error near unexpected token `}'
empty_function.sh: line 3: `}'

我想这是因为空函数的定义.
 我想知道为什么我不能定义一个空函数?

解决方法

bash shell’s grammar根本不允许空功能.函数的语法是:

name () compound-command [redirection]
  function name [()] compound-command [redirection]

并在以下形式的复合命令中:

{ list; }

列表不能为空.你可以得到的最接近的是使用null语句或返回:

function empty_func() {
    : 
}

要么

function empty_func() {
    return
}

c# – 为什么我不能在out参数中传递未分配的对象变量然后分配它

c# – 为什么我不能在out参数中传递未分配的对象变量然后分配它

@H_301_1@
在C#中,为什么我不能在out参数中传递未分配的对象变量然后分配它?

如果我尝试这样做,则会出现编译器错误:“本地变量< xyz>不能在此范围内声明,因为它会给< xyz> …带来不同的含义”

例如.

void MyMethod(int x,out MyObject mo) {  **MyObject** mo = new MyObject(); }
// in some other scope:
MyObject mo;
MyMethod(1,out mo);

编辑:我现在可以看到我的错误.我已将上述代码更改为我的代码.星号中的MyObject不应该存在.

解决方法

您发布的原始代码不正确,但现在我们发现问题实际上在这里:

void MyMethod(int x,out MyObject mo)
{
    MyObject mo = new MyObject();
    // should be:
    // mo = new MyObject();
}

您正在创建一个新的局部变量mo,它“隐藏”参数mo.

很高兴我们最终到达那里:-)

c# – 为什么我不能在字节上做布尔逻辑?

c# – 为什么我不能在字节上做布尔逻辑?

在C#(3.5)中,我尝试以下方法:
byte byte1 = 0x00;
byte byte2 = 0x00;
byte byte3 = byte1 & byte2;

我得到错误132:“不能隐式地将类型’int’转换为’byte’.存在显式转换(你是否错过了演员?)”. |也是如此和^.

我究竟做错了什么?为什么要问我关于整数?为什么我不能在字节上做布尔逻辑?

解决方法

不会为字节声明各种运算符 – 两个操作数都被提升为int,结果为int.例如,添加:
byte byte1 = 0x00;
byte byte2 = 0x00;
byte byte3 = byte1 + byte2; // Compilation error

请注意,复合赋值确实有效:

byte1 += byte2;

有一个recent SO question on this.我同意这对于按位操作特别令人厌烦,但结果应始终是相同的大小,并且它是逻辑上完全有效的操作.

作为解决方法,您可以将结果转换回字节:

byte byte3 = (byte) (byte1 & byte2);

delphi – 为什么我不能在for循环中使用Int64?

delphi – 为什么我不能在for循环中使用Int64?

我可以为整数值编写.do进程..
但我无法为int64值写它.
例如:
var
  i:int64;
begin
  for i:=1 to 1000 do 
end;

编译器拒绝编译它,为什么拒绝?

解决方法

Delphi编译器还不支持Int64循环计数器.

今天关于为什么我不能在熊猫函数中应用shift?熊猫绘画为什么添加不了图层的介绍到此结束,谢谢您的阅读,有关bash – 为什么我不能在shell中定义一个空函数?、c# – 为什么我不能在out参数中传递未分配的对象变量然后分配它、c# – 为什么我不能在字节上做布尔逻辑?、delphi – 为什么我不能在for循环中使用Int64?等更多相关知识的信息可以在本站进行查询。

本文标签: