Backfill only last N items












2















I have this simple time series



In [1]: df = pd.DataFrame({'fire': [1, 1, 1]}, 
...: index=pd.to_datetime([
...: '2016-03-16 23:20:10',
...: '2016-03-16 23:28:58',
...: '2016-03-16 23:38:15']))
...:

In [2]: df
Out[2]:
fire
2016-03-16 23:20:10 1
2016-03-16 23:28:58 1
2016-03-16 23:41:15 1


I want to downsample it by 1 minute and add another column named fire_in_the_next_5_minutes. The resampling is done easily but I could not find a way to limit the backfilling to only 5 previous rows. The closest data I get is this:



In [3]: df = df.resample('1min').mean()
...: df['fire_in_the_next_5_minutes'] = df['fire'].fillna(method='backfill')
...:

In [4]: df
Out[4]:
fire fire_in_the_next_5_minutes
2016-03-16 23:20:00 1.0 1.0
2016-03-16 23:21:00 NaN 1.0 <-- should remain NaN
2016-03-16 23:22:00 NaN 1.0 <-- should remain NaN
2016-03-16 23:23:00 NaN 1.0
2016-03-16 23:24:00 NaN 1.0
2016-03-16 23:25:00 NaN 1.0
2016-03-16 23:26:00 NaN 1.0
2016-03-16 23:27:00 NaN 1.0
2016-03-16 23:28:00 1.0 1.0
2016-03-16 23:29:00 NaN 1.0 <-- should remain NaN
2016-03-16 23:30:00 NaN 1.0 <-- should remain NaN
2016-03-16 23:31:00 NaN 1.0 <-- should remain NaN
2016-03-16 23:32:00 NaN 1.0 <-- should remain NaN
2016-03-16 23:33:00 NaN 1.0
2016-03-16 23:34:00 NaN 1.0
2016-03-16 23:35:00 NaN 1.0
2016-03-16 23:36:00 NaN 1.0
2016-03-16 23:37:00 NaN 1.0
2016-03-16 23:38:00 1.0 1.0


Can I backfill another way, not by using fillna method?










