【发布时间】:2019-02-25 20:25:55
【问题描述】:
我的输入数据框有四分之一格式的Date 和Rate。 Date 中缺少四分之一。现在我正在尝试根据逻辑计算Update,如果最后一个季度的第三行可用,则选择此速率,否则选择最后一行的速率。我当前的代码如下所示:
# Create dataframe
df1 = pd.DataFrame([[1, '2015Q3'], [2, '2015Q4'],
[6, '2017Q1'], [7, '2017Q4'],
[9, '2018Q1'], [3, '2018Q2'],
[5, '2018Q3'], [4, '2018Q4']],
columns=['Rate', 'Date'])
df1.index = pd.PeriodIndex(df1.Date, freq='Q')
df1.drop(columns='Date', inplace=True)
# This Code produces 'FutureWarning' message
df1['Update'] = np.where(np.in1d(df1.index - 3, df1.index.values),
df1.loc[df1.index - 3].Rate, df1.Rate.shift(1))
完整的警告信息如下:
FutureWarning:
Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative.
See the documentation here:
https://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate-loc-reindex-listlike
警告中的链接:deprecate-loc-reindex-listlike
我了解,由于我的 index 进行了算术计算,并且计算中的某些 index 值不存在,这导致了此警告消息并建议使用 pandas.DataFrame.reindex。虽然我的意图是仅在 index 可用时才能访问 Rate p>
我尝试扩展Date 范围,但在边界附近也会出现同样的问题。所以,现在我的问题是如何解决这个警告信息。任何见解/想法/信息/链接都会有所帮助。
【问题讨论】: