Skip to content

Commit 14f111a

Browse files
committed
BUG: Cast datetime_arr.astype(np.int64) to avoid error on Widows
Fixes error: > TypeError: Converting from datetime64[ns] to int32 is not supported. Do obj.astype('int64').astype(dtype) instead Contrary to numpy docs: * https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.int_ even 64-bit Windos apparently has 32-bit `long`s. * https://stackoverflow.com/questions/36278590/numpy-array-dtype-is-coming-as-int32-by-default-in-a-windows-10-64-bit-machine * https://learn.microsoft.com/en-us/cpp/cpp/data-type-ranges?view=msvc-170 Fixes #1214
1 parent c79ffb0 commit 14f111a

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

backtesting/_plotting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ def _weighted_returns(s, trades=trades):
165165
return ((df['Size'].abs() * df['ReturnPct']) / df['Size'].abs().sum()).sum()
166166

167167
def _group_trades(column):
168-
def f(s, new_index=pd.Index(df.index.astype(int)), bars=trades[column]):
168+
def f(s, new_index=pd.Index(df.index.astype(np.int64)), bars=trades[column]):
169169
if s.size:
170170
# Via int64 because on pandas recently broken datetime
171-
mean_time = int(bars.loc[s.index].astype(int).mean())
171+
mean_time = int(bars.loc[s.index].astype(np.int64).mean())
172172
new_bar_idx = new_index.get_indexer([mean_time], method='nearest')[0]
173173
return new_bar_idx
174174
return f

backtesting/_stats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def compute_drawdown_duration_peaks(dd: pd.Series):
1515
iloc = np.unique(np.r_[(dd == 0).values.nonzero()[0], len(dd) - 1])
1616
iloc = pd.Series(iloc, index=dd.index[iloc])
1717
df = iloc.to_frame('iloc').assign(prev=iloc.shift())
18-
df = df[df['iloc'] > df['prev'] + 1].astype(int)
18+
df = df[df['iloc'] > df['prev'] + 1].astype(np.int64)
1919

2020
# If no drawdown since no trade, avoid below for pandas sake and return nan series
2121
if not len(df):

backtesting/backtesting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ def _optimize_sambo() -> Union[pd.Series,
15361536
if values.dtype.kind in 'mM': # timedelta, datetime64
15371537
# these dtypes are unsupported in SAMBO, so convert to raw int
15381538
# TODO: save dtype and convert back later
1539-
values = values.astype(int)
1539+
values = values.astype(np.int64)
15401540

15411541
if values.dtype.kind in 'iumM':
15421542
dimensions.append((values.min(), values.max() + 1))

0 commit comments

Comments
 (0)