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

如何在Python中拆分日间时段并将Timestamp列表转换为哑铃图所需的四列表格?

How to Split Daytime Intervals & Format Timestamp Data for Dumbbell Plots in Python

Let’s break this down into two straightforward, actionable tasks—no fluff, just working code and explanations tailored to your needs.

1. Splitting Daytime Intervals from Timestamps

First, let’s define common daytime intervals (you can tweak these to match your exact requirements). We’ll use pandas since it’s the go-to tool for timestamp manipulation in Python.

Step-by-Step Implementation:

  • Start by importing pandas and setting up your timestamp data (replace the sample list with your actual data).
  • Extract the hour from each timestamp, then map hours to your predefined daytime blocks.
  • Convert the results into a clean, readable table.
import pandas as pd

# Replace this with your actual Timestamp list
timestamps = pd.to_datetime([
    "2024-03-10 08:15:00", "2024-03-10 14:30:00",
    "2024-03-10 20:45:00", "2024-03-11 03:00:00"
])

# Create a DataFrame to organize the data
df = pd.DataFrame({"timestamp": timestamps})

# Define your custom daytime intervals (adjust these as needed)
def assign_daytime_interval(hour):
    if 6 <= hour < 12:
        return "Morning (6:00–12:00)"
    elif 12 <= hour < 18:
        return "Afternoon (12:00–18:00)"
    elif 18 <= hour < 24:
        return "Evening (18:00–24:00)"
    else:
        return "Night (00:00–6:00)"

# Add the interval column to the DataFrame
df["daytime_interval"] = df["timestamp"].dt.hour.apply(assign_daytime_interval)

# View the final table
print(df)

Sample Output:

timestamp          daytime_interval
0 2024-03-10 08:15:00  Morning (6:00–12:00)
1 2024-03-10 14:30:00  Afternoon (12:00–18:00)
2 2024-03-10 20:45:00  Evening (18:00–24:00)
3 2024-03-11 03:00:00  Night (00:00–6:00)

2. Formatting Timestamp Data for Dumbbell Plots

Dumbbell plots rely on comparing start and end values per group. For your use case, we’ll group timestamps by year and month, then pull the first (start) and last (end) day of each group to build your 4-column table.

Step-by-Step Implementation:

  • Extract year, month, and day from each timestamp.
  • Group by year and month, calculate the minimum (start day) and maximum (end day) for each group.
  • Reshape the results to match your desired column structure.
# Using the same DataFrame from the first task (or your own data)
df["year"] = df["timestamp"].dt.year
df["month"] = df["timestamp"].dt.month
df["day"] = df["timestamp"].dt.day

# Group by year + month, get start and end days
dumbbell_table = df.groupby(["year", "month"])["day"].agg(["min", "max"]).reset_index()

# Rename columns to match your requirement
dumbbell_table.columns = ["year", "month", "start day", "end day"]

# View the final table ready for plotting
print(dumbbell_table)

Sample Output:

year  month  start day  end day
0  2024      3         10       11

Quick Notes:

  • If your timestamps span multiple years/months, this code will automatically group each unique (year, month) pair.
  • For plotting, you can use libraries like seaborn or matplotlib—this table gives you exactly the structure needed to map start/end days to each (year, month) group for your dumbbell plot.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 08:17:41