2013-03-01 116 views
0

從大熊貓的文件運行代碼樣本時遇到錯誤。大熊貓Timedelta錯誤

我懷疑這可能與我使用熊貓的版本,但我一直無法證實。

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 

這似乎是一致的:

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

下面的例子是直接從大熊貓的文檔在這裏拍攝與期望根據文件編輯輸出。

樣品下一行代碼會產生錯誤:

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

感謝任何幫助或建議;非常感謝。

-Austin

回答

1

如果你看一下網頁的標題要鏈接到(你的瀏覽器窗口的頂部),你可以看到它是大熊貓的開發版: 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 

這是您的版本的頂部。

+0

衛生署!感謝您的快速回復! – hernamesbarbara 2013-03-01 02:42:03