【问题标题】:Python: How to develop a between_time similar method when on pandas 0.9.0?Python:如何在 pandas 0.9.0 上开发一个 between_time 类似的方法?
【发布时间】:2017-05-07 02:47:25
【问题描述】:

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

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

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

【问题讨论】:

    标签: python pandas python-2.5


    【解决方案1】:

    更新:

    尝试使用:

    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 的源代码,并查看 indexer_between_time() 函数在 pandas/tseries/index.py 中的定义 - 您可以根据需要克隆它


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

    【讨论】:

    • 似乎至少可以工作几个小时。但是,这样我从 08:00 到 09:50。如果我尝试 df[(df.index.hour >= 8) & (df.index.hour
    • 似乎pandas 0.10.0 已经实现了 between_time。
    • 在 Pandas 0.9.0 中试试这个df.ix[df.index.indexer_between_time('08:00','09:50')]
    • pd.tseries.indexer_between_time()pandas 0.9.0 中不可用。我正在研究pandas 0.10.0,但我发现这可能有点难以实现,因为pandas 0.9.0中使用了许多新的库,但没有定义@
    • 伟大的@MaxU!这个新的解决方案正是我想要的。
    【解决方案2】:

    这是一种基于 NumPy 的方法:

    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'])
    
    epoch = np.datetime64('1970-01-01')
    start = np.datetime64('1970-01-01 08:00:00')
    end = np.datetime64('1970-01-01 09:00:00')
    
    # convert the dates to a NumPy datetime64 array
    date_array = df.index.asi8.astype('<M8[ns]') 
    
    # replace the year/month/day with 1970-01-01
    truncated = (date_array - date_array.astype('M8[D]')) + epoch
    
    # compare the hour/minute/seconds etc with `start` and `end`
    mask = (start <= truncated) & (truncated <=end)
    
    print(df[mask])
    

    产量

                               Power
    2009-08-01 08:00:00  1007.289466
    2009-08-01 08:10:00   770.732422
    2009-08-01 08:20:00   617.388909
    2009-08-01 08:30:00  1348.384210
    ...
    2012-07-31 08:30:00   999.133350
    2012-07-31 08:40:00  1451.500408
    2012-07-31 08:50:00  1161.003167
    2012-07-31 09:00:00   670.545371
    

    【讨论】:

    • 不确定这是否可能是因为我正在运行 Pythn 2.5,但我得到以下错误尝试您的解决方案:Traceback(最近一次调用最后):文件“”,第 8 行,在 TypeError:无法使用转换规则“same_kind”将“1970-01-01”解析为单位“ns”
    • 啊,我忘了您需要NumPy version 1.7 or greater 才能使用此代码。
    • 这很奇怪@unutbu,因为我有 Numpy 版本 1.7.1
    • 我已将代码更改为使用epoch = np.datetime64('1970-01-01') 而不是epoch = np.array(['1970-01-01'], dtype='&lt;M8[ns]')。这应该是valid in NumPy 1.7
    • 现在我得到以下错误:AttributeError: 'DataFrame' object has no attribute 'loc'
    猜你喜欢
    • 1970-01-01
    • 2017-11-29
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 2022-01-19
    • 1970-01-01
    • 2021-10-10
    相关资源
    最近更新 更多