Python column datetime subtract datetime












1















understand that there is many similar question out there on subtraction for pandas column, but my scenario is rather unique



I have 2 sets of data which both includes datetime. My program requires me to match this 2 date time therefore I used merge_asof to match to the closest time. After matching the data, I will need to find the time difference between this 2 set of data.



EndTime,Datetime
3/10/2010 0:00:33, 3/10/2010 0:00:26
3/10/2010 0:01:15,
3/10/2010 0:01:30,
3/10/2010 0:02:09, 3/10/2010 0:01:36
3/10/2010 0:02:50,
3/10/2010 0:05:09,
3/10/2010 0:06:00, 3/10/2010 0:05:48


The UNIQUE part will be that I would like to use Datetime subtract 1row before of endtime, (e.g. last row 3/10/2010 0:05:48 - 2nd last row 3/10/2010 0:05:09 = 39seconds)



Expected outcome would be:



EndTime,Datetime,SecondsDiff
3/10/2010 0:00:33, 3/10/2010 0:00:26, *not sure how to compute this but not important for 1st row of data*
3/10/2010 0:01:15,,
3/10/2010 0:01:30,,
3/10/2010 0:02:09, 3/10/2010 0:01:36,6
3/10/2010 0:02:50,,
3/10/2010 0:05:09,,
3/10/2010 0:06:00, 3/10/2010 0:05:48,39


I've tried some methods that caused some error, please advice!



As when I matched data, it cause duplicate so I uses



dm.loc[(dm['Datetime'].notnull())&(dm.duplicated('Datetime')==True),'Datetime'] = ' '


to give empty space that doesnt cause duplicate but it give me "Error parsing datetime string " " at position 1"



-



I've also tried indicating the duplicates as 0 and converting the datetime type but causes the



error ufunc subtract cannot use operands with types dtype('O') anddtype('<M8[ns]')


, therefore I tried the below method but resulting to my datetime column becoming only date and the seconds difference was totally off



dm.loc[(dm['Datetime'].notnull())&(dm.duplicated('Datetime')==True),'Datetime'] = 0
dm['EndTime'] = dm['EndTime'].values.astype('datetime64')
dm['Datetime'] = dm['Datetime'].values.astype('datetime64')
dm['Seconds'] = (dm.Datetime -
dm.EndTime.shift(-1)).astype('timedelta64[s]')









