akshare-one 0.3.6__py3-none-any.whl → 0.3.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. akshare_one/__init__.py +214 -214
  2. akshare_one/eastmoney/client.py +80 -80
  3. akshare_one/eastmoney/utils.py +102 -102
  4. akshare_one/indicators.py +395 -395
  5. akshare_one/modules/cache.py +27 -27
  6. akshare_one/modules/financial/base.py +27 -27
  7. akshare_one/modules/financial/eastmoney_direct.py +183 -183
  8. akshare_one/modules/financial/factory.py +46 -46
  9. akshare_one/modules/financial/sina.py +292 -292
  10. akshare_one/modules/historical/base.py +47 -47
  11. akshare_one/modules/historical/eastmoney.py +236 -236
  12. akshare_one/modules/historical/eastmoney_direct.py +78 -78
  13. akshare_one/modules/historical/factory.py +48 -48
  14. akshare_one/modules/historical/sina.py +250 -250
  15. akshare_one/modules/indicators/base.py +158 -158
  16. akshare_one/modules/indicators/factory.py +33 -33
  17. akshare_one/modules/indicators/simple.py +384 -230
  18. akshare_one/modules/indicators/talib.py +263 -263
  19. akshare_one/modules/info/base.py +25 -25
  20. akshare_one/modules/info/eastmoney.py +51 -51
  21. akshare_one/modules/info/factory.py +44 -44
  22. akshare_one/modules/insider/base.py +28 -28
  23. akshare_one/modules/insider/factory.py +44 -44
  24. akshare_one/modules/insider/xueqiu.py +110 -110
  25. akshare_one/modules/news/base.py +22 -22
  26. akshare_one/modules/news/eastmoney.py +43 -43
  27. akshare_one/modules/news/factory.py +44 -44
  28. akshare_one/modules/realtime/base.py +27 -27
  29. akshare_one/modules/realtime/eastmoney.py +53 -53
  30. akshare_one/modules/realtime/eastmoney_direct.py +36 -36
  31. akshare_one/modules/realtime/factory.py +48 -48
  32. akshare_one/modules/realtime/xueqiu.py +57 -57
  33. akshare_one/modules/utils.py +10 -10
  34. {akshare_one-0.3.6.dist-info → akshare_one-0.3.8.dist-info}/METADATA +74 -74
  35. akshare_one-0.3.8.dist-info/RECORD +39 -0
  36. {akshare_one-0.3.6.dist-info → akshare_one-0.3.8.dist-info}/licenses/LICENSE +21 -21
  37. akshare_one-0.3.6.dist-info/RECORD +0 -39
  38. {akshare_one-0.3.6.dist-info → akshare_one-0.3.8.dist-info}/WHEEL +0 -0
  39. {akshare_one-0.3.6.dist-info → akshare_one-0.3.8.dist-info}/top_level.txt +0 -0
@@ -1,292 +1,292 @@
1
- import pandas as pd
2
- import akshare as ak
3
-
4
- from ..cache import cache
5
- from .base import FinancialDataProvider
6
-
7
-
8
- class SinaFinancialReport(FinancialDataProvider):
9
- """Financial data provider for Sina finance reports.
10
-
11
- Provides standardized access to balance sheet, income statement,
12
- and cash flow data from Sina finance API.
13
- """
14
-
15
- def __init__(self, symbol: str) -> None:
16
- super().__init__(symbol)
17
- self.stock = (
18
- f"sh{symbol}" if not symbol.startswith(("sh", "sz", "bj")) else symbol
19
- )
20
-
21
- @cache(
22
- "financial_cache", key=lambda self, symbol=None: f"sina_balance_{self.symbol}"
23
- )
24
- def get_balance_sheet(self) -> pd.DataFrame:
25
- """获取资产负债表数据
26
-
27
- Args:
28
- symbol: 股票代码 (如 "600600")
29
-
30
- Returns:
31
- Standardized DataFrame with balance sheet data
32
- """
33
- raw_df = ak.stock_financial_report_sina(stock=self.stock, symbol="资产负债表")
34
- return self._clean_balance_data(raw_df)
35
-
36
- @cache(
37
- "financial_cache", key=lambda self, symbol=None: f"sina_income_{self.symbol}"
38
- )
39
- def get_income_statement(self) -> pd.DataFrame:
40
- """获取利润表数据
41
-
42
- Args:
43
- symbol: 股票代码 (如 "600600")
44
-
45
- Returns:
46
- Standardized DataFrame with income statement data
47
- """
48
- raw_df = ak.stock_financial_report_sina(stock=self.stock, symbol="利润表")
49
- return self._clean_income_data(raw_df)
50
-
51
- @cache("financial_cache", key=lambda self, symbol=None: f"sina_cash_{self.symbol}")
52
- def get_cash_flow(self) -> pd.DataFrame:
53
- """获取现金流量表数据
54
-
55
- Args:
56
- symbol: 股票代码 (如 "600600")
57
-
58
- Returns:
59
- Standardized DataFrame with cash flow data
60
- """
61
- raw_df = ak.stock_financial_report_sina(stock=self.stock, symbol="现金流量表")
62
- return self._clean_cash_data(raw_df)
63
-
64
- def _clean_cash_data(self, raw_df: pd.DataFrame) -> pd.DataFrame:
65
- """清理和标准化现金流量表数据
66
-
67
- Args:
68
- raw_df: Raw DataFrame from Sina API
69
-
70
- Returns:
71
- Standardized DataFrame with consistent columns
72
- """
73
- # Convert timestamp columns if exists
74
- if "报告日" in raw_df.columns:
75
- raw_df = raw_df.rename(columns={"报告日": "report_date"})
76
- raw_df["report_date"] = pd.to_datetime(
77
- raw_df["report_date"], format="%Y%m%d"
78
- )
79
-
80
- # Define column mappings and required columns
81
- column_mapping = {
82
- "币种": "currency",
83
- "经营活动产生的现金流量净额": "net_cash_flow_from_operations",
84
- "购建固定资产、无形资产和其他长期资产支付的现金": "capital_expenditure",
85
- "取得子公司及其他营业单位支付的现金净额": "business_acquisitions_and_disposals",
86
- "投资活动产生的现金流量净额": "net_cash_flow_from_investing",
87
- "取得借款收到的现金": "issuance_or_repayment_of_debt_securities",
88
- "吸收投资收到的现金": "issuance_or_purchase_of_equity_shares",
89
- "筹资活动产生的现金流量净额": "net_cash_flow_from_financing",
90
- "现金及现金等价物净增加额": "change_in_cash_and_equivalents",
91
- "汇率变动对现金及现金等价物的影响": "effect_of_exchange_rate_changes",
92
- "期末现金及现金等价物余额": "ending_cash_balance",
93
- "销售商品、提供劳务收到的现金": "cash_from_sales",
94
- "收到的税费返还": "tax_refunds_received",
95
- "支付给职工以及为职工支付的现金": "cash_paid_to_employees",
96
- "支付的各项税费": "taxes_paid",
97
- "经营活动现金流入小计": "total_cash_inflow_from_operations",
98
- "经营活动现金流出小计": "total_cash_outflow_from_operations",
99
- "收回投资所收到的现金": "cash_from_investment_recovery",
100
- "取得投资收益收到的现金": "cash_from_investment_income",
101
- "处置固定资产、无形资产收回的现金": "cash_from_asset_sales",
102
- "投资活动现金流入小计": "total_cash_inflow_from_investing",
103
- "投资活动现金流出小计": "total_cash_outflow_from_investing",
104
- "分配股利、利润或偿付利息所支付的现金": "cash_paid_for_dividends_and_interest",
105
- "偿还债务支付的现金": "cash_paid_for_debt_repayment",
106
- "筹资活动现金流入小计": "total_cash_inflow_from_financing",
107
- "筹资活动现金流出小计": "total_cash_outflow_from_financing",
108
- "期初现金及现金等价物余额": "beginning_cash_balance",
109
- "现金的期末余额": "ending_cash",
110
- "现金等价物的期末余额": "ending_cash_equivalents",
111
- }
112
-
113
- required_columns = ["report_date"] + list(column_mapping.values())
114
- return raw_df.rename(columns=column_mapping).reindex(columns=required_columns)
115
-
116
- def _clean_balance_data(self, raw_df: pd.DataFrame) -> pd.DataFrame:
117
- """清理和标准化资产负债表数据
118
-
119
- Args:
120
- raw_df: Raw DataFrame from Sina API
121
-
122
- Returns:
123
- Standardized DataFrame with consistent columns
124
- """
125
- # Convert timestamp columns if exists
126
- if "报告日" in raw_df.columns:
127
- raw_df = raw_df.rename(columns={"报告日": "report_date"})
128
- raw_df["report_date"] = pd.to_datetime(
129
- raw_df["report_date"], format="%Y%m%d"
130
- )
131
-
132
- # Define and apply column mappings in one optimized operation
133
- raw_df = raw_df.rename(
134
- columns={
135
- "币种": "currency",
136
- "资产总计": "total_assets",
137
- "流动资产合计": "current_assets",
138
- "货币资金": "cash_and_equivalents",
139
- "存货": "inventory",
140
- "交易性金融资产": "current_investments",
141
- "应收票据及应收账款": "trade_and_non_trade_receivables",
142
- "非流动资产合计": "non_current_assets",
143
- "固定资产": "property_plant_and_equipment",
144
- "商誉": "goodwill_and_intangible_assets",
145
- "长期股权投资": "investments",
146
- "其他非流动金融资产": "non_current_investments",
147
- "实收资本(或股本)": "outstanding_shares",
148
- "递延所得税资产": "tax_assets",
149
- "负债合计": "total_liabilities",
150
- "流动负债合计": "current_liabilities",
151
- "短期借款": "current_debt",
152
- "应付票据及应付账款": "trade_and_non_trade_payables",
153
- "合同负债": "deferred_revenue",
154
- "吸收存款及同业存放": "deposit_liabilities",
155
- "非流动负债合计": "non_current_liabilities",
156
- "长期借款": "non_current_debt",
157
- "递延所得税负债": "tax_liabilities",
158
- "所有者权益(或股东权益)合计": "shareholders_equity",
159
- "未分配利润": "retained_earnings",
160
- "其他综合收益": "accumulated_other_comprehensive_income",
161
- "应收账款": "accounts_receivable",
162
- "预付款项": "prepayments",
163
- "其他应收款": "other_receivables",
164
- "固定资产净值": "fixed_assets_net",
165
- "在建工程": "construction_in_progress",
166
- "资本公积": "capital_reserve",
167
- "少数股东权益": "minority_interest",
168
- }
169
- )
170
-
171
- # Select only required columns
172
- required_columns = [
173
- "report_date",
174
- "currency",
175
- "total_assets",
176
- "current_assets",
177
- "cash_and_equivalents",
178
- "inventory",
179
- "current_investments",
180
- "trade_and_non_trade_receivables",
181
- "non_current_assets",
182
- "property_plant_and_equipment",
183
- "goodwill_and_intangible_assets",
184
- "investments",
185
- "non_current_investments",
186
- "outstanding_shares",
187
- "tax_assets",
188
- "total_liabilities",
189
- "current_liabilities",
190
- "current_debt",
191
- "trade_and_non_trade_payables",
192
- "deferred_revenue",
193
- "deposit_liabilities",
194
- "non_current_liabilities",
195
- "non_current_debt",
196
- "tax_liabilities",
197
- "shareholders_equity",
198
- "retained_earnings",
199
- "accumulated_other_comprehensive_income",
200
- "accounts_receivable",
201
- "prepayments",
202
- "other_receivables",
203
- "fixed_assets_net",
204
- "construction_in_progress",
205
- "capital_reserve",
206
- "current_ratio",
207
- "debt_to_assets",
208
- "minority_interest",
209
- ]
210
-
211
- # Calculate financial ratios using vectorized operations
212
- cols = ["current_debt", "non_current_debt"]
213
- raw_df[cols] = raw_df[cols].apply(pd.to_numeric, errors="coerce")
214
- raw_df["total_debt"] = raw_df[cols].fillna(0).sum(axis=1)
215
-
216
- # Pre-calculate denominator conditions
217
- valid_current_liab = raw_df["current_liabilities"].ne(0)
218
- valid_total_assets = raw_df["total_assets"].ne(0)
219
-
220
- # Calculate ratios in one operation
221
- ratios = pd.DataFrame(
222
- {
223
- "current_ratio": raw_df["current_assets"]
224
- / raw_df["current_liabilities"],
225
- "cash_ratio": raw_df["cash_and_equivalents"]
226
- / raw_df["current_liabilities"],
227
- "debt_to_assets": raw_df["total_debt"] / raw_df["total_assets"],
228
- }
229
- )
230
-
231
- # Apply conditions
232
- cond = pd.DataFrame(
233
- {
234
- "current_ratio": valid_current_liab,
235
- "cash_ratio": valid_current_liab,
236
- "debt_to_assets": valid_total_assets,
237
- },
238
- index=ratios.index,
239
- )
240
- raw_df = raw_df.join(ratios.where(cond))
241
-
242
- return raw_df.reindex(columns=required_columns)
243
-
244
- def _clean_income_data(self, raw_df: pd.DataFrame) -> pd.DataFrame:
245
- """清理和标准化利润表数据
246
-
247
- Args:
248
- raw_df: Raw DataFrame from Sina API
249
-
250
- Returns:
251
- Standardized DataFrame with consistent columns
252
- """
253
- # Convert timestamp columns if exists
254
- if "报告日" in raw_df.columns:
255
- raw_df = raw_df.rename(columns={"报告日": "report_date"})
256
- raw_df["report_date"] = pd.to_datetime(
257
- raw_df["report_date"], format="%Y%m%d"
258
- )
259
-
260
- # Define column mappings and required columns
261
- column_mapping = {
262
- "币种": "currency",
263
- "营业总收入": "revenue",
264
- "营业收入": "operating_revenue",
265
- "营业总成本": "total_operating_costs",
266
- "营业成本": "cost_of_revenue",
267
- "营业利润": "operating_profit",
268
- "销售费用": "selling_general_and_administrative_expenses",
269
- "管理费用": "operating_expense",
270
- "研发费用": "research_and_development",
271
- "利息支出": "interest_expense",
272
- "利润总额": "ebit",
273
- "所得税费用": "income_tax_expense",
274
- "净利润": "net_income",
275
- "归属于母公司所有者的净利润": "net_income_common_stock",
276
- "少数股东损益": "net_income_non_controlling_interests",
277
- "基本每股收益": "earnings_per_share",
278
- "稀释每股收益": "earnings_per_share_diluted",
279
- "投资收益": "investment_income",
280
- "公允价值变动收益": "fair_value_adjustments",
281
- "资产减值损失": "asset_impairment_loss",
282
- "财务费用": "financial_expenses",
283
- "营业税金及附加": "taxes_and_surcharges",
284
- "其他综合收益": "other_comprehensive_income",
285
- "综合收益总额": "total_comprehensive_income",
286
- }
287
-
288
- required_columns = ["report_date"] + list(column_mapping.values())
289
- return raw_df.rename(columns=column_mapping).reindex(columns=required_columns)
290
-
291
- def get_financial_metrics(self):
292
- pass
1
+ import pandas as pd
2
+ import akshare as ak
3
+
4
+ from ..cache import cache
5
+ from .base import FinancialDataProvider
6
+
7
+
8
+ class SinaFinancialReport(FinancialDataProvider):
9
+ """Financial data provider for Sina finance reports.
10
+
11
+ Provides standardized access to balance sheet, income statement,
12
+ and cash flow data from Sina finance API.
13
+ """
14
+
15
+ def __init__(self, symbol: str) -> None:
16
+ super().__init__(symbol)
17
+ self.stock = (
18
+ f"sh{symbol}" if not symbol.startswith(("sh", "sz", "bj")) else symbol
19
+ )
20
+
21
+ @cache(
22
+ "financial_cache", key=lambda self, symbol=None: f"sina_balance_{self.symbol}"
23
+ )
24
+ def get_balance_sheet(self) -> pd.DataFrame:
25
+ """获取资产负债表数据
26
+
27
+ Args:
28
+ symbol: 股票代码 (如 "600600")
29
+
30
+ Returns:
31
+ Standardized DataFrame with balance sheet data
32
+ """
33
+ raw_df = ak.stock_financial_report_sina(stock=self.stock, symbol="资产负债表")
34
+ return self._clean_balance_data(raw_df)
35
+
36
+ @cache(
37
+ "financial_cache", key=lambda self, symbol=None: f"sina_income_{self.symbol}"
38
+ )
39
+ def get_income_statement(self) -> pd.DataFrame:
40
+ """获取利润表数据
41
+
42
+ Args:
43
+ symbol: 股票代码 (如 "600600")
44
+
45
+ Returns:
46
+ Standardized DataFrame with income statement data
47
+ """
48
+ raw_df = ak.stock_financial_report_sina(stock=self.stock, symbol="利润表")
49
+ return self._clean_income_data(raw_df)
50
+
51
+ @cache("financial_cache", key=lambda self, symbol=None: f"sina_cash_{self.symbol}")
52
+ def get_cash_flow(self) -> pd.DataFrame:
53
+ """获取现金流量表数据
54
+
55
+ Args:
56
+ symbol: 股票代码 (如 "600600")
57
+
58
+ Returns:
59
+ Standardized DataFrame with cash flow data
60
+ """
61
+ raw_df = ak.stock_financial_report_sina(stock=self.stock, symbol="现金流量表")
62
+ return self._clean_cash_data(raw_df)
63
+
64
+ def _clean_cash_data(self, raw_df: pd.DataFrame) -> pd.DataFrame:
65
+ """清理和标准化现金流量表数据
66
+
67
+ Args:
68
+ raw_df: Raw DataFrame from Sina API
69
+
70
+ Returns:
71
+ Standardized DataFrame with consistent columns
72
+ """
73
+ # Convert timestamp columns if exists
74
+ if "报告日" in raw_df.columns:
75
+ raw_df = raw_df.rename(columns={"报告日": "report_date"})
76
+ raw_df["report_date"] = pd.to_datetime(
77
+ raw_df["report_date"], format="%Y%m%d"
78
+ )
79
+
80
+ # Define column mappings and required columns
81
+ column_mapping = {
82
+ "币种": "currency",
83
+ "经营活动产生的现金流量净额": "net_cash_flow_from_operations",
84
+ "购建固定资产、无形资产和其他长期资产支付的现金": "capital_expenditure",
85
+ "取得子公司及其他营业单位支付的现金净额": "business_acquisitions_and_disposals",
86
+ "投资活动产生的现金流量净额": "net_cash_flow_from_investing",
87
+ "取得借款收到的现金": "issuance_or_repayment_of_debt_securities",
88
+ "吸收投资收到的现金": "issuance_or_purchase_of_equity_shares",
89
+ "筹资活动产生的现金流量净额": "net_cash_flow_from_financing",
90
+ "现金及现金等价物净增加额": "change_in_cash_and_equivalents",
91
+ "汇率变动对现金及现金等价物的影响": "effect_of_exchange_rate_changes",
92
+ "期末现金及现金等价物余额": "ending_cash_balance",
93
+ "销售商品、提供劳务收到的现金": "cash_from_sales",
94
+ "收到的税费返还": "tax_refunds_received",
95
+ "支付给职工以及为职工支付的现金": "cash_paid_to_employees",
96
+ "支付的各项税费": "taxes_paid",
97
+ "经营活动现金流入小计": "total_cash_inflow_from_operations",
98
+ "经营活动现金流出小计": "total_cash_outflow_from_operations",
99
+ "收回投资所收到的现金": "cash_from_investment_recovery",
100
+ "取得投资收益收到的现金": "cash_from_investment_income",
101
+ "处置固定资产、无形资产收回的现金": "cash_from_asset_sales",
102
+ "投资活动现金流入小计": "total_cash_inflow_from_investing",
103
+ "投资活动现金流出小计": "total_cash_outflow_from_investing",
104
+ "分配股利、利润或偿付利息所支付的现金": "cash_paid_for_dividends_and_interest",
105
+ "偿还债务支付的现金": "cash_paid_for_debt_repayment",
106
+ "筹资活动现金流入小计": "total_cash_inflow_from_financing",
107
+ "筹资活动现金流出小计": "total_cash_outflow_from_financing",
108
+ "期初现金及现金等价物余额": "beginning_cash_balance",
109
+ "现金的期末余额": "ending_cash",
110
+ "现金等价物的期末余额": "ending_cash_equivalents",
111
+ }
112
+
113
+ required_columns = ["report_date"] + list(column_mapping.values())
114
+ return raw_df.rename(columns=column_mapping).reindex(columns=required_columns)
115
+
116
+ def _clean_balance_data(self, raw_df: pd.DataFrame) -> pd.DataFrame:
117
+ """清理和标准化资产负债表数据
118
+
119
+ Args:
120
+ raw_df: Raw DataFrame from Sina API
121
+
122
+ Returns:
123
+ Standardized DataFrame with consistent columns
124
+ """
125
+ # Convert timestamp columns if exists
126
+ if "报告日" in raw_df.columns:
127
+ raw_df = raw_df.rename(columns={"报告日": "report_date"})
128
+ raw_df["report_date"] = pd.to_datetime(
129
+ raw_df["report_date"], format="%Y%m%d"
130
+ )
131
+
132
+ # Define and apply column mappings in one optimized operation
133
+ raw_df = raw_df.rename(
134
+ columns={
135
+ "币种": "currency",
136
+ "资产总计": "total_assets",
137
+ "流动资产合计": "current_assets",
138
+ "货币资金": "cash_and_equivalents",
139
+ "存货": "inventory",
140
+ "交易性金融资产": "current_investments",
141
+ "应收票据及应收账款": "trade_and_non_trade_receivables",
142
+ "非流动资产合计": "non_current_assets",
143
+ "固定资产": "property_plant_and_equipment",
144
+ "商誉": "goodwill_and_intangible_assets",
145
+ "长期股权投资": "investments",
146
+ "其他非流动金融资产": "non_current_investments",
147
+ "实收资本(或股本)": "outstanding_shares",
148
+ "递延所得税资产": "tax_assets",
149
+ "负债合计": "total_liabilities",
150
+ "流动负债合计": "current_liabilities",
151
+ "短期借款": "current_debt",
152
+ "应付票据及应付账款": "trade_and_non_trade_payables",
153
+ "合同负债": "deferred_revenue",
154
+ "吸收存款及同业存放": "deposit_liabilities",
155
+ "非流动负债合计": "non_current_liabilities",
156
+ "长期借款": "non_current_debt",
157
+ "递延所得税负债": "tax_liabilities",
158
+ "所有者权益(或股东权益)合计": "shareholders_equity",
159
+ "未分配利润": "retained_earnings",
160
+ "其他综合收益": "accumulated_other_comprehensive_income",
161
+ "应收账款": "accounts_receivable",
162
+ "预付款项": "prepayments",
163
+ "其他应收款": "other_receivables",
164
+ "固定资产净值": "fixed_assets_net",
165
+ "在建工程": "construction_in_progress",
166
+ "资本公积": "capital_reserve",
167
+ "少数股东权益": "minority_interest",
168
+ }
169
+ )
170
+
171
+ # Select only required columns
172
+ required_columns = [
173
+ "report_date",
174
+ "currency",
175
+ "total_assets",
176
+ "current_assets",
177
+ "cash_and_equivalents",
178
+ "inventory",
179
+ "current_investments",
180
+ "trade_and_non_trade_receivables",
181
+ "non_current_assets",
182
+ "property_plant_and_equipment",
183
+ "goodwill_and_intangible_assets",
184
+ "investments",
185
+ "non_current_investments",
186
+ "outstanding_shares",
187
+ "tax_assets",
188
+ "total_liabilities",
189
+ "current_liabilities",
190
+ "current_debt",
191
+ "trade_and_non_trade_payables",
192
+ "deferred_revenue",
193
+ "deposit_liabilities",
194
+ "non_current_liabilities",
195
+ "non_current_debt",
196
+ "tax_liabilities",
197
+ "shareholders_equity",
198
+ "retained_earnings",
199
+ "accumulated_other_comprehensive_income",
200
+ "accounts_receivable",
201
+ "prepayments",
202
+ "other_receivables",
203
+ "fixed_assets_net",
204
+ "construction_in_progress",
205
+ "capital_reserve",
206
+ "current_ratio",
207
+ "debt_to_assets",
208
+ "minority_interest",
209
+ ]
210
+
211
+ # Calculate financial ratios using vectorized operations
212
+ cols = ["current_debt", "non_current_debt"]
213
+ raw_df[cols] = raw_df[cols].apply(pd.to_numeric, errors="coerce")
214
+ raw_df["total_debt"] = raw_df[cols].fillna(0).sum(axis=1)
215
+
216
+ # Pre-calculate denominator conditions
217
+ valid_current_liab = raw_df["current_liabilities"].ne(0)
218
+ valid_total_assets = raw_df["total_assets"].ne(0)
219
+
220
+ # Calculate ratios in one operation
221
+ ratios = pd.DataFrame(
222
+ {
223
+ "current_ratio": raw_df["current_assets"]
224
+ / raw_df["current_liabilities"],
225
+ "cash_ratio": raw_df["cash_and_equivalents"]
226
+ / raw_df["current_liabilities"],
227
+ "debt_to_assets": raw_df["total_debt"] / raw_df["total_assets"],
228
+ }
229
+ )
230
+
231
+ # Apply conditions
232
+ cond = pd.DataFrame(
233
+ {
234
+ "current_ratio": valid_current_liab,
235
+ "cash_ratio": valid_current_liab,
236
+ "debt_to_assets": valid_total_assets,
237
+ },
238
+ index=ratios.index,
239
+ )
240
+ raw_df = raw_df.join(ratios.where(cond))
241
+
242
+ return raw_df.reindex(columns=required_columns)
243
+
244
+ def _clean_income_data(self, raw_df: pd.DataFrame) -> pd.DataFrame:
245
+ """清理和标准化利润表数据
246
+
247
+ Args:
248
+ raw_df: Raw DataFrame from Sina API
249
+
250
+ Returns:
251
+ Standardized DataFrame with consistent columns
252
+ """
253
+ # Convert timestamp columns if exists
254
+ if "报告日" in raw_df.columns:
255
+ raw_df = raw_df.rename(columns={"报告日": "report_date"})
256
+ raw_df["report_date"] = pd.to_datetime(
257
+ raw_df["report_date"], format="%Y%m%d"
258
+ )
259
+
260
+ # Define column mappings and required columns
261
+ column_mapping = {
262
+ "币种": "currency",
263
+ "营业总收入": "revenue",
264
+ "营业收入": "operating_revenue",
265
+ "营业总成本": "total_operating_costs",
266
+ "营业成本": "cost_of_revenue",
267
+ "营业利润": "operating_profit",
268
+ "销售费用": "selling_general_and_administrative_expenses",
269
+ "管理费用": "operating_expense",
270
+ "研发费用": "research_and_development",
271
+ "利息支出": "interest_expense",
272
+ "利润总额": "ebit",
273
+ "所得税费用": "income_tax_expense",
274
+ "净利润": "net_income",
275
+ "归属于母公司所有者的净利润": "net_income_common_stock",
276
+ "少数股东损益": "net_income_non_controlling_interests",
277
+ "基本每股收益": "earnings_per_share",
278
+ "稀释每股收益": "earnings_per_share_diluted",
279
+ "投资收益": "investment_income",
280
+ "公允价值变动收益": "fair_value_adjustments",
281
+ "资产减值损失": "asset_impairment_loss",
282
+ "财务费用": "financial_expenses",
283
+ "营业税金及附加": "taxes_and_surcharges",
284
+ "其他综合收益": "other_comprehensive_income",
285
+ "综合收益总额": "total_comprehensive_income",
286
+ }
287
+
288
+ required_columns = ["report_date"] + list(column_mapping.values())
289
+ return raw_df.rename(columns=column_mapping).reindex(columns=required_columns)
290
+
291
+ def get_financial_metrics(self):
292
+ pass