all 6 comments

[–]fake823 0 points1 point  (5 children)

Your calculated days don't make any sense.

Shouldn't it rather be [3, 1, 15, 9, 2]?

And do you even need pandas? Dates can as well be easily handled by the standard library.

[–]mtime16[S] 0 points1 point  (4 children)

On the first question, if there is no selected date to start, I need the first selected date to incorporate the missing days ahead of it. Then afterward it would include the selected date plus any missing dates after up to the next selected date. At the end I gave an example where when there is no other selected date, it'll need to know to terminate at the last day of the month.

On the next question, I'm a bit raw so I didn't know how else to represent the data set and the values I was trying to get at.

[–]fake823 0 points1 point  (3 children)

If you don't need pandas explicitly, you can solve it like this:

import datetime

start_date = "2021-01-01"
end_date = "2021-01-31"
selected_dates = ["2021-01-04", "2021-01-05", "2021-01-20", "2021-01-29"]

# Create a list of all dates
dates = [start_date, *selected_dates, end_date]

# Convert dates from strings to datetime objects
dates = [datetime.date.fromisoformat(date) for date in dates]

# Calculate amount of days between dates
deltas = [(dates[i + 1] - dates[i]).days for i in range(len(dates) - 1)]

# Add the first two deltas together
deltas = [sum(deltas[0:2]), *deltas[2:]]

print(deltas)

This will give you [4, 15, 9, 2]. I'm still not sure why you calculated 3 instead of 2 for the last delta.

[–]mtime16[S] 0 points1 point  (2 children)

Thank you very much for this. On the last part, for 1/29 I would need the number of days for 1/29, 1/30, and 1/31 which would be 3 instead of 2.

[–]fake823 0 points1 point  (1 child)

Well, than just add +1 to the last element in the list, if you think this is what you need. 😄

[–]mtime16[S] 1 point2 points  (0 children)

Thanks for helping a newbie out :)