question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: Reindexing only valid with uniquely valued Index objects

See original GitHub issue

Using python 3.7.7

When calling ATR i get an error:

2021-07-03 08:01:49.416460 1 cdX9cpVr23rnQjEVnRTh4x ERROR Error from indicator.py,values line nr 115,

 Code: <class 'pandas.errors.InvalidIndexError'> with message: Reindexing only valid with uniquely valued Index objects

  File "c:\traderbot\engine\src\indicators\indicator.py", line 115, in values
    tbi.calculate()
  File "c:\traderbot\engine\src\indicators\atr.py", line 138, in calculate
    self.__calculate_atr()
  File "c:\traderbot\engine\src\indicators\atr.py", line 151, in __calculate_atr
    self.ohlc_data['value'] = pta.atr(self.ohlc_data['high'], self.ohlc_data['low'], self.ohlc_data['close'], length=self.length)
  File "C:\Users\Marcel\miniconda3\lib\site-packages\pandas_ta\volatility\atr.py", line 19, in atr
    tr = true_range(high=high, low=low, close=close, drift=drift)
  File "C:\Users\Marcel\miniconda3\lib\site-packages\pandas_ta\volatility\true_range.py", line 19, in true_range
    true_range = DataFrame(ranges).T
  File "C:\Users\Marcel\miniconda3\lib\site-packages\pandas\core\frame.py", line 509, in __init__
    arrays, columns = to_arrays(data, columns, dtype=dtype)
  File "C:\Users\Marcel\miniconda3\lib\site-packages\pandas\core\internals\construction.py", line 531, in to_arrays
    data, columns, coerce_float=coerce_float, dtype=dtype
  File "C:\Users\Marcel\miniconda3\lib\site-packages\pandas\core\internals\construction.py", line 593, in _list_of_series_to_arrays
    indexer = indexer_cache[id(index)] = index.get_indexer(columns)
  File "C:\Users\Marcel\miniconda3\lib\site-packages\pandas\core\indexes\base.py", line 2987, in get_indexer
    "Reindexing only valid with uniquely valued Index objects"

I use the following code:

   self.ohlc_data['value'] = pta.atr(self.ohlc_data['high'], self.ohlc_data['low'], self.ohlc_data['close'], length=self.length)

The values of self.length = 14 (int) The values of self.ohlc_data is:


              time      open      high       low     close    volume             utctime
568  1622668800000  31044.37  31078.11  30978.01  30995.23  6.426382 2021-06-02 21:20:00
569  1622669100000  30991.14  31010.72  30959.10  30965.50  3.925410 2021-06-02 21:25:00
570  1622669400000  30973.65  30995.46  30960.07  30969.20  2.848319 2021-06-02 21:30:00
571  1622669700000  30974.26  30974.26  30884.00  30947.30  6.709024 2021-06-02 21:35:00
572  1622670000000  30942.12  31023.78  30938.60  31005.30  6.739472 2021-06-02 21:40:00
..             ...       ...       ...       ...       ...       ...                 ...
665  1625289900000  28318.14  28393.51  28318.14  28345.29  3.075262 2021-07-03 05:25:00
666  1625290200000  28345.29  28404.71  28345.29  28391.58  5.038435 2021-07-03 05:30:00
667  1625290500000  28391.57  28430.28  28388.75  28430.28  7.276387 2021-07-03 05:35:00
668  1625290800000  28430.28  28441.98  28415.00  28433.43  5.479073 2021-07-03 05:40:00
669  1625291100000  28433.49  28439.95  28402.98  28439.95  1.732114 2021-07-03 05:45:00
[8742 rows x 7 columns]

I have two scenario’s: Running in production Running in backtest

Running in production goes good. Running in backtest (data above) is producing this error, because the index is not unique

So If i reindex the dataframe with it works well: self.ohlc_data = self.ohlc_data.reset_index(drop=True)

Which is a little bit strange as I pass series to the atr, so somewhere in the code is a referall to the complete df.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
justmeonthegitcommented, Jul 7, 2021

Hi KJ,

I resolved it by doing a reindex in one of the data components, so then everything was fine for me. If you need test info just let me knwo.

Meanwhile : I update pandas-TA, thxs for mentioning.

Regards,

0reactions
twopirllccommented, Jul 7, 2021

@justmeonthegit,

No worries. Glad you have a working solution and replied in a timely manner. 😎

GL 🍀

KJ

Read more comments on GitHub >

github_iconTop Results From Across the Web

Concat DataFrame Reindexing only valid with uniquely ...
I think the error usually occurs with column names, because mostly people will concatenate over the row-axis (i.e. axis=0 or unspecified). In ...
Read more >
Pandas : Concat DataFrame Reindexing only valid ... - YouTube
Pandas : Concat DataFrame Reindexing only valid with uniquely valued Index objects [ Beautify Your Computer ...
Read more >
Reindexing only valid with uniquely valued Index objects ...
I am getting this error when trying to concat a list of 1x13 data frames. I am having trouble looking at the contents...
Read more >
InvalidIndexError: Reindexing only valid with uniquely valued ...
Problem description I am expriencing a weird bug while trying to convert a list to datetime. Here is a bug repro: import pandas...
Read more >
Reindexing only valid with uniquely valued Index objects ...
I have two different dfs that I want to combine using: pd.concat([df1, df2], 1). The end result being a df with the date...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found