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










share|improve this question









New contributor




Srikanth Ayithy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 3




    Can you please explain the logic? Also please don't add pictures of data. Give a reproducible example
    – Sotos
    yesterday















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










share|improve this question









New contributor




Srikanth Ayithy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 3




    Can you please explain the logic? Also please don't add pictures of data. Give a reproducible example
    – Sotos
    yesterday













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










share|improve this question









New contributor




Srikanth Ayithy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




Srikanth Ayithy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Srikanth Ayithy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









Sotos

26.9k51540




26.9k51540






New contributor




Srikanth Ayithy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Srikanth Ayithy

83




83




New contributor




Srikanth Ayithy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Srikanth Ayithy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Srikanth Ayithy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 3




    Can you please explain the logic? Also please don't add pictures of data. Give a reproducible example
    – Sotos
    yesterday














  • 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












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





share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Srikanth Ayithy is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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

























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





share|improve this answer





















  • 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















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





share|improve this answer





















  • 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













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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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










Srikanth Ayithy is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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





















































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







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]