Get next available date in Pandas filter by day












2















I have filtered the datetime64[ns] type in pandas dataframe to get data that falls on specific date of each month using the following line of code.



df[df['Date'].map(lambda x: x.day) == 1]


The output is as follows:



19.9    2013-07-01
34.8 2013-08-01
12.9 2013-10-01
12.6 2013-11-01


But if you notice the entry for 2013-09-01 is missing as it is not available in the original dataset. In such situation I want to get data for 2013-09-02. Ideally if a date falls on weekend (Saturday and Sunday or any missing date like holidays or data not available for specific date), I want to get the data for the next available date. Wondering if we can achieve using pandas or I need to manually iterate over perform this functionality.










share|improve this question





























    2















    I have filtered the datetime64[ns] type in pandas dataframe to get data that falls on specific date of each month using the following line of code.



    df[df['Date'].map(lambda x: x.day) == 1]


    The output is as follows:



    19.9    2013-07-01
    34.8 2013-08-01
    12.9 2013-10-01
    12.6 2013-11-01


    But if you notice the entry for 2013-09-01 is missing as it is not available in the original dataset. In such situation I want to get data for 2013-09-02. Ideally if a date falls on weekend (Saturday and Sunday or any missing date like holidays or data not available for specific date), I want to get the data for the next available date. Wondering if we can achieve using pandas or I need to manually iterate over perform this functionality.










    share|improve this question



























      2












      2








      2








      I have filtered the datetime64[ns] type in pandas dataframe to get data that falls on specific date of each month using the following line of code.



      df[df['Date'].map(lambda x: x.day) == 1]


      The output is as follows:



      19.9    2013-07-01
      34.8 2013-08-01
      12.9 2013-10-01
      12.6 2013-11-01


      But if you notice the entry for 2013-09-01 is missing as it is not available in the original dataset. In such situation I want to get data for 2013-09-02. Ideally if a date falls on weekend (Saturday and Sunday or any missing date like holidays or data not available for specific date), I want to get the data for the next available date. Wondering if we can achieve using pandas or I need to manually iterate over perform this functionality.










      share|improve this question
















      I have filtered the datetime64[ns] type in pandas dataframe to get data that falls on specific date of each month using the following line of code.



      df[df['Date'].map(lambda x: x.day) == 1]


      The output is as follows:



      19.9    2013-07-01
      34.8 2013-08-01
      12.9 2013-10-01
      12.6 2013-11-01


      But if you notice the entry for 2013-09-01 is missing as it is not available in the original dataset. In such situation I want to get data for 2013-09-02. Ideally if a date falls on weekend (Saturday and Sunday or any missing date like holidays or data not available for specific date), I want to get the data for the next available date. Wondering if we can achieve using pandas or I need to manually iterate over perform this functionality.







      python pandas lambda datetime64






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 16:03









      Adam Mitchell

      7581627




      7581627










      asked Nov 12 '18 at 15:59









      vientoviento

      47311231




      47311231
























          2 Answers
          2






          active

          oldest

          votes


















          1














          I think you need DatetimeIndex with asfreq and method='bfill' for back filling missing values:



          df = df.set_index('Date').asfreq('d', method='bfill')


          Then filter by DatetimeIndex.day:



          df1 = df[df.index.day == 1]


          Sample:



          print (df)
          Val Date
          0 19.9 2013-07-01
          1 34.8 2013-08-01
          2 10.4 2013-09-02
          3 12.9 2013-10-01
          4 12.6 2013-11-01

          print (df.dtypes)
          Val float64
          Date datetime64[ns]

          df = df.set_index('Date').asfreq('d', method='bfill')
          df1 = df[df.index.day == 1]
          print (df1)
          Val
          Date
          2013-07-01 19.9
          2013-08-01 34.8
          2013-09-01 10.4
          2013-10-01 12.9
          2013-11-01 12.6





          share|improve this answer





















          • 1





            Thanks! This approach even makes day wise accessing easy instead of using lambda expressions

            – viento
            Nov 12 '18 at 16:16



















          1














          You can also do so by setting the date as the index and searching for the next existing date to the first day of each month using index.get_loc() and set method to be bfill:



          print(df)
          Val
          Date
          2013-07-01 19.9
          2013-08-01 34.8
          2013-08-02 34.8
          2013-09-02 10.4
          2013-10-01 12.9
          2013-11-01 12.6

          df = df.set_index('Date')
          df.iloc[[df.index.get_loc(datetime.datetime(date[0],date[1],1),
          method='bfill') for date,_ in df.groupby(
          [df.index.year,df.index.month])]]

          Val
          Date
          2013-07-01 19.9
          2013-08-01 34.8
          2013-09-02 10.4
          2013-10-01 12.9
          2013-11-01 12.6





          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%2f53265823%2fget-next-available-date-in-pandas-filter-by-day%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









            1














            I think you need DatetimeIndex with asfreq and method='bfill' for back filling missing values:



            df = df.set_index('Date').asfreq('d', method='bfill')


            Then filter by DatetimeIndex.day:



            df1 = df[df.index.day == 1]


            Sample:



            print (df)
            Val Date
            0 19.9 2013-07-01
            1 34.8 2013-08-01
            2 10.4 2013-09-02
            3 12.9 2013-10-01
            4 12.6 2013-11-01

            print (df.dtypes)
            Val float64
            Date datetime64[ns]

            df = df.set_index('Date').asfreq('d', method='bfill')
            df1 = df[df.index.day == 1]
            print (df1)
            Val
            Date
            2013-07-01 19.9
            2013-08-01 34.8
            2013-09-01 10.4
            2013-10-01 12.9
            2013-11-01 12.6





            share|improve this answer





















            • 1





              Thanks! This approach even makes day wise accessing easy instead of using lambda expressions

              – viento
              Nov 12 '18 at 16:16
















            1














            I think you need DatetimeIndex with asfreq and method='bfill' for back filling missing values:



            df = df.set_index('Date').asfreq('d', method='bfill')


            Then filter by DatetimeIndex.day:



            df1 = df[df.index.day == 1]


            Sample:



            print (df)
            Val Date
            0 19.9 2013-07-01
            1 34.8 2013-08-01
            2 10.4 2013-09-02
            3 12.9 2013-10-01
            4 12.6 2013-11-01

            print (df.dtypes)
            Val float64
            Date datetime64[ns]

            df = df.set_index('Date').asfreq('d', method='bfill')
            df1 = df[df.index.day == 1]
            print (df1)
            Val
            Date
            2013-07-01 19.9
            2013-08-01 34.8
            2013-09-01 10.4
            2013-10-01 12.9
            2013-11-01 12.6





            share|improve this answer





















            • 1





              Thanks! This approach even makes day wise accessing easy instead of using lambda expressions

              – viento
              Nov 12 '18 at 16:16














            1












            1








            1







            I think you need DatetimeIndex with asfreq and method='bfill' for back filling missing values:



            df = df.set_index('Date').asfreq('d', method='bfill')


            Then filter by DatetimeIndex.day:



            df1 = df[df.index.day == 1]


            Sample:



            print (df)
            Val Date
            0 19.9 2013-07-01
            1 34.8 2013-08-01
            2 10.4 2013-09-02
            3 12.9 2013-10-01
            4 12.6 2013-11-01

            print (df.dtypes)
            Val float64
            Date datetime64[ns]

            df = df.set_index('Date').asfreq('d', method='bfill')
            df1 = df[df.index.day == 1]
            print (df1)
            Val
            Date
            2013-07-01 19.9
            2013-08-01 34.8
            2013-09-01 10.4
            2013-10-01 12.9
            2013-11-01 12.6





            share|improve this answer















            I think you need DatetimeIndex with asfreq and method='bfill' for back filling missing values:



            df = df.set_index('Date').asfreq('d', method='bfill')


            Then filter by DatetimeIndex.day:



            df1 = df[df.index.day == 1]


            Sample:



            print (df)
            Val Date
            0 19.9 2013-07-01
            1 34.8 2013-08-01
            2 10.4 2013-09-02
            3 12.9 2013-10-01
            4 12.6 2013-11-01

            print (df.dtypes)
            Val float64
            Date datetime64[ns]

            df = df.set_index('Date').asfreq('d', method='bfill')
            df1 = df[df.index.day == 1]
            print (df1)
            Val
            Date
            2013-07-01 19.9
            2013-08-01 34.8
            2013-09-01 10.4
            2013-10-01 12.9
            2013-11-01 12.6






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 12 '18 at 16:09

























            answered Nov 12 '18 at 16:04









            jezraeljezrael

            324k22266342




            324k22266342








            • 1





              Thanks! This approach even makes day wise accessing easy instead of using lambda expressions

              – viento
              Nov 12 '18 at 16:16














            • 1





              Thanks! This approach even makes day wise accessing easy instead of using lambda expressions

              – viento
              Nov 12 '18 at 16:16








            1




            1





            Thanks! This approach even makes day wise accessing easy instead of using lambda expressions

            – viento
            Nov 12 '18 at 16:16





            Thanks! This approach even makes day wise accessing easy instead of using lambda expressions

            – viento
            Nov 12 '18 at 16:16













            1














            You can also do so by setting the date as the index and searching for the next existing date to the first day of each month using index.get_loc() and set method to be bfill:



            print(df)
            Val
            Date
            2013-07-01 19.9
            2013-08-01 34.8
            2013-08-02 34.8
            2013-09-02 10.4
            2013-10-01 12.9
            2013-11-01 12.6

            df = df.set_index('Date')
            df.iloc[[df.index.get_loc(datetime.datetime(date[0],date[1],1),
            method='bfill') for date,_ in df.groupby(
            [df.index.year,df.index.month])]]

            Val
            Date
            2013-07-01 19.9
            2013-08-01 34.8
            2013-09-02 10.4
            2013-10-01 12.9
            2013-11-01 12.6





            share|improve this answer






























              1














              You can also do so by setting the date as the index and searching for the next existing date to the first day of each month using index.get_loc() and set method to be bfill:



              print(df)
              Val
              Date
              2013-07-01 19.9
              2013-08-01 34.8
              2013-08-02 34.8
              2013-09-02 10.4
              2013-10-01 12.9
              2013-11-01 12.6

              df = df.set_index('Date')
              df.iloc[[df.index.get_loc(datetime.datetime(date[0],date[1],1),
              method='bfill') for date,_ in df.groupby(
              [df.index.year,df.index.month])]]

              Val
              Date
              2013-07-01 19.9
              2013-08-01 34.8
              2013-09-02 10.4
              2013-10-01 12.9
              2013-11-01 12.6





              share|improve this answer




























                1












                1








                1







                You can also do so by setting the date as the index and searching for the next existing date to the first day of each month using index.get_loc() and set method to be bfill:



                print(df)
                Val
                Date
                2013-07-01 19.9
                2013-08-01 34.8
                2013-08-02 34.8
                2013-09-02 10.4
                2013-10-01 12.9
                2013-11-01 12.6

                df = df.set_index('Date')
                df.iloc[[df.index.get_loc(datetime.datetime(date[0],date[1],1),
                method='bfill') for date,_ in df.groupby(
                [df.index.year,df.index.month])]]

                Val
                Date
                2013-07-01 19.9
                2013-08-01 34.8
                2013-09-02 10.4
                2013-10-01 12.9
                2013-11-01 12.6





                share|improve this answer















                You can also do so by setting the date as the index and searching for the next existing date to the first day of each month using index.get_loc() and set method to be bfill:



                print(df)
                Val
                Date
                2013-07-01 19.9
                2013-08-01 34.8
                2013-08-02 34.8
                2013-09-02 10.4
                2013-10-01 12.9
                2013-11-01 12.6

                df = df.set_index('Date')
                df.iloc[[df.index.get_loc(datetime.datetime(date[0],date[1],1),
                method='bfill') for date,_ in df.groupby(
                [df.index.year,df.index.month])]]

                Val
                Date
                2013-07-01 19.9
                2013-08-01 34.8
                2013-09-02 10.4
                2013-10-01 12.9
                2013-11-01 12.6






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 '18 at 16:53

























                answered Nov 12 '18 at 16:47









                yatuyatu

                6,2181726




                6,2181726






























                    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%2f53265823%2fget-next-available-date-in-pandas-filter-by-day%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

                    Full-time equivalent

                    Bicuculline

                    さくらももこ