Creating conditions to replace missing values in pandas that include Strings, Date and Time - pandas

In my df I am trying to replace values in column 'Amount in (L)' based on the date and time and the Milking events that occurred that day.
Milk Event Date Time Amount in (L)
Start 04/08/2001 09:00 nan
End 04/08/2001 10:00 1500
Start 04/08/2001 14:00 nan
End 04/08/2001 15:00 2000
Start 05/08/2001 08:00 nan
I am unsure whether to use a for loop or if statement to create conditions that replace the nan with the previous 'End' events amount. I would want it to look something like:
Milk Event Date Time Amount in (L)
Start 04/08/2001 09:00 0
End 04/08/2001 10:00 1500
Start 04/08/2001 14:00 1500
End 04/08/2001 15:00 2000
Start 05/08/2001 08:00 2000

If possible you can use:
df['Amount in (L)'] = pd.to_numeric(df['Amount in (L)'], errors='coerce').ffill().fillna(0)

Related

Ability to count per hour per day?

I'm new to SSAS so be gentle!
I have (simplified):
fact table that has an ID, start date, start datetime, end date, end datetime
A date dimension that has a granularity from Year to Calendar Date.
What I'd like to be able to do is get the count of ID per hour per date member/current member. However I'm not exactly sure how to get there.
Fact Table Example
ID
Start Date
End Date
Start DateTime
End DateTime
1
2022-01-01
2022-01-04
2022-01-01 23:00
2022-01-04 05:33
53
2022-01-01
2022-01-07
2022-01-01 04:00
2022-01-07 12:05
Wanted results:
Date
Hour
Count
2022-01-02
00:00
1
2022-01-02
01:00
1
2022-01-02
02:00
1
2022-01-02
03:00
1
2022-01-02
04:00
2
2022-01-02
05:00
2
I expect I need an hour dimension that somehow links to the date dimension and then some sort of measure that does a between comparison but not exactly sure how to go about this.
Any help is appreciated!
Edit: above tables may not be showing right for some reason. Looks great when I go to edit them...

Split a time duration into time segments

I have an incident start time and end time e.g Sr=tart time of 15/01/2018 11:30 and end time of 16/01/2018 02:40 in an excel table.
How can I split this time range into different time segments. The segments are:-
06:00 - 11:59, 12:00 - 14:59, 15:00 - 17:59, 18:00 - 22:59, 23:00 - 05:59
For 06:00 - 11:59 I would expect 0.50 as this is 30 mins.
For 12:00 - 14:59 I would expect 3.0 for 3 hours.
For 15:00 - 17:59 I would expect 3.0 for 3 hours again.
For 18:00 - 22:59 this should be 5hrs
and for 23:00 - 02:40 should be 3.67 hrs.
What formula would I need to achieve this?
For the example you give with the layout as shown below this formula should work:
=MIN($D3-F$1,MIN(F$2-F$1,IF($C3<F$2,F$2-$C3+SUM($E3:E3),)))
The formula in C3 is:
=24*(MOD(A3,1))
and in D3:
=24*(MOD(B3,1)+INT(B3>INT(A3)))

Find duration on overlapping time segments in SQL

I am working on building a query report where i have multiple types of segment with priority ranking on one table and second table with all types of segment with date, time etc. (as shown below) The duration for lower ranked segment during overlap should not be considered.
Please help me as I am unable to figure out query and get the duration of segment excluding the overlap based on ranking
table_Rank
Rank Code
1 x
2 y
3 z
4 a
5 b
6 c
7 d
8 r
9 f
Table_Segments
Code Date Start Time End Time Duration
a 18-Jul-15 17:30 17:45 0:15
c 18-Jul-15 18:00 19:00 1:00
y 18-Jul-15 18:45 19:00 0:15
a 18-Jul-15 20:15 20:20 0:05
b 18-Jul-15 23:45 1:00 1:15
z 19-Jul-15 0:30 1:15 0:45
f 19-Jul-15 2:00 3:00 1:00
Table With Ranks = Table_Rank
Table With Data = Table_Segments
What i am trying to achieves is, in an overlap situation overlap span should be considered for code with higher rank.
e.g.
Code Date Start Time End Time Duration
b 18-Jul-15 23:45 1:00 1:15
z 19-Jul-15 0:30 1:15 0:45
The actual duration output for b should 45 minutes as it has a lower rank compared to z and z should be 45 minutes

How to count the records per half hour from a period (datetimefrom and datetimeto) field?

I have a table which looks like you can see below:
Id Date ScheduledTimeFrom ScheduledTimeTo ActualTimeFrom ActualTimeTo
1 2013-01-01 1899-12-30 07:00:00 1899-12-30 18:00:00 1899-12-30 07:23:00 1899-12-30 17:15:00
I need to calculate per half hour how many records exists, the output should be like:
Time Actual Count:
7:00 4
7:30 4
8:00 4
8:30 4
9:00 4
9:30 5
10:00 5
10:30 6
11:00 7
11:30 8
12:00 8
12:30 8
13:00 8
13:30 8
14:00 8
14:30 8
15:00 7
15:30 7
16:00 7
16:30 6
17:00 5
17:30 4
18:00 4
I already tried to make a helper table which should hold the times per halfhour. I have joined this helpertable with the table that contains the data and after that I tried to use a group by function but it was not working.
My query was like:
Create table period (timefrom datetime, timeto datetime)
insert into period
select '1899-12-30 07:00:00.000', '1899-12-30 07:30:00.000'
Union all
select '1899-12-30 07:30:00.000', '1899-12-30 08:00:00.000'
select *
from period p left join table1 t on t.ActualTimeFrom < p.timeto and t.ActualTimeTo >=p.timefrom
Grouping this give me no desired result....
Anyone an idea how to come to the result?
P.s. I am using sql server 2005.
After snooping around and testing it on my side, looks like this date function could be the answer:
DATEADD(mi,DATEDIFF(mi,0,YOUR_DATE_COLUMN)/30*30,0)

SQL How Many Employees Are Working, Group By Hour

I have a table, a timetable, with check-in and check-out times of the employees:
ID Date Check-in Check out
1 1-1-2011 11:00 18:00
2 1-1-2011 11:00 19:00
3 1-1-2011 16:00 18:30
4 1-1-2011 17:00 20:00
Now I want to know how many employees are working, every (half) hour.
The result I want to see:
Hour Count
11 2
12 2
13 2
14 2
15 2
16 3
17 3
18 2,5
19 1
Every 'Hour' you must read as 'till the next full hour', ex. 11 -> 11:00 - 12:00
Any ideas?
Build an additional table, called Hours, containing the following data:
h
00:00
00:30
01:00
...
23:30
then, run
Select h as 'hour' ,count(ID) as 'count' from timetable,hours where [Check_in]<=h and h<=[Check_out] group by h