循环许多日期并比较持续时间 - 熊猫

时间:2017-03-13 15:09:04

标签: python pandas

我正在尝试遍历一堆不同的日期,并将其持续时间写出到预定义列。到目前为止我的代码看起来像这样(日期比显示的还多):

First = ['ADate','BDate','CDate']
Second = ['BDate','CDate','DDate']
Result = ['AtoB','BtoC','CtoD']
for x in First:
  for y in Second:
    for z in Result:
      Data[z] = (Data.y - Data.x).astype('timedelta64[m]')

这给了我错误“DataFrame对象没有属性'y。'”我做错了什么,我怎样才能使这个工作?谢谢!

2 个答案:

答案 0 :(得分:2)

通过提供上下文,我可以假设您需要计算:

Data[z] = (y - x).astype('timedelta64[m]')

虽然,如果你能使问题更清楚一些,我们可以帮助你进一步......

答案 1 :(得分:2)

您似乎需要添加[],但数据会被覆盖:

First = ['ADate','BDate','CDate']
Second = ['BDate','CDate','DDate']
Result = ['AtoB','BtoC','CtoD']
for x in First:
  for y in Second:
    for z in Result:
      Data[z] = (Data[y] - Data[x]).astype('timedelta64[m]')

但如果需要列的所有组合:

for x in First:
  for y in Second:
    for z in Result:
      #new column from x,y,z values
      Data[z + x + y] = (Data[y] - Data[x]).astype('timedelta64[m]')

如果需要从First的{​​{1}}列中删除Second中的列,我认为需要重命名列以对齐数据:

d = dict(zip(First, Second))
Data[Result] = (Data[Second] - Data[First].rename(columns=d)).astype('timedelta64[m]')

样品:

rng = pd.date_range('2015-01-01', periods=5, freq='Min')
np.random.seed(201)
rng1 = np.random.choice(rng, size=5)
np.random.seed(110)
rng2 = np.random.choice(rng, size=5)
np.random.seed(112)
rng3 = np.random.choice(rng, size=5)
np.random.seed(114)
rng4 = np.random.choice(rng, size=5)

Data = pd.DataFrame({'ADate': rng1,
                   'BDate':rng2,
                   'CDate':rng3,
                   'DDate':rng4})

print (Data)
                ADate               BDate               CDate  \
0 2015-01-01 00:00:00 2015-01-01 00:00:00 2015-01-01 00:04:00   
1 2015-01-01 00:01:00 2015-01-01 00:03:00 2015-01-01 00:03:00   
2 2015-01-01 00:00:00 2015-01-01 00:01:00 2015-01-01 00:00:00   
3 2015-01-01 00:01:00 2015-01-01 00:00:00 2015-01-01 00:02:00   
4 2015-01-01 00:04:00 2015-01-01 00:00:00 2015-01-01 00:01:00   

                DDate  
0 2015-01-01 00:02:00  
1 2015-01-01 00:03:00  
2 2015-01-01 00:02:00  
3 2015-01-01 00:03:00  
4 2015-01-01 00:04:00
First = ['ADate','BDate','CDate']
Second = ['BDate','CDate','DDate']
Result = ['AtoB','BtoC','CtoD']

d = dict(zip(First, Second,))
Data[Result] = (Data[Second] - Data[First].rename(columns=d)).astype('timedelta64[m]')
print (Data)

                ADate               BDate               CDate  \
0 2015-01-01 00:00:00 2015-01-01 00:00:00 2015-01-01 00:04:00   
1 2015-01-01 00:01:00 2015-01-01 00:03:00 2015-01-01 00:03:00   
2 2015-01-01 00:00:00 2015-01-01 00:01:00 2015-01-01 00:00:00   
3 2015-01-01 00:01:00 2015-01-01 00:00:00 2015-01-01 00:02:00   
4 2015-01-01 00:04:00 2015-01-01 00:00:00 2015-01-01 00:01:00   

                DDate  AtoB  BtoC  CtoD  
