microsoft / microsoft/qlib

TResample报错TypeError: Only valid with DatetimeIndex

Open
#1,457 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
48.7k
Forks
7.7k
PR merge metrics
No merged PRs in 30d

Description

🐛 Bug Description

使用TResample将1min数据转为30min K线时报错

To Reproduce

from qlib.data.ops import TResample, Feature
from qlib.data import D
fea = "TResample($open, '30min', 'first')"
D.features(instruments=['BTCUSDT'], fields=[fea], start_time='2022-01-01 9:00:00', end_time='2022-01-01 20:00:00', freq='1min')

报错

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'

原因是self.feature.load返回的series的索引是RangeIndex,并不能直接放到pandas的resample里使用,resample只接受Datetime的Index

class TResample(ElemOperator):
    ... ...
    def _load_internal(self, instrument, start_index, end_index, *args):
        series = self.feature.load(instrument, start_index, end_index, *args) 

        if series.empty:
            return series
        else:
            if self.func == "sum":
                return getattr(series.resample(self.freq), self.func)(min_count=1)
            else:
                return getattr(series.resample(self.freq), self.func)()

望修复,谢谢

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at TResample._load_internal and inspect the index returned by self.feature.load before the pandas resample call. Re-run the supplied D.features example with the 1-minute BTCUSDT data and confirm that 30-minute K-line resampling completes without the DatetimeIndex TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data-engineering
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.