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

非时序数据集新增week列及按5天划分weekOftheYear列的技术咨询

Got it, let's break down your two requirements one by one with practical pandas solutions tailored to your needs:

1. Adding a week Column to Non-Time-Series Datasets

First, let's clarify what a "non-time-series" dataset might mean here—usually it lacks an explicit date column. We'll cover two common scenarios:

Scenario A: Data follows a hidden date sequence

If your rows are ordered by a specific start date (even if not recorded), you can generate a date range first, then extract the week number:

import pandas as pd

# Example non-time-series DataFrame
df = pd.DataFrame({'sales': [150, 200, 180, 250, 300]})

# Define your data's start date
start_date = '2024-01-01'
# Generate matching date range
df['date'] = pd.date_range(start=start_date, periods=len(df))
# Extract ISO week number (starts on Monday, standard for most use cases)
df['week'] = df['date'].dt.isocalendar().week

Scenario B: You just need sequential week labels

If you don't care about real calendar weeks and just want to group rows into weekly batches (e.g., every 7 rows = 1 week), use integer division:

# Assign week numbers starting from 1, 7 rows per week
df['week'] = (df.index // 7) + 1

2. Creating weekOftheYear for DataFrame with day (1-365, with missing values)

Your rule of "every 5 days = 1 week" can be solved with ceiling division, and we'll make sure missing values stay intact:

import pandas as pd
import numpy as np

# Example DataFrame with missing day values
df = pd.DataFrame({'day': [1, 4, 5, 6, 10, np.nan, 13, 365]})

# Calculate week number: (day + 4) // 5 ensures correct grouping
# Missing values (NaN) will automatically remain as NaN
df['weekOftheYear'] = (df['day'] + 4) // 5

# Optional: Use nullable integer type to keep NaN and integer values compatible
df['weekOftheYear'] = df['weekOftheYear'].astype('Int64')

Why this logic works:

  • Day 1-5: (day +4)//5 equals 1 → Week 1
  • Day 6-10: (day +4)//5 equals 2 → Week 2
  • All the way to day 365: (365+4)//5 = 369//5 = 73.8 → floor division gives 73, which is the 73rd 5-day week

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:56:16