Output the distance in time in NEGATIVE number to create sequence












0















Need a little help for a project.



I have two lists each containing 4 or more specific weeks -
True_SH:



[Timestamp('2012-01-08 00:00:00'), Timestamp('2012-04-22 00:00:00'), Timestamp('2012-08-19 00:00:00'), Timestamp('2012-10-07 00:00:00')


True_SL:



[Timestamp('2011-11-20 00:00:00'), Timestamp('2012-03-25 00:00:00'), Timestamp('2012-05-13 00:00:00'), Timestamp('2012-09-02 00:00:00')


They alternate and it is not possible to have two SL or SH one after the other, in this example:



SL->SH->SL->SH->SL->SH->SL->SH


How to output the distance in weeks between the dates in a third list BUT with the following rule - the distance from SL to SH produces the positive number and the distance from SH to SL outputs the negative number of weeks? The number should include the start and end week in the count.



In this example distance from first two:
(1st)SL('2011-11-20 00:00:00') to (2nd) SH('2012-01-08 00:00:00') outputs positive 7, -> distance from 2nd(SH) to (3rd) SL('2012-03-25 00:00:00') outputs NEGATIVE -10 , then from 3rd(SL) to 4th(SH) outputs a positive number again and then from SH to SL negative and so on.



I need this list to further analyze if there are repeating sequences.



Really hope anyone can help me as I am struggling to find a way to do that.



Thanks!










share|improve this question

























  • Can you show us code you've already tried? Maybe a block of input data and a block of output data?

    – Tvde1
    Nov 14 '18 at 8:25
















0















Need a little help for a project.



I have two lists each containing 4 or more specific weeks -
True_SH:



[Timestamp('2012-01-08 00:00:00'), Timestamp('2012-04-22 00:00:00'), Timestamp('2012-08-19 00:00:00'), Timestamp('2012-10-07 00:00:00')


True_SL:



[Timestamp('2011-11-20 00:00:00'), Timestamp('2012-03-25 00:00:00'), Timestamp('2012-05-13 00:00:00'), Timestamp('2012-09-02 00:00:00')


They alternate and it is not possible to have two SL or SH one after the other, in this example:



SL->SH->SL->SH->SL->SH->SL->SH


How to output the distance in weeks between the dates in a third list BUT with the following rule - the distance from SL to SH produces the positive number and the distance from SH to SL outputs the negative number of weeks? The number should include the start and end week in the count.



In this example distance from first two:
(1st)SL('2011-11-20 00:00:00') to (2nd) SH('2012-01-08 00:00:00') outputs positive 7, -> distance from 2nd(SH) to (3rd) SL('2012-03-25 00:00:00') outputs NEGATIVE -10 , then from 3rd(SL) to 4th(SH) outputs a positive number again and then from SH to SL negative and so on.



I need this list to further analyze if there are repeating sequences.



Really hope anyone can help me as I am struggling to find a way to do that.



Thanks!










share|improve this question

























  • Can you show us code you've already tried? Maybe a block of input data and a block of output data?

    – Tvde1
    Nov 14 '18 at 8:25














0












0








0


1






Need a little help for a project.



I have two lists each containing 4 or more specific weeks -
True_SH:



[Timestamp('2012-01-08 00:00:00'), Timestamp('2012-04-22 00:00:00'), Timestamp('2012-08-19 00:00:00'), Timestamp('2012-10-07 00:00:00')


True_SL:



[Timestamp('2011-11-20 00:00:00'), Timestamp('2012-03-25 00:00:00'), Timestamp('2012-05-13 00:00:00'), Timestamp('2012-09-02 00:00:00')


They alternate and it is not possible to have two SL or SH one after the other, in this example:



SL->SH->SL->SH->SL->SH->SL->SH


How to output the distance in weeks between the dates in a third list BUT with the following rule - the distance from SL to SH produces the positive number and the distance from SH to SL outputs the negative number of weeks? The number should include the start and end week in the count.



In this example distance from first two:
(1st)SL('2011-11-20 00:00:00') to (2nd) SH('2012-01-08 00:00:00') outputs positive 7, -> distance from 2nd(SH) to (3rd) SL('2012-03-25 00:00:00') outputs NEGATIVE -10 , then from 3rd(SL) to 4th(SH) outputs a positive number again and then from SH to SL negative and so on.



I need this list to further analyze if there are repeating sequences.



Really hope anyone can help me as I am struggling to find a way to do that.



Thanks!










share|improve this question
















Need a little help for a project.



I have two lists each containing 4 or more specific weeks -
True_SH:



[Timestamp('2012-01-08 00:00:00'), Timestamp('2012-04-22 00:00:00'), Timestamp('2012-08-19 00:00:00'), Timestamp('2012-10-07 00:00:00')


True_SL:



[Timestamp('2011-11-20 00:00:00'), Timestamp('2012-03-25 00:00:00'), Timestamp('2012-05-13 00:00:00'), Timestamp('2012-09-02 00:00:00')


They alternate and it is not possible to have two SL or SH one after the other, in this example:



SL->SH->SL->SH->SL->SH->SL->SH


How to output the distance in weeks between the dates in a third list BUT with the following rule - the distance from SL to SH produces the positive number and the distance from SH to SL outputs the negative number of weeks? The number should include the start and end week in the count.



In this example distance from first two:
(1st)SL('2011-11-20 00:00:00') to (2nd) SH('2012-01-08 00:00:00') outputs positive 7, -> distance from 2nd(SH) to (3rd) SL('2012-03-25 00:00:00') outputs NEGATIVE -10 , then from 3rd(SL) to 4th(SH) outputs a positive number again and then from SH to SL negative and so on.



I need this list to further analyze if there are repeating sequences.



Really hope anyone can help me as I am struggling to find a way to do that.



Thanks!







python date datetime time time-series






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 8:58









Sabir Al Fateh

96121021




96121021










asked Nov 14 '18 at 8:24









DeadCat BounceDeadCat Bounce

31




31













  • Can you show us code you've already tried? Maybe a block of input data and a block of output data?

    – Tvde1
    Nov 14 '18 at 8:25



















  • Can you show us code you've already tried? Maybe a block of input data and a block of output data?

    – Tvde1
    Nov 14 '18 at 8:25

















Can you show us code you've already tried? Maybe a block of input data and a block of output data?

– Tvde1
Nov 14 '18 at 8:25





Can you show us code you've already tried? Maybe a block of input data and a block of output data?

– Tvde1
Nov 14 '18 at 8:25












1 Answer
1






active

oldest

votes


















1














My approach is to merge the two arrays, sort the merged array, then iterate over the merged array to calculate the number of weeks between neighbouring dates. You can then test the larger of the two neighbouring dates to see which of the original lists it came from, and return a negative value if it came from the true_sl list.



Note that my choice to sort the merged lists assumes that the dates in the true_sh and true_sl lists will always interleave.



Here's the code:



from datetime import datetime


true_sh = [datetime(2012, 1, 8), datetime(2012, 4, 22), datetime(2012, 8, 19), datetime(2012, 10, 7)]
true_sl = [datetime(2011, 11, 20), datetime(2012, 3, 25), datetime(2012, 5, 13), datetime(2012, 9, 2)]

out_array = true_sh
out_array.extend(true_sl)
out_array.sort()

for i, elem in enumerate(out_array):
if i == 0:
continue

num_days = elem - out_array[i - 1]
num_weeks = num_days.days // 7
if elem in true_sl:
num_weeks = -num_weeks

print(num_weeks)


And here's the output:



7
-11
4
-3
14
-2
5


Note also that you gave the expected 2nd output as -10, but there are 77 days between 8th Jan 2012 and 25th Mar 2012, so I believe that the output here of -11 is correct. Can you confirm, please?






share|improve this answer
























  • Did not think of this approach. Absolutely brilliant ! It works perfectly. Yes , indeed 77 days is indeed 11 weeks and not 10, the count was started from a specific day in the week and this is why it showed up 10. Thank you so much.

    – DeadCat Bounce
    Nov 14 '18 at 10:38











  • You're welcome. I'm glad it works for you. Don't forget to mark the answer as the accepted solution if you're happy that it meets your needs.

    – DatHydroGuy
    Nov 14 '18 at 11:19











  • Marked ! Thank you again , Sir !

    – DeadCat Bounce
    Nov 14 '18 at 18:06











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%2f53295783%2foutput-the-distance-in-time-in-negative-number-to-create-sequence%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














My approach is to merge the two arrays, sort the merged array, then iterate over the merged array to calculate the number of weeks between neighbouring dates. You can then test the larger of the two neighbouring dates to see which of the original lists it came from, and return a negative value if it came from the true_sl list.



Note that my choice to sort the merged lists assumes that the dates in the true_sh and true_sl lists will always interleave.



Here's the code:



from datetime import datetime


true_sh = [datetime(2012, 1, 8), datetime(2012, 4, 22), datetime(2012, 8, 19), datetime(2012, 10, 7)]
true_sl = [datetime(2011, 11, 20), datetime(2012, 3, 25), datetime(2012, 5, 13), datetime(2012, 9, 2)]

out_array = true_sh
out_array.extend(true_sl)
out_array.sort()

for i, elem in enumerate(out_array):
if i == 0:
continue

num_days = elem - out_array[i - 1]
num_weeks = num_days.days // 7
if elem in true_sl:
num_weeks = -num_weeks

print(num_weeks)


And here's the output:



7
-11
4
-3
14
-2
5


Note also that you gave the expected 2nd output as -10, but there are 77 days between 8th Jan 2012 and 25th Mar 2012, so I believe that the output here of -11 is correct. Can you confirm, please?






share|improve this answer
























  • Did not think of this approach. Absolutely brilliant ! It works perfectly. Yes , indeed 77 days is indeed 11 weeks and not 10, the count was started from a specific day in the week and this is why it showed up 10. Thank you so much.

    – DeadCat Bounce
    Nov 14 '18 at 10:38











  • You're welcome. I'm glad it works for you. Don't forget to mark the answer as the accepted solution if you're happy that it meets your needs.

    – DatHydroGuy
    Nov 14 '18 at 11:19











  • Marked ! Thank you again , Sir !

    – DeadCat Bounce
    Nov 14 '18 at 18:06
















1














My approach is to merge the two arrays, sort the merged array, then iterate over the merged array to calculate the number of weeks between neighbouring dates. You can then test the larger of the two neighbouring dates to see which of the original lists it came from, and return a negative value if it came from the true_sl list.



Note that my choice to sort the merged lists assumes that the dates in the true_sh and true_sl lists will always interleave.



Here's the code:



from datetime import datetime


true_sh = [datetime(2012, 1, 8), datetime(2012, 4, 22), datetime(2012, 8, 19), datetime(2012, 10, 7)]
true_sl = [datetime(2011, 11, 20), datetime(2012, 3, 25), datetime(2012, 5, 13), datetime(2012, 9, 2)]

out_array = true_sh
out_array.extend(true_sl)
out_array.sort()

for i, elem in enumerate(out_array):
if i == 0:
continue

num_days = elem - out_array[i - 1]
num_weeks = num_days.days // 7
if elem in true_sl:
num_weeks = -num_weeks

print(num_weeks)


And here's the output:



7
-11
4
-3
14
-2
5


Note also that you gave the expected 2nd output as -10, but there are 77 days between 8th Jan 2012 and 25th Mar 2012, so I believe that the output here of -11 is correct. Can you confirm, please?






share|improve this answer
























  • Did not think of this approach. Absolutely brilliant ! It works perfectly. Yes , indeed 77 days is indeed 11 weeks and not 10, the count was started from a specific day in the week and this is why it showed up 10. Thank you so much.

    – DeadCat Bounce
    Nov 14 '18 at 10:38











  • You're welcome. I'm glad it works for you. Don't forget to mark the answer as the accepted solution if you're happy that it meets your needs.

    – DatHydroGuy
    Nov 14 '18 at 11:19











  • Marked ! Thank you again , Sir !

    – DeadCat Bounce
    Nov 14 '18 at 18:06














1












1








1







My approach is to merge the two arrays, sort the merged array, then iterate over the merged array to calculate the number of weeks between neighbouring dates. You can then test the larger of the two neighbouring dates to see which of the original lists it came from, and return a negative value if it came from the true_sl list.



Note that my choice to sort the merged lists assumes that the dates in the true_sh and true_sl lists will always interleave.



Here's the code:



from datetime import datetime


true_sh = [datetime(2012, 1, 8), datetime(2012, 4, 22), datetime(2012, 8, 19), datetime(2012, 10, 7)]
true_sl = [datetime(2011, 11, 20), datetime(2012, 3, 25), datetime(2012, 5, 13), datetime(2012, 9, 2)]

out_array = true_sh
out_array.extend(true_sl)
out_array.sort()

for i, elem in enumerate(out_array):
if i == 0:
continue

num_days = elem - out_array[i - 1]
num_weeks = num_days.days // 7
if elem in true_sl:
num_weeks = -num_weeks

print(num_weeks)


And here's the output:



7
-11
4
-3
14
-2
5


Note also that you gave the expected 2nd output as -10, but there are 77 days between 8th Jan 2012 and 25th Mar 2012, so I believe that the output here of -11 is correct. Can you confirm, please?






share|improve this answer













My approach is to merge the two arrays, sort the merged array, then iterate over the merged array to calculate the number of weeks between neighbouring dates. You can then test the larger of the two neighbouring dates to see which of the original lists it came from, and return a negative value if it came from the true_sl list.



Note that my choice to sort the merged lists assumes that the dates in the true_sh and true_sl lists will always interleave.



Here's the code:



from datetime import datetime


true_sh = [datetime(2012, 1, 8), datetime(2012, 4, 22), datetime(2012, 8, 19), datetime(2012, 10, 7)]
true_sl = [datetime(2011, 11, 20), datetime(2012, 3, 25), datetime(2012, 5, 13), datetime(2012, 9, 2)]

out_array = true_sh
out_array.extend(true_sl)
out_array.sort()

for i, elem in enumerate(out_array):
if i == 0:
continue

num_days = elem - out_array[i - 1]
num_weeks = num_days.days // 7
if elem in true_sl:
num_weeks = -num_weeks

print(num_weeks)


And here's the output:



7
-11
4
-3
14
-2
5


Note also that you gave the expected 2nd output as -10, but there are 77 days between 8th Jan 2012 and 25th Mar 2012, so I believe that the output here of -11 is correct. Can you confirm, please?







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 10:02









DatHydroGuyDatHydroGuy

6812413




6812413













  • Did not think of this approach. Absolutely brilliant ! It works perfectly. Yes , indeed 77 days is indeed 11 weeks and not 10, the count was started from a specific day in the week and this is why it showed up 10. Thank you so much.

    – DeadCat Bounce
    Nov 14 '18 at 10:38











  • You're welcome. I'm glad it works for you. Don't forget to mark the answer as the accepted solution if you're happy that it meets your needs.

    – DatHydroGuy
    Nov 14 '18 at 11:19











  • Marked ! Thank you again , Sir !

    – DeadCat Bounce
    Nov 14 '18 at 18:06



















  • Did not think of this approach. Absolutely brilliant ! It works perfectly. Yes , indeed 77 days is indeed 11 weeks and not 10, the count was started from a specific day in the week and this is why it showed up 10. Thank you so much.

    – DeadCat Bounce
    Nov 14 '18 at 10:38











  • You're welcome. I'm glad it works for you. Don't forget to mark the answer as the accepted solution if you're happy that it meets your needs.

    – DatHydroGuy
    Nov 14 '18 at 11:19











  • Marked ! Thank you again , Sir !

    – DeadCat Bounce
    Nov 14 '18 at 18:06

















Did not think of this approach. Absolutely brilliant ! It works perfectly. Yes , indeed 77 days is indeed 11 weeks and not 10, the count was started from a specific day in the week and this is why it showed up 10. Thank you so much.

– DeadCat Bounce
Nov 14 '18 at 10:38





Did not think of this approach. Absolutely brilliant ! It works perfectly. Yes , indeed 77 days is indeed 11 weeks and not 10, the count was started from a specific day in the week and this is why it showed up 10. Thank you so much.

– DeadCat Bounce
Nov 14 '18 at 10:38













You're welcome. I'm glad it works for you. Don't forget to mark the answer as the accepted solution if you're happy that it meets your needs.

– DatHydroGuy
Nov 14 '18 at 11:19





You're welcome. I'm glad it works for you. Don't forget to mark the answer as the accepted solution if you're happy that it meets your needs.

– DatHydroGuy
Nov 14 '18 at 11:19













Marked ! Thank you again , Sir !

– DeadCat Bounce
Nov 14 '18 at 18:06





Marked ! Thank you again , Sir !

– DeadCat Bounce
Nov 14 '18 at 18:06


















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%2f53295783%2foutput-the-distance-in-time-in-negative-number-to-create-sequence%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

Bressuire

Vorschmack

Quarantine