creating duration into separate half an hour bands Pandas datetime
up vote
0
down vote
favorite
Need a small help. Working on the following. Separating rows.
enter image description here
Input:
Name, Channel, Duration, Start_Time
John, A, 2, 15:55:00
John, A, 3, 15:57:00
John, A, 5, 16:00:00
Joseph, B, 10, 15:25:00
Output
Name, Channel, TB, Count, Duration
John, A, 15:30:00-16:00:00,1,5
John, A, 16:00:00-16:30:00, 1, 5
Joseph, B, 15:00:00-15:30:00, 1, 5
Joseph, B, 15:30:00-16:00:00, 1, 5
Thank you in advance
python pandas datetime pandas-groupby timedelta
New contributor
add a comment |
up vote
0
down vote
favorite
Need a small help. Working on the following. Separating rows.
enter image description here
Input:
Name, Channel, Duration, Start_Time
John, A, 2, 15:55:00
John, A, 3, 15:57:00
John, A, 5, 16:00:00
Joseph, B, 10, 15:25:00
Output
Name, Channel, TB, Count, Duration
John, A, 15:30:00-16:00:00,1,5
John, A, 16:00:00-16:30:00, 1, 5
Joseph, B, 15:00:00-15:30:00, 1, 5
Joseph, B, 15:30:00-16:00:00, 1, 5
Thank you in advance
python pandas datetime pandas-groupby timedelta
New contributor
3
Can you please explain the logic? Also please don't add pictures of data. Give a reproducible example
– Sotos
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Need a small help. Working on the following. Separating rows.
enter image description here
Input:
Name, Channel, Duration, Start_Time
John, A, 2, 15:55:00
John, A, 3, 15:57:00
John, A, 5, 16:00:00
Joseph, B, 10, 15:25:00
Output
Name, Channel, TB, Count, Duration
John, A, 15:30:00-16:00:00,1,5
John, A, 16:00:00-16:30:00, 1, 5
Joseph, B, 15:00:00-15:30:00, 1, 5
Joseph, B, 15:30:00-16:00:00, 1, 5
Thank you in advance
python pandas datetime pandas-groupby timedelta
New contributor
Need a small help. Working on the following. Separating rows.
enter image description here
Input:
Name, Channel, Duration, Start_Time
John, A, 2, 15:55:00
John, A, 3, 15:57:00
John, A, 5, 16:00:00
Joseph, B, 10, 15:25:00
Output
Name, Channel, TB, Count, Duration
John, A, 15:30:00-16:00:00,1,5
John, A, 16:00:00-16:30:00, 1, 5
Joseph, B, 15:00:00-15:30:00, 1, 5
Joseph, B, 15:30:00-16:00:00, 1, 5
Thank you in advance
python pandas datetime pandas-groupby timedelta
python pandas datetime pandas-groupby timedelta
New contributor
New contributor
edited yesterday
Sotos
26.9k51540
26.9k51540
New contributor
asked yesterday
Srikanth Ayithy
83
83
New contributor
New contributor
3
Can you please explain the logic? Also please don't add pictures of data. Give a reproducible example
– Sotos
yesterday
add a comment |
3
Can you please explain the logic? Also please don't add pictures of data. Give a reproducible example
– Sotos
yesterday
3
3
Can you please explain the logic? Also please don't add pictures of data. Give a reproducible example
– Sotos
yesterday
Can you please explain the logic? Also please don't add pictures of data. Give a reproducible example
– Sotos
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Use -
df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='15:00:00', end='16:30:00', freq='30min'))
Output
Name Channel Duration Start_Time Start_time TB
0 John A 2 15:55:00 2018-11-19 15:55:00 (2018-11-19 15:30:00, 2018-11-19 16:00:00]
1 John A 3 15:57:00 2018-11-19 15:57:00 (2018-11-19 15:30:00, 2018-11-19 16:00:00]
2 John A 5 16:00:00 2018-11-19 16:00:00 (2018-11-19 15:30:00, 2018-11-19 16:00:00]
3 Joseph B 10 15:25:00 2018-11-19 15:25:00 (2018-11-19 15:00:00, 2018-11-19 15:30:00]
If you want the exact format, do -
df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='15:00:00', end='16:30:00', freq='30min')).apply(lambda x: ' - '.join(str(x).replace('(','').replace(']','').split(',')))
This will yield -
Name Channel Duration Start_Time TB
0 John A 2 15:55:00 2018-11-19 15:30:00 - 2018-11-19 16:00:00
1 John A 3 15:57:00 2018-11-19 15:30:00 - 2018-11-19 16:00:00
2 John A 5 16:00:00 2018-11-19 15:30:00 - 2018-11-19 16:00:00
3 Joseph B 10 15:25:00 2018-11-19 15:00:00 - 2018-11-19 15:30:00
df=df['Start_time'].astype('datetime64[D]').dtype df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='20:30:00', end='21:00:00', freq='30min')) Getting an Error - 'There are no fields in dtype datetime64[ns].' Issue is with the data types it appears to be. I tried converting into datetime formats. Still it is showing different datatype error everytime.
– Srikanth Ayithy
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Use -
df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='15:00:00', end='16:30:00', freq='30min'))
Output
Name Channel Duration Start_Time Start_time TB
0 John A 2 15:55:00 2018-11-19 15:55:00 (2018-11-19 15:30:00, 2018-11-19 16:00:00]
1 John A 3 15:57:00 2018-11-19 15:57:00 (2018-11-19 15:30:00, 2018-11-19 16:00:00]
2 John A 5 16:00:00 2018-11-19 16:00:00 (2018-11-19 15:30:00, 2018-11-19 16:00:00]
3 Joseph B 10 15:25:00 2018-11-19 15:25:00 (2018-11-19 15:00:00, 2018-11-19 15:30:00]
If you want the exact format, do -
df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='15:00:00', end='16:30:00', freq='30min')).apply(lambda x: ' - '.join(str(x).replace('(','').replace(']','').split(',')))
This will yield -
Name Channel Duration Start_Time TB
0 John A 2 15:55:00 2018-11-19 15:30:00 - 2018-11-19 16:00:00
1 John A 3 15:57:00 2018-11-19 15:30:00 - 2018-11-19 16:00:00
2 John A 5 16:00:00 2018-11-19 15:30:00 - 2018-11-19 16:00:00
3 Joseph B 10 15:25:00 2018-11-19 15:00:00 - 2018-11-19 15:30:00
df=df['Start_time'].astype('datetime64[D]').dtype df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='20:30:00', end='21:00:00', freq='30min')) Getting an Error - 'There are no fields in dtype datetime64[ns].' Issue is with the data types it appears to be. I tried converting into datetime formats. Still it is showing different datatype error everytime.
– Srikanth Ayithy
yesterday
add a comment |
up vote
0
down vote
Use -
df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='15:00:00', end='16:30:00', freq='30min'))
Output
Name Channel Duration Start_Time Start_time TB
0 John A 2 15:55:00 2018-11-19 15:55:00 (2018-11-19 15:30:00, 2018-11-19 16:00:00]
1 John A 3 15:57:00 2018-11-19 15:57:00 (2018-11-19 15:30:00, 2018-11-19 16:00:00]
2 John A 5 16:00:00 2018-11-19 16:00:00 (2018-11-19 15:30:00, 2018-11-19 16:00:00]
3 Joseph B 10 15:25:00 2018-11-19 15:25:00 (2018-11-19 15:00:00, 2018-11-19 15:30:00]
If you want the exact format, do -
df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='15:00:00', end='16:30:00', freq='30min')).apply(lambda x: ' - '.join(str(x).replace('(','').replace(']','').split(',')))
This will yield -
Name Channel Duration Start_Time TB
0 John A 2 15:55:00 2018-11-19 15:30:00 - 2018-11-19 16:00:00
1 John A 3 15:57:00 2018-11-19 15:30:00 - 2018-11-19 16:00:00
2 John A 5 16:00:00 2018-11-19 15:30:00 - 2018-11-19 16:00:00
3 Joseph B 10 15:25:00 2018-11-19 15:00:00 - 2018-11-19 15:30:00
df=df['Start_time'].astype('datetime64[D]').dtype df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='20:30:00', end='21:00:00', freq='30min')) Getting an Error - 'There are no fields in dtype datetime64[ns].' Issue is with the data types it appears to be. I tried converting into datetime formats. Still it is showing different datatype error everytime.
– Srikanth Ayithy
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
Use -
df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='15:00:00', end='16:30:00', freq='30min'))
Output
Name Channel Duration Start_Time Start_time TB
0 John A 2 15:55:00 2018-11-19 15:55:00 (2018-11-19 15:30:00, 2018-11-19 16:00:00]
1 John A 3 15:57:00 2018-11-19 15:57:00 (2018-11-19 15:30:00, 2018-11-19 16:00:00]
2 John A 5 16:00:00 2018-11-19 16:00:00 (2018-11-19 15:30:00, 2018-11-19 16:00:00]
3 Joseph B 10 15:25:00 2018-11-19 15:25:00 (2018-11-19 15:00:00, 2018-11-19 15:30:00]
If you want the exact format, do -
df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='15:00:00', end='16:30:00', freq='30min')).apply(lambda x: ' - '.join(str(x).replace('(','').replace(']','').split(',')))
This will yield -
Name Channel Duration Start_Time TB
0 John A 2 15:55:00 2018-11-19 15:30:00 - 2018-11-19 16:00:00
1 John A 3 15:57:00 2018-11-19 15:30:00 - 2018-11-19 16:00:00
2 John A 5 16:00:00 2018-11-19 15:30:00 - 2018-11-19 16:00:00
3 Joseph B 10 15:25:00 2018-11-19 15:00:00 - 2018-11-19 15:30:00
Use -
df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='15:00:00', end='16:30:00', freq='30min'))
Output
Name Channel Duration Start_Time Start_time TB
0 John A 2 15:55:00 2018-11-19 15:55:00 (2018-11-19 15:30:00, 2018-11-19 16:00:00]
1 John A 3 15:57:00 2018-11-19 15:57:00 (2018-11-19 15:30:00, 2018-11-19 16:00:00]
2 John A 5 16:00:00 2018-11-19 16:00:00 (2018-11-19 15:30:00, 2018-11-19 16:00:00]
3 Joseph B 10 15:25:00 2018-11-19 15:25:00 (2018-11-19 15:00:00, 2018-11-19 15:30:00]
If you want the exact format, do -
df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='15:00:00', end='16:30:00', freq='30min')).apply(lambda x: ' - '.join(str(x).replace('(','').replace(']','').split(',')))
This will yield -
Name Channel Duration Start_Time TB
0 John A 2 15:55:00 2018-11-19 15:30:00 - 2018-11-19 16:00:00
1 John A 3 15:57:00 2018-11-19 15:30:00 - 2018-11-19 16:00:00
2 John A 5 16:00:00 2018-11-19 15:30:00 - 2018-11-19 16:00:00
3 Joseph B 10 15:25:00 2018-11-19 15:00:00 - 2018-11-19 15:30:00
answered yesterday
Vivek Kalyanarangan
3,8821725
3,8821725
df=df['Start_time'].astype('datetime64[D]').dtype df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='20:30:00', end='21:00:00', freq='30min')) Getting an Error - 'There are no fields in dtype datetime64[ns].' Issue is with the data types it appears to be. I tried converting into datetime formats. Still it is showing different datatype error everytime.
– Srikanth Ayithy
yesterday
add a comment |
df=df['Start_time'].astype('datetime64[D]').dtype df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='20:30:00', end='21:00:00', freq='30min')) Getting an Error - 'There are no fields in dtype datetime64[ns].' Issue is with the data types it appears to be. I tried converting into datetime formats. Still it is showing different datatype error everytime.
– Srikanth Ayithy
yesterday
df=df['Start_time'].astype('datetime64[D]').dtype df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='20:30:00', end='21:00:00', freq='30min')) Getting an Error - 'There are no fields in dtype datetime64[ns].' Issue is with the data types it appears to be. I tried converting into datetime formats. Still it is showing different datatype error everytime.
– Srikanth Ayithy
yesterday
df=df['Start_time'].astype('datetime64[D]').dtype df['TB'] = pd.cut(df['Start_time'], bins=pd.date_range(start='20:30:00', end='21:00:00', freq='30min')) Getting an Error - 'There are no fields in dtype datetime64[ns].' Issue is with the data types it appears to be. I tried converting into datetime formats. Still it is showing different datatype error everytime.
– Srikanth Ayithy
yesterday
add a comment |
Srikanth Ayithy is a new contributor. Be nice, and check out our Code of Conduct.
Srikanth Ayithy is a new contributor. Be nice, and check out our Code of Conduct.
Srikanth Ayithy is a new contributor. Be nice, and check out our Code of Conduct.
Srikanth Ayithy is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372704%2fcreating-duration-into-separate-half-an-hour-bands-pandas-datetime%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Can you please explain the logic? Also please don't add pictures of data. Give a reproducible example
– Sotos
yesterday