Time into duration/Session using Python












2















I have 1 pandas data-frame with date and time. I want to add 1 more column called session, with values morning afternoon, evening, night.



   Time
2016-07-10 01:18:00
2016-07-10 11:21:00
2016-07-10 17:29:00
2016-07-10 21:43:00


I want output like



   Time                     Session
2016-07-10 01:18:00 Night
2016-07-10 11:21:00 Morning
2016-07-10 17:29:00 Afternoon
2016-07-10 21:43:00 Evening


How to do this ?



I tried this by defining breaks



00:00 - 06:00 Night
06:00 - 12:00 Morning
12:00 - 18:00 Afternoon
18:00 -23:59 Evening


df.Time[(df.Time.dt.hour < 6) | (df.Time.dt.hour > 0) | (df.Time.dt.minute < 59) | (df.Time.dt.minute > 0)] = "Night"


But its not working properly. Thanks in advance










share|improve this question

























  • Your 2016-07-10 05:26:00 how is that afternoon and not morning? There is no AM PM designation nor 24 hour clock. Is it ordered based?

    – Scott Boston
    May 10 '17 at 12:54













  • @Scott Boston - Corrected the question. Sorry. Now it will make sense

    – Ajay jadhav
    May 10 '17 at 12:58
















2















I have 1 pandas data-frame with date and time. I want to add 1 more column called session, with values morning afternoon, evening, night.



   Time
2016-07-10 01:18:00
2016-07-10 11:21:00
2016-07-10 17:29:00
2016-07-10 21:43:00


I want output like



   Time                     Session
2016-07-10 01:18:00 Night
2016-07-10 11:21:00 Morning
2016-07-10 17:29:00 Afternoon
2016-07-10 21:43:00 Evening


How to do this ?



I tried this by defining breaks



00:00 - 06:00 Night
06:00 - 12:00 Morning
12:00 - 18:00 Afternoon
18:00 -23:59 Evening


df.Time[(df.Time.dt.hour < 6) | (df.Time.dt.hour > 0) | (df.Time.dt.minute < 59) | (df.Time.dt.minute > 0)] = "Night"


But its not working properly. Thanks in advance










share|improve this question

























  • Your 2016-07-10 05:26:00 how is that afternoon and not morning? There is no AM PM designation nor 24 hour clock. Is it ordered based?

    – Scott Boston
    May 10 '17 at 12:54













  • @Scott Boston - Corrected the question. Sorry. Now it will make sense

    – Ajay jadhav
    May 10 '17 at 12:58














2












2








2








I have 1 pandas data-frame with date and time. I want to add 1 more column called session, with values morning afternoon, evening, night.



   Time
2016-07-10 01:18:00
2016-07-10 11:21:00
2016-07-10 17:29:00
2016-07-10 21:43:00


I want output like



   Time                     Session
2016-07-10 01:18:00 Night
2016-07-10 11:21:00 Morning
2016-07-10 17:29:00 Afternoon
2016-07-10 21:43:00 Evening


How to do this ?



I tried this by defining breaks



00:00 - 06:00 Night
06:00 - 12:00 Morning
12:00 - 18:00 Afternoon
18:00 -23:59 Evening


df.Time[(df.Time.dt.hour < 6) | (df.Time.dt.hour > 0) | (df.Time.dt.minute < 59) | (df.Time.dt.minute > 0)] = "Night"


But its not working properly. Thanks in advance










share|improve this question
















I have 1 pandas data-frame with date and time. I want to add 1 more column called session, with values morning afternoon, evening, night.



   Time
2016-07-10 01:18:00
2016-07-10 11:21:00
2016-07-10 17:29:00
2016-07-10 21:43:00


I want output like



   Time                     Session
2016-07-10 01:18:00 Night
2016-07-10 11:21:00 Morning
2016-07-10 17:29:00 Afternoon
2016-07-10 21:43:00 Evening


How to do this ?



I tried this by defining breaks



00:00 - 06:00 Night
06:00 - 12:00 Morning
12:00 - 18:00 Afternoon
18:00 -23:59 Evening


df.Time[(df.Time.dt.hour < 6) | (df.Time.dt.hour > 0) | (df.Time.dt.minute < 59) | (df.Time.dt.minute > 0)] = "Night"


But its not working properly. Thanks in advance







python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 10 '17 at 12:56







Ajay jadhav

















asked May 10 '17 at 12:48









Ajay jadhavAjay jadhav

446




446













  • Your 2016-07-10 05:26:00 how is that afternoon and not morning? There is no AM PM designation nor 24 hour clock. Is it ordered based?

    – Scott Boston
    May 10 '17 at 12:54













  • @Scott Boston - Corrected the question. Sorry. Now it will make sense

    – Ajay jadhav
    May 10 '17 at 12:58



















  • Your 2016-07-10 05:26:00 how is that afternoon and not morning? There is no AM PM designation nor 24 hour clock. Is it ordered based?

    – Scott Boston
    May 10 '17 at 12:54













  • @Scott Boston - Corrected the question. Sorry. Now it will make sense

    – Ajay jadhav
    May 10 '17 at 12:58

















Your 2016-07-10 05:26:00 how is that afternoon and not morning? There is no AM PM designation nor 24 hour clock. Is it ordered based?

– Scott Boston
May 10 '17 at 12:54







