Reputation: 35776
I'm reading a text file which has time(hours and minutes) and IP addresses. Then I want to get the time differences and do some activity for every 5 minutes. Following code does not calculate the time difference.
sample text file:
06:03 65.55.215.62
06:04 157.56.92.152
06:04 66.249.74.175
06:05 173.199.116.171
code:
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'])
Upvotes: 1
Views: 1743
Reputation: 375915
You should use read_csv
to read a csv into a 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 doesn't (yet) have any built in time object, and doing this in python isn't the easy... you can make the time column of time objects:
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
Not least because you can't do arithmetic on datetime.time objects. At any rate, I think you're going to get into a sticky situation by not having the year/month/day here too, for one thing, how to deal with the midnight?
So let's start again, assuming you had a datetime...
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
Then you can grab out the difference using 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]
Much easier. :)
Upvotes: 1
Reputation: 60024
You can use the datetime
module
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]
Prints:
[1, 0, 1]
Upvotes: 0
Reputation: 16763
>>> 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
Upvotes: 0