- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我见过这个问题被问过几次,但没有答案。简短版本:
我有一个带有两级MultiIndex
索引的pandas DataFrame
;两个级别都是整数。如何在此 DataFrame
上使用 .asof()
?
长版:
我有一个带有一些时间序列数据的DataFrame
:
>>> df
A
2016-01-01 00:00:00 1.560878
2016-01-01 01:00:00 -1.029380
... ...
2016-01-30 20:00:00 0.429422
2016-01-30 21:00:00 -0.182349
2016-01-30 22:00:00 -0.939461
2016-01-30 23:00:00 0.009930
2016-01-31 00:00:00 -0.854283
[721 rows x 1 columns]
然后我将构建该数据的每周模型:
>>> df['weekday'] = df.index.weekday
>>> df['hour_of_day'] = df.index.hour
>>> weekly_model = df.groupby(['weekday', 'hour_of_day']).mean()
>>> weekly_model
A
weekday hour_of_day
0 0 0.260597
1 0.333094
... ...
20 0.388932
21 -0.082020
22 -0.346888
23 1.525928
[168 rows x 1 columns]
这就是给我一个带有上述索引的 DataFrame
的原因。
我现在正在尝试将该模型推断为年度时间序列:
>>> dates = pd.date_range('2015/1/1', '2015/12/31 23:59', freq='H')
>>> annual_series = weekly
weekly weekly_model
>>> annual_series = weekly_model.A.asof((dates.weekday, dates.hour))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/tkcook/azimuth-web/lib/python3.5/site-packages/pandas/core/series.py", line 2657, in asof
locs = self.index.asof_locs(where, notnull(values))
File "/home/tkcook/azimuth-web/lib/python3.5/site-packages/pandas/indexes/base.py", line 1553, in asof_locs
locs = self.values[mask].searchsorted(where.values, side='right')
ValueError: operands could not be broadcast together with shapes (8760,) (2,)
>>> dates = pd.date_range('2015/1/1', '2015/12/31 23:59', freq='H')
>>> annual_series = weekly_model.A.asof((dates.weekday, dates.hour))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/tkcook/azimuth-web/lib/python3.5/site-packages/pandas/core/series.py", line 2657, in asof
locs = self.index.asof_locs(where, notnull(values))
File "/home/tkcook/azimuth-web/lib/python3.5/site-packages/pandas/indexes/base.py", line 1553, in asof_locs
locs = self.values[mask].searchsorted(where.values, side='right')
ValueError: operands could not be broadcast together with shapes (8760,) (2,)
此错误意味着什么?执行此操作的最佳方法是什么?
到目前为止我想出的最好的办法是:
>>> annual_series = weekly_model.A.loc[list(zip(dates.weekday, dates.hour))]
它可以工作,但这意味着首先将 zip
迭代器转换为列表,这并不完全是内存友好的。有办法避免这种情况吗?
最佳答案
我多次阅读了您的帖子,我想我终于明白了您想要实现的目标。
试试这个:
df['weekday'] = df.index.weekday
df['hour_of_day'] = df.index.hour
weekly_model = df.groupby(['weekday', 'hour_of_day']).mean()
dates = pd.date_range('2015/1/1', '2015/12/31 23:59', freq='H')
然后像这样使用合并:
annual_series = pd.merge(df.reset_index(), weekly_model.reset_index(), on=['weekday', 'hour_of_day']).set_index('date')
现在你可以使用 asof 因为你有日期作为索引
annual_series.asof(dates)
这就是您要找的吗?
关于python - 在 Pandas 中使用 .asof 和 MultiIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39922050/
我有两个 pandas 数据框 X 和 Y,每个都包含过去一个月的日内价格和时间数据。我想在 X 上运行 Y 的连接,即每次我们看到 X 的更新时我们都采用 Y 的现行价格。我想运行日内分析(因为隔夜
我已经阅读了 Pandas 的文档。有一个有用的函数叫做 merge_asof这似乎将两个数据帧与靠近的行合并在一起。但我不知道是什么asof方法。是吗as of ?或者它是某种东西的缩写? http
我希望做一些类似 Pandas 的事情 merge_asof 或 QuestDB 的 ASOF JOIN 在 Julia 。 至关重要的是,我还需要应用分组操作 . 我很乐意使用 Julia 的任何
我有每个实体的时间序列数据: id event_date value 1 2013-12-21 3.82 1 2013-12-22 2.47 1 2013-12-25 2.13
我有一大堆时间间隔不规则的数据帧。 我想创建一个新的 data.frame 并将其他 data.frame 加入其中,对于加入的每个 data.frame,从新的 data.frame 中选取最新的值
我有两个数据框,我希望将它们连接在一起,其中左侧数据框的信息索引为(日期,ID),右侧数据框的信息索引为(期间,ID),其中期间为年月。 我最终对左帧按 ID 进行分组,迭代组,在右帧上选择相同的组,
aj[`time`sym;trade;quote] 将每笔交易与之前的报值(value)连接起来。 我想进行相同的连接,但是是在下一个引用值而不是前一个值上进行连接。 我怎样才能实现这个目标? 最佳答
我见过这个问题被问过几次,但没有答案。简短版本: 我有一个带有两级MultiIndex索引的pandas DataFrame;两个级别都是整数。如何在此 DataFrame 上使用 .asof()?
kdb+ 有一个 aj通常用于沿时间列连接表的函数。 这是一个例子,我有交易和报价表,我得到了每笔交易的现行报价。 q)5# t time sym price size ------
我对excel一无所知,我想知道是否有一种方法可以将q-coding中的asof join语句转换为使用excel公式的语句 :update string issueSeries from aj[`s
我在传递 datetime 时遇到问题进入Pandas.Series.asof : def valueAsOf(self, date): if type(date) is str:
如何使用Series.asof函数?我传入了一个 datetime 对象 datetime.strptime('20150101', '%Y%m%d'),但为什么会报错 File "/Users/
我是一名优秀的程序员,十分优秀!