tools
针对一些不常见的文件格式,读取数据文件的一些工具函数,以及其他数据工具
pure_dawn
¶
Bases: object
因子切割论的母框架,可以对两个因子进行类似于因子切割的操作 可用于派生任何"以两个因子生成一个因子"的子类 使用举例 cut函数里,必须带有输入变量df,df有两个columns,一个名为'fac1',一个名为'fac2',df是最近一个回看期内的数据
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 | |
__call__()
¶
返回最终月度因子值
Returns¶
pd.DataFrame
最终因子值
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
445 446 447 448 449 450 451 452 453 | |
__init__(fac1, fac2, *args)
¶
几个因子的操作,每个月操作一次
Parameters¶
fac1 : pd.DataFrame 因子值1,index为时间,columns为股票代码,values为因子值 fac2 : pd.DataFrame 因子2,index为时间,columns为股票代码,values为因子值
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | |
find_begin(tradedays, end_day, backsee=20)
¶
找出回看若干天的开始日,默认为20
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
472 473 474 475 476 477 | |
for_cross_via_zip(func)
staticmethod
¶
返回值为多个pd.Series,每个pd.Series的index为股票代码,values为单个因子值 例如
| Python | |
|---|---|
1 2 3 4 | |
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 | |
get_fac_long_and_tradedays()
¶
将两个因子的矩阵转化为长列表
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
455 456 457 | |
get_month_starts_and_ends(backsee=20)
¶
计算出每个月回看期间的起点日和终点日
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
459 460 461 462 463 464 465 466 467 468 469 470 | |
get_monthly_factor(func, whole_cross=0, daily=0, history_file=None)
¶
运行自己写的函数,获得月度因子
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 | |
make_monthly_factors_single_code(df, func, daily)
¶
对单一股票来计算月度因子 func为单月执行的函数,返回值应为月度因子,如一个float或一个list df为一个股票的四列表,包含时间、代码、因子1和因子2
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | |
run(func, backsee=20, whole_cross=0, daily=0, history_file=None)
¶
执行计算的框架,产生因子值
Parameters¶
func : Callable 每个月要进行的操作 backsee : int, optional 回看期,即每个月月底对过去多少天进行计算, by default 20 whole_cross : bool, optional 是否同时取横截面上所有股票进行计算, by default 20 daily : bool, optional 是否每日计算, by default 20 history_file : str, optional 存储历史数据的文件名, by default None
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 | |
add_suffix(code)
¶
给没有后缀的股票代码加上wind后缀
Parameters¶
code : str 没有后缀的股票代码
Returns¶
str
加完wind后缀的股票代码
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
all_pos(df)
¶
将因子值每个截面上减去最小值,从而都变成非负数
Parameters¶
df : pd.DataFrame 因子值,index为时间,columns为股票代码,values为因子值
Returns¶
pd.DataFrame 变化后非负的因子值
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 | |
calcWeightedStd(series, weights)
¶
计算半衰加权标准差
Parameters¶
series : pd.Series 目标序列 weights : Union[pd.Series,np.ndarray] 权重序列
Returns¶
float
半衰加权标准差
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 | |
calc_exp_list(window, half_life)
¶
生成半衰序列
Parameters¶
window : int 窗口期 half_life : int 半衰期
Returns¶
np.ndarray
半衰序列
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 | |
clip(df, mad=0, three_sigma=0, percentile=0, parameter=None)
¶
对因子值进行截面去极值的操作
Parameters¶
df : pd.DataFrame 要处理的因子表,columns为股票代码,index为时间 mad : bool, optional 使用mad法去极值,先计算所有因子与平均值之间的距离总和来检测离群值, by default 0 three_sigma : bool, optional 根据均值和几倍标准差做调整, by default 0 percentile : bool, optional 根据上下限的分位数去极值, by default 0 parameter : Union[float,tuple], optional 参数,mad和three_sigma默认参数为3,输入float形式;而percentile默认参数为(0.025,0.975),输入tuple形式, by default None 参考资料
Returns¶
pd.DataFrame 去极值后的参数
Raises¶
ValueError 不指定方法或参数类型错误,将报错
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 | |
coin_reverse(ret20, vol20, mean=1, positive_negtive=0)
¶
球队硬币法:根据vol20的大小,翻转一半ret20,把vol20较大的部分,给ret20添加负号
Parameters¶
ret20 : pd.DataFrame 要被翻转的因子,index是时间,columns是股票代码 vol20 : pd.DataFrame 翻转的依据,index是时间,columns是股票代码 mean : bool, optional 为1则以是否大于截面均值为标准翻转,否则以是否大于截面中位数为标准, by default 1 positive_negtive : bool, optional 是否截面上正负值的两部分,各翻转一半, by default 0
Returns¶
pd.DataFrame
翻转后的因子值
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 | |
convert_code(x)
¶
将米筐代码转换为wind代码,并识别其是股票还是指数
Parameters¶
x : str 米筐的股票/指数代码,以 XSHE 或 XSHG 结尾
Returns¶
Tuple[str,str]
转换后的股票/指数代码,以及该代码属于股票还是指数
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
corr_two_daily(df1, df2, history=None, rolling_window=20, n_jobs=1, daily=1, method='pearson')
¶
求两个因子,在相同股票上,时序上滚动窗口下的相关系数
Parameters¶
df1 : pd.DataFrame 第一个因子,index为时间,columns为股票代码 df2 : pd.DataFrame 第二个因子,index为时间,columns为股票代码 history : str, optional 从某处读取计算好的历史文件 rolling_window : int, optional 滚动窗口, by default 20 n_jobs : int, optional 并行数量, by default 1 daily : bool, optional 是否每天计算, by default 1 method : str, optional 使用哪种方法计算相关系数, by default 'pearson'
Returns¶
pd.DataFrame 相关系数后的结果,index为时间,columns为股票代码
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 | |
count_value(df, with_zero=0)
¶
计算dataframe中总共有多少(非0)非空的值
Parameters¶
df : pd.DataFrame 要检测的dataframe with_zero : bool, optional 统计数量时,是否也把值为0的数据统计进去, by default 0
Returns¶
int (非0)非空的数据的个数
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 | |
cov_two_daily(df1, df2, history=None, rolling_window=20, n_jobs=1, daily=1)
¶
求两个因子,在相同股票上,时序上滚动窗口下的协方差
Parameters¶
df1 : pd.DataFrame 第一个因子,index为时间,columns为股票代码 df2 : pd.DataFrame 第二个因子,index为时间,columns为股票代码 history : str, optional 从某处读取计算好的历史文件 rolling_window : int, optional 滚动窗口, by default 20 n_jobs : int, optional 并行数量, by default 1 daily : bool, optional 是否每天计算, by default 1
Returns¶
pd.DataFrame 求协方差后的结果,index为时间,columns为股票代码
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 | |
debj(df)
¶
去除因子中的北交所数据
Parameters¶
df : pd.DataFrame 包含北交所的因子dataframe,index是时间,columns是股票代码
Returns¶
pd.DataFrame 去除北交所股票的因子dataframe
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 | |
detect_nan(df)
¶
检查一个pd.DataFrame中是否存在空值
Parameters¶
df : pd.DataFrame 待检查的pd.DataFrame
Returns¶
bool
检查结果,有空值为True,否则为False
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 | |
drop_duplicates_index(new)
¶
对dataframe依照其index进行去重,并保留最上面的行
Parameters¶
new : pd.DataFrame 要去重的dataframe
Returns¶
pd.DataFrame 去重后的dataframe
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 | |
feather_to_parquet(folder)
¶
将某个路径下的所有feather文件都转化为parquet文件
Parameters¶
folder : str 要转化的文件夹路径
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 | |
feather_to_parquet_all()
¶
将数据库中所有的feather文件都转化为parquet文件
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1447 1448 1449 1450 1451 1452 1453 1454 1455 | |
func_two_daily(df1, df2, func, history=None, rolling_window=20, n_jobs=1)
¶
求两个因子,在相同股票上,时序上滚动窗口下的相关系数
Parameters¶
df1 : pd.DataFrame 第一个因子,index为时间,columns为股票代码 df2 : pd.DataFrame 第二个因子,index为时间,columns为股票代码 func : Callable 要对两列数进行操作的函数 history : str, optional 从某处读取计算好的历史文件 rolling_window : int, optional 滚动窗口, by default 20 n_jobs : int, optional 并行数量, by default 1
Returns¶
pd.DataFrame 计算后的结果,index为时间,columns为股票代码
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 | |
get_abs(df, quantile=None, square=0)
¶
均值距离化:计算因子与截面均值的距离
Parameters¶
df : pd.DataFrame 未均值距离化的因子,index为时间,columns为股票代码 quantile : bool, optional 为1则计算到某个分位点的距离, by default None square : bool, optional 为1则计算距离的平方, by default 0
Returns¶
pd.DataFrame
均值距离化之后的因子值
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 | |
get_fac_cross_via_func(df, func, history_file=None, backsee=20, fillna_method='ffill', daily=0)
¶
对一个日频因子,对其滚动时间窗口进行因子月度化计算。 具体操作为每天(或每月月底)截取过去一段窗口,并进行某个自定义的操作,
Parameters¶
df : pd.DataFrame
日频因子,index为时间,columns为股票代码,values为时间
func : Callable
自定义的操作函数,需要对一个窗口时间内的面板数据进行处理,最终要返回一个series,index为股票代码,values为月度化的因子值,name无所谓
history_file : str, optional
用于存储历史数据的本地文件, by default None
backsee : int, optional
滚动窗口长度, by default 20
fillna_method : Union[float, str], optional
对缺失值进行补全,可选择补全方式,输入'ffill'或'bfill'即为取前取后填充;输入数字则为用固定数字填充;输入None则不填充缺失值 by default "ffill"
daily : bool, optional
是否每天滚动, by default 0
Returns¶
pd.DataFrame 月度化后的因子值
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 | |
get_fac_via_corr(df, history_file=None, backsee=20, fillna_method='ffill', corr_method='pearson', daily=0, abs=0, riskmetrics=0, riskmetrics_lambda=0.94)
¶
对一个日频因子,对其滚动时间窗口进行因子月度化计算。 具体操作为每天(或每月月底)计算过去20天因子值的相关性矩阵, 然后对每个股票的所有相关系数求均值
Parameters¶
df : pd.DataFrame
日频因子,index为时间,columns为股票代码,values为时间
history_file : str, optional
用于存储历史数据的本地文件, by default None
backsee : int, optional
滚动窗口长度, by default 20
fillna_method : Union[float, str], optional
由于存在缺失值时,相关性矩阵的计算存在问题,因此这里对其进行补全,可选择补全方式,输入‘ffill'或'bfill'即为取前取后填充,输入数字则为用固定数字填充 by default "ffill"
corr_method : str, optional
求相关性的方法,可以指定'pearson'、'spearman'、'kendall', default 'pearson'
daily : bool, optional
是否每天滚动, by default 0
abs : bool, optional
是否要对相关系数矩阵取绝对值, by default 0
riskmetrics : bool, optional
使用RiskMetrics方法,对相关性进行调整,增加临近交易日的权重, by default 0
riskmetrics_lambda : float, optional
使用RiskMetrics方法时的lambda参数, by default 0.94
Returns¶
pd.DataFrame 月度化后的因子值
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 | |
get_list_std(delta_sts)
¶
同一天多个因子,计算这些因子在当天的标准差
Parameters¶
delta_sts : List[pd.DataFrame] 多个因子构成的list,每个因子index为时间,columns为股票代码
Returns¶
pd.DataFrame
每天每只股票多个因子的标准差
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 | |
get_list_std_weighted(delta_sts, weights)
¶
对多个df对应位置上的值求加权标准差
Parameters¶
delta_sts : List[pd.DataFrame] 多个dataframe weights : list 权重序列
Returns¶
pd.DataFrame 标准差序列
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 | |
get_normal(df)
¶
将因子横截面正态化
Parameters¶
df : pd.DataFrame 原始因子,index是时间,columns是股票代码
Returns¶
pd.DataFrame
每个横截面都呈现正态分布的因子
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 | |
get_value(df, n)
¶
很多因子计算时,会一次性生成很多值,使用时只取出一个值
Parameters¶
df : pd.DataFrame 每个value是一个列表或元组的pd.DataFrame n : int 取第n个值
Returns¶
pd.DataFrame
仅有第n个值构成的pd.DataFrame
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
get_values(df)
¶
从一个values为列表的dataframe中,一次性取出所有值,分别设置为一个dataframe,并依照顺序存储在列表中
Parameters¶
df : pd.DataFrame 一个values为list的dataframe
Returns¶
List[pd.DataFrame] 多个dataframe,每一个的values都是float形式
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 | |
indus_name(df, col_name=None)
¶
将2021版申万行业的代码,转化为对应行业的名字
Parameters¶
df : pd.DataFrame 一个包含申万一级行业代码的pd.DataFrame,其中某一列或index为行业代码 col_name : str, optional 仅某列为行业代码时指定该参数,该列的名字,否则默认转化index, by default None
Returns¶
pd.DataFrame
转化后的pd.DataFrame
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
judge_factor_by_third(fac1, fac2, judge)
¶
对于fac1和fac2两个因子,依据judge这个series或dataframe进行判断, judge可能为全市场的某个时序指标,也可能是每个股票各一个的指标, 如果judge这一期的值大于0,则取fac1的值,小于0则取fac2的值
Parameters¶
fac1 : pd.DataFrame 因子1,index为时间,columns为股票代码,values为因子值 fac2 : pd.DataFrame 因子2,index为时间,columns为股票代码,values为因子值 judge : Union[pd.DataFrame,pd.Series] 市场指标或个股指标,为市场指标时,则输入series形式,index为时间,values为指标值 为个股指标时,则输入dataframe形式,index为时间,columns为股票代码,values为因子值
Returns¶
pd.DataFrame 合成后的因子值,index为时间,columns为股票代码,values为因子值
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 | |
merge_many(dfs, names=None, how='outer')
¶
将多个宽dataframe依据columns和index,拼接在一起,拼成一个长dataframe
Parameters¶
dfs : List[pd.DataFrame] 将所有要拼接的宽表放在一个列表里 names : list, optional 拼接后,每一列宽表对应的名字, by default None how : str, optional 拼接的方式, by default 'outer'
Returns¶
pd.DataFrame 拼接后的dataframe
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
multidfs_to_one(*args)
¶
很多个df,各有一部分,其余位置都是空, 想把各自df有值的部分保留,都没有值的部分继续设为空
Returns¶
pd.DataFrame
合并后的df
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 | |
read_mat(path)
¶
读取mat文件
Parameters¶
path : str mat文件路径
Returns¶
pd.DataFrame
字典的第4个value
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
rqdatac_show_used()
¶
查询流量使用情况
Returns¶
float
当日已经使用的流量MB数
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
255 256 257 258 259 260 261 262 263 264 265 | |
same_columns(dfs)
¶
保留多个dataframe共同columns的部分
Parameters¶
dfs : List[pd.DataFrame] 多个dataframe
Returns¶
List[pd.DataFrame] 保留共同部分后的结果
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 | |
same_index(dfs)
¶
保留多个dataframe共同index的部分
Parameters¶
dfs : List[pd.DataFrame] 多个dataframe
Returns¶
List[pd.DataFrame] 保留共同部分后的结果
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 | |
select_max(df1, df2)
¶
两个columns与index完全相同的df,每个值都挑出较大值
Parameters¶
df1 : pd.DataFrame 第一个df df2 : pd.DataFrame 第二个df
Returns¶
pd.DataFrame
两个df每个value中的较大者
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 | |
select_min(df1, df2)
¶
两个columns与index完全相同的df,每个值都挑出较小值
Parameters¶
df1 : pd.DataFrame 第一个df df2 : pd.DataFrame 第二个df
Returns¶
pd.DataFrame
两个df每个value中的较小者
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 | |
set_index_first(df)
¶
将dataframe的第一列,无论其是什么名字,都设置为index
Parameters¶
df : pd.DataFrame 要修改的dataframe Returns
pd.DataFrame 修改后的dataframe
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | |
standardlize(df, all_pos=0)
¶
对因子dataframe做横截面z-score标准化
Parameters¶
df : pd.DataFrame 要做中性化的因子值,index是时间,columns是股票代码 all_pos : bool, optional 是否要将值都变成正数,通过减去截面的最小值实现, by default 0
Returns¶
pd.DataFrame 标准化之后的因子
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 | |
to_group(df, group=10)
¶
把一个index为时间,code为时间的df,每个截面上的值,按照排序分为group组,将值改为组号,从0开始
Parameters¶
df : pd.DataFrame 要改为组号的df group : int, optional 分为多少组, by default 10
Returns¶
pd.DataFrame 组号组成的dataframe
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 | |
to_percent(x)
¶
把小数转化为2位小数的百分数
Parameters¶
x : float 要转换的小数
Returns¶
Union[float,str] 空值则依然为空,否则返回带%的字符串
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 | |
zip_many_dfs(dfs)
¶
将多个dataframe,拼在一起,相同index和columns指向的那个values,变为多个dataframe的值的列表 通常用于存储整合分钟数据计算的因子值
Parameters¶
dfs : List[pd.DataFrame] 多个dataframe,每一个的values都是float形式
Returns¶
pd.DataFrame 整合后的dataframe,每一个values都是list的形式
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 | |
生成每日分类表(df, code, entry, exit, kind)
¶
| Text Only | |
|---|---|
1 2 3 4 5 6 7 | |
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
计算连续期数(ret0, point=0)
¶
计算一列数,持续大于或持续小于某个临界点的期数
Parameters¶
ret0 : pd.Series 收益率序列、或者某个指标的序列 point : float, optional 临界值, by default 0
Returns¶
pd.Series 持续大于或小于的期数
Source code in pure_ocean_breeze/data/tools.py
| Python | |
|---|---|
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 | |