在DataFrame中查找两列之间的时差

时间:2016-02-29 13:36:18

标签: python python-2.7 pandas

我试图找到下一帧的两列之间的时差:

测试日期|测试类型|首次使用日期

我使用以下函数定义来区分:

age_veh = []
for i in range(0, len(data_manufacturer)-1):
    age_veh[i].append(days_between(data_manufacturer.iloc[i,0], data_manufacturer.iloc[i,4]))

它工作正常,但它不需要一系列作为输入。所以我必须构造一个循环遍历索引的for循环:

if ($('.submit').length > 0) {
    $('.submit').prop('value', 'Other Text');
    $('.submit').click(function(){
    $('.submit').prop('value', 'blah'); 
    });}

然而,它确实返回错误: IndexError:列表索引超出范围

我不知道这是正确的做法,我做错了什么,或者我会非常感谢另类解决方案。还请记住,我有大约2密排的行。

2 个答案:

答案 0 :(得分:2)

使用to_datetime转换列,然后您可以减去列以在abs值上生成timedelta,然后您可以调用dt.days来获取总数天,例如:

In [119]:
import io
import pandas as pd
t="""Test Date,Test Type,First Use Date
2011-02-05,A,2010-01-05
2012-02-05,A,2010-03-05
2013-02-05,A,2010-06-05
2014-02-05,A,2010-08-05"""
df = pd.read_csv(io.StringIO(t))
df
Out[119]:
    Test Date Test Type First Use Date
0  2011-02-05         A     2010-01-05
1  2012-02-05         A     2010-03-05
2  2013-02-05         A     2010-06-05
3  2014-02-05         A     2010-08-05

In [121]:    
df['Test Date'] = pd.to_datetime(df['Test Date'])
df['First Use Date'] = pd.to_datetime(df['First Use Date'])
df.info()

<class 'pandas.core.frame.DataFrame'>
Int64Index: 4 entries, 0 to 3
Data columns (total 3 columns):
Test Date         4 non-null datetime64[ns]
Test Type         4 non-null object
First Use Date    4 non-null datetime64[ns]
dtypes: datetime64[ns](2), object(1)
memory usage: 128.0+ bytes

In [122]:
df['days'] = (df['Test Date'] - df['First Use Date']).abs().dt.days
df

Out[122]:
   Test Date Test Type First Use Date  days
0 2011-02-05         A     2010-01-05   396
1 2012-02-05         A     2010-03-05   702
2 2013-02-05         A     2010-06-05   976
3 2014-02-05         A     2010-08-05  1280

答案 1 :(得分:0)

IIUC您可以先转换to_datetime列,使用abs,然后将timedelta转换为days

print df
  id  value      date1      date2  sum
0  A    150 2014-04-08 2014-03-08  NaN
1  B    100 2014-05-08 2014-02-08  NaN
2  B    200 2014-01-08 2014-07-08  100
3  A    200 2014-04-08 2014-03-08  NaN
4  A    300 2014-06-08 2014-04-08  350

df['date1'] =  pd.to_datetime(df['date1'])
df['date2'] =  pd.to_datetime(df['date2'])

df['diff'] = (df['date1'] - df['date2']).abs()  / np.timedelta64(1, 'D')
print df
  id  value      date1      date2  sum  diff
0  A    150 2014-04-08 2014-03-08  NaN    31
1  B    100 2014-05-08 2014-02-08  NaN    89
2  B    200 2014-01-08 2014-07-08  100   181
3  A    200 2014-04-08 2014-03-08  NaN    31
4  A    300 2014-06-08 2014-04-08  350    61

修改

我认为更好用于在较大的np.timedelta64(1, 'D')中将days转换为DataFrames,因为它更快:

我使用EdChum sample,仅使用len(df) = 4k

import io
import pandas as pd
import numpy as np

t=u"""Test Date,Test Type,First Use Date
2011-02-05,A,2010-01-05
2012-02-05,A,2010-03-05
2013-02-05,A,2010-06-05
2014-02-05,A,2010-08-05"""

df = pd.read_csv(io.StringIO(t))

df =  pd.concat([df]*1000).reset_index(drop=True)

df['Test Date'] = pd.to_datetime(df['Test Date'])
df['First Use Date'] = pd.to_datetime(df['First Use Date'])

print (df['Test Date'] - df['First Use Date']).abs().dt.days

print (df['Test Date'] - df['First Use Date']).abs()  / np.timedelta64(1, 'D')

<强>计时

In [174]: %timeit (df['Test Date'] - df['First Use Date']).abs().dt.days
10 loops, best of 3: 38.8 ms per loop

In [175]: %timeit (df['Test Date'] - df['First Use Date']).abs()  / np.timedelta64(1, 'D')
1000 loops, best of 3: 1.62 ms per loop