跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 如何用 pandas 优雅拼装 k 线数据
未分類
16 9 月 2020

如何用 pandas 优雅拼装 k 线数据

如何用 pandas 优雅拼装 k 线数据

資深大佬 : yellowtail 4

方法一使用 resample 按照网上案例写得,很简短,不过 apply 给字典参数的操作没有看懂。而且可能是因为中午休市的原因,超过六十分钟以上,取样会出问题,一天会出现五根一小时 k 线(一天四个交易时) 写法为 ohlc_dict ={ ‘o’:’first’,
‘h’:’max’,
‘l’:’min’,
‘c’: ‘last’
} dft = dft.resample(period, closed=’right’,label = ‘right’).apply(ohlc_dict).dropna()

方法二是用 cut dfx =pd.DataFrame()

df[‘tst’] = pd.cut(df.index,right=False,bins=range(len(df))[::120])

dfx[‘o’]=df.groupby(‘tst’)[‘o’].first()

dfx[‘c’]=df.groupby(‘tst’)[‘c’].last()

dfx[‘h’]=df.groupby(‘tst’)[‘h’].max()

dfx[‘l’]=df.groupby(‘tst’)[‘l’].min()

dfx[‘trade_date’]=df.groupby(‘tst’)[‘trade_date’].last() 感觉太丑了…希望能给点优化意见

大佬有話說 (2)

  • 主 資深大佬 : yellowtail

    https://imgchr.com/i/wTGMOf resample 方法 60 分钟以上的错误

  • 資深大佬 : volvo007

    resample 函数接受字典参数,key 为列名并体现在返回的 df 里,value 为对应列需要执行的函数

    dict of axis labels -> functions, function names or list of such.

    https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.resample.Resampler.apply.html#pandas.core.resample.Resampler.apply

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具