Your 2016-07-10 05:26:00 how is that afternoon and not morning? There is no AM PM designation nor 24 hour clock. Is it ordered based?

– Scott Boston
May 10 '17 at 12:54















@Scott Boston - Corrected the question. Sorry. Now it will make sense

– Ajay jadhav
May 10 '17 at 12:58





@Scott Boston - Corrected the question. Sorry. Now it will make sense

– Ajay jadhav
May 10 '17 at 12:58












1 Answer
1






active

oldest

votes


















7














Use pd.cut to create bins for your session labels.



df.assign(session=pd.cut(df.Time.dt.hour,[0,6,12,18,24],labels=['Night','Morning','Afternoon','Evening']))


Output:



                 Time    session
0 2016-07-10 01:18:00 Night
1 2016-07-10 11:21:00 Morning
2 2016-07-10 17:29:00 Afternoon
3 2016-07-10 21:43:00 Evening





share|improve this answer
























  • Thanks @Scott Boston, Its working properly, But this is based on hours only, what if I want to 1 more condition of Minutes.

    – Ajay jadhav
    May 10 '17 at 13:04











  • You could use .dt.hours and .dt.mintutes to create a calculation then create bins to match that calcuation.

    – Scott Boston
    May 10 '17 at 13:06











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%2f43893002%2ftime-into-duration-session-using-python%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









7














Use pd.cut to create bins for your session labels.



df.assign(session=pd.cut(df.Time.dt.hour,[0,6,12,18,24],labels=['Night','Morning','Afternoon','Evening']))


Output:



                 Time    session
0 2016-07-10 01:18:00 Night
1 2016-07-10 11:21:00 Morning
2 2016-07-10 17:29:00 Afternoon
3 2016-07-10 21:43:00 Evening





share|improve this answer
























  • Thanks @Scott Boston, Its working properly, But this is based on hours only, what if I want to 1 more condition of Minutes.

    – Ajay jadhav
    May 10 '17 at 13:04











  • You could use .dt.hours and .dt.mintutes to create a calculation then create bins to match that calcuation.

    – Scott Boston
    May 10 '17 at 13:06
















7














Use pd.cut to create bins for your session labels.



df.assign(session=pd.cut(df.Time.dt.hour,[0,6,12,18,24],labels=['Night','Morning','Afternoon','Evening']))


Output:



                 Time    session
0 2016-07-10 01:18:00 Night
1 2016-07-10 11:21:00 Morning
2 2016-07-10 17:29:00 Afternoon
3 2016-07-10 21:43:00 Evening





share|improve this answer
























  • Thanks @Scott Boston, Its working properly, But this is based on hours only, what if I want to 1 more condition of Minutes.

    – Ajay jadhav
    May 10 '17 at 13:04











  • You could use .dt.hours and .dt.mintutes to create a calculation then create bins to match that calcuation.

    – Scott Boston
    May 10 '17 at 13:06














7












7








7







Use pd.cut to create bins for your session labels.



df.assign(session=pd.cut(df.Time.dt.hour,[0,6,12,18,24],labels=['Night','Morning','Afternoon','Evening']))


Output:



                 Time    session
0 2016-07-10 01:18:00 Night
1 2016-07-10 11:21:00 Morning
2 2016-07-10 17:29:00 Afternoon
3 2016-07-10 21:43:00 Evening





share|improve this answer













Use pd.cut to create bins for your session labels.



df.assign(session=pd.cut(df.Time.dt.hour,[0,6,12,18,24],labels=['Night','Morning','Afternoon','Evening']))


Output:



                 Time    session
0 2016-07-10 01:18:00 Night
1 2016-07-10 11:21:00 Morning
2 2016-07-10 17:29:00 Afternoon
3 2016-07-10 21:43:00 Evening






share|improve this answer












share|improve this answer



share|improve this answer










answered May 10 '17 at 13:00









Scott BostonScott Boston

54.6k73056




54.6k73056













  • Thanks @Scott Boston, Its working properly, But this is based on hours only, what if I want to 1 more condition of Minutes.

    – Ajay jadhav
    May 10 '17 at 13:04











  • You could use .dt.hours and .dt.mintutes to create a calculation then create bins to match that calcuation.

    – Scott Boston
    May 10 '17 at 13:06



















  • Thanks @Scott Boston, Its working properly, But this is based on hours only, what if I want to 1 more condition of Minutes.

    – Ajay jadhav
    May 10 '17 at 13:04











  • You could use .dt.hours and .dt.mintutes to create a calculation then create bins to match that calcuation.

    – Scott Boston
    May 10 '17 at 13:06

















Thanks @Scott Boston, Its working properly, But this is based on hours only, what if I want to 1 more condition of Minutes.

– Ajay jadhav
May 10 '17 at 13:04





Thanks @Scott Boston, Its working properly, But this is based on hours only, what if I want to 1 more condition of Minutes.

– Ajay jadhav
May 10 '17 at 13:04













You could use .dt.hours and .dt.mintutes to create a calculation then create bins to match that calcuation.

– Scott Boston
May 10 '17 at 13:06





You could use .dt.hours and .dt.mintutes to create a calculation then create bins to match that calcuation.

– Scott Boston
May 10 '17 at 13:06




















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%2f43893002%2ftime-into-duration-session-using-python%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

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks