Using grouper to group a timestamp in a specific range2019 Community Moderator ElectionHow do you get a timestamp in JavaScript?Should I use the datetime or timestamp data type in MySQL?How can one change the timestamp of an old commit in Git?“Large data” work flows using pandasHow to count number of rows per group (and other statistics) in pandas group by?Convert range to timestamp in PandasHow to group dataframe by hour using timestamp with PandasPandas - Group by weeks from first/last entrypandas - how to organised dataframe based on date and assign new values to columndf grouping rows with same dates

Theorems like the Lovász Local Lemma?

When do we add an hyphen (-) to a complex adjective word?

Why do Australian milk farmers need to protest supermarkets' milk price?

Ban on all campaign finance?

Did CPM support custom hardware using device drivers?

How could a scammer know the apps on my phone / iTunes account?

Professor being mistaken for a grad student

What are the possible solutions of the given equation?

Official degrees of earth’s rotation per day

PTIJ: Who should pay for Uber rides: the child or the parent?

Sword in the Stone story where the sword was held in place by electromagnets

Dot in front of file

Replacing Windows 7 security updates with anti-virus?

Is having access to past exams cheating and, if yes, could it be proven just by a good grade?

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

What is IP squat space

Brexit - No Deal Rejection

Does the statement `int val = (++i > ++j) ? ++i : ++j;` invoke undefined behavior?

RegionDifference for Cylinder and Cuboid

How could a female member of a species produce eggs unto death?

Can anyone tell me why this program fails?

Is a lawful good "antagonist" effective?

How to explain that I do not want to visit a country due to personal safety concern?

My adviser wants to be the first author



Using grouper to group a timestamp in a specific range



2019 Community Moderator ElectionHow do you get a timestamp in JavaScript?Should I use the datetime or timestamp data type in MySQL?How can one change the timestamp of an old commit in Git?“Large data” work flows using pandasHow to count number of rows per group (and other statistics) in pandas group by?Convert range to timestamp in PandasHow to group dataframe by hour using timestamp with PandasPandas - Group by weeks from first/last entrypandas - how to organised dataframe based on date and assign new values to columndf grouping rows with same dates










0















Suppose that I have a data-frame (DF). Index of this data-frame is timestamp from 11 AM to 6 PM every day and this data-frame contains 30 days. I want to group it every 30 minutes. This is the function I'm using:



out = DF.groupby(pd.Grouper(freq='30min'))


The start date of output is correct, but it considers the whole day (24h) for grouping. For example, In the new timestamp, I have something like this:



11:00:00
11:30:00
12:00:00
12:30:00
...
18:00:00
18:30:00
...
23:00:00
23:30:00
...
2:00:00
2:30:00
...
...
10:30:00
11:00:00
11:30:00


As a result, many outputs are empty because from 6:00 PM to 11 AM, I don't have any data.










share|improve this question
























  • Can you add some data sample? Can you explain 24h for grouping if freq='30min' ?

    – jezrael
    Mar 7 at 12:26







  • 1





    The output is correct and as expected. If you do not want to keep the empty intervals simply filter them away afterwards.

    – John Sloper
    Mar 7 at 12:35











  • @JohnSloper Is there any way to handle that in groupby?

    – user2991243
    Mar 7 at 12:36















0















Suppose that I have a data-frame (DF). Index of this data-frame is timestamp from 11 AM to 6 PM every day and this data-frame contains 30 days. I want to group it every 30 minutes. This is the function I'm using:



out = DF.groupby(pd.Grouper(freq='30min'))


The start date of output is correct, but it considers the whole day (24h) for grouping. For example, In the new timestamp, I have something like this:



11:00:00
11:30:00
12:00:00
12:30:00
...
18:00:00
18:30:00
...
23:00:00
23:30:00
...
2:00:00
2:30:00
...
...
10:30:00
11:00:00
11:30:00


As a result, many outputs are empty because from 6:00 PM to 11 AM, I don't have any data.










share|improve this question
























  • Can you add some data sample? Can you explain 24h for grouping if freq='30min' ?

    – jezrael
    Mar 7 at 12:26







  • 1





    The output is correct and as expected. If you do not want to keep the empty intervals simply filter them away afterwards.

    – John Sloper
    Mar 7 at 12:35











  • @JohnSloper Is there any way to handle that in groupby?

    – user2991243
    Mar 7 at 12:36













0












0








0


1






Suppose that I have a data-frame (DF). Index of this data-frame is timestamp from 11 AM to 6 PM every day and this data-frame contains 30 days. I want to group it every 30 minutes. This is the function I'm using:



out = DF.groupby(pd.Grouper(freq='30min'))


The start date of output is correct, but it considers the whole day (24h) for grouping. For example, In the new timestamp, I have something like this:



11:00:00
11:30:00
12:00:00
12:30:00
...
18:00:00
18:30:00
...
23:00:00
23:30:00
...
2:00:00
2:30:00
...
...
10:30:00
11:00:00
11:30:00


As a result, many outputs are empty because from 6:00 PM to 11 AM, I don't have any data.










share|improve this question
















Suppose that I have a data-frame (DF). Index of this data-frame is timestamp from 11 AM to 6 PM every day and this data-frame contains 30 days. I want to group it every 30 minutes. This is the function I'm using:



