unstacking shift data (start and end time) into hourly data

2024/10/13 11:20:01

I have a df as follows which shows when a person started a shift, ended a shift, the amount of hours and the date worked.

Business_Date   Number PayTimeStart PayTimeEnd          Hours
0   2019-05-24  1       2019-05-24 11:00:00 2019-05-24 12:15:00 1.250
1   2019-05-24  2       2019-05-24 12:30:00 2019-05-24 13:30:00 1.00

Now what I'm trying to do is break this into an hourly format, so I know how many hours were used between 11:00 - 12:00

so, in my head, for the above, I want to put the 1 hour between 11 - 12 into the bin for 11:00 and the remainder 0.25 into the next bin of 12

so I would end up with something like

    Business Date   Time Hour
0   2019-05-24  11:00 1
1   2019-05-24  12:00 0.75
2   2019-05-24  13:00 0.5
Answer

One idea is working with minutes - first use list comprehension with flattening for Series and then grouping by hours with hours for count by GroupBy.size and last divide by 60 for final hours:

s = pd.Series([z for x, y in zip(df['Pay Time Start'], df['Pay Time End'] - pd.Timedelta(60, unit='s')) for z in pd.date_range(x, y, freq='Min')])df = (s.groupby([s.dt.date.rename('Business Date'), s.dt.hour.rename('Time')]).size().div(60).reset_index(name='Hour'))
print (df)Business Date  Time  Hour
0    2019-05-24    11  1.00
1    2019-05-24    12  0.75
2    2019-05-24    13  0.50

If you need to group by a location or ID

 df1 = pd.DataFrame([(z, w) for x, y, w in zip(df['Pay Time Start'], df['Pay Time End'] - pd.Timedelta(60, unit='s'), df['Location']) for z in pd.date_range(x, y, freq='Min')], columns=['Date','Location']) df = (df1.groupby([df1['Date'].dt.date.rename('Business Date'), df1['Date'].dt.hour.rename('Time'), df1['Location']]) .size() .div(60) .reset_index(name='Hour'))
https://en.xdnf.cn/q/69537.html

Related Q&A

Tensorflow model prediction is slow

I have a TensorFlow model with a single Dense layer: model = tf.keras.Sequential([tf.keras.layers.Dense(2)]) model.build(input_shape=(None, None, 25))I construct a single input vector in float32: np_ve…

Pandas Sqlite query using variable

With sqlite3 in Python if I want to make a db query using a variable instead of a fixed command I can do something like this :name = MSFTc.execute(INSERT INTO Symbol VALUES (?) , (name,))And when I tr…

How to remove ^M from a text file and replace it with the next line

So suppose I have a text file of the following contents:Hello what is up. ^M ^M What are you doing?I want to remove the ^M and replace it with the line that follows. So my output would look like:Hello…

Cython: size attribute of memoryviews

Im using a lot of 3D memoryviews in Cython, e.g.cython.declare(a=double[:, :, ::1]) a = np.empty((10, 20, 30), dtype=double)I often want to loop over all elements of a. I can do this using a triple loo…

python asynchronous httprequest

I am trying to use twitter search web service in python. I want to call a web service like:http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mix…

What are response codes for 256 and 512 for os.system in python scripting

When i ping servers with os.system in python i get multiple response codes. Command used - os.system("ping -q -c 30 -s SERVERANME")0 - Online 256 - Offline 512 - what does 512 mean ?

Sphinx floating point formatting

Im using Sphinx to generate documentation from code. Does anyone know if there is a way to control the formatting of floating point numbers generated from default arguments. For example if I have the f…

Truncating column width in pandas

Im reading in large csv files into pandas some of them with String columns in the thousands of characters. Is there any quick way to limit the width of a column, i.e. only keep the first 100 characters…

Django - CreateView with multiple models

Can I use Django CreateViews to make a form that add data to multiple tables? Ive created a model called UserMeta to store some additional informations of my users. The ProblemI want to create a view …

Is there a way to pass dictionary in tf.data.Dataset w/ tf.py_func?

Im using tf.data.Dataset in data processing and I want to do apply some python code with tf.py_func.BTW, I found that in tf.py_func, I cannot return a dictionary. Is there any way to do it or workaroun…