Skip to content

Commit

Permalink
Fix MultiIndex assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuh25 committed May 16, 2024
1 parent ec10032 commit 8419da8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tradingstrategy/charting/candle_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def validate_ohclv_dataframe(candles: pd.DataFrame):
if isinstance(candles.index, pd.MultiIndex):
levels = candles.index.levels
# (pair_id, timestamp) index - sharing index for multiple pairs in the same dataframe
assert levels[0].dtype == "int64" and isinstance(levels[1], pd.DatetimeIndex), f"Bad OHLCV MultiIndex - not (pair, timestamp) data: {levels[0]} and {levels[1]}"
assert pd.api.types.is_integer_dtype(levels[0]) and isinstance(levels[1], pd.DatetimeIndex), f"Bad OHLCV MultiIndex - not (pair, timestamp) data: {levels[0]} and {levels[1]}"
elif not isinstance(candles.index, pd.DatetimeIndex):
if (
candles.index.name not in {"date", "timestamp"} and \
Expand Down
4 changes: 2 additions & 2 deletions tradingstrategy/utils/df_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def flatten_dataframe_datetime_index(df: pd.DataFrame) -> pd.DataFrame:
assert isinstance(df.index, pd.MultiIndex), f"Got wrong index: {type(df.index)}"

# (pair id, timestamp) tuples
assert df.index.levels[0].dtype == "int64"
assert pd.api.types.is_integer_dtype(df.index.levels[0])
assert isinstance(df.index.levels[1], pd.DatetimeIndex)

new_index = df.index.get_level_values(1) # assume pair id, timestamp tuples
Expand All @@ -49,7 +49,7 @@ def get_timestamp_index(df: pd.DataFrame) -> pd.DatetimeIndex:
assert isinstance(df.index, pd.MultiIndex), f"Got wrong index: {type(df.index)}"

# (pair id, timestamp) tuples
assert df.index.levels[0].dtype == "int64"
assert pd.api.types.is_integer_dtype(df.index.levels[0])
assert isinstance(df.index.levels[1], pd.DatetimeIndex)

return df.index.get_level_values(1)

0 comments on commit 8419da8

Please sign in to comment.