首页 > 解决方案 > ValueError:值的长度 (1) 与索引索引的长度不匹配 (12797) - 索引长度相同

问题描述

所以这让我发疯了,因为我真的没有看到问题所在。

我有以下代码:

    dataframe.to_csv(f"user_data/candle_data.csv")
    print (dataframe)

    st12 = self.supertrend(dataframe, 3, 12)
    st12['ST'].to_csv(f"user_data/st12.csv")
    print (st12)

    print(dataframe.index.difference(st12.index))

    dataframe.loc[:, 'st_12'] = st12['ST'], 

检查csv文件,我可以看到第一个索引是0,最后一个索引是12796. 最后一行也在行号上12798。这两个文件都是如此。

三者的输出print如下

                           date     open     high      low    close       volume
0     2020-12-29 21:45:00+00:00   723.33   726.14   723.26   725.05   3540.48612
1     2020-12-29 22:00:00+00:00   725.17   728.77   723.78   726.94   3983.90892
2     2020-12-29 22:15:00+00:00   726.94   727.30   724.72   724.75   3166.57435
3     2020-12-29 22:30:00+00:00   724.94   725.99   723.80   725.91   2848.08122
4     2020-12-29 22:45:00+00:00   725.99   730.30   725.95   729.64   6288.69499
...                         ...      ...      ...      ...      ...          ...
12792 2021-05-12 03:45:00+00:00  4292.42  4351.85  4292.35  4332.81  24410.30155
12793 2021-05-12 04:00:00+00:00  4332.12  4347.60  4300.07  4343.05  16545.66776
12794 2021-05-12 04:15:00+00:00  4342.84  4348.00  4305.87  4313.82  10048.32828
12795 2021-05-12 04:30:00+00:00  4313.82  4320.68  4273.35  4287.49  13201.88547
12796 2021-05-12 04:45:00+00:00  4287.49  4306.79  4276.87  4300.80   9663.73327

[12797 rows x 6 columns]
                ST  STX
0         0.000000  nan
1         0.000000  nan
2         0.000000  nan
3         0.000000  nan
4         0.000000  nan
...            ...  ...
12792  4217.075684   up
12793  4217.075684   up
12794  4217.260609   up
12795  4217.260609   up
12796  4217.260609   up

[12797 rows x 2 columns]
RangeIndex(start=0, stop=0, step=1)

完整的错误回溯:

Traceback (most recent call last):
  File "/freqtrade/freqtrade/main.py", line 37, in main
    return_code = args['func'](args)
  File "/freqtrade/freqtrade/commands/optimize_commands.py", line 53, in start_backtesting
    backtesting.start()
  File "/freqtrade/freqtrade/optimize/backtesting.py", line 479, in start
    min_date, max_date = self.backtest_one_strategy(strat, data, timerange)
  File "/freqtrade/freqtrade/optimize/backtesting.py", line 437, in backtest_one_strategy
    preprocessed = self.strategy.ohlcvdata_to_dataframe(data)
  File "/freqtrade/freqtrade/strategy/interface.py", line 670, in ohlcvdata_to_dataframe
    return {pair: self.advise_indicators(pair_data.copy(), {'pair': pair})
  File "/freqtrade/freqtrade/strategy/interface.py", line 670, in <dictcomp>
    return {pair: self.advise_indicators(pair_data.copy(), {'pair': pair})
  File "/freqtrade/freqtrade/strategy/interface.py", line 687, in advise_indicators
    return self.populate_indicators(dataframe, metadata)
  File "/freqtrade/user_data/strategies/TrippleSuperTrendStrategy.py", line 94, in populate_indicators
    dataframe.loc[:, 'st_12'] = st12['ST'],
  File "/home/ftuser/.local/lib/python3.9/site-packages/pandas/core/indexing.py", line 692, in __setitem__
    iloc._setitem_with_indexer(indexer, value, self.name)
  File "/home/ftuser/.local/lib/python3.9/site-packages/pandas/core/indexing.py", line 1597, in _setitem_with_indexer
    self.obj[key] = value
  File "/home/ftuser/.local/lib/python3.9/site-packages/pandas/core/frame.py", line 3163, in __setitem__
    self._set_item(key, value)
  File "/home/ftuser/.local/lib/python3.9/site-packages/pandas/core/frame.py", line 3242, in _set_item
    value = self._sanitize_column(key, value)
  File "/home/ftuser/.local/lib/python3.9/site-packages/pandas/core/frame.py", line 3899, in _sanitize_column
    value = sanitize_index(value, self.index)
  File "/home/ftuser/.local/lib/python3.9/site-packages/pandas/core/internals/construction.py", line 751, in sanitize_index
    raise ValueError(
ValueError: Length of values (1) does not match length of index (12797)
ERROR: 1

因此,如果两个数据帧具有完全相同的行数并且索引完全相同,为什么会出现此错误?

标签: pythonpandasdataframe

解决方案


有一个错字:

dataframe.loc[:, 'st_12'] = st12['ST']

逗号是错别字。


推荐阅读