剔除停盘股票的python代码
时间: 2024-06-23 08:01:24 浏览: 6
在Python中,我们可以使用pandas库和金融数据API(如Yahoo Finance、Alpha Vantage或Quandl)来获取股票数据并剔除停盘股票。以下是一个基本的示例,假设我们使用pandas_datareader库来获取股票数据:
```python
import pandas as pd
from pandas_datareader import data as pdr
import datetime
# 定义一个函数来获取股票数据并检查是否停盘
def get_stock_data(ticker, start_date, end_date):
try:
# 使用pandas_datareader获取股票数据
df = pdr.get_data_yahoo(ticker, start=start_date, end=end_date)
# 检查'Close'列是否有缺失值,这通常表示股票停盘
if df['Close'].isna().any():
print(f"{ticker}在指定期间内存在停盘情况.")
return None
else:
return df
except Exception as e:
print(f"无法获取{ticker}的数据: {str(e)}")
return None
# 示例:获取AAPL(苹果公司)的股票数据,从2020-01-01到2023-03-31
start_date = datetime.datetime(2020, 1, 1)
end_date = datetime.datetime(2023, 3, 31)
ticker = 'AAPL'
stock_df = get_stock_data(ticker, start_date, end_date)
if stock_df is not None:
# 剔除非交易日数据并展示结果
stock_df = stock_df.dropna()
print(stock_df)
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)