【问题标题】:AttributeError: 'DataFrame' object has no attribute 'date'AttributeError:“DataFrame”对象没有属性“日期”
【发布时间】:2016-07-03 19:39:02
【问题描述】:

我正在关注Harvard-CS109-homework-1的解决方案代码 https://github.com/cs109/content

在iPythonNotebook上,运行这行代码“errors = all_error_data()”后出现此错误(我没有更改解决方案代码):

errors = all_error_data()


/anaconda/lib/python2.7/site-packages/ipykernel/__main__.py:67: FutureWarning: sort(columns=....) is deprecated, use sort_values(by=.....)
/anaconda/lib/python2.7/site-packages/ipykernel/__main__.py:46: FutureWarning: .resample() is now a deferred operation
use .resample(...).mean() instead of .resample(...)
---------------------------------------------------------------------------



AttributeError                            Traceback (most recent call last)
<ipython-input-18-aea6d425eefd> in <module>()
----> 1 errors = all_error_data()

<ipython-input-17-2a9c686164e8> in all_error_data()
     17 #your code here
     18 def all_error_data():
---> 19     data = [error_data(race_page) for race_page in find_governor_races(page)]
     20     return pd.concat(data, ignore_index=True)

<ipython-input-14-5c576ec1e316> in error_data(url)
     48     #compute forecast length in days
     49     #(assuming that last forecast happens on the day of the election, for simplicity)
---> 50     forecast_length = (df.date.max() - df.date).values
     51     forecast_length = forecast_length / np.timedelta64(1, 'D')  # convert to number of days
     52 

//anaconda/lib/python2.7/site-packages/pandas/core/generic.pyc in __getattr__(self, name)
   2667             if name in self._info_axis:
   2668                 return self[name]
-> 2669             return object.__getattribute__(self, name)
   2670 
   2671     def __setattr__(self, name, value):

AttributeError: 'DataFrame' object has no attribute 'date' 

非常感谢分享建议!!

【问题讨论】:

    标签: python-2.7 pandas anaconda


    【解决方案1】:

    我遇到了同样的问题并做了一些研究。 pandas.resample 发生了几处更改,现在更改了 DataFrame 并删除了非数字数据。

    在 pd.resample 之后没有“数据”列,因此为了修复您的代码,只需更改第 50 行以解决索引列而不是数据列:

        forecast_length = (df.index.max() - df.index).values
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-23
      • 2017-01-24
      • 2018-10-10
      • 2019-08-18
      • 2021-01-20
      • 2020-05-16
      • 2018-10-04
      • 2021-04-05
      相关资源
      最近更新 更多