【问题标题】:pandas Timedelta error熊猫时间增量错误
【发布时间】:2013-02-15 10:54:19
【问题描述】:

运行 pandas 文档中的代码示例时出现错误。

我怀疑这可能与我使用的 pandas 版本有关,但我无法确认。

pandas VERSION 0.10.1  
numpy VERSION 1.7.0  
scipy VERSION 0.12.0.dev-14b1e07  

以下示例直接取自此处的 pandas 文档:

pandas - Time Deltas
这行得通

from datetime import datetime, timedelta  
from pandas import *  

s  = Series(date_range('2012-1-1', periods=3, freq='D'))  
s  

Out[52]:  
0   2012-01-01 00:00:00  
1   2012-01-02 00:00:00  
2   2012-01-03 00:00:00  

这样

td = Series([ timedelta(days=i) for i in range(3) ])  
td  
Out[53]:  
0            0:00:00  
1     1 day, 0:00:00  
2    2 days, 0:00:00  

df = DataFrame(dict(A = s, B = td))  
df  
Out[54]:  
                    A                B  
0 2012-01-01 00:00:00          0:00:00  
1 2012-01-02 00:00:00   1 day, 0:00:00  
2 2012-01-03 00:00:00  2 days, 0:00:00  

这似乎与文档中的预期输出一致。

示例代码中的下一行产生错误:

df['C'] = df['A'] + df['B']  

...

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-55-7057e174d79e> in <module>()
----> 1 df['C'] = df['A'] + df['B']

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/series.pyc in wrapper(self, other)
     91             if self.index.equals(other.index):
     92                 name = _maybe_match_name(self, other)
---> 93                 return Series(wrap_results(na_op(lvalues, rvalues)),
     94                               index=self.index, name=name)
     95 

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/series.pyc in na_op(x, y)
     63             if isinstance(y, np.ndarray):
     64                 mask = notnull(x) & notnull(y)
---> 65                 result[mask] = op(x[mask], y[mask])
     66             else:
     67                 mask = notnull(x)

TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('O')

数据类型:

df.dtypes

Out[56]: 
A    datetime64[ns]
B            object

同样,当我做加法/减法时,我得到一个错误:

s - s.max()

<ipython-input-57-8d53e24db927> in <module>()
----> 1 s - s.max()

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/series.pyc in wrapper(self, other)
     78 
     79         if (com.is_datetime64_dtype(self) and
---> 80             com.is_datetime64_dtype(other)):
     81             lvalues = lvalues.view('i8')
     82             rvalues = rvalues.view('i8')

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/common.pyc in is_datetime64_dtype(arr_or_dtype)
   1003         tipo = arr_or_dtype.type
   1004     else:
-> 1005         tipo = arr_or_dtype.dtype.type
   1006     return issubclass(tipo, np.datetime64)
   1007 

AttributeError: 'Timestamp' object has no attribute 'dtype'

此代码是一个要点,以便于参考。

https://gist.github.com/hernamesbarbara/5061972

感谢您的任何帮助或建议;非常感谢。

-奥斯汀

【问题讨论】:

    标签: datetime pandas time-series series timedelta


    【解决方案1】:

    我尝试关闭所有 python 实例并重新启动一切,但它仍然没有工作。
    然而,这对我有用。
    卸载熊猫

    conda remove pandas 
    

    pip uninstall pandas 
    

    然后重新安装

    conda install pandas
    

    pip install pandas
    

    【讨论】:

      【解决方案2】:

      如果您查看链接到的页面标题(浏览器窗口顶部),您会发现它是 pandas 的开发版本: http://pandas.pydata.org/pandas-docs/dev/timeseries.html#time-deltas

      所以,今天就是版本

      '0.11.0.dev-13ae597'
      

      这段代码运行良好的地方。

      稳定版的文档在这里:

      http://pandas.pydata.org/pandas-docs/stable/

      您将在浏览器窗口顶部看到的位置

      pandas 0.10.1
      

      这是你的版本。

      【讨论】:

      • 噢!感谢您的快速回复!
      猜你喜欢
      • 2014-06-25
      • 2017-06-10
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      • 2016-11-09
      • 2021-07-01
      相关资源
      最近更新 更多