DatetimeIndex.get_loc 已弃用

tab*_*_co 7 python deprecated pandas

我使用 yfinance 0.1.70 将 Pandas 更新到 1.4.0。此前,我不得不继续使用 Pandas 1.3.5,因为 Pandas 和 yfinance 不能很好地协同工作。这些最新版本的 Pandas 和 yfinance 现在可以一起工作,但是 Pandas 现在给我这个警告:

Future Warning: Passing method to DatetimeIndex.get_loc is deprecated... Use index.get_indexer([item], method=...) instead
Run Code Online (Sandbox Code Playgroud)

作为一个 Python 新手,我在让原始get_loc语句发挥作用时遇到了很多麻烦:

last_week = format((df.index[df.index.get_loc(last_week, method='nearest')]).strftime('%Y-%m-%d'))
Run Code Online (Sandbox Code Playgroud)

该语句允许我从数据框中获取日期,我可以进一步使用该日期来确定与该日期关联的值:

week_value = df.loc[last_week, ans]
Run Code Online (Sandbox Code Playgroud)

说实话,我对尝试更改此声明以符合新的和改进的get_indexer功能感到害怕。有人可以帮我吗?

小智 12

应该很简单。只需更改get_loc(XXX, ...)get_indexer([XXX], ...)[0]

last_week = format((df.index[df.index.get_indexer([last_week], method='nearest')[0]]).strftime('%Y-%m-%d'))
Run Code Online (Sandbox Code Playgroud)