Convert dataframe to quarter











up vote
1
down vote

favorite












I have a dataframe (df). The Date variable is a string. I want to convert it to date and reformat it to the date at the end of the quarter. A sample is below:



df:
Date
0 201601
1 201602
2 201603
3 201604


201601 is the first quarter of 2016 and 201604 is the fourth quarter of 2016. The desired outcome is:



df:
Date
0 2016-03-31
1 2016-06-30
2 2016-09-30
3 2016-12-31


This is what I tried, but it doesn't work.



df['date'] = pd.to_datetime(df.Date, format = '%Y%q')


Thanks!










share|improve this question


























    up vote
    1
    down vote

    favorite












    I have a dataframe (df). The Date variable is a string. I want to convert it to date and reformat it to the date at the end of the quarter. A sample is below:



    df:
    Date
    0 201601
    1 201602
    2 201603
    3 201604


    201601 is the first quarter of 2016 and 201604 is the fourth quarter of 2016. The desired outcome is:



    df:
    Date
    0 2016-03-31
    1 2016-06-30
    2 2016-09-30
    3 2016-12-31


    This is what I tried, but it doesn't work.



    df['date'] = pd.to_datetime(df.Date, format = '%Y%q')


    Thanks!










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a dataframe (df). The Date variable is a string. I want to convert it to date and reformat it to the date at the end of the quarter. A sample is below:



      df:
      Date
      0 201601
      1 201602
      2 201603
      3 201604


      201601 is the first quarter of 2016 and 201604 is the fourth quarter of 2016. The desired outcome is:



      df:
      Date
      0 2016-03-31
      1 2016-06-30
      2 2016-09-30
      3 2016-12-31


      This is what I tried, but it doesn't work.



      df['date'] = pd.to_datetime(df.Date, format = '%Y%q')


      Thanks!










      share|improve this question













      I have a dataframe (df). The Date variable is a string. I want to convert it to date and reformat it to the date at the end of the quarter. A sample is below:



      df:
      Date
      0 201601
      1 201602
      2 201603
      3 201604


      201601 is the first quarter of 2016 and 201604 is the fourth quarter of 2016. The desired outcome is:



      df:
      Date
      0 2016-03-31
      1 2016-06-30
      2 2016-09-30
      3 2016-12-31


      This is what I tried, but it doesn't work.



      df['date'] = pd.to_datetime(df.Date, format = '%Y%q')


      Thanks!







      python






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 14:20









      Roger

      5911




      5911
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          You could define a function to compute the date and then apply that function.



          Using the monthrange function from the calendar module (https://docs.python.org/3.7/library/calendar.html#calendar.monthrange), your function might look something like:



          from calendar import monthrange
          from datetime import datetime

          def end_quarter(quarter):
          year = int(quarter[:4])
          month = int(quarter[-2:]) * 3
          day = monthrange(year, month)[1]
          return datetime(year, month, day).date()


          and you could then apply it using:



          df["Date"] = df["Date"].apply(end_quarter)





          share|improve this answer






























            up vote
            0
            down vote













            One way is to keep treating it as a string, and simply do an if / else statement, i.e.



            res = 
            for i in df['Date']:
            v1 = i[-2:]
            if v1 == '01':
            res.append(i[:4] + '-03-31')
            elif v1 == '02':
            res.append(i[:4] + '-06-30')
            elif v1 == '03':
            res.append(i[4:] + '-09-30')
            else:
            res.append(i[4:] + '-12-31')

            #>>> res
            #['2016-03-31', '2016-06-30', '03-09-30', '04-12-31']





            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',
              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%2f53376598%2fconvert-dataframe-to-quarter%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








              up vote
              1
              down vote













              You could define a function to compute the date and then apply that function.



              Using the monthrange function from the calendar module (https://docs.python.org/3.7/library/calendar.html#calendar.monthrange), your function might look something like:



              from calendar import monthrange
              from datetime import datetime

              def end_quarter(quarter):
              year = int(quarter[:4])
              month = int(quarter[-2:]) * 3
              day = monthrange(year, month)[1]
              return datetime(year, month, day).date()


              and you could then apply it using:



              df["Date"] = df["Date"].apply(end_quarter)





              share|improve this answer



























                up vote
                1
                down vote













                You could define a function to compute the date and then apply that function.



                Using the monthrange function from the calendar module (https://docs.python.org/3.7/library/calendar.html#calendar.monthrange), your function might look something like:



                from calendar import monthrange
                from datetime import datetime

                def end_quarter(quarter):
                year = int(quarter[:4])
                month = int(quarter[-2:]) * 3
                day = monthrange(year, month)[1]
                return datetime(year, month, day).date()


                and you could then apply it using:



                df["Date"] = df["Date"].apply(end_quarter)





                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You could define a function to compute the date and then apply that function.



                  Using the monthrange function from the calendar module (https://docs.python.org/3.7/library/calendar.html#calendar.monthrange), your function might look something like:



                  from calendar import monthrange
                  from datetime import datetime

                  def end_quarter(quarter):
                  year = int(quarter[:4])
                  month = int(quarter[-2:]) * 3
                  day = monthrange(year, month)[1]
                  return datetime(year, month, day).date()


                  and you could then apply it using:



                  df["Date"] = df["Date"].apply(end_quarter)





                  share|improve this answer














                  You could define a function to compute the date and then apply that function.



                  Using the monthrange function from the calendar module (https://docs.python.org/3.7/library/calendar.html#calendar.monthrange), your function might look something like:



                  from calendar import monthrange
                  from datetime import datetime

                  def end_quarter(quarter):
                  year = int(quarter[:4])
                  month = int(quarter[-2:]) * 3
                  day = monthrange(year, month)[1]
                  return datetime(year, month, day).date()


                  and you could then apply it using:



                  df["Date"] = df["Date"].apply(end_quarter)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 at 10:26

























                  answered Nov 19 at 14:48









                  Owen

                  3,1301914




                  3,1301914
























                      up vote
                      0
                      down vote













                      One way is to keep treating it as a string, and simply do an if / else statement, i.e.



                      res = 
                      for i in df['Date']:
                      v1 = i[-2:]
                      if v1 == '01':
                      res.append(i[:4] + '-03-31')
                      elif v1 == '02':
                      res.append(i[:4] + '-06-30')
                      elif v1 == '03':
                      res.append(i[4:] + '-09-30')
                      else:
                      res.append(i[4:] + '-12-31')

                      #>>> res
                      #['2016-03-31', '2016-06-30', '03-09-30', '04-12-31']





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        One way is to keep treating it as a string, and simply do an if / else statement, i.e.



                        res = 
                        for i in df['Date']:
                        v1 = i[-2:]
                        if v1 == '01':
                        res.append(i[:4] + '-03-31')
                        elif v1 == '02':
                        res.append(i[:4] + '-06-30')
                        elif v1 == '03':
                        res.append(i[4:] + '-09-30')
                        else:
                        res.append(i[4:] + '-12-31')

                        #>>> res
                        #['2016-03-31', '2016-06-30', '03-09-30', '04-12-31']





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          One way is to keep treating it as a string, and simply do an if / else statement, i.e.



                          res = 
                          for i in df['Date']:
                          v1 = i[-2:]
                          if v1 == '01':
                          res.append(i[:4] + '-03-31')
                          elif v1 == '02':
                          res.append(i[:4] + '-06-30')
                          elif v1 == '03':
                          res.append(i[4:] + '-09-30')
                          else:
                          res.append(i[4:] + '-12-31')

                          #>>> res
                          #['2016-03-31', '2016-06-30', '03-09-30', '04-12-31']





                          share|improve this answer












                          One way is to keep treating it as a string, and simply do an if / else statement, i.e.



                          res = 
                          for i in df['Date']:
                          v1 = i[-2:]
                          if v1 == '01':
                          res.append(i[:4] + '-03-31')
                          elif v1 == '02':
                          res.append(i[:4] + '-06-30')
                          elif v1 == '03':
                          res.append(i[4:] + '-09-30')
                          else:
                          res.append(i[4:] + '-12-31')

                          #>>> res
                          #['2016-03-31', '2016-06-30', '03-09-30', '04-12-31']






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 19 at 14:42









                          Sotos

                          26.9k51540




                          26.9k51540






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376598%2fconvert-dataframe-to-quarter%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

                              Wiesbaden

                              To store a contact into the json file from server.js file using a class in NodeJS

                              Marschland