Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-sonnet-4-20250514· AnthropicHugging Face LMArena Human Preference 140K

時間序列生成: 創建全年每小時的時間索引 這要用什麼程式

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o4-mini-2025-04-16’s answer.
  1. User

    時間序列生成: 創建全年每小時的時間索引 這要用什麼程式

  2. Claude Assistant

    你可以使用 Python 的 pandas 庫來創建全年每小時的時間索引,這是最常用且簡潔的方法:

    import pandas as pd
    
    # 創建2024年全年每小時的時間索引
    hourly_index = pd.date_range(start='2024-01-01', end='2024-12-31 23:00:00', freq='H')
    
    # 或者更簡潔的寫法
    hourly_index = pd.date_range(start='2024-01-01', periods=8784, freq='H')  # 2024年是閏年,共8784小時
    
    print(f"總共有 {len(hourly_index)} 個時間點")
    print("前5個時間點:")
    print(hourly_index[:5])
    print("後5個時間點:")
    print(hourly_index[-5:])
    

    如果你需要創建時間序列數據框:

    import pandas as pd
    import numpy as np
    
    # 創建時間索引
    time_index = pd.date_range(start='2024-01-01', periods=8784, freq='H')
    
    # 創建包含隨機數據的時間序列
    df = pd.DataFrame({
        'timestamp': time_index,
        'value': np.random.randn(len(time_index))  # 隨機數據作為示例
    })
    
    # 或者直接將時間設為索引
    df = pd.DataFrame(
        {'value': np.random.randn(len(time_index))},
        index=time_index
    )
    

    其他程式語言的選擇:

    R語言:

    library(lubridate)
    hourly_seq <- seq(ymd_hms("2024-01-01 00:00:00"), 
                      ymd_hms("2024-12-31 23:00:00"), 
                      by = "hour")
    

    JavaScript:

    // 使用moment.js或date-fns庫會更方便
    const moment = require('moment');
    let hourlyTimes = [];
    let current = moment('2024-01-01');
    while (current.year() === 2024) {
        hourlyTimes.push(current.clone());
        current.add(1, 'hour');
    }
    

    pandas 是處理時間序列數據最推薦的工具,功能強大且語法簡潔。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.