out = DF.groupby(pd.Grouper(freq='30min'))


The start date of output is correct, but it considers the whole day (24h) for grouping. For example, In the new timestamp, I have something like this:



11:00:00
11:30:00
12:00:00
12:30:00
...
18:00:00
18:30:00
...
23:00:00
23:30:00
...
2:00:00
2:30:00
...
...
10:30:00
11:00:00
11:30:00


As a result, many outputs are empty because from 6:00 PM to 11 AM, I don't have any data.







pandas timestamp pandas-groupby






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 12:29







user2991243

















asked Mar 7 at 12:23









user2991243user2991243

1,40963067




1,40963067












  • Can you add some data sample? Can you explain 24h for grouping if freq='30min' ?

    – jezrael
    Mar 7 at 12:26







  • 1





    The output is correct and as expected. If you do not want to keep the empty intervals simply filter them away afterwards.

    – John Sloper
    Mar 7 at 12:35











  • @JohnSloper Is there any way to handle that in groupby?

    – user2991243
    Mar 7 at 12:36

















  • Can you add some data sample? Can you explain 24h for grouping if freq='30min' ?

    – jezrael
    Mar 7 at 12:26







  • 1





    The output is correct and as expected. If you do not want to keep the empty intervals simply filter them away afterwards.

    – John Sloper
    Mar 7 at 12:35











  • @JohnSloper Is there any way to handle that in groupby?

    – user2991243
    Mar 7 at 12:36
















Can you add some data sample? Can you explain 24h for grouping if freq='30min' ?

– jezrael
Mar 7 at 12:26






Can you add some data sample? Can you explain 24h for grouping if freq='30min' ?

– jezrael
Mar 7 at 12:26





1




1





The output is correct and as expected. If you do not want to keep the empty intervals simply filter them away afterwards.

– John Sloper
Mar 7 at 12:35





The output is correct and as expected. If you do not want to keep the empty intervals simply filter them away afterwards.

– John Sloper
Mar 7 at 12:35













@JohnSloper Is there any way to handle that in groupby?

– user2991243
Mar 7 at 12:36





@JohnSloper Is there any way to handle that in groupby?

– user2991243
Mar 7 at 12:36












2 Answers
2






active

oldest

votes


















0














One possible solution should be DatetimeIndex.floor:



out = DF.groupby(DF.index.floor('30min'))


Or use dropna after aggregate function:



out = DF.groupby(pd.Grouper(freq='30min')).mean().dropna()





share|improve this answer






























    0














    As mentioned in comment to original post this is as expected. If you want to remove empty groups simply slice them afterwards. Assuming in this case you are using count to aggregate:



    df = df.groupby(pd.Grouper(freq='30min')).count()
    df = df[df > 0]





    share|improve this answer






















      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%2f55043724%2fusing-grouper-to-group-a-timestamp-in-a-specific-range%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      One possible solution should be DatetimeIndex.floor:



      out = DF.groupby(DF.index.floor('30min'))


      Or use dropna after aggregate function:



      out = DF.groupby(pd.Grouper(freq='30min')).mean().dropna()





      share|improve this answer



























        0














        One possible solution should be DatetimeIndex.floor:



        out = DF.groupby(DF.index.floor('30min'))


        Or use dropna after aggregate function:



        out = DF.groupby(pd.Grouper(freq='30min')).mean().dropna()





        share|improve this answer

























          0












          0








          0







          One possible solution should be DatetimeIndex.floor:



          out = DF.groupby(DF.index.floor('30min'))


          Or use dropna after aggregate function:



          out = DF.groupby(pd.Grouper(freq='30min')).mean().dropna()





          share|improve this answer













          One possible solution should be DatetimeIndex.floor:



          out = DF.groupby(DF.index.floor('30min'))


          Or use dropna after aggregate function:



          out = DF.groupby(pd.Grouper(freq='30min')).mean().dropna()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 7 at 12:40









          jezraeljezrael

          347k25302378




          347k25302378























              0














              As mentioned in comment to original post this is as expected. If you want to remove empty groups simply slice them afterwards. Assuming in this case you are using count to aggregate:



              df = df.groupby(pd.Grouper(freq='30min')).count()
              df = df[df > 0]





              share|improve this answer



























                0














                As mentioned in comment to original post this is as expected. If you want to remove empty groups simply slice them afterwards. Assuming in this case you are using count to aggregate:



                df = df.groupby(pd.Grouper(freq='30min')).count()
                df = df[df > 0]





                share|improve this answer

























                  0












                  0








                  0







                  As mentioned in comment to original post this is as expected. If you want to remove empty groups simply slice them afterwards. Assuming in this case you are using count to aggregate:



                  df = df.groupby(pd.Grouper(freq='30min')).count()
                  df = df[df > 0]





                  share|improve this answer













                  As mentioned in comment to original post this is as expected. If you want to remove empty groups simply slice them afterwards. Assuming in this case you are using count to aggregate:



                  df = df.groupby(pd.Grouper(freq='30min')).count()
                  df = df[df > 0]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 7 at 12:42









                  John SloperJohn Sloper

                  803510




                  803510



























                      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%2f55043724%2fusing-grouper-to-group-a-timestamp-in-a-specific-range%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

                      Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                      2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

                      Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme