mirror of
https://github.com/PR0M3TH3AN/python_charts.git
synced 2026-10-27 08:50:39 +00:00
Drop NaN rows before validation
This commit is contained in:
1 parent
02a89740bb
commit
f33fac02c2
2 files changed
+53
-7
No files matched your search
@@ -351,11 +351,20 @@ def main(argv: list[str] | None = None) -> plt.Figure:
|
|||||||
# 15) FETCH SERIES
|
# 15) FETCH SERIES
|
||||||
# ──────────────────────────────────────────────────────────────────────────
|
# ──────────────────────────────────────────────────────────────────────────
|
||||||
try:
|
try:
|
||||||
btc, m2 = fetch_series(start_dt, end_dt, args.btc_series, args.m2_series, args.db)
|
btc, m2 = fetch_series(
|
||||||
|
start_dt, end_dt, args.btc_series, args.m2_series, args.db
|
||||||
|
)
|
||||||
except FileNotFoundError as fnf:
|
except FileNotFoundError as fnf:
|
||||||
print(fnf, file=sys.stderr)
|
print(fnf, file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────────────────
|
||||||
|
# DROP ANY ROWS WITH NULL "value" BEFORE VALIDATION
|
||||||
|
# ──────────────────────────────────────────────────────────────────────────
|
||||||
|
# (GLOBAL_M2 often has one or two trailing NaNs for "unpublished" months.)
|
||||||
|
btc = btc.dropna(subset=["value"])
|
||||||
|
m2 = m2.dropna(subset=["value"])
|
||||||
|
|
||||||
# If either series is empty, print a friendly message and exit:
|
# If either series is empty, print a friendly message and exit:
|
||||||
if btc.empty:
|
if btc.empty:
|
||||||
print(
|
print(
|
||||||
|
|||||||
@@ -12,15 +12,25 @@ from scripts.bitcoin_m2_chart import plot_bitcoin_m2, main as btc_m2_main
|
|||||||
|
|
||||||
|
|
||||||
def sample_data():
|
def sample_data():
|
||||||
dates = pd.date_range("2021-01-01", periods=5, freq="D")
|
btc_dates = pd.date_range("2024-07-01", periods=10, freq="D")
|
||||||
btc = pd.DataFrame({"value": [10, 11, 12, 13, 14]}, index=dates)
|
btc = pd.DataFrame({"value": range(10, 20)}, index=btc_dates)
|
||||||
m2 = pd.DataFrame({"value": [1, 2, 3, 4, 5]}, index=dates)
|
m2_dates = btc_dates
|
||||||
|
m2 = pd.DataFrame({"value": range(1, 11)}, index=m2_dates)
|
||||||
|
return btc, m2
|
||||||
|
|
||||||
|
|
||||||
|
def sample_data_with_nan():
|
||||||
|
btc_dates = pd.date_range("2024-07-01", periods=10, freq="D")
|
||||||
|
btc = pd.DataFrame({"value": range(10, 20)}, index=btc_dates)
|
||||||
|
m2_dates = btc_dates
|
||||||
|
m2_values = list(range(1, 10)) + [pd.NA]
|
||||||
|
m2 = pd.DataFrame({"value": m2_values}, index=m2_dates)
|
||||||
return btc, m2
|
return btc, m2
|
||||||
|
|
||||||
|
|
||||||
def test_plot_bitcoin_m2_returns_figure():
|
def test_plot_bitcoin_m2_returns_figure():
|
||||||
btc, m2 = sample_data()
|
btc, m2 = sample_data()
|
||||||
fig = plot_bitcoin_m2(btc, m2, 94, 1)
|
fig = plot_bitcoin_m2(btc, m2, 0, 1)
|
||||||
assert len(fig.axes) == 2
|
assert len(fig.axes) == 2
|
||||||
assert any(ax.get_lines() for ax in fig.axes)
|
assert any(ax.get_lines() for ax in fig.axes)
|
||||||
|
|
||||||
@@ -32,11 +42,14 @@ def test_btc_m2_main(monkeypatch):
|
|||||||
return btc, m2
|
return btc, m2
|
||||||
|
|
||||||
monkeypatch.setattr("scripts.bitcoin_m2_chart.fetch_series", fake_fetch)
|
monkeypatch.setattr("scripts.bitcoin_m2_chart.fetch_series", fake_fetch)
|
||||||
fig = btc_m2_main([
|
fig = btc_m2_main(
|
||||||
|
[
|
||||||
"--btc-series",
|
"--btc-series",
|
||||||
"BTC",
|
"BTC",
|
||||||
"--m2-series",
|
"--m2-series",
|
||||||
"M2",
|
"M2",
|
||||||
|
"--offset",
|
||||||
|
"0",
|
||||||
"--width",
|
"--width",
|
||||||
"8",
|
"8",
|
||||||
"--height",
|
"--height",
|
||||||
@@ -44,7 +57,29 @@ def test_btc_m2_main(monkeypatch):
|
|||||||
"--dpi",
|
"--dpi",
|
||||||
"150",
|
"150",
|
||||||
"--no-show",
|
"--no-show",
|
||||||
])
|
]
|
||||||
|
)
|
||||||
|
assert len(fig.axes) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_handles_trailing_nan(monkeypatch):
|
||||||
|
btc, m2 = sample_data_with_nan()
|
||||||
|
|
||||||
|
def fake_fetch(*_args, **_kwargs):
|
||||||
|
return btc, m2
|
||||||
|
|
||||||
|
monkeypatch.setattr("scripts.bitcoin_m2_chart.fetch_series", fake_fetch)
|
||||||
|
fig = btc_m2_main(
|
||||||
|
[
|
||||||
|
"--btc-series",
|
||||||
|
"BTC",
|
||||||
|
"--m2-series",
|
||||||
|
"M2",
|
||||||
|
"--offset",
|
||||||
|
"0",
|
||||||
|
"--no-show",
|
||||||
|
]
|
||||||
|
)
|
||||||
assert len(fig.axes) == 2
|
assert len(fig.axes) == 2
|
||||||
|
|
||||||
|
|
||||||
@@ -64,6 +99,8 @@ def test_btc_m2_main_saves_output(tmp_path, monkeypatch):
|
|||||||
"M2",
|
"M2",
|
||||||
"--output",
|
"--output",
|
||||||
str(out_file),
|
str(out_file),
|
||||||
|
"--offset",
|
||||||
|
"0",
|
||||||
"--width",
|
"--width",
|
||||||
"8",
|
"8",
|
||||||
"--height",
|
"--height",
|
||||||
|
|||||||
Reference in new issue
Block a user