Skip to content

Commit

Permalink
Merge pull request #18 from FilipBolt/pandas_update
Browse files Browse the repository at this point in the history
Updating library to be compatible with latest pandas
  • Loading branch information
Heerozh authored Nov 28, 2023
2 parents a0d3362 + df561ac commit 334c09e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
17 changes: 11 additions & 6 deletions spectre/data/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,20 @@ def load(self, start: Optional[pd.Timestamp] = None, end: Optional[pd.Timestamp]
end = index[-1]

if index[0] > start:
raise ValueError("`start` time cannot less than earliest time of data: {}."
.format(index[0]))
raise ValueError(
f"`start` time ({start}) cannot be less " \
f"than earliest time of data: {index[0]}."
)

if index[-1] < end:
raise ValueError("`end` time cannot greater than latest time of data: {}."
.format(index[-1]))
raise ValueError(
f"`end` time ({end}) cannot be greater " \
f"than latest time of data: {index[-1]}."
)

start_loc = index.get_loc(start, 'bfill')
start_loc = index.get_indexer([start], method='bfill')[0]
backward_loc = max(start_loc - backwards, 0)
end_loc = index.get_loc(end, 'ffill')
end_loc = index.get_indexer([end], method='ffill')[0]
assert end_loc >= start_loc, 'There is no data between `start` and `end`.'

backward_start = index[backward_loc]
Expand Down
8 changes: 4 additions & 4 deletions spectre/factors/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ def _prepare_tensor(self, start, end, max_backwards):
df = self._loader.load(start, end, max_backwards).copy()
# If possible, pre-screen
if isinstance(self._filter, StaticAssets):
df = df.loc[(slice(None), self._filter.assets), :]
df = df.loc[(slice(None), list(self._filter.assets)), :]
if df.shape[0] == 0:
raise ValueError("The assets {} specified by StaticAssets filter, was not found in "
"DataLoader.".format(self._filter.assets))
# check history data is insufficient
df.index = df.index.remove_unused_levels()
history_win = df.index.levels[0].get_loc(start, 'bfill')
history_win = df.index.levels[0].get_indexer([start], method='bfill')[0]
if history_win < max_backwards:
warnings.warn("Historical data seems insufficient. "
"{} rows of historical data are required, but only {} rows are obtained. "
Expand Down Expand Up @@ -353,7 +353,7 @@ def run(self, start: Union[str, pd.Timestamp], end: Union[str, pd.Timestamp],
# if any factors delayed, return df also should be delayed
if delayed:
index = ret.index.levels[0]
start_ind = index.get_loc(start, 'bfill')
start_ind = index.get_indexer([start], method='bfill')[0]
if (start_ind + 1) >= len(index):
raise ValueError('There is no data between start and end.')
start = index[start_ind + 1]
Expand All @@ -369,7 +369,7 @@ def run_raw(self, start: Union[str, pd.Timestamp], end: Union[str, pd.Timestamp]
results, shifted_mask, delayed = self._run(start, end, delay_factor)

index = self._dataframe.index.levels[0]
start_ind = index.get_loc(start, 'bfill')
start_ind = index.get_indexer([start], method='bfill')[0]
if delayed: # if any factors delayed, return df also should be delayed
start_ind += 1
if start_ind >= len(index):
Expand Down

0 comments on commit 334c09e

Please sign in to comment.