将yFinance数据导出到pandas的to_csv中。

huangapple go评论70阅读模式
英文:

pulling yFinance to pandas to_csv

问题

以下是您要翻译的内容:

I'm trying to pull information for 5y of ticker history(where applicable) from a series of stocks and save it to a csv so I only have to do this step once (can load the 5 year data locally and just update daily to it for the values I end up following). however it keeps throwing an error on the write side and not sure how to correct as all sources I've seen are either cryptic(as in the official documentation) or don't work (other solutions here and a few other sites I can't remember). From the error it seems i need to reshape the data however I'm not quite sure how to go about doing that, and researching it just left me more confused.

Code and Error are as follows.

获取股票的5年历史信息(如果适用),并将其保存到CSV文件中,这样我只需要执行一次此步骤(可以在本地加载5年数据,然后每天更新我关注的值)。然而,在写入数据时不断出现错误,我不确定如何纠正,因为我看到的所有来源要么很晦涩(比如官方文档),要么不起作用(其他解决方案在这里和其他一些网站上,我记不清了)。从错误信息看,似乎需要重塑数据,但我不太确定如何做,研究只让我更加困惑。

以下是代码和错误信息:

import yfinance as yf
import pandas as pd

data = pd.DataFrame()
foo = yf.Ticker('GRAB')
temp = pd.DataFrame([foo.history('5y')])

temp.to_csv('out.csv')

出现错误:

Traceback (most recent call last):
  File "C:\Users\jmshe\AppData\Local\Programs\Python\Python310\stock 2.py", line 10, in <module>
    temp = pd.DataFrame([foo.history('5y')])
  File "C:\Users\jmshe\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 762, in __init__
    mgr = ndarray_to_mgr(
  File "C:\Users\jmshe\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\construction.py", line 329, in ndarray_to_mgr
    values = _prep_ndarraylike(values, copy=copy_on_sanitize)
  File "C:\Users\jmshe\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\construction.py", line 583, in _prep_ndarraylike
    raise ValueError(f"Must pass 2-d input. shape={values.shape}")
ValueError: Must pass 2-d input. shape=(1, 561, 7)

请记住这只是一个测试代码用于在编写完整代码之前进行测试以避免在测试过程中不断向Yahoo发起1000次请求

尝试过`pandas.shape()`,但不确定具体语法
英文:

I'm trying to pull information for 5y of ticker history(where applicable) from a series of stocks and save it to a csv so I only have to do this step once (can load the 5 year data locally and just update daily to it for the values I end up following). however it keeps throwing an error on the write side and not sure how to correct as all sources I've seen are either cryptic(as in the official documentation) or don't work (other solutions here and a few other sites I can't remember). From the error it seems i need to reshape the data however I'm not quite sure how to go about doing that, and researching it just left me more confused.

Code and Error are as follows.

import yfinance as yf
import pandas as pd


data = pd.DataFrame()
foo = yf.Ticker(&#39;GRAB&#39;)
temp = pd.DataFrame([foo.history(&#39;5y&#39;)])

temp.to_csv(&#39;out.csv&#39;)

getting error:

Traceback (most recent call last):
File "C:\Users\jmshe\AppData\Local\Programs\Python\Python310\stock 2.py", line 10, in <module>
temp = pd.DataFrame([foo.history('5y')])
File "C:\Users\jmshe\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 762, in init
mgr = ndarray_to_mgr(
File "C:\Users\jmshe\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\construction.py", line 329, in ndarray_to_mgr
values = _prep_ndarraylike(values, copy=copy_on_sanitize)
File "C:\Users\jmshe\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\construction.py", line 583, in _prep_ndarraylike
raise ValueError(f"Must pass 2-d input. shape={values.shape}")
ValueError: Must pass 2-d input. shape=(1, 561, 7)

please keep in mind this is just a test code before writing the full thing so I don't have to constantly poll yahoo 1000 times during testing

Attempted pandas.shape() but unsure of exact syntax

答案1

得分: 1

foo.history('5y')已经返回一个DataFrame,所以你不需要创建一个:

import yfinance as yf
import pandas as pd

data = pd.DataFrame()
foo = yf.Ticker('GRAB')
temp = foo.history('5y')
# 或者 temp = yf.Ticker('GRAB').history('5y')

temp.to_csv('out.csv')

输出:

>>> temp
             Open   High    Low  Close    Volume  Dividends  Stock Splits
Date                                                                     
2020-12-01  11.89  11.89  11.89  11.89       500          0             0
2020-12-02  12.48  12.48  11.82  11.82      1000          0             0
2020-12-03  11.82  11.90  11.80  11.90     14100          0             0
2020-12-04  12.99  13.98  11.50  12.40     15500          0             0
2020-12-07  13.15  14.00  12.20  12.55     14500          0             0
...           ...    ...    ...    ...       ...        ...           ...
2023-02-16   3.63   3.73   3.56   3.67  20078600          0             0
2023-02-17   3.65   3.69   3.43   3.48  17590000          0             0
2023-02-21   3.47   3.52   3.38   3.40  12430000          0             0
2023-02-22   3.45   3.55   3.35   3.50  18951100          0             0
2023-02-23   3.55   3.57   3.15   3.21  26774253          0             0
英文:

foo.history(&#39;5y&#39;) already returns a DataFrame so you don't need to create one:

import yfinance as yf
import pandas as pd

data = pd.DataFrame()
foo = yf.Ticker(&#39;GRAB&#39;)
temp = foo.history(&#39;5y&#39;)
# or temp = yf.Ticker(&#39;GRAB&#39;).history(&#39;5y&#39;)

temp.to_csv(&#39;out.csv&#39;)

Output:

&gt;&gt;&gt; temp
             Open   High    Low  Close    Volume  Dividends  Stock Splits
Date                                                                     
2020-12-01  11.89  11.89  11.89  11.89       500          0             0
2020-12-02  12.48  12.48  11.82  11.82      1000          0             0
2020-12-03  11.82  11.90  11.80  11.90     14100          0             0
2020-12-04  12.99  13.98  11.50  12.40     15500          0             0
2020-12-07  13.15  14.00  12.20  12.55     14500          0             0
...           ...    ...    ...    ...       ...        ...           ...
2023-02-16   3.63   3.73   3.56   3.67  20078600          0             0
2023-02-17   3.65   3.69   3.43   3.48  17590000          0             0
2023-02-21   3.47   3.52   3.38   3.40  12430000          0             0
2023-02-22   3.45   3.55   3.35   3.50  18951100          0             0
2023-02-23   3.55   3.57   3.15   3.21  26774253          0             0

huangapple
  • 本文由 发表于 2023年2月24日 05:50:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定