Spaces:
Paused
Paused
Commit ·
0636b12
1
Parent(s): 2d86dae
"fix:robust-column-detection"
Browse files- sync_data.py +106 -22
sync_data.py
CHANGED
|
@@ -115,44 +115,128 @@ def get_stock_list() -> pd.DataFrame:
|
|
| 115 |
|
| 116 |
# ETF (增加容错)
|
| 117 |
try:
|
| 118 |
-
df_etf = ak.fund_etf_spot_em()
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
except Exception as e:
|
| 124 |
logger.warning(f"ETF list fetch failed: {e}")
|
| 125 |
|
| 126 |
# LOF
|
| 127 |
try:
|
| 128 |
-
df_lof = ak.fund_lof_spot_em()
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
except Exception as e:
|
| 134 |
logger.warning(f"LOF list fetch failed: {e}")
|
| 135 |
|
| 136 |
# REITs
|
| 137 |
try:
|
| 138 |
-
df_reits = ak.reits_realtime_em()
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
except Exception as e:
|
| 144 |
logger.warning(f"REITs list fetch failed: {e}")
|
| 145 |
|
| 146 |
# 可转债
|
| 147 |
try:
|
| 148 |
df_cb = ak.bond_zh_hs_cov_spot()
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
except Exception as e:
|
| 157 |
logger.warning(f"Convertible bond list fetch failed: {e}")
|
| 158 |
|
|
|
|
| 115 |
|
| 116 |
# ETF (增加容错)
|
| 117 |
try:
|
| 118 |
+
df_etf = ak.fund_etf_spot_em()
|
| 119 |
+
# 检查实际列名
|
| 120 |
+
code_cols = ['代码', 'code', 'fund_code', 'ETF代码']
|
| 121 |
+
name_cols = ['名称', 'name', 'fund_name', 'ETF名称']
|
| 122 |
+
|
| 123 |
+
c_code = None
|
| 124 |
+
c_name = None
|
| 125 |
+
|
| 126 |
+
for col in code_cols:
|
| 127 |
+
if col in df_etf.columns:
|
| 128 |
+
c_code = col
|
| 129 |
+
break
|
| 130 |
+
|
| 131 |
+
for col in name_cols:
|
| 132 |
+
if col in df_etf.columns:
|
| 133 |
+
c_name = col
|
| 134 |
+
break
|
| 135 |
+
|
| 136 |
+
if c_code and c_name:
|
| 137 |
+
df_etf = df_etf[[c_code, c_name]]
|
| 138 |
+
df_etf.columns = ['code', 'name']
|
| 139 |
+
df_etf['market'] = 'ETF'
|
| 140 |
+
all_lists.append(df_etf)
|
| 141 |
+
logger.info(f"ETF list fetched: {len(df_etf)} funds")
|
| 142 |
+
else:
|
| 143 |
+
logger.warning(f"Could not find code/name columns in ETF data. Available columns: {df_etf.columns.tolist()}")
|
| 144 |
except Exception as e:
|
| 145 |
logger.warning(f"ETF list fetch failed: {e}")
|
| 146 |
|
| 147 |
# LOF
|
| 148 |
try:
|
| 149 |
+
df_lof = ak.fund_lof_spot_em()
|
| 150 |
+
# 检查实际列名
|
| 151 |
+
code_cols = ['代码', 'code', 'fund_code', 'LOF代码']
|
| 152 |
+
name_cols = ['名称', 'name', 'fund_name', 'LOF名称']
|
| 153 |
+
|
| 154 |
+
c_code = None
|
| 155 |
+
c_name = None
|
| 156 |
+
|
| 157 |
+
for col in code_cols:
|
| 158 |
+
if col in df_lof.columns:
|
| 159 |
+
c_code = col
|
| 160 |
+
break
|
| 161 |
+
|
| 162 |
+
for col in name_cols:
|
| 163 |
+
if col in df_lof.columns:
|
| 164 |
+
c_name = col
|
| 165 |
+
break
|
| 166 |
+
|
| 167 |
+
if c_code and c_name:
|
| 168 |
+
df_lof = df_lof[[c_code, c_name]]
|
| 169 |
+
df_lof.columns = ['code', 'name']
|
| 170 |
+
df_lof['market'] = 'LOF'
|
| 171 |
+
all_lists.append(df_lof)
|
| 172 |
+
logger.info(f"LOF list fetched: {len(df_lof)} funds")
|
| 173 |
+
else:
|
| 174 |
+
logger.warning(f"Could not find code/name columns in LOF data. Available columns: {df_lof.columns.tolist()}")
|
| 175 |
except Exception as e:
|
| 176 |
logger.warning(f"LOF list fetch failed: {e}")
|
| 177 |
|
| 178 |
# REITs
|
| 179 |
try:
|
| 180 |
+
df_reits = ak.reits_realtime_em()
|
| 181 |
+
# 检查实际列名
|
| 182 |
+
code_cols = ['代码', 'code', 'REITs代码']
|
| 183 |
+
name_cols = ['名称', 'name', 'REITs名称']
|
| 184 |
+
|
| 185 |
+
c_code = None
|
| 186 |
+
c_name = None
|
| 187 |
+
|
| 188 |
+
for col in code_cols:
|
| 189 |
+
if col in df_reits.columns:
|
| 190 |
+
c_code = col
|
| 191 |
+
break
|
| 192 |
+
|
| 193 |
+
for col in name_cols:
|
| 194 |
+
if col in df_reits.columns:
|
| 195 |
+
c_name = col
|
| 196 |
+
break
|
| 197 |
+
|
| 198 |
+
if c_code and c_name:
|
| 199 |
+
df_reits = df_reits[[c_code, c_name]]
|
| 200 |
+
df_reits.columns = ['code', 'name']
|
| 201 |
+
df_reits['market'] = 'REITs'
|
| 202 |
+
all_lists.append(df_reits)
|
| 203 |
+
logger.info(f"REITs list fetched: {len(df_reits)} products")
|
| 204 |
+
else:
|
| 205 |
+
logger.warning(f"Could not find code/name columns in REITs data. Available columns: {df_reits.columns.tolist()}")
|
| 206 |
except Exception as e:
|
| 207 |
logger.warning(f"REITs list fetch failed: {e}")
|
| 208 |
|
| 209 |
# 可转债
|
| 210 |
try:
|
| 211 |
df_cb = ak.bond_zh_hs_cov_spot()
|
| 212 |
+
# 检查实际列名
|
| 213 |
+
logger.info(f"Convertible bond columns: {df_cb.columns.tolist()}")
|
| 214 |
+
|
| 215 |
+
# 尝试找到正确的代码列名
|
| 216 |
+
code_cols = ['代码', 'symbol', 'bond_code', '代码', '转债代码', '代码']
|
| 217 |
+
name_cols = ['名称', 'name', 'bond_name', '转债名称', '名称']
|
| 218 |
+
|
| 219 |
+
c_code = None
|
| 220 |
+
c_name = None
|
| 221 |
+
|
| 222 |
+
for col in code_cols:
|
| 223 |
+
if col in df_cb.columns:
|
| 224 |
+
c_code = col
|
| 225 |
+
break
|
| 226 |
+
|
| 227 |
+
for col in name_cols:
|
| 228 |
+
if col in df_cb.columns:
|
| 229 |
+
c_name = col
|
| 230 |
+
break
|
| 231 |
+
|
| 232 |
+
if c_code and c_name:
|
| 233 |
+
df_cb = df_cb[[c_code, c_name]]
|
| 234 |
+
df_cb.columns = ['code', 'name']
|
| 235 |
+
df_cb['market'] = '可转债'
|
| 236 |
+
all_lists.append(df_cb)
|
| 237 |
+
logger.info(f"Convertible bond list fetched: {len(df_cb)} bonds")
|
| 238 |
+
else:
|
| 239 |
+
logger.warning(f"Could not find code/name columns in convertible bond data. Available columns: {df_cb.columns.tolist()}")
|
| 240 |
except Exception as e:
|
| 241 |
logger.warning(f"Convertible bond list fetch failed: {e}")
|
| 242 |
|