You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

生成分钟级整数列表:判断时间是否在指定Timestamp区间内

Solution to Create Binary Minute Marker List

Let's break this down into actionable steps using Python and pandas, since you're already working with Timestamp objects:

Step 1: Set Up Dependencies & Input Data

First, make sure you have pandas installed. Then, define your existing time interval list:

import pandas as pd

# Your original time interval list
time_intervals = [
    (pd.Timestamp('2018-12-17 07:30:45', freq='S'), pd.Timestamp('2018-12-17 07:32:45', freq='S')),
    (pd.Timestamp('2018-12-03 07:14:12', freq='S'), pd.Timestamp('2018-12-03 07:15:39', freq='S')),
    (pd.Timestamp('2018-12-03 07:32:47', freq='S'), pd.Timestamp('2018-12-03 07:34:10', freq='S')),
    (pd.Timestamp('2018-12-03 08:00:36', freq='S'), pd.Timestamp('2018-12-03 08:02:28', freq='S')),
    (pd.Timestamp('2018-12-19 07:34:02', freq='S'), pd.Timestamp('2018-12-19 07:34:19', freq='S')),
    (pd.Timestamp('2018-12-19 07:33:26', freq='S'), pd.Timestamp('2018-12-19 07:35:25', freq='S')),
    (pd.Timestamp('2018-12-19 07:49:28', freq='S'), pd.Timestamp('2018-12-19 07:49:44', freq='S')),
    (pd.Timestamp('2018-12-19 07:49:08', freq='S'), pd.Timestamp('2018-12-19 07:50:32', freq='S')),
    (pd.Timestamp('2018-12-18 07:47:24', freq='S'), pd.Timestamp('2018-12-18 07:48:56', freq='S')),
    (pd.Timestamp('2018-12-13 07:56:24', freq='S'), pd.Timestamp('2018-12-13 07:57:58', freq='S'))
]

Step 2: Generate the Full Minute Timeline

Define your time span (Dec 2018 to Apr 2019) and create a sequence of every minute in this period:

# Define the start and end of your time span
start_span = pd.Timestamp('2018-12-01 00:00:00')
end_span = pd.Timestamp('2019-04-30 23:59:59')

# Create a DatetimeIndex of every minute in the span
minute_timeline = pd.date_range(start=start_span, end=end_span, freq='T')

Step 3: Mark Minutes That Fall in Any Interval

We'll create a boolean array first, then convert it to integers (0/1):

# Initialize a boolean array filled with False (0)
in_interval = [False] * len(minute_timeline)

# Check each minute against all intervals
for idx, minute in enumerate(minute_timeline):
    # Convert the minute to a time range (start of minute to end of minute)
    minute_start = minute
    minute_end = minute + pd.Timedelta(minutes=1) - pd.Timedelta(seconds=1)
    
    # Check if this minute overlaps with any interval
    for interval_start, interval_end in time_intervals:
        if (minute_start <= interval_end) and (minute_end >= interval_start):
            in_interval[idx] = True
            break  # No need to check other intervals once we find a match

# Convert boolean list to integer list (0/1)
result_list = [1 if val else 0 for val in in_interval]

Key Notes

  • Overlap Check: We check if the minute's time range overlaps with any interval—this handles cases where an interval starts/ends mid-minute (e.g., an interval from 07:30:45 to 07:32:45 covers the full 07:30, 07:31, and part of 07:32 minutes, so all three minutes get marked as 1).
  • Efficiency: If you have a huge number of intervals, you might want to optimize this by sorting intervals first and using binary search, but for your current list size, the above approach works perfectly.
  • Timeline Length: The length of result_list will equal the total number of minutes between start_span and end_span, which matches your requirement.

内容的提问来源于stack exchange,提问作者user2741831

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 09:42:37