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

如何将印度央行RBI的LAF走廊宽度不规则数据转为日度时间序列?

把LAF走廊宽度的时间段数据转换为每日时间序列

嘿,这个需求在金融时间序列处理里很常见,用Python的pandas库就能高效搞定,我给你一步步拆解操作:

步骤1:准备环境与原始数据

首先确保你已经安装了pandas,如果没有的话可以用pip install pandas安装。

先把你的原始数据整理成结构化格式,比如模拟你的数据如下:

import pandas as pd

# 模拟你提供的LAF走廊宽度数据
raw_data = [
    ("27/04/2001 - 29/04/2001", 225),
    ("30/04/2001 - 27/05/2001", 200),
    ("28/05/2001 - 06/06/2001", 225),
    ("07/06/2001 - 04/03/2002", 200)
]

# 转换成初始DataFrame
df = pd.DataFrame(raw_data, columns=["time_period", "laf_corridor_width"])

如果你的数据是从文本/CSV文件读取的,比如原始文件是用|分隔的,可以这样读取并清理:

# 读取带|分隔的原始文件
df = pd.read_csv("your_data_file.txt", sep="|", header=None)
# 提取有效列(时间段和宽度值),清理空值
df = df[[0,1]].dropna().rename(columns={0: "time_period", 1: "laf_corridor_width"})
# 去除字符串两端的空格
df["time_period"] = df["time_period"].str.strip()
df["laf_corridor_width"] = df["laf_corridor_width"].astype(int)

步骤2:拆分时间段为开始/结束日期

把每个时间段拆成单独的开始和结束日期,并转换为datetime格式(注意你的日期是日/月/年格式):

# 拆分时间段
df[["start_date", "end_date"]] = df["time_period"].str.split(" - ", expand=True)

# 转换为datetime类型
df["start_date"] = pd.to_datetime(df["start_date"], format="%d/%m/%Y")
df["end_date"] = pd.to_datetime(df["end_date"], format="%d/%m/%Y")

步骤3:生成每日时间序列

定义一个函数,对每一行数据生成从开始到结束的所有日期,并关联对应的LAF宽度值,最后合并所有结果:

def create_daily_records(row):
    # 生成该时间段内的所有日期
    daily_dates = pd.date_range(start=row["start_date"], end=row["end_date"], freq="D")
    # 返回包含日期和宽度的DataFrame
    return pd.DataFrame({
        "date": daily_dates,
        "laf_corridor_width": row["laf_corridor_width"]
    })

# 应用函数并合并所有结果
daily_time_series = pd.concat(df.apply(create_daily_records, axis=1), ignore_index=True)

步骤4:验证与导出结果

现在你可以查看生成的每日数据:

# 查看前10条记录
print(daily_time_series.head(10))

# 导出为CSV文件(可选)
daily_time_series.to_csv("daily_laf_corridor.csv", index=False)

这样处理后,你就得到了2001年4月到2011年3月期间每日一条的LAF走廊宽度时间序列数据了。如果你的数据量很大,这个方法也能高效处理,因为pandas的向量化操作性能很不错。

内容的提问来源于stack exchange,提问作者Arjun J Rao

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:03:44