Anaconda_virtual env_Tensorflow_ipykernel_plotly_yfinance multi_level_index_DatetimeIndex为shortDate

最近系统更新后原先安装的TensorFlow和jupyter notebook都没了

1.Anaconda的下载

Download Now | Anaconda

C:\Users\liqin\anaconda3

2. create a new virtual Python environment

ts13_install tf env_RNN_Bidirectional LSTM_GRU_Minimal gated_TimeDistributed_Time Series Forecasting_minimal gated unit keras-优快云博客

conda create -n tensorflow python=3.8 -y

type command: conda info --envs

4. install tensorflow 

ts13_install tf env_RNN_Bidirectional LSTM_GRU_Minimal gated_TimeDistributed_Time Series Forecasting_minimal gated unit keras-优快云博客

conda activate tensorflow


pip install tensorflow

5. ipykernel 

pip install ipykernel

To make the new tensorflow environment visible within Jupyter, run the following code:ts11_pmdarima_edgecolor_bokeh plotly_Prophet_Fourier_VAR_endog exog_Granger causality_IRF_Garch vola_layout.xaxis.rangebreaks-优快云博客

python -m ipykernel install --name tensorflow --display-name "tensorflow"

python -m ipykernel install --name tensorflow --display-name "tensorflow"

6. conda install jupyter notebook

conda install jupyter notebook

 

Put all jupyter notebook together

OR you can copy a shortcut from other jupyter notebook then rename
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Anaconda3 (64-bit)

C:\Users\liqin\anaconda3\python.exe C:\Users\liqin\anaconda3\cwp.py C:\Users\liqin\anaconda3\envs\tensorflow C:\Users\liqin\anaconda3\envs\tensorflow\python.exe C:\Users\liqin\anaconda3\envs\tensorflow\Scripts\jupyter-notebook-script.py %USERPROFILE%

C:\Users\liqin\anaconda3\python.exe C:\Users\liqin\anaconda3\cwp.py C:\Users\liqin\anaconda3\envs\tensorflow C:\Users\liqin\anaconda3\envs\tensorflow\python.exe C:\Users\liqin\anaconda3\envs\tensorflow\Scripts\jupyter-notebook-script.py %USERPROFILE%

 Show Line Numbers

7.update pip if you need

python -m pip install -U pip

8.python -m pip install -U matplotlib

python -m pip install -U matplotlib

9.  pip install plotly

ts11_pmdarima_edgecolor_bokeh plotly_Prophet_Fourier_VAR_endog exog_Granger causality_IRF_Garch vola_layout.xaxis.rangebreaks-优快云博客

pip install plotly

10. pip install pandas_datareader

pip install pandas_datareader

 

11. pip install yfinance 

pip install yfinance

虽然正确安装了但是Jupyter不能用 

import yfinance as yf

logger.error(f"Failed to get screener data for '{self._body.get('query', "query not set")}' reason: {e}") ^ SyntaxError: invalid syntax

Yfinance | Anaconda.org

conda install conda-forge::yfinance

 

 链接问题
再次运行

conda install conda-forge::yfinance

 

stack Ticker in the MultiIndex for dropping Ticker columns

import yfinance as yf

stock_symbol='ADTN' #ADTRAN Inc
start_date='2008-02-15'
new_start_date = pd.to_datetime('2007-04-18')
new_end_date = '2008-04-16'

df = yf.download( stock_symbol, start=new_start_date, end=new_end_date)

df


小细节出现了Ticker这一行
vs

level=0堆叠就是按Price进行堆叠

import yfinance as yf

stock_symbol='ADTN' #ADTRAN Inc
start_date='2008-02-15'
new_start_date = pd.to_datetime('2007-04-18')
new_end_date = '2008-04-16'

df = yf.download( [stock_symbol], start=new_start_date, end=new_end_date)
# Transform the DataFrame: stack the ticker symbols to create a multi-index (Date, Ticker), 
# then reset the 'Ticker' level to turn it into a column
df = df.stack(level=1)#.rename_axis(['Date', 'Ticker']).reset_index(level=1)
#df.drop(columns='Ticker', inplace=True)
df

Level(s) to stack from the column axis onto the index axis, defined as one index or label, or a list of indices or labels.
列轴堆叠到索引轴上的级别,定义为一个索引或标签,或者索引或标签的列表。
 按level=1堆叠就是按Ticker进行堆叠

import yfinance as yf

stock_symbol='ADTN' #ADTRAN Inc
start_date='2008-02-15'
new_start_date = pd.to_datetime('2007-04-18')
new_end_date = '2008-04-16'

df = yf.download( [stock_symbol], start=new_start_date, end=new_end_date)
# Transform the DataFrame: stack the ticker symbols to create a multi-index (Date, Ticker), 
# then reset the 'Ticker' level to turn it into a column

#df = df.stack(level=1).rename_axis(['Date', 'Ticker']).reset_index(level=1)
#OR
df = df.stack(level=1).reset_index(level=1)
#df.drop(columns='Ticker', inplace=True)
df

import yfinance as yf

stock_symbol='ADTN' #ADTRAN Inc
start_date='2008-02-15'
new_start_date = pd.to_datetime('2007-04-18')
new_end_date = '2008-04-16'

df = yf.download( [stock_symbol], start=new_start_date, end=new_end_date)
# Transform the DataFrame: stack the ticker symbols to create a multi-index (Date, Ticker), 
# then reset the 'Ticker' level to turn it into a column
#df = df.stack(level=1).rename_axis(['Date', 'Ticker']).reset_index(level=1)
df = df.stack(level=1).reset_index(level=1)
df.drop(columns='Ticker', inplace=True)
df.columns.name=''###
df

DatetimeIndex  to short Date

import yfinance as yf

stock_symbol='ADTN' #ADTRAN Inc
start_date='2008-02-15'
new_start_date = pd.to_datetime('2007-04-18')
new_end_date = '2008-04-16'

df = yf.download( [stock_symbol], start=new_start_date, end=new_end_date)
# Transform the DataFrame: stack the ticker symbols to create a multi-index (Date, Ticker), 
# then reset the 'Ticker' level to turn it into a column
#df = df.stack(level=1).rename_axis(['Date', 'Ticker']).reset_index(level=1)
df = df.stack(level=1).reset_index(level=1)
df.drop(columns='Ticker', inplace=True)
df.columns.name=''
#df['Date'] = df['Date'].dt.strftime('%Y-%m-%d')
df.index=df.index.strftime('%Y-%m-%d')
df

不对,我们需要让dae保持为DateTimeIndex

import yfinance as yf

stock_symbol='ADTN' #ADTRAN Inc
start_date='2008-02-15'
new_start_date = pd.to_datetime('2007-04-18')
new_end_date = '2008-04-16'

df = yf.download( [stock_symbol], start=new_start_date, end=new_end_date)
# Transform the DataFrame: stack the ticker symbols to create a multi-index (Date, Ticker), 
# then reset the 'Ticker' level to turn it into a column
#df = df.stack(level=1).rename_axis(['Date', 'Ticker']).reset_index(level=1)
df = df.stack(level=1).reset_index(level=1)
df.drop(columns='Ticker', inplace=True)
df.columns.name=''
#df['Date'] = df['Date'].dt.strftime('%Y-%m-%d')
#df.index=df.index.strftime('%Y-%m-%d')
df.index=pd.to_datetime(df.index.date)
df

multi_level_index: bool : Default is True

Optional. Always return a MultiIndex DataFrame? Default is True

import yfinance as yf

stock_symbol='ADTN' #ADTRAN Inc
start_date='2008-02-15'
new_start_date = pd.to_datetime('2007-04-18')
new_end_date = '2008-04-16'

df = yf.download( [stock_symbol], start=new_start_date, end=new_end_date, multi_level_index=False)

df

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LIQING LIN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值