Bug Report: Errors occur for NaN columns.

Open Beginner friendly
#672 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start with the reproduction in the issue and inspect mplfinance/plotting.py at _addplot_columns, especially the np.nanmax call around line 1108. Confirm the failure with an addplot column containing only NaN values; done means plotting that data no longer raises the zero-size reduction ValueError.

Written by the indexing model from the issue text.

Description

bug

issue case

When animating over a limited visual range, as in this example, the possibility arises that the signal columns are all NaN.

https://github.com/matplotlib/mplfinance/assets/37892107/a5afea85-57ce-4282-ad7c-955240c54787

issue

mplfinance\plotting.py", line 1108, in _addplot_columns
ymhi = math.log(max(math.fabs(np.nanmax(yd)),1e-7),10)
This Numpy code generates an error.
"ValueError: zero-size array to reduction operation maximum which has no identity"

Error reproduction

import datetime

import numpy as np
import pandas as pd
import mplfinance as mpf

# ランダムなOHLCデータを生成する関数
def generate_sample_ohlc_data(n=10):
    # 現在の日付を取得
    base_date = datetime.datetime.now()
    # 日付のリストを生成
    dates = [base_date - datetime.timedelta(days=i) for i in range(n)]
    dates.reverse()

    # ランダムな価格データを生成
    rand = np.random.default_rng().uniform
    open = [100]
    high = [100.02]
    low = [100.02]
    close = [100.05]
    for i in range(n-1):
        open.append(close[i])
        close.append(open[-1] + rand(-0.1, 0.1))
        high.append(max(open[-1],close[-1]) + rand(0, 0.05))
        low.append(min(open[-1],close[-1]) - rand(0, 0.05))

    # DataFrameに格納
    ohlc_data = pd.DataFrame({
        "date": dates,
        "open": open,
        "high": high,
        "low": low,
        "close": close
    })

    return ohlc_data.set_index("date", drop=True)

period_range = 50
df = generate_sample_ohlc_data(period_range)
df["signal"] = np.nan
print(df)

some_signal = mpf.make_addplot(df["signal"])
mpf.plot(df, type='candle', addplot=some_signal) # ValueError

Currently, we have worked around this by doing NaN pre-checking on the user side, but it would be great if the library could handle this.

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

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.

More from matplotlib/mplfinance

All issues in matplotlib/mplfinance

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.