【问题标题】:Iterate over custom date time index in pandas?迭代熊猫中的自定义日期时间索引?
【发布时间】:2022-08-14 00:35:27
【问题描述】:

我有一个带有时间戳索引的大型数据框。我使用.to_pydatetime() 转换了这个索引。我试图以 3 分钟为单位迭代这个索引,但是虽然数据框有超过 2,000 行,但我的迭代在 53 处停止。代码如下:

# create Time column out of index for comparison purposes
historydf[\'Time\']=historydf.index
starttime = historydf[\'Time\'][0].to_pydatetime()
endtime = historydf[\'Time\'][2261].to_pydatetime()

example_list=[]
increment = 0
for i in historydf.index:
     if i <= endtime:
          if historydf[\'Time\'][i] == starttime + timedelta(minutes = increment):
                examplelist.append(i)
                increment = increment + 3

但是,此代码仅停留在 53 个值处。显然这小于 2260 /3 (~750)。现在工作了几个小时,无法得到它。任何帮助表示赞赏!

下面是我正在使用的数据帧的 sn-p(如果需要,用于复制/粘贴目的)。请记住,真正的数据框要长得多。

Datetime
2022-08-04 09:30:00-04:00    90.949997
2022-08-04 09:32:00-04:00    90.790001
2022-08-04 09:33:00-04:00    90.730003
2022-08-04 09:34:00-04:00    90.839996
2022-08-04 09:35:00-04:00    90.775002
2022-08-04 09:36:00-04:00    90.769997
2022-08-04 09:37:00-04:00    90.775002
2022-08-04 09:38:00-04:00    90.610001
2022-08-04 09:39:00-04:00    90.860001
2022-08-04 09:40:00-04:00    90.900002
2022-08-04 09:41:00-04:00    91.074997
2022-08-04 09:42:00-04:00    91.120003
2022-08-04 09:43:00-04:00    91.139999
2022-08-04 09:44:00-04:00    91.099998
2022-08-04 09:45:00-04:00    91.205002
2022-08-04 09:46:00-04:00    91.120003
2022-08-04 09:47:00-04:00    91.199997
2022-08-04 09:48:00-04:00    91.114998
2022-08-04 09:49:00-04:00    91.114998
2022-08-04 09:50:00-04:00    91.074997
2022-08-04 09:51:00-04:00    90.970100
2022-08-04 09:52:00-04:00    90.949997
2022-08-04 09:53:00-04:00    91.110001
2022-08-04 09:54:00-04:00    91.224998
2022-08-04 09:55:00-04:00    91.250000
2022-08-04 09:56:00-04:00    91.190002
2022-08-04 09:57:00-04:00    91.074997
2022-08-04 09:58:00-04:00    91.089996
2022-08-04 09:59:00-04:00    91.184998
2022-08-04 10:00:00-04:00    91.070000
2022-08-04 10:01:00-04:00    91.070000
2022-08-04 10:02:00-04:00    91.010002
2022-08-04 10:03:00-04:00    91.010002
2022-08-04 10:04:00-04:00    91.004997
2022-08-04 10:05:00-04:00    91.010002
2022-08-04 10:06:00-04:00    91.139999
2022-08-04 10:07:00-04:00    91.209999
2022-08-04 10:08:00-04:00    91.239998
2022-08-04 10:09:00-04:00    91.209999
2022-08-04 10:11:00-04:00    91.250000
2022-08-04 10:12:00-04:00    91.309998
2022-08-04 10:14:00-04:00    91.279999
2022-08-04 10:15:00-04:00    91.300003
2022-08-04 10:16:00-04:00    91.235001
2022-08-04 10:17:00-04:00    91.320000
2022-08-04 10:18:00-04:00    91.224998
2022-08-04 10:20:00-04:00    91.235001
2022-08-04 10:21:00-04:00    91.214996
2022-08-04 10:22:00-04:00    91.209999
2022-08-04 10:23:00-04:00    91.129997
2022-08-04 10:24:00-04:00    91.139999
2022-08-04 10:25:00-04:00    91.160004
2022-08-04 10:26:00-04:00    91.175003
2022-08-04 10:27:00-04:00    91.154999
2022-08-04 10:28:00-04:00    91.220001
2022-08-04 10:29:00-04:00    91.339996
2022-08-04 10:30:00-04:00    91.239998
2022-08-04 10:31:00-04:00    91.264999
2022-08-04 10:32:00-04:00    91.290001
2022-08-04 10:33:00-04:00    91.239998
  • 如果需要更多信息,请告诉我。我不确定我的解释是否足够......
  • 你能添加一个简单的可重复数据来测试我吗?
  • @RanA 当然。我会将其添加到原始问题中
  • 09:30:00-04:00 是什么?你能解释一下这种格式吗?
  • 检查循环内的第一个 example_list ,

标签: python pandas loops datetime


【解决方案1】:

对于任何想知道的人,我想出了一个更简单的解决方案。

与其麻烦地增加和检查索引与 timedelta 增加的日期时间值,不如简单地检查.minute(或您尝试增加的任何内容)是否可以被您所需的间隔整除。

我通过以下代码在我的代码中完成了这一点:

historydf['Time']=historydf.index
starttime = historydf['Time'][0].to_pydatetime()
endtime = historydf['Time'][2261].to_pydatetime()

example_list=[]
increment = 0
for i in historydf.index:
     if i <= endtime:
         if historydf['Time'][i].minute % 3 == 0:
             example_list.append(i)

这工作得很好。感谢大家的回应!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 2023-01-12
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    相关资源
    最近更新 更多