share|improve this question





























    1















    understand that there is many similar question out there on subtraction for pandas column, but my scenario is rather unique



    I have 2 sets of data which both includes datetime. My program requires me to match this 2 date time therefore I used merge_asof to match to the closest time. After matching the data, I will need to find the time difference between this 2 set of data.



    EndTime,Datetime
    3/10/2010 0:00:33, 3/10/2010 0:00:26
    3/10/2010 0:01:15,
    3/10/2010 0:01:30,
    3/10/2010 0:02:09, 3/10/2010 0:01:36
    3/10/2010 0:02:50,
    3/10/2010 0:05:09,
    3/10/2010 0:06:00, 3/10/2010 0:05:48


    The UNIQUE part will be that I would like to use Datetime subtract 1row before of endtime, (e.g. last row 3/10/2010 0:05:48 - 2nd last row 3/10/2010 0:05:09 = 39seconds)



    Expected outcome would be:



    EndTime,Datetime,SecondsDiff
    3/10/2010 0:00:33, 3/10/2010 0:00:26, *not sure how to compute this but not important for 1st row of data*
    3/10/2010 0:01:15,,
    3/10/2010 0:01:30,,
    3/10/2010 0:02:09, 3/10/2010 0:01:36,6
    3/10/2010 0:02:50,,
    3/10/2010 0:05:09,,
    3/10/2010 0:06:00, 3/10/2010 0:05:48,39


    I've tried some methods that caused some error, please advice!



    As when I matched data, it cause duplicate so I uses



    dm.loc[(dm['Datetime'].notnull())&(dm.duplicated('Datetime')==True),'Datetime'] = ' '


    to give empty space that doesnt cause duplicate but it give me "Error parsing datetime string " " at position 1"



    -



    I've also tried indicating the duplicates as 0 and converting the datetime type but causes the



    error ufunc subtract cannot use operands with types dtype('O') anddtype('<M8[ns]')


    , therefore I tried the below method but resulting to my datetime column becoming only date and the seconds difference was totally off



    dm.loc[(dm['Datetime'].notnull())&(dm.duplicated('Datetime')==True),'Datetime'] = 0
    dm['EndTime'] = dm['EndTime'].values.astype('datetime64')
    dm['Datetime'] = dm['Datetime'].values.astype('datetime64')
    dm['Seconds'] = (dm.Datetime -
    dm.EndTime.shift(-1)).astype('timedelta64[s]')









    share|improve this question



























      1












      1








      1








      understand that there is many similar question out there on subtraction for pandas column, but my scenario is rather unique



      I have 2 sets of data which both includes datetime. My program requires me to match this 2 date time therefore I used merge_asof to match to the closest time. After matching the data, I will need to find the time difference between this 2 set of data.



      EndTime,Datetime
      3/10/2010 0:00:33, 3/10/2010 0:00:26
      3/10/2010 0:01:15,
      3/10/2010 0:01:30,
      3/10/2010 0:02:09, 3/10/2010 0:01:36
      3/10/2010 0:02:50,
      3/10/2010 0:05:09,
      3/10/2010 0:06:00, 3/10/2010 0:05:48


      The UNIQUE part will be that I would like to use Datetime subtract 1row before of endtime, (e.g. last row 3/10/2010 0:05:48 - 2nd last row 3/10/2010 0:05:09 = 39seconds)



      Expected outcome would be:



      EndTime,Datetime,SecondsDiff
      3/10/2010 0:00:33, 3/10/2010 0:00:26, *not sure how to compute this but not important for 1st row of data*
      3/10/2010 0:01:15,,
      3/10/2010 0:01:30,,
      3/10/2010 0:02:09, 3/10/2010 0:01:36,6
      3/10/2010 0:02:50,,
      3/10/2010 0:05:09,,
      3/10/2010 0:06:00, 3/10/2010 0:05:48,39


      I've tried some methods that caused some error, please advice!



      As when I matched data, it cause duplicate so I uses



      dm.loc[(dm['Datetime'].notnull())&(dm.duplicated('Datetime')==True),'Datetime'] = ' '


      to give empty space that doesnt cause duplicate but it give me "Error parsing datetime string " " at position 1"



      -



      I've also tried indicating the duplicates as 0 and converting the datetime type but causes the



      error ufunc subtract cannot use operands with types dtype('O') anddtype('<M8[ns]')


      , therefore I tried the below method but resulting to my datetime column becoming only date and the seconds difference was totally off



      dm.loc[(dm['Datetime'].notnull())&(dm.duplicated('Datetime')==True),'Datetime'] = 0
      dm['EndTime'] = dm['EndTime'].values.astype('datetime64')
      dm['Datetime'] = dm['Datetime'].values.astype('datetime64')
      dm['Seconds'] = (dm.Datetime -
      dm.EndTime.shift(-1)).astype('timedelta64[s]')









      share|improve this question
















      understand that there is many similar question out there on subtraction for pandas column, but my scenario is rather unique



      I have 2 sets of data which both includes datetime. My program requires me to match this 2 date time therefore I used merge_asof to match to the closest time. After matching the data, I will need to find the time difference between this 2 set of data.



      EndTime,Datetime
      3/10/2010 0:00:33, 3/10/2010 0:00:26
      3/10/2010 0:01:15,
      3/10/2010 0:01:30,
      3/10/2010 0:02:09, 3/10/2010 0:01:36
      3/10/2010 0:02:50,
      3/10/2010 0:05:09,
      3/10/2010 0:06:00, 3/10/2010 0:05:48


      The UNIQUE part will be that I would like to use Datetime subtract 1row before of endtime, (e.g. last row 3/10/2010 0:05:48 - 2nd last row 3/10/2010 0:05:09 = 39seconds)



      Expected outcome would be:



      EndTime,Datetime,SecondsDiff
      3/10/2010 0:00:33, 3/10/2010 0:00:26, *not sure how to compute this but not important for 1st row of data*
      3/10/2010 0:01:15,,
      3/10/2010 0:01:30,,
      3/10/2010 0:02:09, 3/10/2010 0:01:36,6
      3/10/2010 0:02:50,,
      3/10/2010 0:05:09,,
      3/10/2010 0:06:00, 3/10/2010 0:05:48,39


      I've tried some methods that caused some error, please advice!



      As when I matched data, it cause duplicate so I uses



      dm.loc[(dm['Datetime'].notnull())&(dm.duplicated('Datetime')==True),'Datetime'] = ' '


      to give empty space that doesnt cause duplicate but it give me "Error parsing datetime string " " at position 1"



      -



      I've also tried indicating the duplicates as 0 and converting the datetime type but causes the



      error ufunc subtract cannot use operands with types dtype('O') anddtype('<M8[ns]')


      , therefore I tried the below method but resulting to my datetime column becoming only date and the seconds difference was totally off



      dm.loc[(dm['Datetime'].notnull())&(dm.duplicated('Datetime')==True),'Datetime'] = 0
      dm['EndTime'] = dm['EndTime'].values.astype('datetime64')
      dm['Datetime'] = dm['Datetime'].values.astype('datetime64')
      dm['Seconds'] = (dm.Datetime -
      dm.EndTime.shift(-1)).astype('timedelta64[s]')






      python pandas datetime






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 2:31









      Abhi

      2,540421




      2,540421










      asked Nov 23 '18 at 2:03









      ThanksForHelpingThanksForHelping

      647




      647
























          1 Answer
          1






          active

          oldest

          votes


















          0














          IIUC you can use :



          print (df)

          EndTime Datetime
          0 3/10/2010 0:00:33 3/10/2010 0:00:26
          1 3/10/2010 0:01:15
          2 3/10/2010 0:01:30 NaN
          3 3/10/2010 0:02:09 3/10/2010 0:01:36
          4 3/10/2010 0:02:50 NaN
          5 3/10/2010 0:05:09 NaN
          6 3/10/2010 0:06:00 3/10/2010 0:05:48




          df.Datetime = pd.to_datetime(df.Datetime, errors='coerce')
          df.EndTime = pd.to_datetime(df.EndTime, errors='coerce')

          df['SecondsDiff'] = df.Datetime.sub(df.EndTime.shift()).dt.seconds

          print (df)

          EndTime Datetime SecondsDiff
          0 2010-03-10 00:00:33 2010-03-10 00:00:26 NaN
          1 2010-03-10 00:01:15 NaT NaN
          2 2010-03-10 00:01:30 NaT NaN
          3 2010-03-10 00:02:09 2010-03-10 00:01:36 6.0
          4 2010-03-10 00:02:50 NaT NaN
          5 2010-03-10 00:05:09 NaT NaN
          6 2010-03-10 00:06:00 2010-03-10 00:05:48 39.0





          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%2f53439844%2fpython-column-datetime-subtract-datetime%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









            0














            IIUC you can use :



            print (df)

            EndTime Datetime
            0 3/10/2010 0:00:33 3/10/2010 0:00:26
            1 3/10/2010 0:01:15
            2 3/10/2010 0:01:30 NaN
            3 3/10/2010 0:02:09 3/10/2010 0:01:36
            4 3/10/2010 0:02:50 NaN
            5 3/10/2010 0:05:09 NaN
            6 3/10/2010 0:06:00 3/10/2010 0:05:48




            df.Datetime = pd.to_datetime(df.Datetime, errors='coerce')
            df.EndTime = pd.to_datetime(df.EndTime, errors='coerce')

            df['SecondsDiff'] = df.Datetime.sub(df.EndTime.shift()).dt.seconds

            print (df)

            EndTime Datetime SecondsDiff
            0 2010-03-10 00:00:33 2010-03-10 00:00:26 NaN
            1 2010-03-10 00:01:15 NaT NaN
            2 2010-03-10 00:01:30 NaT NaN
            3 2010-03-10 00:02:09 2010-03-10 00:01:36 6.0
            4 2010-03-10 00:02:50 NaT NaN
            5 2010-03-10 00:05:09 NaT NaN
            6 2010-03-10 00:06:00 2010-03-10 00:05:48 39.0





            share|improve this answer




























              0














              IIUC you can use :



              print (df)

              EndTime Datetime
              0 3/10/2010 0:00:33 3/10/2010 0:00:26
              1 3/10/2010 0:01:15
              2 3/10/2010 0:01:30 NaN
              3 3/10/2010 0:02:09 3/10/2010 0:01:36
              4 3/10/2010 0:02:50 NaN
              5 3/10/2010 0:05:09 NaN
              6 3/10/2010 0:06:00 3/10/2010 0:05:48




              df.Datetime = pd.to_datetime(df.Datetime, errors='coerce')
              df.EndTime = pd.to_datetime(df.EndTime, errors='coerce')

              df['SecondsDiff'] = df.Datetime.sub(df.EndTime.shift()).dt.seconds

              print (df)

              EndTime Datetime SecondsDiff
              0 2010-03-10 00:00:33 2010-03-10 00:00:26 NaN
              1 2010-03-10 00:01:15 NaT NaN
              2 2010-03-10 00:01:30 NaT NaN
              3 2010-03-10 00:02:09 2010-03-10 00:01:36 6.0
              4 2010-03-10 00:02:50 NaT NaN
              5 2010-03-10 00:05:09 NaT NaN
              6 2010-03-10 00:06:00 2010-03-10 00:05:48 39.0





              share|improve this answer


























                0












                0








                0







                IIUC you can use :



                print (df)

                EndTime Datetime
                0 3/10/2010 0:00:33 3/10/2010 0:00:26
                1 3/10/2010 0:01:15
                2 3/10/2010 0:01:30 NaN
                3 3/10/2010 0:02:09 3/10/2010 0:01:36
                4 3/10/2010 0:02:50 NaN
                5 3/10/2010 0:05:09 NaN
                6 3/10/2010 0:06:00 3/10/2010 0:05:48




                df.Datetime = pd.to_datetime(df.Datetime, errors='coerce')
                df.EndTime = pd.to_datetime(df.EndTime, errors='coerce')

                df['SecondsDiff'] = df.Datetime.sub(df.EndTime.shift()).dt.seconds

                print (df)

                EndTime Datetime SecondsDiff
                0 2010-03-10 00:00:33 2010-03-10 00:00:26 NaN
                1 2010-03-10 00:01:15 NaT NaN
                2 2010-03-10 00:01:30 NaT NaN
                3 2010-03-10 00:02:09 2010-03-10 00:01:36 6.0
                4 2010-03-10 00:02:50 NaT NaN
                5 2010-03-10 00:05:09 NaT NaN
                6 2010-03-10 00:06:00 2010-03-10 00:05:48 39.0





                share|improve this answer













                IIUC you can use :



                print (df)

                EndTime Datetime
                0 3/10/2010 0:00:33 3/10/2010 0:00:26
                1 3/10/2010 0:01:15
                2 3/10/2010 0:01:30 NaN
                3 3/10/2010 0:02:09 3/10/2010 0:01:36
                4 3/10/2010 0:02:50 NaN
                5 3/10/2010 0:05:09 NaN
                6 3/10/2010 0:06:00 3/10/2010 0:05:48




                df.Datetime = pd.to_datetime(df.Datetime, errors='coerce')
                df.EndTime = pd.to_datetime(df.EndTime, errors='coerce')

                df['SecondsDiff'] = df.Datetime.sub(df.EndTime.shift()).dt.seconds

                print (df)

                EndTime Datetime SecondsDiff
                0 2010-03-10 00:00:33 2010-03-10 00:00:26 NaN
                1 2010-03-10 00:01:15 NaT NaN
                2 2010-03-10 00:01:30 NaT NaN
                3 2010-03-10 00:02:09 2010-03-10 00:01:36 6.0
                4 2010-03-10 00:02:50 NaT NaN
                5 2010-03-10 00:05:09 NaT NaN
                6 2010-03-10 00:06:00 2010-03-10 00:05:48 39.0






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 2:27









                AbhiAbhi

                2,540421




                2,540421
































                    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%2f53439844%2fpython-column-datetime-subtract-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

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    RAC Tourist Trophy