public

  • Download Gist
pandas_datetime_error.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
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-65-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')
 
s - s.max()
# ---------------------------------------------------------------------------
# AttributeError Traceback (most recent call last)
# <ipython-input-66-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'

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.