siat 3.8.20__py3-none-any.whl → 3.8.25__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.
siat/economy2.py CHANGED
@@ -170,25 +170,29 @@ if __name__ =="__main__":
170
170
 
171
171
  indicator_name_wb(indicator)
172
172
 
173
- def indicator_name_wb(indicator='NY.GDP.MKTP.KN'):
173
+ def indicator_name_wb(indicator):
174
174
  """
175
175
  ===========================================================================
176
176
  功能:抓取World Bank网页上指标的名称
177
- indicator:WB指标名称,默认'NY.GDP.MKTP.KN'
177
+ indicator:WB指标名称,例如'NY.GDP.MKTP.KN'
178
178
  """
179
+ # 优先查询本地词典
180
+ indicator_name=economic_translate(indicator)
179
181
 
180
- # 构造 API 请求 URL
181
- url = f"https://api.worldbank.org/v2/indicator/{indicator}?format=json"
182
-
183
- # 发送请求
184
- response = requests.get(url)
185
- data = response.json()
186
-
187
- # 提取指标名称
188
- try:
189
- indicator_name = data[1][0]['name']
190
- except:
191
- indicator_name = ''
182
+ # 查询WB网页
183
+ if indicator_name == indicator:
184
+ # 构造 API 请求 URL
185
+ url = f"https://api.worldbank.org/v2/indicator/{indicator}?format=json"
186
+
187
+ # 发送请求
188
+ response = requests.get(url)
189
+ data = response.json()
190
+
191
+ # 提取指标名称
192
+ try:
193
+ indicator_name = data[1][0]['name']
194
+ except:
195
+ indicator_name = indicator
192
196
 
193
197
  return indicator_name
194
198
 
