Convert object to time Pandas and filter between times





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







1















I have a dataframe with a column containing a series of dtype'o' items:



0         17:32:16
1 17:32:20
2 17:32:24
3 17:32:28
4 17:32:32
5 17:32:36
6 17:32:40
7 17:32:44
8 17:32:48
9 17:32:52
10 17:32:56
11 17:33:00
12 17:33:04
13 17:33:08
14 17:33:12
15 17:33:16


how can I convert it into time in order to filter then between for instance 17:32:30 and 17:33:10?
At the Moment I was trying without success:



df_result['a']=datetime.datetime(df_result['a'], '%H%M%S').time()


any help? thank you in advance










share|improve this question































    1















    I have a dataframe with a column containing a series of dtype'o' items:



    0         17:32:16
    1 17:32:20
    2 17:32:24
    3 17:32:28
    4 17:32:32
    5 17:32:36
    6 17:32:40
    7 17:32:44
    8 17:32:48
    9 17:32:52
    10 17:32:56
    11 17:33:00
    12 17:33:04
    13 17:33:08
    14 17:33:12
    15 17:33:16


    how can I convert it into time in order to filter then between for instance 17:32:30 and 17:33:10?
    At the Moment I was trying without success:



    df_result['a']=datetime.datetime(df_result['a'], '%H%M%S').time()


    any help? thank you in advance










    share|improve this question



























      1












      1








      1








      I have a dataframe with a column containing a series of dtype'o' items:



      0         17:32:16
      1 17:32:20
      2 17:32:24
      3 17:32:28
      4 17:32:32
      5 17:32:36
      6 17:32:40
      7 17:32:44
      8 17:32:48
      9 17:32:52
      10 17:32:56
      11 17:33:00
      12 17:33:04
      13 17:33:08
      14 17:33:12
      15 17:33:16


      how can I convert it into time in order to filter then between for instance 17:32:30 and 17:33:10?
      At the Moment I was trying without success:



      df_result['a']=datetime.datetime(df_result['a'], '%H%M%S').time()


      any help? thank you in advance










      share|improve this question
















      I have a dataframe with a column containing a series of dtype'o' items:



      0         17:32:16
      1 17:32:20
      2 17:32:24
      3 17:32:28
      4 17:32:32
      5 17:32:36
      6 17:32:40
      7 17:32:44
      8 17:32:48
      9 17:32:52
      10 17:32:56
      11 17:33:00
      12 17:33:04
      13 17:33:08
      14 17:33:12
      15 17:33:16


      how can I convert it into time in order to filter then between for instance 17:32:30 and 17:33:10?
      At the Moment I was trying without success:



      df_result['a']=datetime.datetime(df_result['a'], '%H%M%S').time()


      any help? thank you in advance







      python pandas






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 7:52









      petezurich

      3,89581936




      3,89581936










      asked Nov 23 '18 at 7:37









      Luca91Luca91

      1928




      1928
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Create times by to_datetime with time and then use between with boolean indexing:



          from datetime import time

          df_result['a'] = pd.to_datetime(df_result['a']).dt.time
          df_result = df_result[df_result['a'].between(time(17, 32, 30), time(17, 33, 10))]

          print (df_result)
          a
          4 17:32:32
          5 17:32:36
          6 17:32:40
          7 17:32:44
          8 17:32:48
          9 17:32:52
          10 17:32:56
          11 17:33:00
          12 17:33:04
          13 17:33:08


          Another approach with timedeltas created by to_timedelta:



          df_result['a'] = pd.to_timedelta(df_result['a'])
          df_result = df_result[df_result['a'].between('17:32:30', '17:33:10')]

          print (df_result)
          a
          4 17:32:32
          5 17:32:36
          6 17:32:40
          7 17:32:44
          8 17:32:48
          9 17:32:52
          10 17:32:56
          11 17:33:00
          12 17:33:04
          13 17:33:08





          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%2f53442440%2fconvert-object-to-time-pandas-and-filter-between-times%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














            Create times by to_datetime with time and then use between with boolean indexing:



            from datetime import time

            df_result['a'] = pd.to_datetime(df_result['a']).dt.time
            df_result = df_result[df_result['a'].between(time(17, 32, 30), time(17, 33, 10))]

            print (df_result)
            a
            4 17:32:32
            5 17:32:36
            6 17:32:40
            7 17:32:44
            8 17:32:48
            9 17:32:52
            10 17:32:56
            11 17:33:00
            12 17:33:04
            13 17:33:08


            Another approach with timedeltas created by to_timedelta:



            df_result['a'] = pd.to_timedelta(df_result['a'])
            df_result = df_result[df_result['a'].between('17:32:30', '17:33:10')]

            print (df_result)
            a
            4 17:32:32
            5 17:32:36
            6 17:32:40
            7 17:32:44
            8 17:32:48
            9 17:32:52
            10 17:32:56
            11 17:33:00
            12 17:33:04
            13 17:33:08





            share|improve this answer






























              1














              Create times by to_datetime with time and then use between with boolean indexing:



              from datetime import time

              df_result['a'] = pd.to_datetime(df_result['a']).dt.time
              df_result = df_result[df_result['a'].between(time(17, 32, 30), time(17, 33, 10))]

              print (df_result)
              a
              4 17:32:32
              5 17:32:36
              6 17:32:40
              7 17:32:44
              8 17:32:48
              9 17:32:52
              10 17:32:56
              11 17:33:00
              12 17:33:04
              13 17:33:08


              Another approach with timedeltas created by to_timedelta:



              df_result['a'] = pd.to_timedelta(df_result['a'])
              df_result = df_result[df_result['a'].between('17:32:30', '17:33:10')]

              print (df_result)
              a
              4 17:32:32
              5 17:32:36
              6 17:32:40
              7 17:32:44
              8 17:32:48
              9 17:32:52
              10 17:32:56
              11 17:33:00
              12 17:33:04
              13 17:33:08





              share|improve this answer




























                1












                1








                1







                Create times by to_datetime with time and then use between with boolean indexing:



                from datetime import time

                df_result['a'] = pd.to_datetime(df_result['a']).dt.time
                df_result = df_result[df_result['a'].between(time(17, 32, 30), time(17, 33, 10))]

                print (df_result)
                a
                4 17:32:32
                5 17:32:36
                6 17:32:40
                7 17:32:44
                8 17:32:48
                9 17:32:52
                10 17:32:56
                11 17:33:00
                12 17:33:04
                13 17:33:08


                Another approach with timedeltas created by to_timedelta:



                df_result['a'] = pd.to_timedelta(df_result['a'])
                df_result = df_result[df_result['a'].between('17:32:30', '17:33:10')]

                print (df_result)
                a
                4 17:32:32
                5 17:32:36
                6 17:32:40
                7 17:32:44
                8 17:32:48
                9 17:32:52
                10 17:32:56
                11 17:33:00
                12 17:33:04
                13 17:33:08





                share|improve this answer















                Create times by to_datetime with time and then use between with boolean indexing:



                from datetime import time

                df_result['a'] = pd.to_datetime(df_result['a']).dt.time
                df_result = df_result[df_result['a'].between(time(17, 32, 30), time(17, 33, 10))]

                print (df_result)
                a
                4 17:32:32
                5 17:32:36
                6 17:32:40
                7 17:32:44
                8 17:32:48
                9 17:32:52
                10 17:32:56
                11 17:33:00
                12 17:33:04
                13 17:33:08


                Another approach with timedeltas created by to_timedelta:



                df_result['a'] = pd.to_timedelta(df_result['a'])
                df_result = df_result[df_result['a'].between('17:32:30', '17:33:10')]

                print (df_result)
                a
                4 17:32:32
                5 17:32:36
                6 17:32:40
                7 17:32:44
                8 17:32:48
                9 17:32:52
                10 17:32:56
                11 17:33:00
                12 17:33:04
                13 17:33:08






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 23 '18 at 7:53

























                answered Nov 23 '18 at 7:40









                jezraeljezrael

                361k26327408




                361k26327408
































                    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%2f53442440%2fconvert-object-to-time-pandas-and-filter-between-times%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 send String Array data to Server using php in android

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Is anime1.com a legal site for watching anime?