diff --git a/mt5cli/trading.py b/mt5cli/trading.py index c5e0d3c..23f2f32 100644 --- a/mt5cli/trading.py +++ b/mt5cli/trading.py @@ -4,7 +4,7 @@ from __future__ import annotations from contextlib import contextmanager from math import floor, isfinite -from numbers import Integral +from numbers import Integral, Real from typing import TYPE_CHECKING, Literal, TypedDict, cast import pandas as pd @@ -1221,9 +1221,7 @@ def _rate_time_to_utc(series: pd.Series, symbol: str) -> pd.DatetimeIndex: object_numbers = ( pd.api.types.is_object_dtype(series) and non_null.map( - lambda value: ( - isinstance(value, int | float) and not isinstance(value, bool) - ), + lambda value: type(value) is not bool and isinstance(value, Real), ).all() ) numeric_dtype = pd.api.types.is_numeric_dtype( diff --git a/tests/test_trading.py b/tests/test_trading.py index ebb5016..19409cf 100644 --- a/tests/test_trading.py +++ b/tests/test_trading.py @@ -8,6 +8,7 @@ from unittest.mock import MagicMock import pandas as pd import pytest +from numpy import float64 as np_float64 from numpy import int64 as np_int64 from pdmt5 import Mt5RuntimeError, Mt5TradingClient, Mt5TradingError from pytest_mock import MockerFixture # noqa: TC002 @@ -2706,13 +2707,17 @@ class TestFetchLatestClosedRatesIndexed: [ [1700000000, 1700003600], [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( - self, mocker: MockerFixture, timestamps: list[int] | list[float] + self, + mocker: MockerFixture, + timestamps: list[int] | list[float] | list[np_int64] | list[np_float64], ) -> None: - """Test object-dtype Python numbers are interpreted as epoch seconds.""" + """Test object-dtype real numbers are interpreted as epoch seconds.""" frame = pd.DataFrame( { "time": pd.Series(timestamps, dtype=object), @@ -2729,7 +2734,7 @@ class TestFetchLatestClosedRatesIndexed: ) 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: