PerformanceWarning:将DateOffsets数组添加/减去Series而不是矢量化的“Series not vectorized”,PerformanceWarning)

时间:2017-06-01 23:04:00

标签: python-3.x pandas datetime vector

我有一张包含以下数据的表格:

    StudentID   LO_0    LO_1
0   student001  2017-02-07T16:37:57 2017-01-21T15:32:46
11  student015  2017-02-07T15:32:09 2017-01-03T21:15:33
56  student072  2017-02-07T19:59:46 2017-02-06T14:03:57
63  student079  2017-02-04T16:52:43 2017-01-23T10:00:27

当我从jupyter“notebook”发出以下命令时,我得到预期的结果

df_test['Delta_t']= abs((df_test.loc[:,item1] - df_test.loc[:,item2]).astype('timedelta64[h]').astype('int'))

    Delta_t
    409
    834
    29
    294

但是当我从python脚本运行同样的命令时,我会得到以下性能警告和同一命令的错误:

anaconda/lib/python3.5/site-packages/pandas/core/ops.py:477: PerformanceWarning: Adding/subtracting array of DateOffsets to Series not vectorized "Series not vectorized", PerformanceWarning)

错误

TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('O')

1 个答案:

答案 0 :(得分:0)

In [172]: df['Delta_t'] = ((df.LO_0 - df.LO_1).dt.total_seconds()/3600).astype(int)

In [173]: df
Out[173]:
     StudentID                LO_0                LO_1  Delta_t
0   student001 2017-02-07 16:37:57 2017-01-21 15:32:46      409
11  student015 2017-02-07 15:32:09 2017-01-03 21:15:33      834
56  student072 2017-02-07 19:59:46 2017-02-06 14:03:57       29
63  student079 2017-02-04 16:52:43 2017-01-23 10:00:27      294