<p>要从csv导入并操作日期,<a href="http://pandas.pydata.org/" rel="nofollow noreferrer">^{<cd1>}</a>是一种方法。因为你提供的关于你的数据的唯一信息是开始和结束时间,我将展示它。你知道吗</p>
<p><strong>代码:</strong></p>
<pre><code>import pandas as pd
df = pd.read_csv(data, parse_dates=['start_time', 'end_time'],
infer_datetime_format=True)
print(df)
df['time_delta'] = df.end_time.values - df.start_time.values
print(df.time_delta)
</code></pre>
<p><strong>测试数据:</strong></p>
<pre><code>from io import StringIO
data = StringIO(u'\n'.join([x.strip() for x in """
start_time,end_time,a_number
2013-09-19 03:00:00,2013-09-19 04:00:00,221.0797
2013-09-19 04:00:00,2013-09-19 05:00:00,220.5083
2013-09-24 03:00:00,2013-09-24 05:00:00,221.7733
2013-09-24 04:00:00,2013-09-24 06:00:00,221.2493
""".split('\n')[1:-1]]))
</code></pre>
<p><strong>结果:</strong></p>
<pre><code> start_time end_time a_number
0 2013-09-19 03:00:00 2013-09-19 04:00:00 221.0797
1 2013-09-19 04:00:00 2013-09-19 05:00:00 220.5083
2 2013-09-24 03:00:00 2013-09-24 05:00:00 221.7733
3 2013-09-24 04:00:00 2013-09-24 06:00:00 221.2493
0 01:00:00
1 01:00:00
2 02:00:00
3 02:00:00
Name: time_delta, dtype: timedelta64[ns]
</code></pre>