免责声明。我是极地新手。
我有一个使用极坐标生成的数据框:
import polars as pl
import numpy as np
from datetime import datetime
# Créer une plage de dates
date_ranges = pl.date_range(start=datetime(2000, 1, 1), end=datetime(2025, 12, 31), interval="1d", eager=True)
# Générer des données aléatoires
data = {
"date": ['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01', '2020-05-01', '2020-06-01', '2020-07-01', '2020-08-01'],
"prix1": [50,55, 50, 60, 70, 80, 90, 100],
"prix2": [60,65, 60, 70, 80, 90, 100, 110],
"prix3": [70,75, 70, 80, 90, 100, 110, 120],
"prix4": [80,85, 80, 90, 100, 110, 120, 130],
"prix5": [90,95, 90, 100, 110, 120, 130, 140],
}
# Créer le DataFrame
df = pl.DataFrame(data)
我想获得最近 3 个月、6 个月和 1 年的价格表现,即获得如下所示的数据框:
一等奖 | 二等奖 | 三等奖 | |
---|---|---|---|
3 个月表现 | =100/70-1 | =110/80-1 | =120/90-1 |
Perf 6 个月 | =100/55-1 | =110/65-1 | =120/75-1 |
或者类似的东西。输出是否转置也没关系
试试这个
import polars as pl
import numpy as np
from datetime import datetime, timedelta
date_ranges = pl.date_range(start=datetime(2000, 1, 1), end=datetime(2025, 12, 31), interval="1d", eager=True)
data = {
"date": date_ranges,
"prix1": 100 + np.random.randn(len(date_ranges.to_list())),
"prix2": 100 + np.random.randn(len(date_ranges.to_list())),
"prix3": 100 + np.random.randn(len(date_ranges.to_list())),
"prix4": 200 + np.random.randn(len(date_ranges.to_list())),
"prix5": 300 + np.random.randn(len(date_ranges.to_list())),
}
df = pl.DataFrame(data)
end_date = datetime(2025, 12, 31)
three_months_ago = end_date - timedelta(days=90)
six_months_ago = end_date - timedelta(days=180)
one_year_ago = end_date - timedelta(days=365)
def calculate_performance(df, start_date, end_date):
start_prices = df.filter(pl.col("date") == start_date).select(["prix1", "prix2", "prix3"]).row(0)
end_prices = df.filter(pl.col("date") == end_date).select(["prix1", "prix2", "prix3"]).row(0)
performance = [(end - start) / start * 100 for start, end in zip(start_prices, end_prices)]
return performance
start_3_months = df.filter(pl.col("date") == three_months_ago).select(pl.col("date")).row(0)[0]
start_6_months = df.filter(pl.col("date") == six_months_ago).select(pl.col("date")).row(0)[0]
start_1_year = df.filter(pl.col("date") == one_year_ago).select(pl.col("date")).row(0)[0]
perf_3_months = calculate_performance(df, start_3_months, end_date)
perf_6_months = calculate_performance(df, start_6_months, end_date)
perf_1_year = calculate_performance(df, start_1_year, end_date)
performance_df = pl.DataFrame({
"Performance": ["3 Month", "6 Month", "1 Year"],
"Prix 1": [perf_3_months[0], perf_6_months[0], perf_1_year[0]],
"Prix 2": [perf_3_months[1], perf_6_months[1], perf_1_year[1]],
"Prix 3": [perf_3_months[2], perf_6_months[2], perf_1_year[2]],
})
print(performance_df)