我的代码正在使用pandas V.12的计算机上工作,而不是使用V.13的计算机。 是v.13 jenkins-pandas-windows-test-py27-1193? 使用此代码
import pandas as pd
from datetime import datetime
d = { 'File' : pd.Series([2., 2.]),
'Status' : pd.Series([1., 1.]),
'Error' : pd.Series([2., 2.]),
'AlertDays' : pd.Series([2., 2.]),
'Date' : pd.Series([datetime(2012, 5, 2), datetime(2012, 5, 2)])}
df=pd.DataFrame(d)
df['Date']=pd.to_datetime(df['Date'])
xx=df.groupby('File')['Date'].max()
xx=pd.DataFrame(xx).reset_index()
df=pd.merge(xx,df,on=['File','Date'],how='left')
df['diff'] = df.apply(lambda x: (datetime.now() - x['Date']).days, axis=1)
我得到了这个输出
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 3431, in apply
return self._apply_standard(f, axis, reduce=reduce)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 3501, in _appl
y_standard
results[i] = func(v)
File "<stdin>", line 1, in <lambda>
TypeError: ("unsupported operand type(s) for -: 'datetime.datetime' and 'float'"
, u'occurred at index 0')
答案 0 :(得分:2)
一个错误......这样做(而不是最大)
In [23]: df.groupby('File')['Date'].apply(lambda x: x.max())
Out[23]:
File
2 2012-05-02 00:00:00
dtype: datetime64[ns]
答案 1 :(得分:0)
我猜这是一个错误:
>>> xx
File Date
0 2 1335916800000000000
解决方法是在索引重置后添加
>>> xx['Date']=pd.to_datetime(xx['Date'])