Skip to content

Commit c90dc1b

Browse files
committed
BUG: Infer units for numeric timestamp indices
1 parent ca2e261 commit c90dc1b

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

β€Žbacktesting/backtesting.pyβ€Ž

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,10 +1224,15 @@ def __init__(self,
12241224
if (not isinstance(data.index, pd.DatetimeIndex) and
12251225
not isinstance(data.index, pd.RangeIndex) and
12261226
# Numeric index with most large numbers
1227-
(data.index.is_numeric() and
1227+
(pd.api.types.is_numeric_dtype(data.index.dtype) and
12281228
(data.index > pd.Timestamp('1975').timestamp()).mean() > .8)):
1229+
index_magnitude = np.nanmedian(np.abs(data.index))
1230+
# Infer seconds, milliseconds, microseconds, or nanoseconds by magnitude
1231+
unit = next((unit for unit, threshold in (
1232+
('s', 1e11), ('ms', 1e14), ('us', 1e17)
1233+
) if index_magnitude < threshold), 'ns')
12291234
try:
1230-
data.index = pd.to_datetime(data.index, infer_datetime_format=True)
1235+
data.index = pd.to_datetime(data.index, unit=unit)
12311236
except ValueError:
12321237
pass
12331238

β€Žbacktesting/test/_test.pyβ€Ž

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ def test_data_invalid(self):
131131
with self.assertRaises(ValueError):
132132
Backtest(GOOG.iloc[:0], SmaCross).run()
133133

134+
def test_data_numeric_timestamp_index(self):
135+
timestamps = {
136+
's': [1609459200, 1609545600],
137+
'ms': [1609459200000, 1609545600000],
138+
'us': [1609459200000000, 1609545600000000],
139+
'ns': [1609459200000000000, 1609545600000000000],
140+
}
141+
expected = pd.DatetimeIndex(['2021-01-01', '2021-01-02'])
142+
143+
for unit, index in timestamps.items():
144+
with self.subTest(unit=unit):
145+
data = GOOG.iloc[:2].copy()
146+
data.index = index
147+
148+
bt = Backtest(data, SmaCross)
149+
150+
self.assertTrue(bt._data.index.equals(expected))
151+
134152
def test_assertions(self):
135153
class Assertive(Strategy):
136154
def init(self):

0 commit comments

Comments
Β (0)