How to set missing Year, Month and Day for Panda's Datetime











up vote
1
down vote

favorite












I have timestamp data in the format %H:%M:%S:%f. When I read it and pass it into a dataframe with pd.to_datetime it will automatically fill in 01/01/1900 in addition to the properly passed %H:%M:%S:%f
How can I set it to a specific year, month, day while reading it and passing it the hour, minute, second, millisecond data?



Code I'm using:



import pandas as pd
df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S:%f')
print(df)


Output:



                     Time
0 1900-01-01 00:00:00.001
1 1900-01-01 00:00:00.005
2 1900-01-01 00:00:00.009


Desired output:



A set date, so for instance:



                     Time
0 2018-11-21 00:00:00.001
1 2018-11-21 00:00:00.005
2 2018-11-21 00:00:00.009









share|improve this question




























    up vote
    1
    down vote

    favorite












    I have timestamp data in the format %H:%M:%S:%f. When I read it and pass it into a dataframe with pd.to_datetime it will automatically fill in 01/01/1900 in addition to the properly passed %H:%M:%S:%f
    How can I set it to a specific year, month, day while reading it and passing it the hour, minute, second, millisecond data?



    Code I'm using:



    import pandas as pd
    df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
    df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S:%f')
    print(df)


    Output:



                         Time
    0 1900-01-01 00:00:00.001
    1 1900-01-01 00:00:00.005
    2 1900-01-01 00:00:00.009


    Desired output:



    A set date, so for instance:



                         Time
    0 2018-11-21 00:00:00.001
    1 2018-11-21 00:00:00.005
    2 2018-11-21 00:00:00.009









    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have timestamp data in the format %H:%M:%S:%f. When I read it and pass it into a dataframe with pd.to_datetime it will automatically fill in 01/01/1900 in addition to the properly passed %H:%M:%S:%f
      How can I set it to a specific year, month, day while reading it and passing it the hour, minute, second, millisecond data?



      Code I'm using:



      import pandas as pd
      df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
      df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S:%f')
      print(df)


      Output:



                           Time
      0 1900-01-01 00:00:00.001
      1 1900-01-01 00:00:00.005
      2 1900-01-01 00:00:00.009


      Desired output:



      A set date, so for instance:



                           Time
      0 2018-11-21 00:00:00.001
      1 2018-11-21 00:00:00.005
      2 2018-11-21 00:00:00.009









      share|improve this question















      I have timestamp data in the format %H:%M:%S:%f. When I read it and pass it into a dataframe with pd.to_datetime it will automatically fill in 01/01/1900 in addition to the properly passed %H:%M:%S:%f
      How can I set it to a specific year, month, day while reading it and passing it the hour, minute, second, millisecond data?



      Code I'm using:



      import pandas as pd
      df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
      df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S:%f')
      print(df)


      Output:



                           Time
      0 1900-01-01 00:00:00.001
      1 1900-01-01 00:00:00.005
      2 1900-01-01 00:00:00.009


      Desired output:



      A set date, so for instance:



                           Time
      0 2018-11-21 00:00:00.001
      1 2018-11-21 00:00:00.005
      2 2018-11-21 00:00:00.009






      python pandas datetime






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 20:16









      Ken Dekalb

      14411




      14411










      asked Nov 21 at 20:11









      Chrisvdberge

      5211822




      5211822
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          There's a few ways you could do this, perhaps the easiest (at least that springs to my mind) is to append the string value of the date you want and then convert to datetime afterwards



          import pandas as pd
          df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
          df["Time"] = "2018-11-21 "+ df["Time"]
          df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
          print(df)





          share|improve this answer





















          • doh! seems simple enough, thx!
            – Chrisvdberge
            Nov 21 at 20:27


















          up vote
          0
          down vote













          df['Time']=df['Time'].apply(lambda dt: dt.replace(year=2018, day=21, month=11)) 





          share|improve this answer



















          • 2




            why not do all that in one application of replace; e.g: dt.replace(2018, 11, 21)?
            – Sam Mason
            Nov 21 at 23:02











          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%2f53419798%2fhow-to-set-missing-year-month-and-day-for-pandas-datetime%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
          3
          down vote



          accepted










          There's a few ways you could do this, perhaps the easiest (at least that springs to my mind) is to append the string value of the date you want and then convert to datetime afterwards



          import pandas as pd
          df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
          df["Time"] = "2018-11-21 "+ df["Time"]
          df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
          print(df)





          share|improve this answer





















          • doh! seems simple enough, thx!
            – Chrisvdberge
            Nov 21 at 20:27















          up vote
          3
          down vote



          accepted










          There's a few ways you could do this, perhaps the easiest (at least that springs to my mind) is to append the string value of the date you want and then convert to datetime afterwards



          import pandas as pd
          df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
          df["Time"] = "2018-11-21 "+ df["Time"]
          df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
          print(df)





          share|improve this answer





















          • doh! seems simple enough, thx!
            – Chrisvdberge
            Nov 21 at 20:27













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          There's a few ways you could do this, perhaps the easiest (at least that springs to my mind) is to append the string value of the date you want and then convert to datetime afterwards



          import pandas as pd
          df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
          df["Time"] = "2018-11-21 "+ df["Time"]
          df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
          print(df)





          share|improve this answer












          There's a few ways you could do this, perhaps the easiest (at least that springs to my mind) is to append the string value of the date you want and then convert to datetime afterwards



          import pandas as pd
          df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
          df["Time"] = "2018-11-21 "+ df["Time"]
          df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
          print(df)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 20:18









          Sven Harris

          1,6871211




          1,6871211












          • doh! seems simple enough, thx!
            – Chrisvdberge
            Nov 21 at 20:27


















          • doh! seems simple enough, thx!
            – Chrisvdberge
            Nov 21 at 20:27
















          doh! seems simple enough, thx!
          – Chrisvdberge
          Nov 21 at 20:27




          doh! seems simple enough, thx!
          – Chrisvdberge
          Nov 21 at 20:27












          up vote
          0
          down vote













          df['Time']=df['Time'].apply(lambda dt: dt.replace(year=2018, day=21, month=11)) 





          share|improve this answer



















          • 2




            why not do all that in one application of replace; e.g: dt.replace(2018, 11, 21)?
            – Sam Mason
            Nov 21 at 23:02















          up vote
          0
          down vote













          df['Time']=df['Time'].apply(lambda dt: dt.replace(year=2018, day=21, month=11)) 





          share|improve this answer



















          • 2




            why not do all that in one application of replace; e.g: dt.replace(2018, 11, 21)?
            – Sam Mason
            Nov 21 at 23:02













          up vote
          0
          down vote










          up vote
          0
          down vote









          df['Time']=df['Time'].apply(lambda dt: dt.replace(year=2018, day=21, month=11)) 





          share|improve this answer














          df['Time']=df['Time'].apply(lambda dt: dt.replace(year=2018, day=21, month=11)) 






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 at 11:39

























          answered Nov 21 at 20:17









          Tom Ron

          1,099619




          1,099619








          • 2




            why not do all that in one application of replace; e.g: dt.replace(2018, 11, 21)?
            – Sam Mason
            Nov 21 at 23:02














          • 2




            why not do all that in one application of replace; e.g: dt.replace(2018, 11, 21)?
            – Sam Mason
            Nov 21 at 23:02








          2




          2




          why not do all that in one application of replace; e.g: dt.replace(2018, 11, 21)?
          – Sam Mason
          Nov 21 at 23:02




          why not do all that in one application of replace; e.g: dt.replace(2018, 11, 21)?
          – Sam Mason
          Nov 21 at 23:02


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419798%2fhow-to-set-missing-year-month-and-day-for-pandas-datetime%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

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          Alexandru Averescu

          Trompette piccolo