Python:DataError:没有要聚合的数字类型

时间:2019-12-08 21:07:43

标签: python pandas matplotlib seaborn

好,我收到此错误:DataError:没有要聚合的数字类型  我试图绘制一个CSV文件,它无法正常工作。有什么建议吗?谢谢这是我的代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.read_csv('spacestocksinfo.csv', skipinitialspace=True) 

df['Date']= pd.to_datetime(df['Date'])  

for col in df.columns:
    try:
        df[col] = df[col].astype(float)
    except:
        continue


pd.read_csv('spacestocksinfo.csv', skipinitialspace=True)
sns.lineplot(data=df, x='Date',y='High', hue='company')  

csv文件的格式是这样,但有更多行:

url,company,Date,Open,High,Low,Close,Adj Close,Volume
https://finance.yahoo.com/quote/I/history?p=I,Intelsat S.A.,"Dec 06, 2019",6.10,6.59,6.01,6.53,6.53,"6,737,585"
https://finance.yahoo.com/quote/I/history?p=I,Intelsat S.A.,"Dec 05, 2019",6.64,6.68,6.02,6.05,6.05,"5,919,800"

这是错误:

File "4.py", line 30, in <module>
    sns.lineplot(data=df, x='Date',y='High', hue='company')
  File "C:\python37\lib\site-packages\seaborn\relational.py", line 1084, in lineplot
    p.plot(ax, kwargs)
  File "C:\python37\lib\site-packages\seaborn\relational.py", line 775, in plot
    x, y, y_ci = self.aggregate(y, x, units)
  File "C:\python37\lib\site-packages\seaborn\relational.py", line 705, in aggregate
    est = grouped.agg(func)
  File "C:\python37\lib\site-packages\pandas\core\groupby\generic.py", line 845, in aggregate
    return getattr(self, func_or_funcs)(*args, **kwargs)
  File "C:\python37\lib\site-packages\pandas\core\groupby\groupby.py", line 1205, in mean
    "mean", alt=lambda x, axis: Series(x).mean(**kwargs), **kwargs
  File "C:\python37\lib\site-packages\pandas\core\groupby\groupby.py", line 888, in _cython_agg_general
    raise DataError("No numeric types to aggregate")
pandas.core.base.DataError: No numeric types to aggregate

怎么回事?

1 个答案:

答案 0 :(得分:0)

所以我注意到很多人都犯了这个错误。让我解释一下这个问题。好的,所以我必须清理数据。一家公司一段时间未发布数据,这导致数据不完整。我必须删除没有发布数据的csv部分。那是导致错误的原因。一旦那不行,我的代码就会运行。至于说我的代码很好的人,他是对的。我在这里发布的内容会很好,因为我在此处发布的部分csv完整而干净。