【问题标题】:How to select specific datetime-indidices by intraday time?如何按日内时间选择特定的日期时间索引?
【发布时间】:2020-11-20 18:47:19
【问题描述】:

我有一个时间索引,需要其中的所有时间索引,这些时间索引介于这些白天之间: (编辑:多天)

a,b = "07:21:39","22:00:01" 

index_ = DatetimeIndex(['2019-08-20 10:21:00', '2019-08-20 10:22:00', '2019-08-20 10:23:00', '2019-08-20 10:24:00', '2019-08-20 10:25:00', '2019-08-20 10:26:00', '2019-08-20 10:27:00', '2019-08-20 10:28:00', '2019-08-20 10:29:00', '2019-08-20 10:30:00', '2019-08-20 10:31:00', '2019-08-20 10:32:00', '2019-08-20 10:33:00', '2019-08-20 10:34:00', '2019-08-20 10:35:00', '2019-08-20 10:36:00', '2019-08-20 10:37:00', '2019-08-20 10:38:00', '2019-08-20 10:39:00', '2019-08-20 10:40:00', '2019-08-20 10:41:00', '2019-08-20 10:42:00', '2019-08-20 10:43:00', '2019-08-20 10:44:00', '2019-08-20 10:45:00', '2019-08-20 10:46:00', '2019-08-20 10:47:00', '2019-08-20 10:48:00', '2019-08-20 10:49:00', '2019-08-20 10:50:00', '2019-08-20 10:51:00', '2019-08-20 10:52:00', '2019-08-20 10:53:00', '2019-08-20 10:54:00', '2019-08-20 10:55:00', '2019-08-20 10:56:00', '2019-08-20 10:57:00', '2019-08-20 10:58:00', '2019-08-20 10:59:00', '2019-08-20 11:00:00', '2019-08-20 11:01:00', '2019-08-20 11:02:00', '2019-08-20 11:03:00', '2019-08-20 11:04:00', '2019-08-20 11:05:00', '2019-08-20 11:06:00', '2019-08-20 11:07:00', '2019-08-20 11:08:00', '2019-08-20 11:09:00', '2019-08-20 11:10:00', '2019-08-20 11:11:00', '2019-08-20 11:12:00', '2019-08-20 11:13:00', '2019-08-20 11:14:00', '2019-08-20 11:15:00', '2019-08-20 11:16:00', '2019-08-20 11:17:00', '2019-08-20 11:18:00', '2019-08-20 11:19:00', '2019-08-20 11:20:00', '2019-08-20 11:21:00', '2019-08-20 11:22:00', '2019-08-20 11:23:00', '2019-08-20 11:24:00', '2019-08-20 11:25:00', '2019-08-20 11:26:00', '2019-08-20 11:27:00', '2019-08-20 11:28:00', '2019-08-20 11:29:00', '2019-08-20 11:30:00', '2019-08-20 11:31:00', '2019-08-20 11:32:00', '2019-08-20 11:33:00', '2019-08-20 11:34:00', '2019-08-20 11:35:00', '2019-08-20 11:36:00', '2019-08-20 11:37:00', '2019-08-20 11:38:00', '2019-08-20 11:39:00', '2019-08-20 11:40:00', '2019-08-20 11:41:00', '2019-08-20 11:42:00', '2019-08-20 11:43:00', '2019-08-20 11:44:00', '2019-08-20 11:45:00', '2019-08-20 11:46:00', '2019-08-20 11:47:00', '2019-08-20 11:48:00', '2019-08-20 11:49:00', '2019-08-20 11:50:00', '2019-08-20 11:51:00', '2019-08-20 11:52:00', '2019-08-20 11:53:00', '2019-08-20 11:54:00', '2019-08-20 11:55:00', '2019-08-20 11:56:00', '2019-08-20 11:57:00', '2019-08-20 11:58:00', '2019-08-20 11:59:00', '2019-08-20 12:00:00'], dtype='datetime64[ns]', freq=None)

已经试过了:


a, b=   pd.to_datetime(a, format='%H:%M:%S').time,     pd.to_datetime(b, format='%H:%M:%S').time

selected_idx     = pd.Series(index_.time, index=index_)
selected_idx     = selected_idx.loc[(selected_idx>=a) & (selected_idx<=b)]

但它会导致:


  File "D:\Main.py", line 946, in main_bars
   selected_idx     = selected_idx.loc[(selected_idx>=a) & (selected_idx<=b)]

  File "c:\users\trent\appdata\local\programs\python\python37\lib\site-packages\pandas\core\ops\common.py", line 64, in new_method
    return method(self, other)

  File "c:\users\trent\appdata\local\programs\python\python37\lib\site-packages\pandas\core\ops\__init__.py", line 529, in wrapper
    res_values = comparison_op(lvalues, rvalues, op)

  File "c:\users\trent\appdata\local\programs\python\python37\lib\site-packages\pandas\core\ops\array_ops.py", line 247, in comparison_op
    res_values = comp_method_OBJECT_ARRAY(op, lvalues, rvalues)

  File "c:\users\trent\appdata\local\programs\python\python37\lib\site-packages\pandas\core\ops\array_ops.py", line 57, in comp_method_OBJECT_ARRAY
    result = libops.scalar_compare(x.ravel(), y, op)

  File "pandas\_libs\ops.pyx", line 96, in pandas._libs.ops.scalar_compare

TypeError: '>=' not supported between instances of 'datetime.time' and 'builtin_function_or_method'
 

否则我会很高兴用另一种方式来做到这一点^^

关于什么不起作用的更新:

更新(什么也不起作用):

import pandas as pd, numpy as np

a,b = pd.to_datetime("07:21:39", format='%H:%M:%S').time, pd.to_datetime("22:00:01" , format='%H:%M:%S').time

index_ = pd.DatetimeIndex(['2019-08-20 10:21:00', '2019-08-20 10:22:00', '2019-08-20 10:23:00', '2019-08-20 10:24:00', '2019-08-20 10:25:00', '2019-08-20 10:26:00', '2019-08-20 10:27:00', '2019-08-20 10:28:00', '2019-08-20 10:29:00', '2019-08-20 10:30:00', '2019-08-20 10:31:00', '2019-08-20 10:32:00', '2019-08-20 10:33:00', '2019-08-20 10:34:00', '2019-08-20 10:35:00', '2019-08-20 10:36:00', '2019-08-20 10:37:00', '2019-08-20 10:38:00', '2019-08-20 10:39:00', '2019-08-20 10:40:00', '2019-08-20 10:41:00', '2019-08-20 10:42:00', '2019-08-20 10:43:00', '2019-08-20 10:44:00', '2019-08-20 10:45:00', '2019-08-20 10:46:00', '2019-08-20 10:47:00', '2019-08-20 10:48:00', '2019-08-20 10:49:00', '2019-08-20 10:50:00', '2019-08-20 10:51:00', '2019-08-20 10:52:00', '2019-08-20 10:53:00', '2019-08-20 10:54:00', '2019-08-20 10:55:00', '2019-08-20 10:56:00', '2019-08-20 10:57:00', '2019-08-20 10:58:00', '2019-08-20 10:59:00', '2019-08-20 11:00:00', '2019-08-20 11:01:00', '2019-08-20 11:02:00', '2019-08-20 11:03:00', '2019-08-20 11:04:00', '2019-08-20 11:05:00', '2019-08-20 11:06:00', '2019-08-20 11:07:00', '2019-08-20 11:08:00', '2019-08-20 11:09:00', '2019-08-20 11:10:00', '2019-08-20 11:11:00', '2019-08-20 11:12:00', '2019-08-20 11:13:00', '2019-08-20 11:14:00', '2019-08-20 11:15:00', '2019-08-20 11:16:00', '2019-08-20 11:17:00', '2019-08-20 11:18:00', '2019-08-20 11:19:00', '2019-08-20 11:20:00', '2019-08-20 11:21:00', '2019-08-20 11:22:00', '2019-08-20 11:23:00', '2019-08-20 11:24:00', '2019-08-20 11:25:00', '2019-08-20 11:26:00', '2019-08-20 11:27:00', '2019-08-20 11:28:00', '2019-08-20 11:29:00', '2019-08-20 11:30:00', '2019-08-20 11:31:00', '2019-08-20 11:32:00', '2019-08-20 11:33:00', '2019-08-20 11:34:00', '2019-08-20 11:35:00', '2019-08-20 11:36:00', '2019-08-20 11:37:00', '2019-08-20 11:38:00', '2019-08-20 11:39:00', '2019-08-20 11:40:00', '2019-08-20 11:41:00', '2019-08-20 11:42:00', '2019-08-20 11:43:00', '2019-08-20 11:44:00', '2019-08-20 11:45:00', '2019-08-20 11:46:00', '2019-08-20 11:47:00', '2019-08-20 11:48:00', '2019-08-20 11:49:00', '2019-08-20 11:50:00', '2019-08-20 11:51:00', '2019-08-20 11:52:00', '2019-08-20 11:53:00', '2019-08-20 11:54:00', '2019-08-20 11:55:00', '2019-08-20 11:56:00', '2019-08-20 11:57:00', '2019-08-20 11:58:00', '2019-08-20 11:59:00', '2019-08-20 12:00:00'], dtype='datetime64[ns]', freq=None)


print(len(index_))

gra = pd.Series(np.nan, index=index_)
print(gra)
print(len(gra.between(a,b)))

导致以下错误:

 File "C:\Users\....p.py", line 11, in <module>
    print(len(gra.between(a,b)))

 File "c:\users\trent\appdata\local\programs\python\python37\lib\site-packages\pandas\core\series.py", line 4364, in between
    lmask = self >= left

  File "c:\users\trent\appdata\local\programs\python\python37\lib\site-packages\pandas\core\ops\common.py", line 64, in new_method
    return method(self, other)

  File "c:\users\trent\appdata\local\programs\python\python37\lib\site-packages\pandas\core\ops\__init__.py", line 529, in wrapper
    res_values = comparison_op(lvalues, rvalues, op)

  File "c:\users\trent\appdata\local\programs\python\python37\lib\site-packages\pandas\core\ops\array_ops.py", line 253, in comparison_op
    res_values = method(rvalues)

TypeError: '>=' not supported between instances of 'float' and 'builtin_function_or_method'

更新(什么也不起作用2):

a,b =    datetime.datetime.strptime("09:21:39", "%H:%M:%S").time ,  datetime.datetime.strptime( "10:00:01", "%H:%M:%S").time
index_ = pd.DatetimeIndex(['2019-08-20 10:21:00', '2019-08-20 10:22:00', '2019-08-20 10:23:00', '2019-08-20 10:24:00', '2019-08-20 10:25:00', '2019-08-20 10:26:00', '2019-08-20 10:27:00', '2019-08-20 10:28:00', '2019-08-20 10:29:00', '2019-08-20 10:30:00', '2019-08-20 10:31:00', '2019-08-20 10:32:00', '2019-08-20 10:33:00', '2019-08-20 10:34:00', '2019-08-20 10:35:00', '2019-08-20 10:36:00', '2019-08-20 10:37:00', '2019-08-20 10:38:00', '2019-08-20 10:39:00', '2019-08-20 10:40:00', '2019-08-20 10:41:00', '2019-08-20 10:42:00', '2019-08-20 10:43:00', '2019-08-20 10:44:00', '2019-08-20 10:45:00', '2019-08-20 10:46:00', '2019-08-20 10:47:00', '2019-08-20 10:48:00', '2019-08-20 10:49:00', '2019-08-20 10:50:00', '2019-08-20 10:51:00', '2019-08-20 10:52:00', '2019-08-20 10:53:00', '2019-08-20 10:54:00', '2019-08-20 10:55:00', '2019-08-20 10:56:00', '2019-08-20 10:57:00', '2019-08-20 10:58:00', '2019-08-20 10:59:00', '2019-08-20 11:00:00', '2019-08-20 11:01:00', '2019-08-20 11:02:00', '2019-08-20 11:03:00', '2019-08-20 11:04:00', '2019-08-20 11:05:00', '2019-08-20 11:06:00', '2019-08-20 11:07:00', '2019-08-20 11:08:00', '2019-08-20 11:09:00', '2019-08-20 11:10:00', '2019-08-20 11:11:00', '2019-08-20 11:12:00', '2019-08-20 11:13:00', '2019-08-20 11:14:00', '2019-08-20 11:15:00', '2019-08-20 11:16:00', '2019-08-20 11:17:00', '2019-08-20 11:18:00', '2019-08-20 11:19:00', '2019-08-20 11:20:00', '2019-08-20 11:21:00', '2019-08-20 11:22:00', '2019-08-20 11:23:00', '2019-08-20 11:24:00', '2019-08-20 11:25:00', '2019-08-20 11:26:00', '2019-08-20 11:27:00', '2019-08-20 11:28:00', '2019-08-20 11:29:00', '2019-08-20 11:30:00', '2019-08-20 11:31:00', '2019-08-20 11:32:00', '2019-08-20 11:33:00', '2019-08-20 11:34:00', '2019-08-20 11:35:00', '2019-08-20 11:36:00', '2019-08-20 11:37:00', '2019-08-20 11:38:00', '2019-08-20 11:39:00', '2019-08-20 11:40:00', '2019-08-20 11:41:00', '2019-08-20 11:42:00', '2019-08-20 11:43:00', '2019-08-20 11:44:00', '2019-08-20 11:45:00', '2019-08-20 11:46:00', '2019-08-20 11:47:00', '2019-08-20 11:48:00', '2019-08-20 11:49:00', '2019-08-20 11:50:00', '2019-08-20 11:51:00', '2019-08-20 11:52:00', '2019-08-20 11:53:00', '2019-08-20 11:54:00', '2019-08-20 11:55:00', '2019-08-20 11:56:00', '2019-08-20 11:57:00', '2019-08-20 11:58:00', '2019-08-20 11:59:00', '2019-08-20 12:00:00'], dtype='datetime64[ns]', freq=None) 

gra    = pd.Series(index_, index=index_)
gra_ix = pd.MultiIndex.from_tuples(zip( gra.index.date, gra.index.time))  
gra = pd.Series(index_.time, index=index_)
# ==> TypeError: '>=' not supported between instances of 'datetime.time' and 'builtin_function_or_method'

注意(可能会有所帮助):

我注意到时间值 a 和 b 上的“.time”可能是有罪的。因为如果我删除它,错误突然说:

TypeError: '>=' not supported between instances of 'datetime.time' and 'datetime.datetime'

【问题讨论】:

  • 它是日期时间索引吗?然后df.between_time(a,b):pandas.pydata.org/pandas-docs/stable/reference/api/…
  • @ALollz 每天同时进行(因为我的索引包含数千天)还是我必须每天都进行迭代
  • 不,这就是它的魔力。它实际上只是时间之间的子集,与天数无关。
  • @ALollz 听起来不错,但我收到错误:TypeError: '>=' not supported between 'float' 和 'builtin_function_or_method' 的实例。 (见更新的问题)

标签: python pandas datetime time


【解决方案1】:

问题来自.time -> 这是一种方法。

使用带有大括号的 .time() 解决了问题^^

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    相关资源
    最近更新 更多