1

我正在阅读一个包含时间(小时和分钟)和 IP 地址的文本文件。然后我想得到时差并每 5 分钟做一些活动。以下代码不计算时间差。

示例文本文件:

06:03 65.55.215.62
06:04 157.56.92.152
06:04 66.249.74.175
06:05 173.199.116.171

代码:

time_ip = []
for line in open('minutes'):
    time_ip.append(line.split(' '))    

df = pandas.DataFrame(time_ip)
df['tvalue'] = df[0]
df['delta'] = (df['tvalue']-df['tvalue'])
4

3 回答 3

1

您应该使用read_csv将 csv 读入 DataFrame:

In [1]: df = pd.read_csv(file_name, sep='\s+', header=None, names=['time', 'ip'])

In [2]: df
Out[2]:
    time               ip
0  06:03     65.55.215.62
1  06:04    157.56.92.152
2  06:04    66.249.74.175
3  06:05  173.199.116.171

Pandas(还)没有任何内置的时间对象,在 python 中这样做并不容易......您可以制作时间对象的时间列:

In [3]: df['time'] = df['time'].apply(lambda x: datetime.time(*map(int, x.split(':'))))

In [4]: df
Out[4]:
       time               ip
0  06:03:00     65.55.215.62
1  06:04:00    157.56.92.152
2  06:04:00    66.249.74.175
3  06:05:00  173.199.116.171

尤其是因为您不能对 datetime.time objects 进行算术运算无论如何,我认为你会因为这里没有年/月/日而陷入困境,一方面,如何处理午夜?

所以让我们重新开始,假设你有一个约会时间......

In [5]: df = pd.read_csv(file_name, sep='\s+', header=None, names=['time', 'ip'])

In [6]: df['time'] = pd.to_datetime(df['time'])  # let's use todays

In [7]: df
Out[7]:
                 time               ip
0 2013-06-12 06:03:00     65.55.215.62
1 2013-06-12 06:04:00    157.56.92.152
2 2013-06-12 06:04:00    66.249.74.175
3 2013-06-12 06:05:00  173.199.116.171

然后您可以使用 a 找出差异shift

In [8]: df['time'].shift()
Out[8]:
0                   NaT
1   2013-06-12 06:03:00
2   2013-06-12 06:04:00
3   2013-06-12 06:04:00
Name: time, dtype: datetime64[ns]

In [9]: d['time'] - df['time'].shift()
Out[9]:
0        NaT
1   00:01:00
2   00:00:00
3   00:01:00
Name: time, dtype: timedelta64[ns]

容易得多。:)

于 2013-06-12T07:49:20.653 回答
0

您可以使用该datetime模块

import datetime
with open('minutes', 'r') as myfile:
    times = myfile.read().split()[::2]
dates = [datetime.datetime.strptime(i, '%H:%M') for i in times]
differences = [j-i for i, j in zip(dates[:-1], dates[1:])]
print [divmod(i.seconds, 60)[0] for i in differences]

印刷:

[1, 0, 1]
于 2013-06-12T06:31:20.160 回答
0
>>> import datetime
>>> end = datetime.datetime.now()
>>> start = datetime.datetime.now()
>>> diff
datetime.timedelta(0, 7, 424199)
>>> diff = start - end
>>> divmod(diff.days * 86400 + diff.seconds, 60)
(0, 7) # 0 minutes, 7 seconds
于 2013-06-12T06:35:21.837 回答