本文介绍了 pandas Timedelta错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在pandas文档中运行代码示例时,我收到错误。



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

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

以下示例直接来自熊猫文档:





这个工作原理

  from datetime import datetime,timedelta 
from pandas import *

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

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

这样

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

df = DataFrame(dict(A = s,B = td))
df
输出[54] :
AB
0 2012-01-01 00:00:00 0:00:00
1 2012-01-02 00:00:00 1天,0:00:00
2 2012-01-03 00:00:00 2天,0:00:00

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



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

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

..

  ------------------------ -------------------------------------------------- -  
TypeError追溯(最近的最后一次调用)
< ipython-input-55-7057e174d79e>在< module>()
----> 1 df ['C'] = df ['A'] + df ['B']

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- package / pandas / core / series.pyc in wrapper(self,other)
91 if self.index.equals(other.index):
92 name = _maybe_match_name(自我,其他)
---> 93返回系列(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个结果[mask] = op(x [mask],y [mask])
66 else:
67 mask = notnull(x)

TypeError:ufunc add不能使用类型为dtype('< M8 [ns]')和dtype('O')的操作数

数据类型:

  df.dtypes 

输出[56]:
A datetime64 [ns ]
B对象

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

  s  -  s.max()

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

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/series.pyc in wrapper(self ,其他)
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'对象没有属性'dtype'

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





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



-Austin

解决方案

如果你看在您链接到的页面标题(您的浏览器窗口的顶部),您可以看到它是熊猫的开发版本:



所以今天,这是版本

 '0.11.0.dev-13ae597' 

此代码正常工作。



稳定版本的文档在这里:





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

 熊猫0.10.1 

这是您的版本。


I'm getting errors when running the code samples from the pandas documentation.

I suspect it might be related to the version of pandas I'm using, but I haven't been able to confirm that.

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

The below examples are taken directly from the pandas documentation here:

pandas - Time Deltas
This works

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

as does this

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

This seems to be consistent with the expected output according to the documentation.

The next line in the sample code yields an error:

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')

Datatypes:

df.dtypes

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

Similarly, I get an error when I do addition/subtraction:

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'

This code is in a gist for easy reference.

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

Thanks for any help or suggestions; it is much appreciated.

-Austin

解决方案

If you look at the title of the page (top of your browser window) you are linking to, you can see that it's the development version of pandas:http://pandas.pydata.org/pandas-docs/dev/timeseries.html#time-deltas

So, today, that's for version

'0.11.0.dev-13ae597'

where this code is working fine.

The docs for the stable version are here:

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

where you will see in at the top of the browser window

pandas 0.10.1

which is your version.

这篇关于 pandas Timedelta错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 02:53