combining 2 dataframes and make it create new lines mathemathically - python

So I created 2 different DataFrame Table and integrate it to tkinter GUI.
First Table looks like this;
Entry
Start
Finish
Total Time (Hour)
Status
Reason for Stoppage
1
23.05.2020 07:30
23.05.2020 08:30
01:00
MANUFACTURE
2
23.05.2020 08:30
23.05.2020 12:00
03:30
MANUFACTURE
3
23.05.2020 12:00
23.05.2020 13:00
01:00
STOPPAGE
MALFUNCTION
4
23.05.2020 13:00
23.05.2020 13:45
00:45
MANUFACTURE
5
23.05.2020 13:45
23.05.2020 17:30
03:45
MANUFACTURE
And second Table looks like this;
Start
Finish
Reason for Stoppage
10:00
10:15
Coffee Break
12:00
12:30
Lunch Break
15:00
15:15
Coffee Break
The main task is,combining these Tables and creating another Table.While doing that we should arrange the lines according to hours.At that time,the program has to create new lines 'itself' and show every starting/finishing hour in the Table.But I just can't do it by combining or merging them.
The third graph has to look like this;
Entry
Start
Finish
Total Time (Hour)
Status
Reason for Stoppage
1
23.05.2020 07:30
23.05.2020 08:30
01:00
MANUFACTURE
2
23.05.2020 08:30
23.05.2020 10:00
01:30
MANUFACTURE
3
23.05.2020 10:00
23.05.2020 10:15
00:15
STOPPAGE
Coffee Break
4
23.05.2020 10:15
23.05.2020 12:00
01:45
MANUFACTURE
5
23.05.2020 12:00
23.05.2020 12:30
00:30
STOPPAGE
Lunch Break
6
23.05.2020 12:30
23.05.2020 13:00
00:30
MANUFACTURE
7
23.05.2020 13:00
23.05.2020 13:45
00:45
STOPPAGE
MALFUNCTION
8
23.05.2020 13:45
23.05.2020 15:00
01:15
MANUFACTURE
9
23.05.2020 15:00
23.05.2020 15:15
00:15
STOPPAGE
Coffee Break
10
23.05.2020 15:15
23.05.2020 17:30
02:15
MANUFACTURE
I hope I explained the problem clearly.Thanks in advance.
from tkinter import *
import tkinter as tk
from tkinter import ttk
from pandastable import Table
import pandas as pd
import numpy as np
# import style
root = tk.Tk()
root.title("Çalışma Ve Mola Saatleri")
root.geometry("1800x1600")
work={"Entry":["1","2","3","4","5"],
"Start":["23.05.2020" " 07:30","23.05.2020 08:30",
"23.05.2020 12:00","23.05.2020" " 13:00","23.05.2020 13:45"],
"Finish":["23.05.2020 08:30","23.05.2020 12:00",
"23.05.2020 13:00","23.05.2020 13:45","23.05.2020 17:30"],
"Total Time (Hour)":["01:00","03:30","01:00","00:45","03:45"],
"Status":["MANUFACTURE","MANUFACTURE","STOPPAGE","MANUFACTURE","MANUFACTURE"],
"Reason For Stoppage":[" "," ","MALFUNCTION"," "," "]}
graph1=pd.DataFrame(work)
frame=tk.Frame(root)
frame.place(width=200)
frame.pack(anchor=W,padx=100,pady=50,ipadx=120,ipady=30)
pt=Table(frame,dataframe=graph1)
pt.show()
Break={"Start":["10:00","12:00","15:00"],
"Finish":["10:15","12:30","15:15"],
"Reason For Stoppage":["Coffee Break","Lunch Break","Coffee Break"]}
graph2=pd.DataFrame(Break)
frame2=tk.Frame(root)
frame2.place(width=100,height=50)
frame2.pack(anchor=NE,padx=150,ipadx=20,ipady=10)
pt2=Table(frame2,dataframe=graph2)
pt2.show()
graph3=pd.concat([graph1,graph2])
frame3=tk.Frame(root)
frame3.place()
frame3.pack(anchor=SW,padx=100,ipadx=120,ipady=500)
pt3=Table(frame3,dataframe=graph3)
pt3.show()
root.mainloop()

