4

My code is working on a computer using pandas V.12 but not a computer using V.13. Is it v.13 jenkins-pandas-windows-test-py27-1193 ? Using this code

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)

i get this output

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

2 Answers 2

2

A bug...do this (instead of max)

In [23]: df.groupby('File')['Date'].apply(lambda x: x.max())
Out[23]: 
File
2      2012-05-02 00:00:00
dtype: datetime64[ns]
2
  • Thanks, now i know where to look for bug reports! Jan 7, 2014 at 20:25
  • 1
    pls report.....try to test everything....this slipped thru; you can try to fix in current master
    – Jeff
    Jan 7, 2014 at 20:28
0

I guess it's a bug:

>>> xx
   File                 Date
0     2  1335916800000000000

A workaround is to add just after index reset

>>> xx['Date']=pd.to_datetime(xx['Date'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.