How to count the number of days between events in a flat file (Python, R, or VBA)?

1860
15
Jump to solution
01-25-2018 06:09 AM
by Anonymous User
Not applicable

Hello all!

So I am working on a project and I've hit a roadblock.

What I want to do is find the number of days that a parcel (identified by 'PREM_CODE') had water service during a two year period.

However, the structure of the data is giving me some issues. Each row in this table represents a water meter reading and whether the water was turned on or off. If the water was turned on, the value in the field 'ACTN_CODE' would be 'IN,' if the meter was being turned off, the value would be 'OUT.'

There are a few assumptions that I need to put into a logic to deal with these that are giving me trouble. Those assumptions are:

If a premises's (PREM_CODE) first chronological event is a turn-on event (i.e. 'ACTN_CODE'='IN'), then use that date should be used as the starting date (Below Example 89 and 99).

If a premises's (PREM_CODE) first chronological event is a turn-off event (i.e. 'ACTN_CODE'='OUT'), then subtract the number of days between the period's first date (7/1/2015) to the date of the event (ACTION_DATE) (Below Example 50,76, and 152).

Then, after that first logic is implemented, I need to count the days between any subsequent 'IN' and 'OUT' events.

If a PREM_CODE's last entry is an 'IN' event, then I need to subtract that 'IN' event's date from the end date, which is 8/1/2017.

And lastly, if a PREM_CODE's last entry is an 'OUT,' then no further calculations should be made.

Here is a sample of what this could look like:

**Start date = 7/1/2015
**End date = 8/1/2017

PREM_CODEACTN_CODEACTION_DATE
50OUT7/30/2016
50IN9/21/2016
89IN10/10/2015
76OUT12/1/2015
76IN1/1/2016
76OUT9/1/2016
76IN5/5/2017
99IN7/30/2015
152OUT7/1/2017

Each of these has its own unique situation.

For PREM_CODE=50, the calulcation would be: (7/1/2015-7/30/2016)+(9/21/2016-8/1/2017)
For PREM_CODE=89, the calculation would be: (10/10/2015-8/1/2017)
For PREM_CODE=76, the calculation would be:(7/1/2015-12/1/2015)+(1/1/2016-9/1/2016)+(5/5/2017-8/1/2017)
For PREM_CODE=99, the calculation would be: (7/30/2015-8/1/2017)
For PREM_CODE=152, the calculation would be: (7/1/2015-7/1/2017)

Any and all help would be appreciated!

0 Kudos
15 Replies
JoshuaBixby
MVP Esteemed Contributor
sum(service_days.values())
0 Kudos
by Anonymous User
Not applicable

Hmmm, that doesn't seem to do it. 

I've attached a video of what I'm seeing...

I feel like i'm missing something.

I've added a few print statements as smoketests as well.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You altered some of my code, make sure the following:

fields = ["PREM_CODE", "ACTION_DATE", "ACTN_CODE"]
by Anonymous User
Not applicable

Made that change and now I'm getting an error of 'can't compare datetime.datetime to NoneType'

Perhaps the issue is coming from the datetime in .gdbs is always stored as MM:DD:YYYY hh:mm:ss, regardless of how the data is actually displayed in the table. 

Do you think the mistmatch between the 'start_period' and 'end_period' and the gdb's date storage would cause an issue here? Or do you think it would be something else?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Are you getting that error with the first record or part-way through the records?  (You can check by seeing if service_days is empty or not).

If a record has a NULL, the cursor returns a NoneType.  Are there any gaps in values in the records or is every row of every column populated?

by Anonymous User
Not applicable

And there it is.

A single null date field

I owe you a beer at either the User Conference or Dev summit! 

0 Kudos