Related

all possible time delta combinations in python

I have a time period from 9:00 till 22:00 and I need to list all possible durations with a step of 30 minutes within this period. E.g.
9:00 - 9:30
9:00 - 10:00
9:00 - 10:30
...
21:00 - 22:00
21:30 - 22:00
I've googled and found itertools.combinations() for numbers but nothing comparable for dates

Grouping the data in python

Suppose, I have the data like this,
Date Time Energy_produced
01.01.2016 00:00 500
01.01.2016 00:15 580
01.01.2016 00:30 600
01.01.2016 00:45 620
01.01.2016 01:00 580
01.01.2016 01:15 520
01.01.2016 01:30 590
01.01.2016 01:45 570
01.01.2016 02:00 540
Now, i want to sum the energy produced based on each hour
suppose ,
Date Hour Energy produced per hour
01.01.2016 00:00 2280(per hour)
01:01:2016 01:00 2240(per hour)
How to sum like this?
If you want to keep Date/Time as strings, you could use:
(df.groupby(['Date', df['Time'].str[:3].rename('Hour')+'00'])
['Energy_produced'].sum()
.reset_index()
)
Output:
Date Hour Energy_produced
0 01.01.2016 00:00 2300
1 01.01.2016 01:00 2260
2 01.01.2016 02:00 540
NB. You can also get the second group with: df['Time'].str.replace(r'\d{2}$', '00', regex=True).rename('Hour')

Formatting pandas dataframe printing

I have the following pandas dataframe that was converted to string with to_string().
It was printed like this:
S T Q U X A D
02:36 06:00 06:00 06:00 06:30 09:46 07:56
02:37 06:10 06:15 06:15 06:40 09:48 08:00
12:00 11:00 12:00 12:00 07:43 12:00 18:03
13:15 13:00 13:15 13:15 07:50 13:15 18:08
14:00 14:00 14:00 14:00 14:00 19:00
15:15 15:00 14:15 15:15 15:15 19:05
16:15 16:00 15:15 16:15 16:15 20:15
17:15 17:00 17:15 17:15 17:15 20:17
18:15 21:22 21:19 19:55 18:15 20:18
19:15 21:24 21:21 19:58 19:15 20:19
The gaps are due to empty values in the dataframe. I would like to keep the column alignment, perhaps by replacing the empty values with tabs. I would also like to center align the header line.
This wasn't printed in a terminal, but was sent over telegram with the requests post command. I think though, it is just a print formatting problem, independent of the telegram requests library.
The desired output would be like this:
S T Q U X A D
02:36 06:00 06:00 06:00 06:30 09:46 07:56
02:37 06:10 06:15 06:15 06:40 09:48 08:00
12:00 11:00 12:00 12:00 07:43 12:00 18:03
13:15 13:00 13:15 13:15 07:50 13:15 18:08
14:00 14:00 14:00 14:00 14:00 19:00
15:15 15:00 14:15 15:15 15:15 19:05
16:15 16:00 15:15 16:15 16:15 20:15
17:15 17:00 17:15 17:15 17:15 20:17
18:15 21:22 21:19 19:55 18:15 20:18
19:15 21:24 21:21 19:58 19:15 20:19
you can use dataframe style.set_properties to set some of these options like:
df.style.set_properties(**{'text-align': 'center'})
read more here:
https://pandas.pydata.org/docs/reference/api/pandas.io.formats.style.Styler.set_properties.html

Python DateTime In and DateTime Out - Calculations regarding a time Window

