fix: handle NumPy object rate timestamps

This commit is contained in:
dceoy
2026-06-22 22:54:34 +09:00
parent 82a39731ed
commit 0610ea732c
2 changed files with 11 additions and 8 deletions
+2 -4
View File
@@ -4,7 +4,7 @@ from __future__ import annotations
from contextlib import contextmanager from contextlib import contextmanager
from math import floor, isfinite from math import floor, isfinite
from numbers import Integral from numbers import Integral, Real
from typing import TYPE_CHECKING, Literal, TypedDict, cast from typing import TYPE_CHECKING, Literal, TypedDict, cast
import pandas as pd import pandas as pd
@@ -1221,9 +1221,7 @@ def _rate_time_to_utc(series: pd.Series, symbol: str) -> pd.DatetimeIndex:
object_numbers = ( object_numbers = (
pd.api.types.is_object_dtype(series) pd.api.types.is_object_dtype(series)
and non_null.map( and non_null.map(
lambda value: ( lambda value: type(value) is not bool and isinstance(value, Real),
isinstance(value, int | float) and not isinstance(value, bool)
),
).all() ).all()
) )
numeric_dtype = pd.api.types.is_numeric_dtype( numeric_dtype = pd.api.types.is_numeric_dtype(
+9 -4
View File
@@ -8,6 +8,7 @@ from unittest.mock import MagicMock
import pandas as pd import pandas as pd
import pytest import pytest
from numpy import float64 as np_float64
from numpy import int64 as np_int64 from numpy import int64 as np_int64
from pdmt5 import Mt5RuntimeError, Mt5TradingClient, Mt5TradingError from pdmt5 import Mt5RuntimeError, Mt5TradingClient, Mt5TradingError
from pytest_mock import MockerFixture # noqa: TC002 from pytest_mock import MockerFixture # noqa: TC002
@@ -2706,13 +2707,17 @@ class TestFetchLatestClosedRatesIndexed:
[ [
[1700000000, 1700003600], [1700000000, 1700003600],
[1700000000.0, 1700003600.0], [1700000000.0, 1700003600.0],
[np_int64(1700000000), np_int64(1700003600)],
[np_float64(1700000000.0), np_float64(1700003600.0)],
], ],
ids=["integers", "floats"], ids=["integers", "floats", "numpy-integers", "numpy-floats"],
) )
def test_converts_object_numeric_epoch_seconds_to_utc_datetime_index( def test_converts_object_numeric_epoch_seconds_to_utc_datetime_index(
self, mocker: MockerFixture, timestamps: list[int] | list[float] self,
mocker: MockerFixture,
timestamps: list[int] | list[float] | list[np_int64] | list[np_float64],
) -> None: ) -> None:
"""Test object-dtype Python numbers are interpreted as epoch seconds.""" """Test object-dtype real numbers are interpreted as epoch seconds."""
frame = pd.DataFrame( frame = pd.DataFrame(
{ {
"time": pd.Series(timestamps, dtype=object), "time": pd.Series(timestamps, dtype=object),
@@ -2729,7 +2734,7 @@ class TestFetchLatestClosedRatesIndexed:
) )
assert list(result.index) == list( assert list(result.index) == list(
pd.to_datetime(timestamps, unit="s", utc=True) pd.to_datetime([1700000000, 1700003600], unit="s", utc=True)
) )
def test_parses_mixed_datetime_like_strings(self, mocker: MockerFixture) -> None: def test_parses_mixed_datetime_like_strings(self, mocker: MockerFixture) -> None: