How to choose specific time in a dataframe





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







0















I want to choose a range of time from my data, but I can't find the approach to choose a range of time in this code.
How do I fix my code?
Thanks!!!



I saw this code



data = pd.DataFrame({'EUROSTOXX': es['SX5E'][es.index > dt.datetime(1999, 1, 1)]}) 


from my textbook.
The time I want to correct is from (1999, 1, 1) to (2016, 1, 1)



I tried several codes to change the time, for example:



data = pd.DataFrame({'EUROSTOXX': es['SX5E'][dt.datetime(2016, 1, 1)> es.index > dt.datetime(1999, 1, 1)]})


but it failed. Is there anyone could save me?










share|improve this question




















  • 1





    Possible duplicate of How to slice a Pandas Time Series using a logical expression involving dates

    – Yuca
    Jan 3 at 15:16











  • Its always advisable to provide the minimal code which can ve reproduce in order to get and provide explicit and more feasible answer, Would you be able to show us few line of your dataframe?

    – pygo
    Jan 3 at 16:10


















0















I want to choose a range of time from my data, but I can't find the approach to choose a range of time in this code.
How do I fix my code?
Thanks!!!



I saw this code



data = pd.DataFrame({'EUROSTOXX': es['SX5E'][es.index > dt.datetime(1999, 1, 1)]}) 


from my textbook.
The time I want to correct is from (1999, 1, 1) to (2016, 1, 1)



I tried several codes to change the time, for example:



data = pd.DataFrame({'EUROSTOXX': es['SX5E'][dt.datetime(2016, 1, 1)> es.index > dt.datetime(1999, 1, 1)]})


but it failed. Is there anyone could save me?










share|improve this question




















  • 1





    Possible duplicate of How to slice a Pandas Time Series using a logical expression involving dates

    – Yuca
    Jan 3 at 15:16











  • Its always advisable to provide the minimal code which can ve reproduce in order to get and provide explicit and more feasible answer, Would you be able to show us few line of your dataframe?

    – pygo
    Jan 3 at 16:10














0












0








0


1






I want to choose a range of time from my data, but I can't find the approach to choose a range of time in this code.
How do I fix my code?
Thanks!!!



I saw this code



data = pd.DataFrame({'EUROSTOXX': es['SX5E'][es.index > dt.datetime(1999, 1, 1)]}) 


from my textbook.
The time I want to correct is from (1999, 1, 1) to (2016, 1, 1)



I tried several codes to change the time, for example:



data = pd.DataFrame({'EUROSTOXX': es['SX5E'][dt.datetime(2016, 1, 1)> es.index > dt.datetime(1999, 1, 1)]})


but it failed. Is there anyone could save me?










share|improve this question
















I want to choose a range of time from my data, but I can't find the approach to choose a range of time in this code.
How do I fix my code?
Thanks!!!



I saw this code



data = pd.DataFrame({'EUROSTOXX': es['SX5E'][es.index > dt.datetime(1999, 1, 1)]}) 


from my textbook.
The time I want to correct is from (1999, 1, 1) to (2016, 1, 1)



I tried several codes to change the time, for example:



data = pd.DataFrame({'EUROSTOXX': es['SX5E'][dt.datetime(2016, 1, 1)> es.index > dt.datetime(1999, 1, 1)]})


but it failed. Is there anyone could save me?







python pandas datetime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 15:13









sacuL

30.9k42044




30.9k42044










asked Jan 3 at 15:12









Scott ShenScott Shen

1




1








  • 1





    Possible duplicate of How to slice a Pandas Time Series using a logical expression involving dates

    – Yuca
    Jan 3 at 15:16











  • Its always advisable to provide the minimal code which can ve reproduce in order to get and provide explicit and more feasible answer, Would you be able to show us few line of your dataframe?

    – pygo
    Jan 3 at 16:10














  • 1





    Possible duplicate of How to slice a Pandas Time Series using a logical expression involving dates

    – Yuca
    Jan 3 at 15:16











  • Its always advisable to provide the minimal code which can ve reproduce in order to get and provide explicit and more feasible answer, Would you be able to show us few line of your dataframe?

    – pygo
    Jan 3 at 16:10








1




1





Possible duplicate of How to slice a Pandas Time Series using a logical expression involving dates

– Yuca
Jan 3 at 15:16





Possible duplicate of How to slice a Pandas Time Series using a logical expression involving dates

– Yuca
Jan 3 at 15:16













Its always advisable to provide the minimal code which can ve reproduce in order to get and provide explicit and more feasible answer, Would you be able to show us few line of your dataframe?

– pygo
Jan 3 at 16:10





Its always advisable to provide the minimal code which can ve reproduce in order to get and provide explicit and more feasible answer, Would you be able to show us few line of your dataframe?

– pygo
Jan 3 at 16:10












2 Answers
2






active

oldest

votes


















0














This syntax should work :



data = pd.DataFrame({'EUROSTOXX': es['SX5E'][(es.index > datetime(1999, 1, 1)) & (es.index < dt.datetime(2016, 1, 1))]})






share|improve this answer































    0














    Just an example for illustration to Select Time Range:



    DataFrame Example:



    >>> df
    date
    0 2001-01-01 00:00:00
    1 2001-01-01 01:00:00
    2 2001-01-01 02:00:00
    3 2001-01-01 03:00:00
    4 2001-01-01 04:00:00
    5 2001-01-01 05:00:00
    6 2001-01-01 06:00:00
    7 2001-01-01 07:00:00
    8 2001-01-01 08:00:00
    9 2001-01-01 09:00:00


    One way you can get it as follows:



    >>> df[(df['date'] > '2001-01-01 00:00:00') & (df['date'] <= '2001-01-01 03:00:00')]
    date
    1 2001-01-01 01:00:00
    2 2001-01-01 02:00:00
    3 2001-01-01 03:00:00


    Secondly, setting the date column as an Index and then applying loc method:



    >>> df = df.set_index(df['date'])
    >>> df
    date
    date
    2001-01-01 00:00:00 2001-01-01 00:00:00
    2001-01-01 01:00:00 2001-01-01 01:00:00
    2001-01-01 02:00:00 2001-01-01 02:00:00
    2001-01-01 03:00:00 2001-01-01 03:00:00
    2001-01-01 04:00:00 2001-01-01 04:00:00
    2001-01-01 05:00:00 2001-01-01 05:00:00
    2001-01-01 06:00:00 2001-01-01 06:00:00
    2001-01-01 07:00:00 2001-01-01 07:00:00
    2001-01-01 08:00:00 2001-01-01 08:00:00
    2001-01-01 09:00:00 2001-01-01 09:00:00


    Now using loc :



    >>> df.loc['2001-01-01 00:00:00':'2001-01-01 03:00:00']
    date
    date
    2001-01-01 00:00:00 2001-01-01 00:00:00
    2001-01-01 01:00:00 2001-01-01 01:00:00
    2001-01-01 02:00:00 2001-01-01 02:00:00
    2001-01-01 03:00:00 2001-01-01 03:00:00


    Hope it will help.






    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%2f54025015%2fhow-to-choose-specific-time-in-a-dataframe%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














      This syntax should work :



      data = pd.DataFrame({'EUROSTOXX': es['SX5E'][(es.index > datetime(1999, 1, 1)) & (es.index < dt.datetime(2016, 1, 1))]})






      share|improve this answer




























        0














        This syntax should work :



        data = pd.DataFrame({'EUROSTOXX': es['SX5E'][(es.index > datetime(1999, 1, 1)) & (es.index < dt.datetime(2016, 1, 1))]})






        share|improve this answer


























          0












          0








          0







          This syntax should work :



          data = pd.DataFrame({'EUROSTOXX': es['SX5E'][(es.index > datetime(1999, 1, 1)) & (es.index < dt.datetime(2016, 1, 1))]})






          share|improve this answer













          This syntax should work :



          data = pd.DataFrame({'EUROSTOXX': es['SX5E'][(es.index > datetime(1999, 1, 1)) & (es.index < dt.datetime(2016, 1, 1))]})







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 16:03









          dmdipdmdip

          849711




          849711

























              0














              Just an example for illustration to Select Time Range:



              DataFrame Example:



              >>> df
              date
              0 2001-01-01 00:00:00
              1 2001-01-01 01:00:00
              2 2001-01-01 02:00:00
              3 2001-01-01 03:00:00
              4 2001-01-01 04:00:00
              5 2001-01-01 05:00:00
              6 2001-01-01 06:00:00
              7 2001-01-01 07:00:00
              8 2001-01-01 08:00:00
              9 2001-01-01 09:00:00


              One way you can get it as follows:



              >>> df[(df['date'] > '2001-01-01 00:00:00') & (df['date'] <= '2001-01-01 03:00:00')]
              date
              1 2001-01-01 01:00:00
              2 2001-01-01 02:00:00
              3 2001-01-01 03:00:00


              Secondly, setting the date column as an Index and then applying loc method:



              >>> df = df.set_index(df['date'])
              >>> df
              date
              date
              2001-01-01 00:00:00 2001-01-01 00:00:00
              2001-01-01 01:00:00 2001-01-01 01:00:00
              2001-01-01 02:00:00 2001-01-01 02:00:00
              2001-01-01 03:00:00 2001-01-01 03:00:00
              2001-01-01 04:00:00 2001-01-01 04:00:00
              2001-01-01 05:00:00 2001-01-01 05:00:00
              2001-01-01 06:00:00 2001-01-01 06:00:00
              2001-01-01 07:00:00 2001-01-01 07:00:00
              2001-01-01 08:00:00 2001-01-01 08:00:00
              2001-01-01 09:00:00 2001-01-01 09:00:00


              Now using loc :



              >>> df.loc['2001-01-01 00:00:00':'2001-01-01 03:00:00']
              date
              date
              2001-01-01 00:00:00 2001-01-01 00:00:00
              2001-01-01 01:00:00 2001-01-01 01:00:00
              2001-01-01 02:00:00 2001-01-01 02:00:00
              2001-01-01 03:00:00 2001-01-01 03:00:00


              Hope it will help.






              share|improve this answer




























                0














                Just an example for illustration to Select Time Range:



                DataFrame Example:



                >>> df
                date
                0 2001-01-01 00:00:00
                1 2001-01-01 01:00:00
                2 2001-01-01 02:00:00
                3 2001-01-01 03:00:00
                4 2001-01-01 04:00:00
                5 2001-01-01 05:00:00
                6 2001-01-01 06:00:00
                7 2001-01-01 07:00:00
                8 2001-01-01 08:00:00
                9 2001-01-01 09:00:00


                One way you can get it as follows:



                >>> df[(df['date'] > '2001-01-01 00:00:00') & (df['date'] <= '2001-01-01 03:00:00')]
                date
                1 2001-01-01 01:00:00
                2 2001-01-01 02:00:00
                3 2001-01-01 03:00:00


                Secondly, setting the date column as an Index and then applying loc method:



                >>> df = df.set_index(df['date'])
                >>> df
                date
                date
                2001-01-01 00:00:00 2001-01-01 00:00:00
                2001-01-01 01:00:00 2001-01-01 01:00:00
                2001-01-01 02:00:00 2001-01-01 02:00:00
                2001-01-01 03:00:00 2001-01-01 03:00:00
                2001-01-01 04:00:00 2001-01-01 04:00:00
                2001-01-01 05:00:00 2001-01-01 05:00:00
                2001-01-01 06:00:00 2001-01-01 06:00:00
                2001-01-01 07:00:00 2001-01-01 07:00:00
                2001-01-01 08:00:00 2001-01-01 08:00:00
                2001-01-01 09:00:00 2001-01-01 09:00:00


                Now using loc :



                >>> df.loc['2001-01-01 00:00:00':'2001-01-01 03:00:00']
                date
                date
                2001-01-01 00:00:00 2001-01-01 00:00:00
                2001-01-01 01:00:00 2001-01-01 01:00:00
                2001-01-01 02:00:00 2001-01-01 02:00:00
                2001-01-01 03:00:00 2001-01-01 03:00:00


                Hope it will help.






                share|improve this answer


























                  0












                  0








                  0







                  Just an example for illustration to Select Time Range:



                  DataFrame Example:



                  >>> df
                  date
                  0 2001-01-01 00:00:00
                  1 2001-01-01 01:00:00
                  2 2001-01-01 02:00:00
                  3 2001-01-01 03:00:00
                  4 2001-01-01 04:00:00
                  5 2001-01-01 05:00:00
                  6 2001-01-01 06:00:00
                  7 2001-01-01 07:00:00
                  8 2001-01-01 08:00:00
                  9 2001-01-01 09:00:00


                  One way you can get it as follows:



                  >>> df[(df['date'] > '2001-01-01 00:00:00') & (df['date'] <= '2001-01-01 03:00:00')]
                  date
                  1 2001-01-01 01:00:00
                  2 2001-01-01 02:00:00
                  3 2001-01-01 03:00:00


                  Secondly, setting the date column as an Index and then applying loc method:



                  >>> df = df.set_index(df['date'])
                  >>> df
                  date
                  date
                  2001-01-01 00:00:00 2001-01-01 00:00:00
                  2001-01-01 01:00:00 2001-01-01 01:00:00
                  2001-01-01 02:00:00 2001-01-01 02:00:00
                  2001-01-01 03:00:00 2001-01-01 03:00:00
                  2001-01-01 04:00:00 2001-01-01 04:00:00
                  2001-01-01 05:00:00 2001-01-01 05:00:00
                  2001-01-01 06:00:00 2001-01-01 06:00:00
                  2001-01-01 07:00:00 2001-01-01 07:00:00
                  2001-01-01 08:00:00 2001-01-01 08:00:00
                  2001-01-01 09:00:00 2001-01-01 09:00:00


                  Now using loc :



                  >>> df.loc['2001-01-01 00:00:00':'2001-01-01 03:00:00']
                  date
                  date
                  2001-01-01 00:00:00 2001-01-01 00:00:00
                  2001-01-01 01:00:00 2001-01-01 01:00:00
                  2001-01-01 02:00:00 2001-01-01 02:00:00
                  2001-01-01 03:00:00 2001-01-01 03:00:00


                  Hope it will help.






                  share|improve this answer













                  Just an example for illustration to Select Time Range:



                  DataFrame Example:



                  >>> df
                  date
                  0 2001-01-01 00:00:00
                  1 2001-01-01 01:00:00
                  2 2001-01-01 02:00:00
                  3 2001-01-01 03:00:00
                  4 2001-01-01 04:00:00
                  5 2001-01-01 05:00:00
                  6 2001-01-01 06:00:00
                  7 2001-01-01 07:00:00
                  8 2001-01-01 08:00:00
                  9 2001-01-01 09:00:00


                  One way you can get it as follows:



                  >>> df[(df['date'] > '2001-01-01 00:00:00') & (df['date'] <= '2001-01-01 03:00:00')]
                  date
                  1 2001-01-01 01:00:00
                  2 2001-01-01 02:00:00
                  3 2001-01-01 03:00:00


                  Secondly, setting the date column as an Index and then applying loc method:



                  >>> df = df.set_index(df['date'])
                  >>> df
                  date
                  date
                  2001-01-01 00:00:00 2001-01-01 00:00:00
                  2001-01-01 01:00:00 2001-01-01 01:00:00
                  2001-01-01 02:00:00 2001-01-01 02:00:00
                  2001-01-01 03:00:00 2001-01-01 03:00:00
                  2001-01-01 04:00:00 2001-01-01 04:00:00
                  2001-01-01 05:00:00 2001-01-01 05:00:00
                  2001-01-01 06:00:00 2001-01-01 06:00:00
                  2001-01-01 07:00:00 2001-01-01 07:00:00
                  2001-01-01 08:00:00 2001-01-01 08:00:00
                  2001-01-01 09:00:00 2001-01-01 09:00:00


                  Now using loc :



                  >>> df.loc['2001-01-01 00:00:00':'2001-01-01 03:00:00']
                  date
                  date
                  2001-01-01 00:00:00 2001-01-01 00:00:00
                  2001-01-01 01:00:00 2001-01-01 01:00:00
                  2001-01-01 02:00:00 2001-01-01 02:00:00
                  2001-01-01 03:00:00 2001-01-01 03:00:00


                  Hope it will help.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 16:23









                  pygopygo

                  3,2171721




                  3,2171721






























                      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%2f54025015%2fhow-to-choose-specific-time-in-a-dataframe%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

                      Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                      Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                      A Topological Invariant for $pi_3(U(n))$