share|improve this question



























    2















    I have this simple time series



    In [1]: df = pd.DataFrame({'fire': [1, 1, 1]}, 
    ...: index=pd.to_datetime([
    ...: '2016-03-16 23:20:10',
    ...: '2016-03-16 23:28:58',
    ...: '2016-03-16 23:38:15']))
    ...:

    In [2]: df
    Out[2]:
    fire
    2016-03-16 23:20:10 1
    2016-03-16 23:28:58 1
    2016-03-16 23:41:15 1


    I want to downsample it by 1 minute and add another column named fire_in_the_next_5_minutes. The resampling is done easily but I could not find a way to limit the backfilling to only 5 previous rows. The closest data I get is this:



    In [3]: df = df.resample('1min').mean()
    ...: df['fire_in_the_next_5_minutes'] = df['fire'].fillna(method='backfill')
    ...:

    In [4]: df
    Out[4]:
    fire fire_in_the_next_5_minutes
    2016-03-16 23:20:00 1.0 1.0
    2016-03-16 23:21:00 NaN 1.0 <-- should remain NaN
    2016-03-16 23:22:00 NaN 1.0 <-- should remain NaN
    2016-03-16 23:23:00 NaN 1.0
    2016-03-16 23:24:00 NaN 1.0
    2016-03-16 23:25:00 NaN 1.0
    2016-03-16 23:26:00 NaN 1.0
    2016-03-16 23:27:00 NaN 1.0
    2016-03-16 23:28:00 1.0 1.0
    2016-03-16 23:29:00 NaN 1.0 <-- should remain NaN
    2016-03-16 23:30:00 NaN 1.0 <-- should remain NaN
    2016-03-16 23:31:00 NaN 1.0 <-- should remain NaN
    2016-03-16 23:32:00 NaN 1.0 <-- should remain NaN
    2016-03-16 23:33:00 NaN 1.0
    2016-03-16 23:34:00 NaN 1.0
    2016-03-16 23:35:00 NaN 1.0
    2016-03-16 23:36:00 NaN 1.0
    2016-03-16 23:37:00 NaN 1.0
    2016-03-16 23:38:00 1.0 1.0


    Can I backfill another way, not by using fillna method?










    share|improve this question

























      2












      2








      2








      I have this simple time series



      In [1]: df = pd.DataFrame({'fire': [1, 1, 1]}, 
      ...: index=pd.to_datetime([
      ...: '2016-03-16 23:20:10',
      ...: '2016-03-16 23:28:58',
      ...: '2016-03-16 23:38:15']))
      ...:

      In [2]: df
      Out[2]:
      fire
      2016-03-16 23:20:10 1
      2016-03-16 23:28:58 1
      2016-03-16 23:41:15 1


      I want to downsample it by 1 minute and add another column named fire_in_the_next_5_minutes. The resampling is done easily but I could not find a way to limit the backfilling to only 5 previous rows. The closest data I get is this:



      In [3]: df = df.resample('1min').mean()
      ...: df['fire_in_the_next_5_minutes'] = df['fire'].fillna(method='backfill')
      ...:

      In [4]: df
      Out[4]:
      fire fire_in_the_next_5_minutes
      2016-03-16 23:20:00 1.0 1.0
      2016-03-16 23:21:00 NaN 1.0 <-- should remain NaN
      2016-03-16 23:22:00 NaN 1.0 <-- should remain NaN
      2016-03-16 23:23:00 NaN 1.0
      2016-03-16 23:24:00 NaN 1.0
      2016-03-16 23:25:00 NaN 1.0
      2016-03-16 23:26:00 NaN 1.0
      2016-03-16 23:27:00 NaN 1.0
      2016-03-16 23:28:00 1.0 1.0
      2016-03-16 23:29:00 NaN 1.0 <-- should remain NaN
      2016-03-16 23:30:00 NaN 1.0 <-- should remain NaN
      2016-03-16 23:31:00 NaN 1.0 <-- should remain NaN
      2016-03-16 23:32:00 NaN 1.0 <-- should remain NaN
      2016-03-16 23:33:00 NaN 1.0
      2016-03-16 23:34:00 NaN 1.0
      2016-03-16 23:35:00 NaN 1.0
      2016-03-16 23:36:00 NaN 1.0
      2016-03-16 23:37:00 NaN 1.0
      2016-03-16 23:38:00 1.0 1.0


      Can I backfill another way, not by using fillna method?










      share|improve this question














      I have this simple time series



      In [1]: df = pd.DataFrame({'fire': [1, 1, 1]}, 
      ...: index=pd.to_datetime([
      ...: '2016-03-16 23:20:10',
      ...: '2016-03-16 23:28:58',
      ...: '2016-03-16 23:38:15']))
      ...:

      In [2]: df
      Out[2]:
      fire
      2016-03-16 23:20:10 1
      2016-03-16 23:28:58 1
      2016-03-16 23:41:15 1


      I want to downsample it by 1 minute and add another column named fire_in_the_next_5_minutes. The resampling is done easily but I could not find a way to limit the backfilling to only 5 previous rows. The closest data I get is this:



      In [3]: df = df.resample('1min').mean()
      ...: df['fire_in_the_next_5_minutes'] = df['fire'].fillna(method='backfill')
      ...:

      In [4]: df
      Out[4]:
      fire fire_in_the_next_5_minutes
      2016-03-16 23:20:00 1.0 1.0
      2016-03-16 23:21:00 NaN 1.0 <-- should remain NaN
      2016-03-16 23:22:00 NaN 1.0 <-- should remain NaN
      2016-03-16 23:23:00 NaN 1.0
      2016-03-16 23:24:00 NaN 1.0
      2016-03-16 23:25:00 NaN 1.0
      2016-03-16 23:26:00 NaN 1.0
      2016-03-16 23:27:00 NaN 1.0
      2016-03-16 23:28:00 1.0 1.0
      2016-03-16 23:29:00 NaN 1.0 <-- should remain NaN
      2016-03-16 23:30:00 NaN 1.0 <-- should remain NaN
      2016-03-16 23:31:00 NaN 1.0 <-- should remain NaN
      2016-03-16 23:32:00 NaN 1.0 <-- should remain NaN
      2016-03-16 23:33:00 NaN 1.0
      2016-03-16 23:34:00 NaN 1.0
      2016-03-16 23:35:00 NaN 1.0
      2016-03-16 23:36:00 NaN 1.0
      2016-03-16 23:37:00 NaN 1.0
      2016-03-16 23:38:00 1.0 1.0


      Can I backfill another way, not by using fillna method?







      pandas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 14:24









      tsionyxtsionyx

      1,12511326




      1,12511326
























          2 Answers
          2






          active

          oldest

          votes


















          2














          Using bfill with limit



          df = df.resample('1min').mean()
          df['fire_in_the_next_5_minutes'] = df['fire'].bfill(limit=5)
          df
          Out[173]:
          fire fire_in_the_next_5_minutes
          2016-03-16 23:20:00 1.0 1.0
          2016-03-16 23:21:00 NaN NaN
          2016-03-16 23:22:00 NaN NaN
          2016-03-16 23:23:00 NaN 1.0
          2016-03-16 23:24:00 NaN 1.0
          2016-03-16 23:25:00 NaN 1.0
          2016-03-16 23:26:00 NaN 1.0
          2016-03-16 23:27:00 NaN 1.0
          2016-03-16 23:28:00 1.0 1.0
          2016-03-16 23:29:00 NaN NaN
          2016-03-16 23:30:00 NaN NaN
          2016-03-16 23:31:00 NaN NaN
          2016-03-16 23:32:00 NaN NaN
          2016-03-16 23:33:00 NaN 1.0
          2016-03-16 23:34:00 NaN 1.0
          2016-03-16 23:35:00 NaN 1.0
          2016-03-16 23:36:00 NaN 1.0
          2016-03-16 23:37:00 NaN 1.0
          2016-03-16 23:38:00 1.0 1.0





          share|improve this answer































            0














            You need to supply the limit argument to fillna:



            df['fire_in_the_next_5_minutes'] = df['fire'].fillna(method='backfill', limit=5)





            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%2f53448448%2fbackfill-only-last-n-items%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









              2














              Using bfill with limit



              df = df.resample('1min').mean()
              df['fire_in_the_next_5_minutes'] = df['fire'].bfill(limit=5)
              df
              Out[173]:
              fire fire_in_the_next_5_minutes
              2016-03-16 23:20:00 1.0 1.0
              2016-03-16 23:21:00 NaN NaN
              2016-03-16 23:22:00 NaN NaN
              2016-03-16 23:23:00 NaN 1.0
              2016-03-16 23:24:00 NaN 1.0
              2016-03-16 23:25:00 NaN 1.0
              2016-03-16 23:26:00 NaN 1.0
              2016-03-16 23:27:00 NaN 1.0
              2016-03-16 23:28:00 1.0 1.0
              2016-03-16 23:29:00 NaN NaN
              2016-03-16 23:30:00 NaN NaN
              2016-03-16 23:31:00 NaN NaN
              2016-03-16 23:32:00 NaN NaN
              2016-03-16 23:33:00 NaN 1.0
              2016-03-16 23:34:00 NaN 1.0
              2016-03-16 23:35:00 NaN 1.0
              2016-03-16 23:36:00 NaN 1.0
              2016-03-16 23:37:00 NaN 1.0
              2016-03-16 23:38:00 1.0 1.0





              share|improve this answer




























                2














                Using bfill with limit



                df = df.resample('1min').mean()
                df['fire_in_the_next_5_minutes'] = df['fire'].bfill(limit=5)
                df
                Out[173]:
                fire fire_in_the_next_5_minutes
                2016-03-16 23:20:00 1.0 1.0
                2016-03-16 23:21:00 NaN NaN
                2016-03-16 23:22:00 NaN NaN
                2016-03-16 23:23:00 NaN 1.0
                2016-03-16 23:24:00 NaN 1.0
                2016-03-16 23:25:00 NaN 1.0
                2016-03-16 23:26:00 NaN 1.0
                2016-03-16 23:27:00 NaN 1.0
                2016-03-16 23:28:00 1.0 1.0
                2016-03-16 23:29:00 NaN NaN
                2016-03-16 23:30:00 NaN NaN
                2016-03-16 23:31:00 NaN NaN
                2016-03-16 23:32:00 NaN NaN
                2016-03-16 23:33:00 NaN 1.0
                2016-03-16 23:34:00 NaN 1.0
                2016-03-16 23:35:00 NaN 1.0
                2016-03-16 23:36:00 NaN 1.0
                2016-03-16 23:37:00 NaN 1.0
                2016-03-16 23:38:00 1.0 1.0





                share|improve this answer


























                  2












                  2








                  2







                  Using bfill with limit



                  df = df.resample('1min').mean()
                  df['fire_in_the_next_5_minutes'] = df['fire'].bfill(limit=5)
                  df
                  Out[173]:
                  fire fire_in_the_next_5_minutes
                  2016-03-16 23:20:00 1.0 1.0
                  2016-03-16 23:21:00 NaN NaN
                  2016-03-16 23:22:00 NaN NaN
                  2016-03-16 23:23:00 NaN 1.0
                  2016-03-16 23:24:00 NaN 1.0
                  2016-03-16 23:25:00 NaN 1.0
                  2016-03-16 23:26:00 NaN 1.0
                  2016-03-16 23:27:00 NaN 1.0
                  2016-03-16 23:28:00 1.0 1.0
                  2016-03-16 23:29:00 NaN NaN
                  2016-03-16 23:30:00 NaN NaN
                  2016-03-16 23:31:00 NaN NaN
                  2016-03-16 23:32:00 NaN NaN
                  2016-03-16 23:33:00 NaN 1.0
                  2016-03-16 23:34:00 NaN 1.0
                  2016-03-16 23:35:00 NaN 1.0
                  2016-03-16 23:36:00 NaN 1.0
                  2016-03-16 23:37:00 NaN 1.0
                  2016-03-16 23:38:00 1.0 1.0





                  share|improve this answer













                  Using bfill with limit



                  df = df.resample('1min').mean()
                  df['fire_in_the_next_5_minutes'] = df['fire'].bfill(limit=5)
                  df
                  Out[173]:
                  fire fire_in_the_next_5_minutes
                  2016-03-16 23:20:00 1.0 1.0
                  2016-03-16 23:21:00 NaN NaN
                  2016-03-16 23:22:00 NaN NaN
                  2016-03-16 23:23:00 NaN 1.0
                  2016-03-16 23:24:00 NaN 1.0
                  2016-03-16 23:25:00 NaN 1.0
                  2016-03-16 23:26:00 NaN 1.0
                  2016-03-16 23:27:00 NaN 1.0
                  2016-03-16 23:28:00 1.0 1.0
                  2016-03-16 23:29:00 NaN NaN
                  2016-03-16 23:30:00 NaN NaN
                  2016-03-16 23:31:00 NaN NaN
                  2016-03-16 23:32:00 NaN NaN
                  2016-03-16 23:33:00 NaN 1.0
                  2016-03-16 23:34:00 NaN 1.0
                  2016-03-16 23:35:00 NaN 1.0
                  2016-03-16 23:36:00 NaN 1.0
                  2016-03-16 23:37:00 NaN 1.0
                  2016-03-16 23:38:00 1.0 1.0






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 14:26









                  Wen-BenWen-Ben

                  1




                  1

























                      0














                      You need to supply the limit argument to fillna:



                      df['fire_in_the_next_5_minutes'] = df['fire'].fillna(method='backfill', limit=5)





                      share|improve this answer




























                        0














                        You need to supply the limit argument to fillna:



                        df['fire_in_the_next_5_minutes'] = df['fire'].fillna(method='backfill', limit=5)





                        share|improve this answer


























                          0












                          0








                          0







                          You need to supply the limit argument to fillna:



                          df['fire_in_the_next_5_minutes'] = df['fire'].fillna(method='backfill', limit=5)





                          share|improve this answer













                          You need to supply the limit argument to fillna:



                          df['fire_in_the_next_5_minutes'] = df['fire'].fillna(method='backfill', limit=5)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 23 '18 at 14:27









                          Toby PettyToby Petty

                          706412




                          706412






























                              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%2f53448448%2fbackfill-only-last-n-items%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

                              404 Error Contact Form 7 ajax form submitting

                              How to know if a Active Directory user can login interactively

                              TypeError: fit_transform() missing 1 required positional argument: 'X'