matplotlib / matplotlib/mplfinance

Bug Report:

Open
#686 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
4.4k
Forks
678
PR merge metrics
No merged PRs in 30d

Description

Bug when using box_size = 'atr' in pnf charts in mplfinance : FutureWarning: Series.__getitem__ treating keys as positions is deprecated

OS Win 11 Pro 24H2 Desktop
IDE Visual Studio Code 1.96.2
Python 3.12.8
mplfinance 0.12.10b0

When using the 'atr' box_size parameter in creating pnf charts in mplfinance the `_utils.py script` throws this warning

\mplfinance\_utils.py:129: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]` high = highs[i]
\mplfinance\_utils.py:130: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]` low = lows[i]
\_utils.py:131: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]` close_prev = closes[i-1]

This is the responsible function:

```
def _calculate_atr(atr_length, highs, lows, closes):
"""Calculate the average true range
atr_length : time period to calculate over
all_highs : list of highs
all_lows : list of lows
all_closes : list of closes
"""
if atr_length < 1:
raise ValueError("Specified atr_length may not be less than 1")
elif atr_length >= len(closes):
raise ValueError("Specified atr_length is larger than the length of the dataset: " + str(len(closes)))
atr = 0
for i in range(len(highs)-atr_length, len(highs)):
high = highs[i]
low = lows[i]
close_prev = closes[i-1]
tr = max(abs(high-low), abs(high-close_prev), abs(low-close_prev))
atr += tr
return atr/atr_length

```
This warning is suppressed when you modify the code like so

```
high = highs.iloc[i]
low = lows.iloc[i]
close_prev = closes.iloc[i-1]
```

This however creates a new problem if you subsequently run a Renko chart, you get this error

LocalCache\local-packages\Python312\site-pa
high = highs.iloc[i]
^^^^^^^^^^
AttributeError: 'numpy.ndarray' object has no attribute 'iloc'

To fix this I created a small helper function

```
def safe_indexing(data, index):
if isinstance(data, pd.Series):
return data.iloc[index]
else:
return data[index]
```

and placed it in _utils.py
and updated _utils.py

```
high = highs[i]
low = lows[i]
close_prev = closes[i-1]
```

to

```
high = safe_indexing(highs, i)
low = safe_indexing(lows, i)
close_prev = safe_indexing(closes, i-1)
```

Now both charts render with no messages

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in _utils.py at _calculate_atr and trace how its highs, lows, and closes inputs are supplied for PNF and Renko charts. Reproduce both chart cases from the report, then verify that the warning is gone for PNF and the Renko path no longer raises the reported ndarray attribute error.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data-visualization
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.