creating duration into separate half an hour bands Pandas datetime












0















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




















  • 3





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

    – Sotos
    Nov 19 '18 at 10:39
















0















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




















  • 3





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

    – Sotos
    Nov 19 '18 at 10:39














0












0








0








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
















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















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 10:37









Sotos

28.9k51640




28.9k51640










asked Nov 19 '18 at 10:33









Srikanth AyithySrikanth Ayithy

83




83








  • 3





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

    – Sotos
    Nov 19 '18 at 10:39














  • 3





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

    – Sotos
    Nov 19 '18 at 10:39








3




3





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

– Sotos
Nov 19 '18 at 10:39





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

– Sotos
Nov 19 '18 at 10:39












1 Answer
1






active

oldest

votes


















0














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
    Nov 19 '18 at 15:24











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',
autoActivateHeartbeat: false,
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
});


}
});














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









0














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
    Nov 19 '18 at 15:24
















0














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
    Nov 19 '18 at 15:24














0












0








0







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 Nov 19 '18 at 11:51









Vivek KalyanaranganVivek Kalyanarangan

4,9961827




4,9961827













  • 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
    Nov 19 '18 at 15:24



















  • 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
    Nov 19 '18 at 15:24

















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
Nov 19 '18 at 15:24





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
Nov 19 '18 at 15:24


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




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

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?