Splitting dataframe by month on a series in Python



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-3















My dataframe has dates that range from month 1-12 for every set. The data looks like:



 Date AB AK AR DC
0 2005-01-01 267724 37140 152536 60004
1 2005-02-01 214444 32710 149821 49280
2 2005-03-01 205938 27484 141526 41345
3 2005-04-01 99262 14562 81254 31609
4 2005-05-01 66059 8172 50241 18705
5 2005-06-01 33556 4880 27216 11796
6 2005-07-01 28057 4138 20156 9126
7 2005-08-01 25466 3892 19005 8262
8 2005-09-01 26819 3923 18776 9480
9 2005-10-01 60849 5942 31255 1664
.
.


The Date is in datetime format:



data_res_num.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 164 entries, 0 to 163
Data columns (total 11 columns):
Date 164 non-null datetime64[ns]


But when I try to pass only certain months (10 to 3) with:



df = df.loc[(df['Date'].dt.month > 10) & (df['Date'].dt.month < 4)]


I get an empty frame for df.



I thought it may have been the range but even if I set it == certain month it also gives me an empty frame.



How can I select the range from November - March?










share|improve this question

















  • 3





    Typo: there are no integers greater than 10 AND less than 3.

    – jpp
    Nov 15 '18 at 15:56












  • That's the thing. I also tried df = df.loc[(df['Date'].dt.month == 10) & (df['Date'].dt.month == 4)] and I get the same result and I know 10 and 4 exist.

    – HelloToEarth
    Nov 15 '18 at 15:58











  • @jpp pandas will select rows which satisfy either of these column conditions, they don't have to be satisfied simultaneously...

    – Dascienz
    Nov 15 '18 at 16:02






  • 2





    @jpp, Oh duh, he needs a |, my bad! Haha, really embarrassing.

    – Dascienz
    Nov 15 '18 at 16:03







  • 1





    Hey @HelloToEarth, | is an OR operator which means that pandas will select rows which satisfy either condition, however, when you use & which is an AND operator it means that both conditions must be satisfied simultaneously. You can't have a month be greater than 10 and less than 4 at the same time, nor can you have a month that is both equal to 4 and 10 at the same time. Therefore, you need to use the OR operator, |.

    – Dascienz
    Nov 15 '18 at 16:09


















-3















My dataframe has dates that range from month 1-12 for every set. The data looks like:



 Date AB AK AR DC
0 2005-01-01 267724 37140 152536 60004
1 2005-02-01 214444 32710 149821 49280
2 2005-03-01 205938 27484 141526 41345
3 2005-04-01 99262 14562 81254 31609
4 2005-05-01 66059 8172 50241 18705
5 2005-06-01 33556 4880 27216 11796
6 2005-07-01 28057 4138 20156 9126
7 2005-08-01 25466 3892 19005 8262
8 2005-09-01 26819 3923 18776 9480
9 2005-10-01 60849 5942 31255 1664
.
.


The Date is in datetime format:



data_res_num.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 164 entries, 0 to 163
Data columns (total 11 columns):
Date 164 non-null datetime64[ns]


But when I try to pass only certain months (10 to 3) with:



df = df.loc[(df['Date'].dt.month > 10) & (df['Date'].dt.month < 4)]


I get an empty frame for df.



I thought it may have been the range but even if I set it == certain month it also gives me an empty frame.



How can I select the range from November - March?










share|improve this question

















  • 3





    Typo: there are no integers greater than 10 AND less than 3.

    – jpp
    Nov 15 '18 at 15:56












  • That's the thing. I also tried df = df.loc[(df['Date'].dt.month == 10) & (df['Date'].dt.month == 4)] and I get the same result and I know 10 and 4 exist.

    – HelloToEarth
    Nov 15 '18 at 15:58











  • @jpp pandas will select rows which satisfy either of these column conditions, they don't have to be satisfied simultaneously...

    – Dascienz
    Nov 15 '18 at 16:02






  • 2





    @jpp, Oh duh, he needs a |, my bad! Haha, really embarrassing.

    – Dascienz
    Nov 15 '18 at 16:03







  • 1





    Hey @HelloToEarth, | is an OR operator which means that pandas will select rows which satisfy either condition, however, when you use & which is an AND operator it means that both conditions must be satisfied simultaneously. You can't have a month be greater than 10 and less than 4 at the same time, nor can you have a month that is both equal to 4 and 10 at the same time. Therefore, you need to use the OR operator, |.

    – Dascienz
    Nov 15 '18 at 16:09














-3












-3








-3








My dataframe has dates that range from month 1-12 for every set. The data looks like:



 Date AB AK AR DC
0 2005-01-01 267724 37140 152536 60004
1 2005-02-01 214444 32710 149821 49280
2 2005-03-01 205938 27484 141526 41345
3 2005-04-01 99262 14562 81254 31609
4 2005-05-01 66059 8172 50241 18705
5 2005-06-01 33556 4880 27216 11796
6 2005-07-01 28057 4138 20156 9126
7 2005-08-01 25466 3892 19005 8262
8 2005-09-01 26819 3923 18776 9480
9 2005-10-01 60849 5942 31255 1664
.
.


The Date is in datetime format:



data_res_num.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 164 entries, 0 to 163
Data columns (total 11 columns):
Date 164 non-null datetime64[ns]


But when I try to pass only certain months (10 to 3) with:



df = df.loc[(df['Date'].dt.month > 10) & (df['Date'].dt.month < 4)]


I get an empty frame for df.



I thought it may have been the range but even if I set it == certain month it also gives me an empty frame.



How can I select the range from November - March?










share|improve this question














My dataframe has dates that range from month 1-12 for every set. The data looks like:



 Date AB AK AR DC
0 2005-01-01 267724 37140 152536 60004
1 2005-02-01 214444 32710 149821 49280
2 2005-03-01 205938 27484 141526 41345
3 2005-04-01 99262 14562 81254 31609
4 2005-05-01 66059 8172 50241 18705
5 2005-06-01 33556 4880 27216 11796
6 2005-07-01 28057 4138 20156 9126
7 2005-08-01 25466 3892 19005 8262
8 2005-09-01 26819 3923 18776 9480
9 2005-10-01 60849 5942 31255 1664
.
.


The Date is in datetime format:



data_res_num.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 164 entries, 0 to 163
Data columns (total 11 columns):
Date 164 non-null datetime64[ns]


But when I try to pass only certain months (10 to 3) with:



df = df.loc[(df['Date'].dt.month > 10) & (df['Date'].dt.month < 4)]


I get an empty frame for df.



I thought it may have been the range but even if I set it == certain month it also gives me an empty frame.



How can I select the range from November - March?







python python-3.x python-2.7 pandas dataframe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 15:52









HelloToEarthHelloToEarth

537215




537215







  • 3





    Typo: there are no integers greater than 10 AND less than 3.

    – jpp
    Nov 15 '18 at 15:56












  • That's the thing. I also tried df = df.loc[(df['Date'].dt.month == 10) & (df['Date'].dt.month == 4)] and I get the same result and I know 10 and 4 exist.

    – HelloToEarth
    Nov 15 '18 at 15:58











  • @jpp pandas will select rows which satisfy either of these column conditions, they don't have to be satisfied simultaneously...

    – Dascienz
    Nov 15 '18 at 16:02






  • 2





    @jpp, Oh duh, he needs a |, my bad! Haha, really embarrassing.

    – Dascienz
    Nov 15 '18 at 16:03







  • 1





    Hey @HelloToEarth, | is an OR operator which means that pandas will select rows which satisfy either condition, however, when you use & which is an AND operator it means that both conditions must be satisfied simultaneously. You can't have a month be greater than 10 and less than 4 at the same time, nor can you have a month that is both equal to 4 and 10 at the same time. Therefore, you need to use the OR operator, |.

    – Dascienz
    Nov 15 '18 at 16:09













  • 3





    Typo: there are no integers greater than 10 AND less than 3.

    – jpp
    Nov 15 '18 at 15:56












  • That's the thing. I also tried df = df.loc[(df['Date'].dt.month == 10) & (df['Date'].dt.month == 4)] and I get the same result and I know 10 and 4 exist.

    – HelloToEarth
    Nov 15 '18 at 15:58











  • @jpp pandas will select rows which satisfy either of these column conditions, they don't have to be satisfied simultaneously...

    – Dascienz
    Nov 15 '18 at 16:02






  • 2





    @jpp, Oh duh, he needs a |, my bad! Haha, really embarrassing.

    – Dascienz
    Nov 15 '18 at 16:03







  • 1





    Hey @HelloToEarth, | is an OR operator which means that pandas will select rows which satisfy either condition, however, when you use & which is an AND operator it means that both conditions must be satisfied simultaneously. You can't have a month be greater than 10 and less than 4 at the same time, nor can you have a month that is both equal to 4 and 10 at the same time. Therefore, you need to use the OR operator, |.

    – Dascienz
    Nov 15 '18 at 16:09








3




3





Typo: there are no integers greater than 10 AND less than 3.

– jpp
Nov 15 '18 at 15:56






Typo: there are no integers greater than 10 AND less than 3.

– jpp
Nov 15 '18 at 15:56














That's the thing. I also tried df = df.loc[(df['Date'].dt.month == 10) & (df['Date'].dt.month == 4)] and I get the same result and I know 10 and 4 exist.

– HelloToEarth
Nov 15 '18 at 15:58





That's the thing. I also tried df = df.loc[(df['Date'].dt.month == 10) & (df['Date'].dt.month == 4)] and I get the same result and I know 10 and 4 exist.

– HelloToEarth
Nov 15 '18 at 15:58













@jpp pandas will select rows which satisfy either of these column conditions, they don't have to be satisfied simultaneously...

– Dascienz
Nov 15 '18 at 16:02





@jpp pandas will select rows which satisfy either of these column conditions, they don't have to be satisfied simultaneously...

– Dascienz
Nov 15 '18 at 16:02




2




2





@jpp, Oh duh, he needs a |, my bad! Haha, really embarrassing.

– Dascienz
Nov 15 '18 at 16:03






@jpp, Oh duh, he needs a |, my bad! Haha, really embarrassing.

– Dascienz
Nov 15 '18 at 16:03





1




1





Hey @HelloToEarth, | is an OR operator which means that pandas will select rows which satisfy either condition, however, when you use & which is an AND operator it means that both conditions must be satisfied simultaneously. You can't have a month be greater than 10 and less than 4 at the same time, nor can you have a month that is both equal to 4 and 10 at the same time. Therefore, you need to use the OR operator, |.

– Dascienz
Nov 15 '18 at 16:09






Hey @HelloToEarth, | is an OR operator which means that pandas will select rows which satisfy either condition, however, when you use & which is an AND operator it means that both conditions must be satisfied simultaneously. You can't have a month be greater than 10 and less than 4 at the same time, nor can you have a month that is both equal to 4 and 10 at the same time. Therefore, you need to use the OR operator, |.

– Dascienz
Nov 15 '18 at 16:09













1 Answer
1






active

oldest

votes


















1














Try slicing without the use of .loc:



df = df[(df['Date'].dt.month > 10) | (df['Date'].dt.month < 4)] 





share|improve this answer

























  • This is incorrect. The problem is a typo, see comments to the question.

    – jpp
    Nov 15 '18 at 15:58












  • @jpp Edited my answer, thanks!

    – Dascienz
    Nov 15 '18 at 16:04











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%2f53323174%2fsplitting-dataframe-by-month-on-a-series-in-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









1














Try slicing without the use of .loc:



df = df[(df['Date'].dt.month > 10) | (df['Date'].dt.month < 4)] 





share|improve this answer

























  • This is incorrect. The problem is a typo, see comments to the question.

    – jpp
    Nov 15 '18 at 15:58












  • @jpp Edited my answer, thanks!

    – Dascienz
    Nov 15 '18 at 16:04















1














Try slicing without the use of .loc:



df = df[(df['Date'].dt.month > 10) | (df['Date'].dt.month < 4)] 





share|improve this answer

























  • This is incorrect. The problem is a typo, see comments to the question.

    – jpp
    Nov 15 '18 at 15:58












  • @jpp Edited my answer, thanks!

    – Dascienz
    Nov 15 '18 at 16:04













1












1








1







Try slicing without the use of .loc:



df = df[(df['Date'].dt.month > 10) | (df['Date'].dt.month < 4)] 





share|improve this answer















Try slicing without the use of .loc:



df = df[(df['Date'].dt.month > 10) | (df['Date'].dt.month < 4)] 






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 16:03

























answered Nov 15 '18 at 15:57









DascienzDascienz

610412




610412












  • This is incorrect. The problem is a typo, see comments to the question.

    – jpp
    Nov 15 '18 at 15:58












  • @jpp Edited my answer, thanks!

    – Dascienz
    Nov 15 '18 at 16:04

















  • This is incorrect. The problem is a typo, see comments to the question.

    – jpp
    Nov 15 '18 at 15:58












  • @jpp Edited my answer, thanks!

    – Dascienz
    Nov 15 '18 at 16:04
















This is incorrect. The problem is a typo, see comments to the question.

– jpp
Nov 15 '18 at 15:58






This is incorrect. The problem is a typo, see comments to the question.

– jpp
Nov 15 '18 at 15:58














@jpp Edited my answer, thanks!

– Dascienz
Nov 15 '18 at 16:04





@jpp Edited my answer, thanks!

– Dascienz
Nov 15 '18 at 16:04



















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%2f53323174%2fsplitting-dataframe-by-month-on-a-series-in-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

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20