TypeError:尝试从一个pandas dataframe列中减去另一个pandas dataframe列中的值时,不支持的操作数类型

时间:2014-06-12 01:45:41

标签: python-2.7 pandas typeerror

我正在尝试编写一个执行以下操作的函数:

  1. 查找大熊猫列的平均值。
  2. 将此值放在新列中。
  3. 查找月平均值(即所有二月份的平均值)
  4. 从年平均值中减去月平均值,并将此值放在新列中(月效)。
  5. 从月份效果中添加值'列到包含建模预测的现有列。
  6. DataFramedatetime编入索引。下面的代码会引发TypeError:unsupported operand type(s) for -: 'instancemethod' and 'float',但我不确定原因。跟踪在代码下方。有人可以建议修复吗?

    def get_RIM2(df,obs, rim):
        """df, observation, modelled value"""
        a= obs.mean #mean of whole dataset
        b= obs.median #median of whole dataset
        df.insert(len(df.columns), 'Mean', a)
        df.insert(len(df.columns), 'Median', b)
        g = obs.groupby(pd.TimeGrouper('M'))
        h = obs.groupby(pd.TimeGrouper('M'))
        g.transform('mean') # mean for each month
        h.transform('median') # median for each month
        df['Mmean'] = df['Mean'] - g.transform('mean') # month effect
        df['Mmedian'] = df['Median'] - h.transform('median') # month effect
        df['RIMs2a'] = rim + df['Mmean']
        df['RIMs2b'] = rim + df['Mmedian']
        return df
    
    get_RIM2(sve_DOC, sve_DOC['DOC_mg/L'], sve_DOC['RIMsDOC'])
    

    错误:

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-69-249d56b28c7a> in <module>()
         14     return df
         15 
    ---> 16 get_RIM2(sve_DOC, sve_DOC['DOC_mg/L'], sve_DOC['RIMsDOC'])
         17 get_RIM2(svv_DOC, svv_DOC['DOC_mg/L'], svv_DOC['RIMsDOC'])
         18 get_RIM2(svw_DOC, svw_DOC['DOC_mg/L'], svw_DOC['RIMsDOC'])
    
    <ipython-input-69-249d56b28c7a> in get_RIM2(df, obs, rim)
          8     g.transform('mean') # mean for each month
          9     h.transform('median') # median for each month
    ---> 10     df['Mmean'] = df['Mean'] - g.transform('mean')
         11     df['Mmedian'] = df['Median'] - h.transform('median')
         12     df['RIMs2a'] = rim + df['Mmean']
    
    C:\Users\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\ops.pyc in wrapper(left, right, name)
        495                     rvalues = com.take_1d(rvalues, ridx)
        496 
    --> 497             arr = na_op(lvalues, rvalues)
        498 
        499             return left._constructor(wrap_results(arr), index=index,
    
    C:\Users\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\ops.pyc in na_op(x, y)
        449                 result = np.empty(x.size, dtype=dtype)
        450                 mask = notnull(x) & notnull(y)
    --> 451                 result[mask] = op(x[mask], y[mask])
        452             else:
        453                 result = pa.empty(len(x), dtype=x.dtype)
    
    TypeError: unsupported operand type(s) for -: 'instancemethod' and 'float'
    

1 个答案:

答案 0 :(得分:1)

在初始均值/中值调用后需要括号(如下所示) - 否则您将函数分配给DataFrame,而不是它返回的值

a= obs.mean() #mean of whole dataset
b= obs.median() #median of whole dataset