我正试图将数据框转换为我可以在DB中存储为JSON blob的字典。我的代码如下:
import json
from yahooquery import Ticker
ticker_collection = ['AAPL', 'TSLA', 'NIO']
for ticker in ticker_collection:
ticker = Ticker(ticker)
stock_history = ticker.history(period='ytd', interval='1d')
dictionary = {
n: grp.loc[n].to_dict('index')
for n, grp in stock_history.set_index(['symbol', 'date']).groupby(level='date')
}
print("n" * 5)
print (json.dumps(dictionary, indent=4))
但是这会导致以下错误:
Traceback (most recent call last):
File "C:UsersXXXXXgitXXXXXXXXXX_4DDE_historical.py", line 23, in <module>
for n, grp in stock_history.set_index(['symbol', 'date']).groupby(level='date')
File "C:UsersXXXXXAppDataRoamingPythonPython39site-packagespandascoreframe.py", line 4724, in set_index
raise KeyError(f"None of {missing} are in the columns")
KeyError: "None of ['symbol', 'date'] are in the columns"
但是数据帧确实包含这些列:
open volume high close low adjclose dividends
symbol date
AAPL 2021-01-04 133.520004 143301900 133.610001 129.410004 126.760002 129.216919 0.000
2021-01-05 128.889999 97664900 131.740005 131.009995 128.429993 130.814514 0.000
2021-01-06 127.720001 155088000 131.050003 126.599998 126.379997 126.411102 0.000
2021-01-07 128.360001 109578200 131.630005 130.919998 127.860001 130.724655 0.000
2021-01-08 132.429993 105158200 132.630005 132.050003 130.229996 131.852966 0.000
2021-01-11 129.190002 100620900 130.169998 128.979996 128.500000 128.787552 0.000
2021-01-12 128.500000 91951100 129.690002 128.800003 126.860001 128.607819 0.000
2021-01-13 128.759995 88636800 131.449997 130.889999 128.490005 130.694702 0.000
2021-01-14 130.800003 90221800 131.000000 128.910004 128.759995 128.717667 0.000
2021-01-15 128.779999 111598500 130.220001 127.139999 127.000000 126.950294 0.000
2021-01-19 127.779999 90757300 128.710007 127.830002 126.940002 127.639267 0.000
2021-01-20 128.660004 104319500 132.490005 132.029999 128.550003 131.832993 0.000
2021-01-21 133.800003 120529500 139.669998 136.869995 133.589996 136.665771 0.000
2021-01-22 136.279999 114459400 139.850006 139.070007 135.020004 138.862503 0.000
2021-01-25 143.070007 157611700 145.089996 142.919998 136.539993 142.706757 0.000
2021-01-26 143.600006 98390600 144.300003 143.160004 141.369995 142.946396 0.000
2021-01-27 143.429993 140843800 144.300003 142.059998 140.410004 141.848038 0.000
2021-01-28 139.520004 142621100 141.990005 137.089996 136.699997 136.885452 0.000
2021-01-29 135.830002 177180600 136.740005 131.960007 130.210007 131.763107 0.000
2021-02-01 133.750000 106239800 135.380005 134.139999 130.929993 133.939850 0.000
2021-02-02 135.729996 83305400 136.309998 134.990005 134.610001 134.788589 0.000
2021-02-03 135.759995 89880900 135.770004 133.940002 133.610001 133.740158 0.000
2021-02-04 136.300003 84183100 137.399994 137.389999 134.589996 137.184998 0.000
2021-02-05 137.350006 75524000 137.419998 136.759995 135.860001 136.759995 0.205
2021-02-08 136.029999 71297200 136.960007 136.910004 134.919998 136.910004 0.000
2021-02-09 136.619995 76774200 137.880005 136.009995 135.850006 136.009995 0.000
2021-02-10 136.479996 73046600 136.990005 135.389999 134.399994 135.389999 0.000
2021-02-11 135.899994 64154400 136.389999 135.130005 133.770004 135.130005 0.000
2021-02-12 134.350006 60029300 135.529999 135.369995 133.690002 135.369995 0.000
2021-02-16 135.490005 80206200 136.009995 133.190002 132.789993 133.190002 0.000
2021-02-17 131.250000 97918500 132.220001 130.839996 129.470001 130.839996 0.000
high open volume close low adjclose
symbol date
TSLA 2021-01-04 744.489990 719.460022 48638200 729.770020 717.190002 729.770020
2021-01-05 740.840027 723.659973 32245200 735.109985 719.200012 735.109985
2021-01-06 774.000000 758.489990 44700000 755.979980 749.099976 755.979980
2021-01-07 816.989990 777.630005 51498900 816.039978 775.200012 816.039978
2021-01-08 884.489990 856.000000 75055500 880.020020 838.390015 880.020020
2021-01-11 854.429993 849.400024 59554100 811.190002 803.619995 811.190002
2021-01-12 868.000000 831.000000 46270700 849.440002 827.340027 849.440002
2021-01-13 860.469971 852.760010 33312500 854.409973 832.000000 854.409973
2021-01-14 863.000000 843.390015 31266300 845.000000 838.750000 845.000000
2021-01-15 859.900024 852.000000 38777600 826.159973 819.099976 826.159973
2021-01-19 850.000000 837.799988 25367000 844.549988 833.000000 844.549988
2021-01-20 859.500000 858.739990 25665900 850.450012 837.280029 850.450012
2021-01-21 855.719971 855.000000 20598100 844.989990 841.419983 844.989990
2021-01-22 848.000000 834.309998 20066500 846.640015 828.619995 846.640015
2021-01-25 900.400024 855.000000 41173400 880.799988 838.820007 880.799988
2021-01-26 895.900024 891.380005 23131600 883.090027 871.599976 883.090027
2021-01-27 891.500000 870.349976 27334000 864.159973 858.659973 864.159973
2021-01-28 848.000000 820.000000 26378000 835.429993 801.000000 835.429993
2021-01-29 842.409973 830.000000 34990800 793.530029 780.099976 793.530029
2021-02-01 842.000000 814.289978 25391400 839.809998 795.559998 839.809998
2021-02-02 880.500000 844.679993 24346200 872.789978 842.200012 872.789978
2021-02-03 878.080017 877.020020 18343500 854.690002 853.059998 854.690002
2021-02-04 856.500000 855.000000 15812700 849.989990 833.419983 849.989990
2021-02-05 864.770020 845.000000 18524800 852.229980 838.969971 852.229980
2021-02-08 877.770020 869.669983 20161700 863.419983 854.750000 863.419983
2021-02-09 859.799988 855.119995 15157700 849.460022 841.750000 849.460022
2021-02-10 844.820007 843.640015 36216100 804.820007 800.020020 804.820007
2021-02-11 829.880005 812.440002 21580700 811.659973 801.729980 811.659973
2021-02-12 817.330017 801.260010 23701700 816.119995 785.330017 816.119995
2021-02-16 821.000000 818.000000 19686700 796.219971 792.440002 796.219971
2021-02-17 799.840027 779.090027 25996500 798.150024 762.010010 798.150024
high open volume close low adjclose
symbol date
NIO 2021-01-04 54.450001 51.200001 213109700 53.490002 50.669998 53.490002
2021-01-05 53.750000 51.970001 147474800 53.200001 50.400002 53.200001
2021-01-06 55.279999 54.020000 176397700 50.500000 49.080002 50.500000
2021-01-07 54.610001 53.049999 119429200 54.279999 52.349998 54.279999
2021-01-08 59.310001 57.759998 218512800 58.919998 55.880001 58.919998
2021-01-11 66.989998 64.949997 279770300 62.700001 62.189999 62.700001
2021-01-12 64.129997 61.419998 184854200 62.040001 60.419998 62.040001
2021-01-13 64.410004 62.939999 116059200 62.150002 61.410000 62.150002
2021-01-14 63.230000 62.630001 99338400 60.869999 60.040001 60.869999
2021-01-15 60.220001 59.840000 132247900 56.270000 55.669998 56.270000
2021-01-19 59.009998 58.209999 99561100 58.439999 56.410000 58.439999
2021-01-20 60.090000 59.849998 94983600 57.709999 56.189999 57.709999
2021-01-21 58.580002 57.020000 73119500 58.340000 55.880001 58.340000
2021-01-22 62.169998 58.950001 133918100 61.950001 58.320000 61.950001
2021-01-25 64.519997 64.050003 111986400 59.990002 58.349998 59.990002
2021-01-26 60.669998 60.040001 70393300 60.310001 58.639999 60.310001
2021-01-27 59.790001 57.950001 73078300 57.150002 56.599998 57.150002
2021-01-28 59.459999 56.759998 73955100 58.369999 55.150002 58.369999
2021-01-29 60.709999 59.029999 78678200 57.000000 56.439999 57.000000
2021-02-01 59.119999 59.070000 79878700 56.990002 54.369999 56.990002
2021-02-02 57.540001 57.060001 66234100 55.770000 54.509998 55.770000
2021-02-03 58.720001 55.959999 80122300 57.980000 54.840000 57.980000
2021-02-04 59.200001 58.790001 48667900 57.599998 57.110001 57.599998
2021-02-05 57.980000 57.740002 42892500 56.669998 56.009998 56.669998
2021-02-08 59.520000 57.080002 64030100 59.070000 56.180000 59.070000
2021-02-09 63.099998 58.950001 97246500 62.840000 58.240002 62.840000
2021-02-10 64.599998 62.549999 90366700 61.259998 60.330002 61.259998
2021-02-11 63.060001 61.689999 55615500 60.270000 59.810001 60.270000
2021-02-12 61.049999 60.259998 39973800 59.849998 59.099998 59.849998
2021-02-16 62.139999 60.400002 54914800 59.230000 58.650002 59.230000
2021-02-17 58.029999 57.919998 63971200 57.320000 55.900002 57.320000
在此之前,我问了一个关于将数据帧转换为字典格式的问题,随着我在文档中的进展,这些是我面临的问题。
我想要的结果是这样的:
{
"APPL": {
"2021-01-04": {
"open" : "133.520004",
"adjclose" : "133.520004",
"low" : "133.520004",
"high" : "133.520004"
},
"2021-01-05": {
"open" : "133.520004",
"adjclose" : "133.520004",
"low" : "133.520004",
"high" : "133.520004"
}
}
}
符号/日期看起来已经索引,所以set_index将失败。你不需要这个。您可以分解长函数链来调试哪一步有问题。