【问题标题】:TypeError: can't multiply sequence by non-int of type 'float' (python 2.7)TypeError:不能将序列乘以“float”类型的非整数(python 2.7)
【发布时间】:2017-02-04 02:38:06
【问题描述】:

我有一个数据框 t_unit,它是 pd.read_csv() 函数的结果。

datetime    B18_LR_T    B18_B1_T
24/03/2016 09:00    21.274  21.179
24/03/2016 10:00    19.987  19.868
24/03/2016 11:00    21.632  21.417
24/03/2016 12:00    26.285  24.779
24/03/2016 13:00    26.897  24.779

我正在重新采样数据帧以使用代码计算第 5 个和第 05 个百分位数:

keys_actual = list(t_unit.columns.values)

for key in keys_actual:
    ts_wk = t_unit[key].resample('W-MON')
    ts_wk_05p = ts_wk.apply(lambda x: x.quantile(0.05)).round(decimals=1).rename(key+'_05p', inplace=True)
    ts_wk_95p = ts_wk.apply(lambda x: x.quantile(0.95)).round(decimals=1).rename(key+'_95p', inplace=True) 

一切正常,但是当我通过pd.concat 向我的数据框添加一列时:

datetime    B18_LR_T    B18_B1_T    ext_T
24/03/2016 09:00    21.274  21.179  6.9
24/03/2016 10:00    19.987  19.868  7.5
24/03/2016 11:00    21.632  21.417  9.1
24/03/2016 12:00    26.285  24.779  9.9
24/03/2016 13:00    26.897  24.779  9.2

ts_wk_05p = ts_wk.apply(lambda x: x.quantile(0.05)).round(decimals=1).rename(key+'_05p', inplace=True)

TypeError: 不能将序列乘以“float”类型的非整数

你知道为什么吗?

【问题讨论】:

  • 什么是key?您的代码有一部分没有显示?
  • 抱歉,为了让问题保持简单,我没有包含部分代码。现已添加密钥
  • 这是一个 mcve stackoverflow.com/help/mcve 用于该类型错误:'a'*1.0。回溯会告诉您执行等效操作的行。使用调试器或在之前添加打印语句来微调序列和浮点的实际值。
  • @TerryJanReedy 看不到该 TypeError 的 mcve。我的回溯发现了File "C:/ENVIDA/_code/T1_wk-mo_stats.py", line 82, in <lambda> ts_wk_05p = ts_wk.apply(lambda x: x.quantile(0.05)).round(decimals=1).rename(key+'_05p', inplace=True) File "C:\Users\andrea.botti\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\series.py", line 1343, in quantile dropna=True) 这是否取决于ext_T 列有一些缺失数据的事实(而另一个没有)?
  • 编辑您的问题以包含完整的回溯。分析时,请记住,回溯只包括跨越多条物理线路的逻辑线路的最后一条物理线路。 dropna=True) 显然是一个续行(在函数quantile 中,错误必须在它上面的某个地方。“它取决于...?”也许,但首先找到标记的表达式。

标签: python pandas time-series resampling quantile


【解决方案1】:

有一些列不是数字的问题。 你可以查看dtypes

print (t_unit.dtypes)
B18_LR_T    float64
B18_B1_T    float64
ext_T        object
dtype: object

然后尝试先通过astype转换为数字:

t_unit.ext_T = t_unit.ext_T.astype(float)

如果:

ValueError: 无法将字符串转换为浮点数

然后使用to_numeric 和参数errors='coerce' 将坏数据转换为NaN

t_unit.ext_T = pd.to_numeric(t_unit.ext_T, errors='coerce')

所有代码:

#simulate string column
t_unit.ext_T = t_unit.ext_T.astype(str)
print (t_unit.dtypes)
B18_LR_T    float64
B18_B1_T    float64
ext_T        object
dtype: object

#convert to float
t_unit.ext_T = t_unit.ext_T.astype(float)

print (t_unit)

L = []
for key in t_unit.columns:
    ts_wk = t_unit[key].resample('W-MON')
    #remove inplace=True
    ts_wk_05p = ts_wk.apply(lambda x: x.quantile(0.05)).round(decimals=1).rename(key+'_05p')
    ts_wk_95p = ts_wk.apply(lambda x: x.quantile(0.95)).round(decimals=1).rename(key+'_95p') 
    L.append(ts_wk_05p)
    L.append(ts_wk_95p)


print (pd.concat(L, axis=1))
            B18_LR_T_05p  B18_LR_T_95p  B18_B1_T_05p  B18_B1_T_95p  ext_T_05p  \
datetime                                                                        
2016-03-28          20.2          26.8          20.1          24.8        7.0   

            ext_T_95p  
datetime               
2016-03-28        9.8  

【讨论】:

猜你喜欢
  • 2019-03-21
  • 2010-12-30
  • 2012-10-09
  • 1970-01-01
  • 2013-04-22
  • 2013-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多