Time into duration/Session using Python
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
add a comment |
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
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
add a comment |
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
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
python pandas
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
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
add a comment |
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
});
}
});
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%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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
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%2f43893002%2ftime-into-duration-session-using-python%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
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