@@ -291,8 +295,8 @@ def economy_indicator_wb(ticker='CN',indicator='NY.GDP.MKTP.KN', \
291
295
  """
292
296
  # 检测指标是否存在,并取得指标名称
293
297
  indicator_name=indicator_name_wb(indicator)
294
- if indicator_name == '':
295
- print(f" #Error(economy_indicator_wb): no indicator found for {indicator}")
298
+ if indicator_name == indicator:
299
+ print(f" #Error(economy_indicator_wb): indicator {indicator} not found")
296
300
  return None
297
301
 
298
302
  # 日期具体化
@@ -302,20 +306,20 @@ def economy_indicator_wb(ticker='CN',indicator='NY.GDP.MKTP.KN', \
302
306
  try:
303
307
  pricedf=wb.download(indicator=indicator,country=ticker,start=start,end=end)
304
308
  except:
305
- print(f" #Error(economy_indicator_wb): either {indicator} deprecated or {ticker} does not exist")
309
+ print(f" #Error(economy_indicator_wb): {indicator} deprecated or {ticker} not found")
306
310
  return None
307
311
 
308
312
  # 是否返回None
309
313
  if pricedf is None:
310
- print(f" #Error(economy_indicator_wb): no data found on {indicator} for {ticker}")
314
+ print(f" #Error(economy_indicator_wb): no data found on {indicator} in {ticker}")
311
315
  return None
312
316
  # 是否返回空的数据表
313
317
  if len(pricedf) == 0:
314
- print(f" #Error(economy_indicator_wb): zero data found on {indicator} for {ticker}")
318
+ print(f" #Error(economy_indicator_wb): zero data found on {indicator} in {ticker}")
315
319
  return None
316
320
  # 是否返回数据表但内容均为NaN
317
321
  if pricedf[indicator].isnull().all():
318
- print(f" #Error(economy_indicator_wb): all empty data found on {indicator} for {ticker}")
322
+ print(f" #Error(economy_indicator_wb): empty data found on {indicator} in {ticker}")
319
323
  return None
320
324
 
321
325
  pricedf.reset_index(inplace=True)
@@ -360,8 +364,8 @@ def economy_indicator_wb(ticker='CN',indicator='NY.GDP.MKTP.KN', \
360
364
  if ind_max * ind_min <0:
361
365
  zeroline=True
362
366
 
363
- titletxt1=text_lang("经济趋势分析","Economic Trend Analysis")
364
- titletxt=titletxt1+': '+country+', '+indicator_name
367
+ titletxt1=text_lang("经济分析","Economic Analysis")
368
+ titletxt=titletxt1+': '+country_translate(country)+', '+indicator_name
365
369
  if unit != '':
366
370
  titletxt=titletxt+', '+unit
367
371
 
@@ -530,7 +534,7 @@ def economy_mindicators_wb(ticker='CN',indicator=['NY.GDP.MKTP.CN','NY.GDP.MKTP.
530
534
  return None
531
535
 
532
536
  # 绘图
533
- titletxt=text_lang("经济趋势分析","Economic Trend Analysis")+': '+country
537
+ titletxt=text_lang("经济趋势分析","Economic Trend Analysis")+': '+country_translate(country)
534
538
 
535
539
  y_label=text_lang('经济指标',"Economic Indicator")
536
540
  import datetime; todaydt = datetime.date.today()
@@ -573,6 +577,10 @@ def economy_mindicators_wb(ticker='CN',indicator=['NY.GDP.MKTP.CN','NY.GDP.MKTP.
573
577
 
574
578
  # 为避免绘图出错,对空值进行插值
575
579
  df.interpolate(method='linear',limit_direction='both',inplace=True)
580
+
581
+ # 翻译指标名称
582
+ for c in list(df):
583
+ df.rename(columns={c:economic_translate(c)},inplace=True)
576
584
 
577
585
  draw_lines2(df,y_label,x_label,axhline_value,axhline_label,titletxt, \
578
586
  data_label=False,resample_freq='1D',smooth=smooth, \
@@ -695,15 +703,15 @@ def economy_mtickers_wb(ticker=['CN','US','JP'],indicator='NY.GDP.MKTP.PP.CD', \
695
703
  dfs=pd.DataFrame(); have_data=False
696
704
  country_list=[]; unit_list=[]
697
705
  for t in tickers:
698
- print(f" Looking indicator info for {t} ... ...")
706
+ print(f" Looking for {measure} info in {t} ... ...")
699
707
  with HiddenPrints():
700
708
  df_tmp=economy_indicator_wb(ticker=t,indicator=measure, \
701
709
  start=start,end=end,graph=False)
702
710
  if df_tmp is None:
703
- print(f" #Warning(economy_mticker_wb): indicator info not found for {t}")
711
+ print(f" #Warning(economy_mticker_wb): {measure} info not found in {t}")
704
712
  continue
705
713
  if len(df_tmp)==0:
706
- print(f" #Warning(economy_mticker_wb): zero info found for {t} between {start} and {end}")
714
+ print(f" #Warning(economy_mticker_wb): zero info found for {measure} in {t}")
707
715
  continue
708
716
 
709
717
  have_data=True
@@ -714,7 +722,10 @@ def economy_mtickers_wb(ticker=['CN','US','JP'],indicator='NY.GDP.MKTP.PP.CD', \
714
722
  unit_list=unit_list+[unit]
715
723
  df_tmp.drop(columns=['country','unit'],inplace=True)
716
724
  indicator_name=list(df_tmp)[0]
717
-
725
+
726
+ if DEBUG:
727
+ print(f"DEBUG: t={t}, band_area={band_area}, df_tmp={list(df_tmp)}")
728
+
718
729
  if t in band_area:
719
730
  band_area = [country if x == t else x for x in band_area]
720
731
 
@@ -724,12 +735,15 @@ def economy_mtickers_wb(ticker=['CN','US','JP'],indicator='NY.GDP.MKTP.PP.CD', \
724
735
  dfs=df_tmp
725
736
  else:
726
737
  dfs=pd.concat([dfs,df_tmp],axis=1,join='outer')
727
-
738
+
739
+ # 翻译band_area
740
+ band_area=[country_translate(x) for x in band_area]
741
+
728
742
  if dfs is None:
729
743
  print(f" #Error(economy_mticker_wb): no records found for {measure}")
730
744
  return None
731
745
  if len(dfs)==0:
732
- print(" #Error(economy_mticker_wb): zero records found for {measure}")
746
+ print(f" #Error(economy_mticker_wb): zero records found for {measure}")
733
747
  return None
734
748
 
735
749
  # 若不绘图则返回原始数据
@@ -740,11 +754,12 @@ def economy_mtickers_wb(ticker=['CN','US','JP'],indicator='NY.GDP.MKTP.PP.CD', \
740
754
  return None
741
755
 
742
756
  # 绘图
743
- titletxt=text_lang("经济趋势分析","Economic Trend Analysis")+': '+indicator_name
744
- y_label=indicator_name
757
+ titletxt=text_lang("经济分析","Economic Analysis")+': '+indicator_name
758
+ #y_label=indicator_name
759
+ y_label=text_lang("经济指标","Economic Indicator")
745
760
 
746
761
  import datetime; todaydt = datetime.date.today()
747
- footnote2=text_lang("数据来源:WB/IMF/FRED","Data source: World Bank")+', '+str(todaydt)
762
+ footnote2=text_lang("数据来源:WB/IMF/FRED","Data source: WB/IMF/FRED")+', '+str(todaydt)
748
763
 
749
764
  one_unit=False
750
765
  if len(set(unit_list)) == 1: one_unit=True
@@ -801,6 +816,10 @@ def economy_mtickers_wb(ticker=['CN','US','JP'],indicator='NY.GDP.MKTP.PP.CD', \
801
816
  if axhline_label=='':
802
817
  axhline_label='零线'
803
818
 
819
+ # 翻译国家名称
820
+ for c in list(dfs2):
821
+ dfs2.rename(columns={c:country_translate(c)},inplace=True)
822
+
804
823
  draw_lines(dfs2,y_label,x_label,axhline_value,axhline_label,titletxt, \
805
824
  data_label=False,resample_freq='D',smooth=smooth,linewidth=linewidth, \
806
825
  band_area=band_area,loc=loc, \
@@ -973,5 +992,210 @@ def economy_trend2(ticker='CN',indicator='NY.GDP.MKTP.KN', \
973
992
  #==============================================================================
974
993
  #==============================================================================
975
994
  #==============================================================================
995
+
996
+
997
+ def economic_translate(indicator):
998
+ """
999
+ ===========================================================================
1000
+ 功能:翻译宏观经济指标术语
1001
+ 参数:
1002
+ indicator: 指标编码,主要是世界银行编码。
1003
+ 注意:部分编码已放弃,可能无数据或无最新数据。
1004
+ 返回值:是否找到,基于语言环境为中文或英文解释。
1005
+ 语言环境判断为check_language()
1006
+
1007
+ 数据结构:['指标编码','中文解释','英文解释']
1008
+ """
1009
+ DEBUG=False
1010
+
1011
+ import pandas as pd
1012
+ trans_dict=pd.DataFrame([
1013
+
1014
+ # NE.CON.PRVT:家庭及NPISH最终消费=======================================
1015
+ ['NE.CON.PRVT.CD','家庭及NPISH最终消费(美元时价)',
1016
+ 'Household & NPISHs Final Consumption (current US$)',
1017
+ 'Households and NPISHs Final consumption expenditure (current US$)'],
1018
+
1019
+ ['NE.CON.PRVT.KD','家庭及NPISH最终消费(2015美元不变价格)',
1020
+ 'Household & NPISHs Final Consumption (constant 2015 US$)',
1021
+ 'Households and NPISHs Final consumption expenditure (constant 2015 US$)'],
1022
+
1023
+ ['NE.CON.PRVT.CN','家庭及NPISH最终消费(本币时价)',
1024
+ 'Household & NPISHs Final Consumption (current LCU)',
1025
+ 'Households and NPISHs Final consumption expenditure (current LCU)'],
1026
+
1027
+ ['NE.CON.PRVT.KN','家庭及NPISH最终消费(本币不变价格)',
1028
+ 'Household & NPISHs Final Consumption (constant LCU)',
1029
+ 'Households and NPISHs Final consumption expenditure (constant LCU)'],
1030
+
1031
+ ['NE.CON.PRVT.ZS','家庭及NPISH最终消费(占GDP%)',
1032
+ 'Household & NPISHs Final Consumption (GDP%)',
1033
+ 'Households and NPISHs Final consumption expenditure (% of GDP)'],
1034
+
1035
+ ['NE.CON.PRVT.KD.ZG','家庭及NPISH最终消费(年增速%)',
1036
+ 'Household & NPISHs Final Consumption (annual % growth)',
1037
+ 'Households and NPISHs Final consumption expenditure (annual % growth)'],
1038
+
1039
+ ['NE.CON.PRVT.PP.CD','家庭及NPISH最终消费(购买力平价,国际美元时价)',
1040
+ 'Household & NPISHs Final Consumption (PPP, current intl $)',
1041
+ 'Households and NPISHs Final consumption expenditure, PPP (current international $)'],
1042
+
1043
+ ['NE.CON.PRVT.PP.KD','家庭及NPISH最终消费(购买力平价,2021国际美元不变价格)',
1044
+ 'Household & NPISHs Final Consumption (PPP, constant 2021 intl $)',
1045
+ 'Households and NPISHs Final consumption expenditure, PPP (constant 2021 international $)'],
1046
+
1047
+ ['NE.CON.PRVT.PC.KD.ZG','人均家庭及NPISH最终消费(年增速%)',
1048
+ 'Household & NPISHs Final Consumption per capita growth (annual %)',
1049
+ 'Households and NPISHs Final consumption expenditure per capita growth (annual %)'],
1050
+
1051
+ ['NE.CON.PRVT.PC.KD','人均家庭及NPISH最终消费(2015美元不变价格)',
1052
+ 'Household & NPISHs Final Consumption per capita (constant 2015 US$)',
1053
+ 'Households and NPISHs Final consumption expenditure per capita (constant 2015 US$)'],
1054
+
1055
+ ['NE.CON.PRVT.CN.AD','家庭及NPISH最终消费(统计口径调整后,本币时价)',
1056
+ 'Household & NPISHs Final Consumption (linked series, current LCU)',
1057
+ 'Households and NPISHs Final consumption expenditure: linked series (current LCU)'],
1058
+
1059
+ # 币种指标:CD=current US$, KD=constant 2015 US$, CN=current LCU
1060
+ # KN=constant LCU, ZS=% of GDP, PC=per capita, PP=PPP
1061
+
1062
+
1063
+
1064
+
1065
+
1066
+
1067
+ ], columns=['indicator','cword','eword','original_eword'])
1068
+
1069
+ found=False; result=indicator
1070
+ try:
1071
+ dict_word=trans_dict[trans_dict['indicator']==indicator]
1072
+ found=True
1073
+ except:
1074
+ #未查到翻译词汇,返回原词
1075
+ pass
1076
+
1077
+ if dict_word is None:
1078
+ found=False
1079
+ elif len(dict_word) == 0:
1080
+ found=False
1081
+
1082
+ if found:
1083
+ lang=check_language()
1084
+
1085
+ if DEBUG:
1086
+ print(f"DEBUG: indicator={indicator}, lang={lang}, dict_word={dict_word}")
1087
+
1088
+ if lang == 'Chinese':
1089
+ result=dict_word['cword'].values[0]
1090
+ else:
1091
+ result=dict_word['eword'].values[0]
1092
+
1093
+ return result
1094
+
1095
+ if __name__=='__main__':
1096
+ indicator='NE.CON.PRVT.CD'
1097
+ indicator='NE.CON.PRVT.KD'
1098
+
1099
+ indicator='NE.CON.PRVT.CN'
1100
+ indicator='NE.CON.PRVT.KN'
1101
+
1102
+ result=economic_translate(indicator)
1103
+ economic_translate(indicator)[1]
1104
+
1105
+ #==============================================================================
1106
+
1107
+
1108
+ def country_translate(country):
1109
+ """
1110
+ ===========================================================================
1111
+ 功能:翻译国家名称
1112
+ 参数:
1113
+ country: 国家名称英文,并非国家代码。
1114
+ 返回值:是否找到,基于语言环境为中文或英文解释。
1115
+ 语言环境判断为check_language()
1116
+
1117
+ 数据结构:['国家名称英文','国家名称中文','国家代码2位','国家代码3位']
1118
+ """
1119
+
1120
+ import pandas as pd
1121
+ trans_dict=pd.DataFrame([
1122
+
1123
+ ['China','中国','CN','CHN'],
1124
+ ['United States','美国','US','USA'],
1125
+ ['Japan','日本','JP','JPN'],
1126
+ ['Germany','德国','DE','DEU'],
1127
+ ['India','印度','IN','IND'],
1128
+ ['Brazil','巴西','BR','BRA'],
1129
+
1130
+ ['France','法国','FR','FRA'],
1131
+ ['United Kingdom','英国','GB','GBR'],
1132
+ ['Russia','俄罗斯','RU','RUS'],
1133
+ ['Canada','加拿大','CA','CAN'],
1134
+ ['Australia','澳大利亚','AU','AUS'],
1135
+ ['South Korea','韩国','KR','KOR'],
1136
+
1137
+ ['Italy','意大利','IT','ITA'],
1138
+ ['Mexico','墨西哥','MX','MEX'],
1139
+ ['South Africa','南非','ZA','ZAF'],
1140
+ ['Saudi Arabia','沙特阿拉伯','SA','SAU'],
1141
+ ['Indonesia','印度尼西亚','ID','IDN'],
1142
+ ['Turkey','土耳其','TR','TUR'],
1143
+
1144
+ ['Argentina','阿根廷','AR','ARG'],
1145
+ ['Egypt','埃及','EG','EGY'],
1146
+ ['European Union','欧盟','EU','EUU'],
1147
+ ['Hong Kong','中国香港','HK','HKG'],
1148
+ ['Hong Kong, China','中国香港','HK','HKG'],
1149
+ ['Taiwan','中国台湾','TW','TWN'],
1150
+ ['Taiwan, China','中国台湾','TW','TWN'],
1151
+ ['World','全球','1W','WLD'],
1152
+
1153
+ ['Singapore','新加坡','SG','SGP'],
1154
+ ['Malaysia','马来西亚','MY','MYS'],
1155
+ ['Thailand','泰国','TH','THA'],
1156
+ ['Israel','以色列','IL','ISR'],
1157
+ ['Vietnam','越南','VN','VNM'],
1158
+ ['Philippines','菲律宾','PH','PHL'],
1159
+ ['Brunei','文莱','BN','BRN'],
1160
+ ['Cambodia','柬埔寨','KH','KHM'],
1161
+
1162
+ ['Laos','老挝','LA','LAO'],
1163
+ ['Myanmar','缅甸','MM','MMR'],
1164
+
1165
+
1166
+
1167
+
1168
+
1169
+ ], columns=['ecountry','ccountry','country code2','country code3'])
1170
+
1171
+ found=False; result=country
1172
+ try:
1173
+ dict_word=trans_dict[trans_dict['ecountry']==country]
1174
+ found=True
1175
+ except:
1176
+ #未查到翻译词汇,返回原词
1177
+ pass
1178
+
1179
+ if dict_word is None:
1180
+ found=False
1181
+ elif len(dict_word) == 0:
1182
+ found=False
1183
+
1184
+ if found:
1185
+ lang=check_language()
1186
+ if lang == 'Chinese':
1187
+ result=dict_word['ccountry'].values[0]
1188
+ else:
1189
+ #result=dict_word['ecountry'].values[0]
1190
+ pass
1191
+
1192
+ return result
1193
+
1194
+ if __name__=='__main__':
1195
+ country='China'
1196
+ country='United States'
1197
+
1198
+ result=country_translate(country)
1199
+
976
1200
  #==============================================================================
977
1201
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: siat
3
- Version: 3.8.20
3
+ Version: 3.8.25
4
4
  Summary: Securities Investment Analysis Tools (siat)
5
5
  Home-page: https://pypi.org/project/siat/
6
6
  Author: Prof. WANG Dehong, International Business School, Beijing Foreign Studies University
@@ -30,7 +30,7 @@ siat/cryptocurrency_test.py,sha256=3AikTNJ7j-HwLGLIYEfyXZ3bLVuLeru9mwiwHQi2SdA,2
30
30
  siat/derivative.py,sha256=qV8n09799eqLc26ojR6vN5n_X-xd7rGwdYjgq-wBih8,41483
31
31
  siat/economy-20230125.py,sha256=vxZZlPnLkh7SpGMVEPLwxjt0yYLSVmdZrO-s2NYLyoM,73848
32
32
  siat/economy.py,sha256=BFVQDxOTbuizyumpCgpZIauH6sqnwUXebpqRMmQCzys,84198
33
- siat/economy2.py,sha256=FOVf5WxKnge3dVEV2CEf--CupXhFtlRUc5k5FUMTD-Q,39857
33
+ siat/economy2.py,sha256=paLZWJFa5in5Pl_isyb-7qnT8EUkvLGvg0G3s-eUONE,48275
34
34
  siat/economy_test.py,sha256=6vjNlPz7W125pJb7simCddobSEp3jmLIMvVkLRZ7zW8,13339
35
35
  siat/esg.py,sha256=GMhaonIKtvOK83rhpQUH5aJt2OL3HQBSVfD__Yw-0oo,19040
36
36
  siat/esg_test.py,sha256=Z9m6GUt8O7oHZSEG9aDYpGdvvrv2AiRJdHTiU6jqmZ0,2944
@@ -144,8 +144,8 @@ siat/valuation_china.py,sha256=CVp1IwIsF3Om0J29RGkyxZLt4n9Ug-ua_RKhLwL9fUQ,69624
144
144
  siat/valuation_market_china_test.py,sha256=gbJ0ioauuo4koTPH6WKUkqcXiQPafnbhU5eKJ6lpdLA,1571
145
145
  siat/var_model_validation.py,sha256=R0caWnuZarrRg9939hxh3vJIIpIyPfvelYmzFNZtPbo,14910
146
146
  siat/yf_name.py,sha256=laNKMTZ9hdenGX3IZ7G0a2RLBKEWtUQJFY9CWuk_fp8,24058
147
- siat-3.8.20.dist-info/LICENSE,sha256=NTEMMROY9_4U1szoKC3N2BLHcDd_o5uTgqdVH8tbApw,1071
148
- siat-3.8.20.dist-info/METADATA,sha256=I4TQUc7XHrSbbQkDRTd2Glcb1hcYKeIyhmUBp3KxMao,8144
149
- siat-3.8.20.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
150
- siat-3.8.20.dist-info/top_level.txt,sha256=r1cVyL7AIKqeAmEJjNR8FMT20OmEzufDstC2gv3NvEY,5
151
- siat-3.8.20.dist-info/RECORD,,
147
+ siat-3.8.25.dist-info/LICENSE,sha256=NTEMMROY9_4U1szoKC3N2BLHcDd_o5uTgqdVH8tbApw,1071
148
+ siat-3.8.25.dist-info/METADATA,sha256=pWsYGwjmUvMD1r3g1lU0Ijl4V_Pif_RRtuBGhrcLw3A,8144
149
+ siat-3.8.25.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
150
+ siat-3.8.25.dist-info/top_level.txt,sha256=r1cVyL7AIKqeAmEJjNR8FMT20OmEzufDstC2gv3NvEY,5
151
+ siat-3.8.25.dist-info/RECORD,,
File without changes
File without changes