alldata3['datetime'] = pd.to_datetime(alldata3['datetime']) alldata3['datetime'] = alldata3['datetime'].dt.strftime('%Y-%m-%d %H:%M:%S') alldata3['datetime'] = alldata3['datetime'].apply(lambda x:datetime.strptime(x,'%Y-%m-%d %H:%M:%S')),出现错误SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
时间: 2023-10-09 22:14:59 浏览: 33
这个错误通常是因为在对 DataFrame 进行操作时,使用了链式索引(chained indexing)而不是使用 .loc 或 .iloc 等显式索引。链式索引是指通过连续的多个中括号索引,例如 alldata3['datetime'][0],这样的代码会返回一个视图对象,而不是 DataFrame 的副本。因此,当我们对这个视图对象进行修改时,可能会影响到原始 DataFrame。为了避免这个问题,我们应该使用显式索引,例如 alldata3.loc[0, 'datetime'],这样的代码会返回一个副本,而不是视图对象。
你可以尝试使用下面的代码,使用 .loc 进行显式索引:
```
alldata3.loc[:, 'datetime'] = pd.to_datetime(alldata3['datetime'])
alldata3.loc[:, 'datetime'] = alldata3['datetime'].dt.strftime('%Y-%m-%d %H:%M:%S')
alldata3.loc[:, 'datetime'] = alldata3['datetime'].apply(lambda x:datetime.strptime(x,'%Y-%m-%d %H:%M:%S'))
```
这样应该可以避免 SettingWithCopyWarning 错误。另外,如果你想要了解更多关于链式索引和显式索引的知识,可以参考 Pandas 官方文档中的相关章节。
相关问题
alldata3['datetime'] = pd.to_datetime(alldata3['datetime']) alldata3['datetime'] = alldata3['datetime'].dt.strftime('%Y-%m-%d %H:%M:%S') alldata3['datetime'] = alldata3['datetime'].apply(lambda x:datetime.strptime(x,'%Y-%m-%d %H:%M:%S')),出现错误SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
这个警告通常出现在你尝试对 DataFrame 的一个切片进行修改时。为了避免此警告,你可以使用 .loc 或 .iloc 来修改 DataFrame 中的值,而不是直接对切片进行修改。
你可以这样修改代码:
```
alldata3.loc[:, 'datetime'] = pd.to_datetime(alldata3['datetime'])
alldata3.loc[:, 'datetime'] = alldata3['datetime'].dt.strftime('%Y-%m-%d %H:%M:%S')
alldata3.loc[:, 'datetime'] = alldata3['datetime'].apply(lambda x:datetime.strptime(x,'%Y-%m-%d %H:%M:%S'))
```
这样就使用了 .loc 来修改 DataFrame 中的值,避免了 SettingWithCopyWarning 错误。
def select_stocks(): today = datetime.date.today() five_days_ago = today - datetime.timedelta(days=5) stocks = ts.get_stock_basics() for index, row in stocks.iterrows(): data = ts.get_hist_data(index, start=five_days_ago.strftime('%Y-%m-%d'), end=today.strftime('%Y-%m-%d')) if len(data) == 5 and all(data['p_change'] > 0): print(index)改正上述代码
在上述代码中,有几个问题需要修正:
1. 缺少必要的库导入。需要导入datetime和tushare库。
2. 由于tushare库接口的变化,需要对 get_stock_basics() 方法进行更新,改为使用 get_stock_basics() 方法的新版本 get_stock_basics()。
3. 由于 get_stock_basics() 方法返回的是一个DataFrame对象,其中index为股票代码,需要将其转换为字符串形式才能作为参数传递给 get_hist_data() 方法。
4. 在循环中,应该首先检查数据是否存在,再进行后续操作。
在以上基础上,修正后的代码如下:
```python
import datetime
import tushare as ts
def select_stocks():
today = datetime.date.today()
five_days_ago = today - datetime.timedelta(days=5)
stocks = ts.get_stock_basics()
for index, row in stocks.iterrows():
data = ts.get_hist_data(str(index), start=five_days_ago.strftime('%Y-%m-%d'), end=today.strftime('%Y-%m-%d'))
if data is not None and len(data) == 5 and all(data['p_change'] > 0):
print(index)
```
修正后的代码中,首先导入了必要的库,然后对 get_stock_basics() 方法进行了更新,将获取股票数据的代码放入了 if 语句中,以确保数据存在后才进行后续操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)