18行报错:发生异常: DataError Cannot aggregate non-numeric type: object ValueError: could not convert string to float: '2023-07-13 17:26:48:27' The above exception was the direct cause of the following exception: TypeError: cannot handle this type -> object The above exception was the direct cause of the following exception: File "/Users/dingfengyan/工作/项目开发/哈理工/批量导出功能/feature.py", line 18, in tsa_processing tsa_df = df.rolling(window=window_size).mean() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/dingfengyan/工作/项目开发/哈理工/批量导出功能/feature.py", line 29, in <module> tsa_processing(input_folder, output_folder, window_size) pandas.errors.DataError: Cannot aggregate non-numeric type: object
时间: 2023-08-05 19:03:07 浏览: 464
这个错误是因为您的CSV文件中包含了非数值类型的数据,而`mean()`函数只能应用于数值类型的列。
为了解决这个问题,您可以在读取CSV文件时指定哪些列应该被解析为数值类型。您可以使用`dtype`参数来指定列的数据类型。下面是修改后的代码:
```python
import os
import pandas as pd
def tsa_processing(input_folder, output_folder, window_size):
# 检查输出文件夹是否存在,若不存在则创建
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 遍历输入文件夹中的所有文件
for root, dirs, files in os.walk(input_folder):
for file in files:
if file.endswith(".csv"):
# 读取CSV文件,指定数值类型的列
input_path = os.path.join(root, file)
df = pd.read_csv(input_path, dtype=float)
# 执行时域同步平均处理
tsa_df = df.rolling(window=window_size).mean()
# 构建输出文件路径
output_file = os.path.splitext(file)[0] + "_tsa.csv"
output_path = os.path.join(output_folder, output_file)
# 将处理后的数据写入新的CSV文件
tsa_df.to_csv(output_path, index=False)
```
在这个修改后的代码中,通过将`dtype`参数设置为`float`,将所有列都解析为数值类型。这样,在执行时域同步平均处理时,就不会出现类似的错误。
请尝试使用这个修改后的代码,如果还有其他问题,请随时提问。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)