fix(flask_server): 根据资产类型动态选择日期格式精度
问题修复: - 加密货币分钟级数据需要保留时分秒 - 日线数据只需日期(年月日) - 之前硬编码 '%Y-%m-%d' 导致分钟级数据丢失时间信息 修复方案: - API 层根据 final_type 判断:CRYPTO 使用 '%Y-%m-%d %H:%M:%S',其他使用 '%Y-%m-%d' - build_premium_result_from_attrs 自动检测索引是否包含时间部分 - build_premium_result 同样支持动态日期格式 - dataframe_to_json 已支持 asset_type 参数(之前已实现) 测试验证: - BTC 小时级数据: '2024-01-01 00:00:00' ✅ - 000300.SH 日线数据: '2024-01-02' ✅ - ETF 溢价率日期: '2026-05-22' ✅
This commit is contained in:
@@ -436,15 +436,22 @@ def build_premium_result(premium_series: pd.Series) -> Dict:
|
||||
if premium_series is None or len(premium_series) == 0:
|
||||
return {}
|
||||
|
||||
# 根据索引是否包含时间部分决定日期格式
|
||||
has_time = any(
|
||||
t.hour != 0 or t.minute != 0 or t.second != 0
|
||||
for t in premium_series.index
|
||||
)
|
||||
date_format = '%Y-%m-%d %H:%M:%S' if has_time else '%Y-%m-%d'
|
||||
|
||||
# 转换为日期-溢价率列表
|
||||
premium_data = [
|
||||
{"date": date.strftime('%Y-%m-%d'), "premium": round(premium, 6)}
|
||||
{"date": date.strftime(date_format), "premium": round(premium, 6)}
|
||||
for date, premium in premium_series.items()
|
||||
]
|
||||
|
||||
# 最新溢价率
|
||||
latest_premium = premium_series.iloc[-1]
|
||||
latest_date = premium_series.index[-1].strftime('%Y-%m-%d')
|
||||
latest_date = premium_series.index[-1].strftime(date_format)
|
||||
|
||||
return {
|
||||
"premium_series": premium_data,
|
||||
@@ -496,15 +503,23 @@ def build_premium_result_from_attrs(premium_data) -> Dict:
|
||||
|
||||
premium_series.index.name = 'date'
|
||||
|
||||
# 根据索引是否包含时间部分决定日期格式
|
||||
# 如果所有时间都是 00:00:00,使用天级格式;否则使用分钟级格式
|
||||
has_time = any(
|
||||
t.hour != 0 or t.minute != 0 or t.second != 0
|
||||
for t in premium_series.index
|
||||
)
|
||||
date_format = '%Y-%m-%d %H:%M:%S' if has_time else '%Y-%m-%d'
|
||||
|
||||
# 转换为日期-溢价率列表
|
||||
premium_list = [
|
||||
{"date": date.strftime('%Y-%m-%d'), "premium": round(float(premium), 6)}
|
||||
{"date": date.strftime(date_format), "premium": round(float(premium), 6)}
|
||||
for date, premium in premium_series.items()
|
||||
]
|
||||
|
||||
# 最新溢价率
|
||||
latest_premium = float(premium_series.iloc[-1])
|
||||
latest_date = premium_series.index[-1].strftime('%Y-%m-%d')
|
||||
latest_date = premium_series.index[-1].strftime(date_format)
|
||||
|
||||
return {
|
||||
"premium_series": premium_list,
|
||||
@@ -742,13 +757,17 @@ def get_ohlcv():
|
||||
if 'attrs' in result:
|
||||
attrs = result['attrs']
|
||||
|
||||
# 根据资产类型决定日期格式精度
|
||||
# 加密货币使用分钟级,其他使用天级
|
||||
date_format = '%Y-%m-%d %H:%M:%S' if final_type == AssetType.CRYPTO else '%Y-%m-%d'
|
||||
|
||||
# 提取净值到顶层(方便调用方使用)
|
||||
if 'nav' in attrs:
|
||||
nav_df = attrs['nav']
|
||||
if isinstance(nav_df, pd.DataFrame):
|
||||
# 将 DataFrame 转换为列表格式(JSON 可序列化)
|
||||
nav_df_copy = nav_df.reset_index().copy()
|
||||
nav_df_copy['date'] = nav_df_copy['date'].dt.strftime('%Y-%m-%d')
|
||||
nav_df_copy['date'] = nav_df_copy['date'].dt.strftime(date_format)
|
||||
nav_dict = {
|
||||
'data': nav_df_copy.to_dict(orient='records'),
|
||||
'count': len(nav_df_copy)
|
||||
@@ -769,7 +788,7 @@ def get_ohlcv():
|
||||
if isinstance(value, pd.DataFrame):
|
||||
df_copy = value.reset_index().copy()
|
||||
if 'date' in df_copy.columns:
|
||||
df_copy['date'] = df_copy['date'].dt.strftime('%Y-%m-%d')
|
||||
df_copy['date'] = df_copy['date'].dt.strftime(date_format)
|
||||
attrs_serializable[key] = {
|
||||
'data': df_copy.to_dict(orient='records'),
|
||||
'count': len(df_copy)
|
||||
@@ -777,7 +796,7 @@ def get_ohlcv():
|
||||
elif isinstance(value, pd.Series):
|
||||
# 将 Series 索引转换为字符串
|
||||
series_copy = value.copy()
|
||||
series_copy.index = series_copy.index.strftime('%Y-%m-%d')
|
||||
series_copy.index = series_copy.index.strftime(date_format)
|
||||
attrs_serializable[key] = {
|
||||
'type': 'series',
|
||||
'data': series_copy.to_dict(),
|
||||
|
||||
Reference in New Issue
Block a user