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

如何基于首行数据创建含100行时序数据的Polars DataFrame

使用Polars生成指定虚拟DataFrame

你可以通过以下两种方式实现需求:

方法一:基于列表构造

直接生成时间戳和Counter的列表,再传入Polars创建DataFrame:

import polars as pl

# 起始时间戳(秒)
start_ts = 164323232
# 每天对应的秒数
daily_interval = 86400

# 生成100个间隔1天的时间戳
timestamps = [start_ts + i * daily_interval for i in range(100)]
# 首行Counter为原始值2,其余行设为0
counter_values = [2] + [0] * 99

# 创建DataFrame
df = pl.DataFrame({
    "timestamp(secs)": timestamps,
    "Counter": counter_values
})

# 查看前5行验证结果
print(df.head())

方法二:使用Polars内置时间函数

利用Polars的date_range函数生成时间序列,更贴合Polars的用法:

import polars as pl

start_ts = 164323232
# 将起始时间戳转为Polars datetime类型
start_datetime = pl.from_epoch(start_ts, unit="s")

# 生成包含100个日期的序列,间隔1天
date_sequence = pl.date_range(
    start=start_datetime,
    end=start_datetime + pl.duration(days=99),
    interval="1d",
    eager=True
)
# 将datetime转回秒级时间戳
timestamps = date_sequence.epoch(unit="s")
# 构造Counter列
counter_values = pl.Series([2] + [0] * 99)

# 创建DataFrame
df = pl.DataFrame({
    "timestamp(secs)": timestamps,
    "Counter": counter_values
})

# 验证结果
print(df.head())

两种方法最终都会生成包含100行的DataFrame:首行保留原始的时间戳和Counter值,后续每行时间戳递增1天(86400秒),Counter字段均为0。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 06:18:28