ZhuLinsen/daily_stock_analysis · error · DataFetchError
Pytdx 获取数据失败: {e}
Error message
Pytdx 获取数据失败: {e} What it means
Catch-all DataFetchError wrapping any non-DataFetchError exception raised inside the pytdx fetch loop (to_df conversion, pandas datetime parsing, date filtering, or TDX API errors). The 'from e' chain preserves the original exception for diagnosis.
Source
Thrown at data_provider/pytdx_fetcher.py:374
count=count
)
if data is None or len(data) == 0:
raise DataFetchError(f"Pytdx 未查询到 {stock_code} 的数据")
# 转换为 DataFrame
df = api.to_df(data)
# 过滤日期范围
df['datetime'] = pd.to_datetime(df['datetime'])
df = df[(df['datetime'] >= start_date) & (df['datetime'] <= end_date)]
return df
except Exception as e:
if isinstance(e, DataFetchError):
raise
raise DataFetchError(f"Pytdx 获取数据失败: {e}") from e
def _normalize_data(self, df: pd.DataFrame, stock_code: str) -> pd.DataFrame:
"""
标准化 Pytdx 数据
Pytdx 返回的列名:
datetime, open, high, low, close, vol, amount
需要映射到标准列名:
date, open, high, low, close, volume, amount, pct_chg
"""
df = df.copy()
# 列名映射
column_mapping = {
'datetime': 'date',
'vol': 'volume',
}View on GitHub (pinned to 5159bd72e8)
Solutions
- Inspect the chained cause (raise ... from e — log e.__cause__) to identify whether it is a parsing, protocol, or data-shape failure.
- Pin/upgrade pytdx to a version whose to_df output matches the normalization code.
- Wrap at the manager level with provider fallback so a single pytdx parse failure does not kill the analysis run.
Example fix
# before
try:
df = pytdx_fetcher.get_stock_data(code, start, end)
except DataFetchError as e:
raise # cause hidden, run aborts
# after
try:
df = pytdx_fetcher.get_stock_data(code, start, end)
except DataFetchError as e:
logger.warning(f"pytdx fetch failed: {e}; cause={e.__cause__}")
df = akshare_fetcher.get_stock_data(code, start, end) Defensive patterns
Strategy: try-catch
Try / catch
try:
df = pytdx_fetcher.get_stock_data(code, start, end)
except DataFetchError as e:
logger.warning(f"pytdx failure, cause: {e.__cause__!r}")
df = akshare_fetcher.get_stock_data(code, start, end) # or re-raise for non-fetch contexts Prevention
- Always log the chained cause (__cause__) for wrapper errors before deciding on a fix.
- Pin the pytdx version so to_df output columns stay stable.
- Do not share one TDX session across threads; open a session per request via _pytdx_session().
When it happens
Trigger: api.to_df(data) failing on malformed payloads; pandas errors while parsing 'datetime' strings; KeyError from unexpected DataFrame columns; low-level TDX protocol errors surfacing mid-request.
Common situations: pytdx version changes altering returned columns; partially received network payloads; float precision / NaN issues in vol/amount columns breaking to_df; concurrent use of a non-thread-safe api session.
Related errors
- Pytdx 无法连接任何服务器
- Akshare 获取 ETF 数据失败: {e}
- Akshare 获取美股数据失败: {e}
- Akshare 获取港股数据失败: {e}
- Baostock 获取数据失败: {e}
AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15).
Data as JSON: /api/errors/225382fc9db3e0bc.
Report an issue: GitHub.