Run tally of events within a given period using datetime

+1 vote

I have a txt file with a number of dates, each signifying an event:

15MAR18 103000      
15MAR18 120518      
17MAR18 121203      
17MAR18 134443      
17MAR18 151733      
19MAR18 165013      
19MAR18 182253      
19MAR18 195533    

I am trying to get a running tally of how many 'events' occur within a 24 hour time period.

I can read the file and parse into datetime objects ok:

for line in range(0, len(event_list):
     eventTime = event_list[line][:14]
     eventTime = datetime.strptime(eventTime, '%d%b%y %H%M%S')

     eventTime_next = event_list[line+1][:14]
     eventTime_next = datetime.strptime(next, '%d%b%y %H%M%S')

I don't know how to next go about it.

I tried to compare the line ahead with the previous and but I don't think that's the way to go about it.

I need it so the following happens

15MAR18 103000      1
15MAR18 120518      2
17MAR18 121203      1
17MAR18 134443      2
17MAR18 151733      3
19MAR18 165013      1
19MAR18 182253      2
19MAR18 195533      3

I.e So the count will go back to 1 when 24 hours has elapsed since the first comparison... and then start again with the new start reference.

I hope this makes sense?

Or is this something for pandas library or something?

Sep 26, 2018 in Python by bug_seeker
• 15,510 points
831 views

1 answer to this question.

0 votes

If you have access to pandas, pd.Series.cumcount is one way. This method uses Date only to extract the running total.

I'm not sure whether you also need to consider Time.

import pandas as pd
from io import StringIO

mystr = StringIO("""15MAR18 103000      
15MAR18 120518      
17MAR18 121203      
17MAR18 134443      
17MAR18 151733      
19MAR18 165013      
19MAR18 182253      
19MAR18 195533""")

# replace mystr with 'file.csv' below
df = pd.read_csv(mystr, header=None, names=['Date', 'Time'], delim_whitespace=True)

df['RunningTotal'] = df.groupby('Date').cumcount() + 1

#       Date    Time  RunningTotal
# 0  15MAR18  103000             1
# 1  15MAR18  120518             2
# 2  17MAR18  121203             1
# 3  17MAR18  134443             2
# 4  17MAR18  151733             3
# 5  19MAR18  165013             1
# 6  19MAR18  182253             2
# 7  19MAR18  195533             3
answered Sep 26, 2018 by Priyaj
• 58,020 points

Related Questions In Python

+2 votes
1 answer

How to check the state of a user given instance id using filters

Seems like the "ec2_con_re.instances" you are using ...READ MORE

answered Aug 1, 2019 in Python by Aishwarya
911 views
+1 vote
1 answer
0 votes
1 answer

how to run test cases of different modules with same marker at a time using pytest

Hey,@Nikitha, Suppose you have multiple files say test_sample1.py, test_sample2.py. To ...READ MORE

answered Apr 28, 2020 in Python by Gitika
• 65,770 points
1,779 views
+2 votes
1 answer

How can I record the X,Y limits of a displayed X,Y plot using the matplotlib show() module?

A couple hours after posting this question ...READ MORE

answered Dec 27, 2018 in Python by anonymous
1,143 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,070 views
0 votes
1 answer

Is there a way of using .lower more effectively in tkinter?

Here is a simple function and some ...READ MORE

answered Sep 25, 2018 in Python by Priyaj
• 58,020 points
2,346 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP