Chet Meinzer
Chet Meinzer

Reputation: 1741

python pandas v.12 v.13 same code datetime

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

Upvotes: 4

Views: 232

Answers (2)

Jeff
Jeff

Reputation: 129018

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]

Upvotes: 2

alko
alko

Reputation: 48347

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

Upvotes: 0

Related Questions