0 2015-01-01 00:02:00   0.0   4.0  -2.0  
1 2015-01-01 00:03:00   2.0   0.0   0.0  
2 2015-01-01 00:02:00   1.0  -1.0   2.0  
3 2015-01-01 00:03:00  -1.0   2.0   1.0  
4 2015-01-01 00:04:00  -4.0   1.0   3.0  
for x in First:
  for y in Second:
    for z in Result:
      Data[z] = (Data[y] - Data[x]).astype('timedelta64[m]')

print (Data)

                ADate               BDate               CDate  \
0 2015-01-01 00:00:00 2015-01-01 00:00:00 2015-01-01 00:04:00   
1 2015-01-01 00:01:00 2015-01-01 00:03:00 2015-01-01 00:03:00   
2 2015-01-01 00:00:00 2015-01-01 00:01:00 2015-01-01 00:00:00   
3 2015-01-01 00:01:00 2015-01-01 00:00:00 2015-01-01 00:02:00   
4 2015-01-01 00:04:00 2015-01-01 00:00:00 2015-01-01 00:01:00   

                DDate  AtoB  BtoC  CtoD  
0 2015-01-01 00:02:00  -2.0  -2.0  -2.0  
1 2015-01-01 00:03:00   0.0   0.0   0.0  
2 2015-01-01 00:02:00   2.0   2.0   2.0  
3 2015-01-01 00:03:00   1.0   1.0   1.0  
4 2015-01-01 00:04:00   3.0   3.0   3.0  
for x in First:
  for y in Second:
    for z in Result:
      Data[x + '_' + y + '_' + z] = (Data[y] - Data[x]).astype('timedelta64[m]')

print (Data)

                ADate               BDate               CDate  \
0 2015-01-01 00:00:00 2015-01-01 00:00:00 2015-01-01 00:04:00   
1 2015-01-01 00:01:00 2015-01-01 00:03:00 2015-01-01 00:03:00   
2 2015-01-01 00:00:00 2015-01-01 00:01:00 2015-01-01 00:00:00   
3 2015-01-01 00:01:00 2015-01-01 00:00:00 2015-01-01 00:02:00   
4 2015-01-01 00:04:00 2015-01-01 00:00:00 2015-01-01 00:01:00   

                DDate  ADate_BDate_AtoB  ADate_BDate_BtoC  ADate_BDate_CtoD  \
0 2015-01-01 00:02:00               0.0               0.0               0.0   
1 2015-01-01 00:03:00               2.0               2.0               2.0   
2 2015-01-01 00:02:00               1.0               1.0               1.0   
3 2015-01-01 00:03:00              -1.0              -1.0              -1.0   
4 2015-01-01 00:04:00              -4.0              -4.0              -4.0   

   ADate_CDate_AtoB  ADate_CDate_BtoC  ADate_CDate_CtoD        ...         \
0               4.0               4.0               4.0        ...          
1               2.0               2.0               2.0        ...          
2               0.0               0.0               0.0        ...          
3               1.0               1.0               1.0        ...          
4              -3.0              -3.0              -3.0        ...          

   BDate_DDate_CtoD  CDate_BDate_AtoB  CDate_BDate_BtoC  CDate_BDate_CtoD  \
0               2.0              -4.0              -4.0              -4.0   
1               0.0               0.0               0.0               0.0   
2               1.0               1.0               1.0               1.0   
3               3.0              -2.0              -2.0              -2.0   
4               4.0              -1.0              -1.0              -1.0   

   CDate_CDate_AtoB  CDate_CDate_BtoC  CDate_CDate_CtoD  CDate_DDate_AtoB  \
0               0.0               0.0               0.0              -2.0   
1               0.0               0.0               0.0               0.0   
2               0.0               0.0               0.0               2.0   
3               0.0               0.0               0.0               1.0   
4               0.0               0.0               0.0               3.0   

   CDate_DDate_BtoC  CDate_DDate_CtoD  
0              -2.0              -2.0  
1               0.0               0.0  
2               2.0               2.0  
3               1.0               1.0  
4               3.0               3.0  

[5 rows x 31 columns]