gpt4 book ai didi

Python:如何在 pandas 0.9.0 上开发 Between_time 类似的方法?

转载 作者:行者123 更新时间:2023-11-30 22:48:32 24 4
gpt4 key购买 nike

我坚持使用 pandas 0.9.0,因为我在 python 2.5 下工作,因此我没有 between_time方法可用。

我有一个日期数据框,想过滤特定时间之间的所有日期,例如对于 DataFrame df 中的所有日期,介于 08:0009:00 之间。

import pandas as pd
import numpy as np
import datetime

dates = pd.date_range(start="08/01/2009",end="08/01/2012",freq="10min")
df = pd.DataFrame(np.random.rand(len(dates), 1)*1500, index=dates, columns=['Power'])

如何开发一种提供与 Between_time 方法相同功能的方法?

注意:我试图解决的原始问题位于Python: Filter DataFrame in Pandas by hour, day and month grouped by year

最佳答案

更新:

尝试使用:

df.loc[df.index.indexer_between_time('08:00','09:50')]

旧答案:

我不确定它是否适用于 Pandas 0.9.0,但值得一试:

df[(df.index.hour >= 8) & (df.index.hour <= 9)]

PS 请注意 - 它与 Between_time 不同,因为它仅检查小时,而 Between_time 能够像 一样检查时间>df. Between_time('08:01:15','09:13:28')

提示:下载新版本 Pandas 的源代码,并查看 pandas/tseries/index 中 indexer_ Between_time() 函数的定义.py - 您可以根据需要克隆它

<小时/>

更新:从 Pandas 0.20.1 the .ix indexer is deprecated, in favor of the more strict .iloc and .loc indexers 开始。

关于Python:如何在 pandas 0.9.0 上开发 Between_time 类似的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40138573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com