【问题标题】:"IndexError: Index X is out of bounds" generated when converting 3.X python script to 2.7将 3.X python 脚本转换为 2.7 时生成“IndexError: Index X is out of bounds”
【发布时间】:2017-06-28 19:19:10
【问题描述】:

我为 3.X 编写了一个运行良好的脚本,但是我需要将其转换为 2.7,并且在这样做时遇到了这个错误,我不知道如何解决。

此函数通过将数据划分为多个周期并为每个周期找到一个百分位数以应用于所述数据来更正数据。

import numpy
import math
import random
from bokeh.plotting import *
from bokeh.layouts import *
from __future__ import division

def rs_percent_corr(start, end, rs, rso, thresh, period):

num_periods = int(math.ceil((end - start) / period))
rs_period = numpy.zeros(period)
rso_period = numpy.zeros(period)
period_corr = numpy.zeros(num_periods)

# Placing intervals in separate array for easy handling
rs_interval = numpy.array(rs[start:end])
rso_interval = numpy.array(rso[start:end])

# separate the interval into predefined periods and compute correction
count_one = 0  # index for full correction interval
count_two = 0  # index for within each period
count_three = 0  # index for number of periods
while count_one < len(rs_interval):
    if (count_two < period) and count_one == len(rs_interval) - 1:
        # if statement handles final period
        rs_period[count_two] = rs_interval[count_one]
        rso_period[count_two] = rso_interval[count_one]
        count_one += 1
        count_two += 1
        while count_two < period:
            # This fills out the rest of the final period with NaNs so
            # they are not impacted by the remaining zeros
            rs_period[count_two] = numpy.nan
            rso_period[count_two] = numpy.nan
            count_two += 1

        ratio = numpy.divide(rs_period, rso_period)
        period_corr[count_three] = numpy.nanpercentile(ratio, thresh)

    elif count_two < period:
        # haven't run out of data points, and period still hasn't been filled
        rs_period[count_two] = rs_interval[count_one]
        rso_period[count_two] = rso_interval[count_one]
        count_one += 1
        count_two += 1
    else:
        # end of a period
        count_two = 0
        ratio = numpy.divide(rs_period, rso_period)
        period_corr[count_three] = numpy.nanpercentile(ratio, thresh)
        count_three += 1

return period_corr

在 3.X 中运行脚本时它可以工作,但尝试在 2.7 中运行它会在 period_corr[count_three] = numpy.nanpercentile(ratio, thresh) 行上生成“IndexError: Index 110 is out of bounds for axis 0 with size 110”

我错过了什么?提前感谢您的宝贵时间。

【问题讨论】:

  • 您可以编辑您的问题并添加完整的回溯吗?

标签: python-2.7 python-3.x numpy


【解决方案1】:

罪魁祸首是num_periods = int(math.ceil((end - start) / period))这一行。

在 Python 3 中,/ 是真正的除法。 3 / 2 将返回 1.5。在 Python 2 中情况并非如此,/ 执行整数除法,因此 3/2 将返回 1

如果您需要同时支持两个版本,您可以在脚本的第一行插入from __future__ import division,那么 Python 2 / 的行为将类似于 Python 3。 p>

【讨论】:

  • 我刚刚注意到 OP 已经使用了from __future__ import division ...
  • 你是对的,但有趣的是我已经有了未来的导入行,但它给我错误的原因是因为我正在运行的脚本不是t 带有该行的脚本版本,所以我真正的问题是版本/工作区管理!感谢您的帮助。我没有运行正确的脚本的真正提示应该是我没有得到一个 SyntaxError ,即 from future import division 需要位于文件的开头。
  • @Jean-FrançoisFabre 我怀疑 OP 在事后添加了它,因为 __future__ 导入必须是第一个 import 否则它是 SyntaxError
  • @SolIV 另见我上面的评论
  • @SolIV 也许您应该编辑您的问题以显示实际脚本,而不是带有错误“来自未来”导入的脚本,因为它误导了我们。
猜你喜欢
  • 2021-05-07
  • 2018-03-26
  • 2017-04-09
  • 2020-05-22
  • 1970-01-01
  • 2021-12-17
  • 2021-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多