I have a dataframe of two DateTime Object columns (one representing a surgery clocking in and the other when it is clocked out). For each row (ie case), I need to create a column of total time within business hours (07:00 - 17:30) and another column of total time outside of business hours. I am not sure the best approach.
Reproducible segment of my dataframe:
Actual Room In DateTime Actual Room Out DateTime
0 2013-11-01 02:16 2013-11-01 04:35
1 2016-06-10 16:42 2016-06-10 19:28
2 2014-12-13 09:15 2014-12-13 10:55
3 2014-01-03 19:46 2014-01-03 22:54
4 2015-01-12 18:13 2015-01-12 19:58
5 2017-03-24 18:55 2017-03-24 19:57
6 2015-08-07 18:46 2015-08-07 19:42
7 2016-03-18 20:43 2016-03-19 00:40
8 2017-02-23 15:21 2017-02-23 17:35
9 2013-11-29 17:08 2013-11-29 17:42
10 2014-05-28 18:17 2014-05-28 19:12
11 2017-07-15 17:04 2017-07-15 18:19
12 2017-02-16 09:14 2017-02-16 21:29
13 2014-07-11 12:04 2014-07-11 17:40
14 2017-07-05 12:27 2017-07-05 20:08
15 2014-08-18 17:55 2014-08-18 19:50
16 2015-01-23 15:41 2015-01-23 19:41
17 2015-01-12 16:59 2015-01-12 17:49
18 2014-02-23 11:24 2014-02-23 15:06
19 2017-09-21 13:40 2017-09-21 18:11
pd.read_clipboard(sep=',')
The maximum amount of time between the two columns is:
df['Room Difference'] = df['Actual Room Out DateTime'] - df['Actual Room In DateTime']
max(df['Room Difference'])
Timedelta('1 days 01:17:00')
Which helps me think about the problem and the algorithm I want to write.
I guess it would go something like this (as pseudocode):
if 00:00:00 <= 'Actual Room In DateTime' < 07:00:00 and 00:00:00 <= 'Actual Room Out DateTime' < 07:00:00:
'After-hours' = 'Actual Room Out DateTime' - 'Actual Room In DateTime'
... to cover all the possible cases.
Is there an easier way or some sort of framework/tool for this exact kind of problem?
Subtract In time from Business Starting time , to get outside hours before day start
Subtract Out time from Business End time, to get outside hours after day end
Subtract In time from Out time, to get total hours of surgery
Add the two outside hours, to get total outside hours
Subtract total outside hours from total hours, to get total within business hours
Make a separate column for every calculation

Subtracting between rows of different columns in an indexed dataframe in python

I have an indexed dataframe (indexed by type then date) and would like to carry out a subtraction between the end time of the top row and start time of the next row in hours :
type date start_time end_time code
A 01/01/2018 01/01/2018 9:00 01/01/2018 14:00 525
01/02/2018 01/02/2018 5:00 01/02/2018 17:00 524
01/04/2018 01/04/2018 8:00 01/04/2018 10:00 528
B 01/01/2018 01/01/2018 5:00 01/01/2018 14:00 525
01/04/2018 01/04/2018 2:00 01/04/2018 17:00 524
01/05/2018 01/05/2018 7:00 01/05/2018 10:00 528
I would like to get the resulting table with a new column['interval']:
type date interval
A 01/01/2018 -
01/02/2018 15
01/04/2018 39
B 01/01/2018 -
01/04/2018 60
01/05/2018 14
The interval column is in hours
You can convert start_time and end_time to datetime format, then use apply to subtract the end_time of the previous row in each group (using groupby). To convert to hours, divide by pd.Timedelta('1 hour'):
df['start_time'] = pd.to_datetime(df['start_time'])
df['end_time'] = pd.to_datetime(df['end_time'])
df['interval'] = (df.groupby(level=0,sort=False).apply(lambda x: x.start_time-x.end_time.shift(1)) / pd.Timedelta('1 hour')).values
>>> df
start_time end_time code interval
type date
A 01/01/2018 2018-01-01 09:00:00 2018-01-01 14:00:00 525 NaN
01/02/2018 2018-01-02 05:00:00 2018-01-02 17:00:00 524 15.0
01/04/2018 2018-01-04 08:00:00 2018-01-04 10:00:00 528 39.0
B 01/01/2018 2018-01-01 05:00:00 2018-01-01 14:00:00 525 NaN
01/04/2018 2018-01-04 02:00:00 2018-01-04 17:00:00 524 60.0
01/05/2018 2018-01-05 07:00:00 2018-01-05 10:00:00 528 14.0

Categories