df['交易日期'] = pd.to_datetime(df['交易日期']) df.set_index('交易日期', inplace=True) r_data = df.groupby('股票代码').resample('M').agg({'开盘价': 'first', '收盘价': 'last','成交量':'sum','成交额':'sum'}) r_data['交易天数'] = r_data.groupby('股票代码').size() print(r_data['交易天数'])
时间: 2023-08-20 09:51:14 浏览: 61
股票交易数据
The code you provided seems to be correct for calculating the number of trading days (`交易天数`) for each stock code (`股票代码`) in monthly data. However, please note that the `size()` function should be used as `size` instead of `size()` to assign the calculated values to the column.
Here's the updated code:
```python
df['交易日期'] = pd.to_datetime(df['交易日期'])
df.set_index('交易日期', inplace=True)
r_data = df.groupby('股票代码').resample('M').agg({'开盘价': 'first', '收盘价': 'last', '成交量': 'sum', '成交额': 'sum'})
r_data['交易天数'] = r_data.groupby('股票代码').size
print(r_data['交易天数'])
```
This code should calculate the number of trading days for each stock code in the monthly data and print the result.
Please let me know if you have any further questions!
阅读全文