python - 第一次出现日期时间索引
问题描述
我有一个名为 Rolling_Max 的 DateTime 索引数据框。它给出了从 2011 年 1 月 1 日到 21 年 1 月 1 日每天的过去 2520 天最大值(价格)。如何向其中添加一个列,其中包含每个最大值首次出现的日期?
dfsp = pdr.get_data_yahoo('^GSPC', start='2011-01-01', end='2021-01-01')['Adj Close']
window = 2520
Roll_Max = dfsp.rolling(window, min_periods=1).max()
解决方案
你可以试试这个,假设价格是 pd.Series
price_argmax = price.reset_index(drop=True).rolling(2520, min_periods=130).apply(lambda x: x.index[x.argmax()])
price_argmax = pd.Series(price.index[price_argmax.dropna().astype("int")], index = price.index[price_argmax.dropna().index]).reindex(price.index)
如果你需要这个速度很快,那么你可能想用 numba/numpy 编写一个循环。
推荐阅读
- oauth - 无法验证 Moodle 发送的 OAuth1 签名
- c++ - 如何定义类类型转换为函数指针?
- python - 将数据框中的列从字符串类型转换为元组
- python-3.x - 构建 docker 的“python:3.5-alpine”映像时“构建正则表达式失败”
- optaplanner - Optaplanner 中的免费期间罚款
- java - Interactive Broker Java API 从historyData 中不返回任何内容
- r - 参数在 R 中应该有相同的长度错误
- python - 尝试转换为日期时间和“ValueError:如果数据无法转换为日期时间”
- php - Laravel Nova 上传的图像文件未在暂存环境中显示
- kubernetes - 如何使用 Traefik 和 Kubernetes 创建到正在侦听 Https 的服务的 HTTPS 路由