Fill list of start/end dates with dates in between - python

Given a list with the following structure :
[start_date_cycle1, end_date_cycle1, start_date_cycle2, end_date_cycle2, ..., end_date_cycleK]
where all elements are timestamps, I would like to get the following
[[start_date_cycle1, start_date_cycle1 +1d, start_date_cycle1 +2d, ..., end_date_cycle1],
[start_date_cycle2, start_date_cycle2 +1d ...]]
So if the input is ['10-23-2019', '10-26-2019' , '11-02-2019', '11-06-2019'], the output would be :
[['10-23-2019', '10-24-2019','10-25-2019', '10-26-2019'] ,
['11-02-2019', '11-03-2019','11-04-2019','11-05-2019','11-06-2019']]
P.S : The length of the list will always be an even number (so no start of cycle without end).

You can use timedelta from datetime module to iterate from start to end date, as below
from datetime import datetime as dt, timedelta as td
strp,strf,fmt=dt.strptime,dt.strftime,"%m-%d-%Y"
a=['10-23-2019', '10-26-2019' , '11-02-2019', '11-06-2019']
print([[strf(k,fmt) for k in (strp(i,fmt)+td(days=n) for n in range((strp(j,fmt)-strp(i,fmt)).days+1))] for i,j in zip(a[::2],a[1::2])])
Output
[['10-23-2019', '10-24-2019', '10-25-2019', '10-26-2019'], ['11-02-2019', '11-03-2019', '11-04-2019', '11-05-2019', '11-06-2019']]

You can parse the strings as datetime objects, use them to do the necessary calculations (by adding timedelta objects), and then convert back to strings at the end.
To produce the output as a nested list, as specified in the question, the temporary variable dates_out is used to generate the inner lists, which are appended to the main list (out) inside the loop.
If you want to use day-month-year ordering, change the '%m-%d-%Y' to '%d-%m-%Y' in the one place where it occurs.
import datetime
dates = ['10-23-2019', '10-26-2019' , '11-02-2019', '11-06-2019']
format = '%m-%d-%Y'
dts = [datetime.datetime.strptime(date, format) for date in dates]
out = []
i = iter(dts)
for start, end in zip(i, i):
dt = start
dates_out = []
while dt <= end:
dates_out.append(datetime.datetime.strftime(dt, format))
dt += datetime.timedelta(days=1)
out.append(dates_out)
print(out)
This gives:
[['10-23-2019', '10-24-2019', '10-25-2019', '10-26-2019'],
['11-02-2019', '11-03-2019', '11-04-2019', '11-05-2019', '11-06-2019']]
(newline inserted for readability)

This is one approach using datetime module
Ex:
import datetime
data = ['10-23-2019', '10-26-2019' , '11-02-2019', '11-06-2019']
result = []
for s, e in zip(data[::2], data[1::2]): # (10-23-2019, 10-26-2019)....
s = datetime.datetime.strptime(s, "%m-%d-%Y")
e = datetime.datetime.strptime(e, "%m-%d-%Y")
temp = []
while s <= e:
temp.append(s.strftime("%m-%d-%Y"))
s += datetime.timedelta(days=1)
if temp:
result.append(temp)
print(result)
Output:
[['10-23-2019', '10-24-2019', '10-25-2019', '10-26-2019'],
['11-02-2019', '11-03-2019', '11-04-2019', '11-05-2019', '11-06-2019']]

You can do this easily using dateutil module. You can install it by doing pip install python-dateutil.
map(parse, lst) would convert the dates from string to datetime obj; zip(*[map(parse, lst)]*2) would create pairs of datetime objs, so that you can navigate them as (start,end) pairs. And finally rrule(freq=DAILY, dtstart=start, until=end) creates a range of datetime objs from start to end
>>> from dateutil.rrule import rrule, DAILY
>>> from dateutil.parser import parse
>>>
>>> lst = ['10-23-2019', '10-26-2019' , '11-02-2019', '11-06-2019']
>>> res = [[dt.strftime('%m-%d-%Y') for dt in rrule(freq=DAILY, dtstart=start, until=end)] for start,end in zip(*[map(parse, lst)]*2)]
>>>
>>> print(res)
[['10-23-2019', '10-24-2019', '10-25-2019', '10-26-2019'],
['11-02-2019', '11-03-2019', '11-04-2019', '11-05-2019', '11-06-2019']]

Related

Concatenate and convert string to date in Python

I have a string ‘2022.10.31’ and I want to convert it to ‘2022-10-31’ and then to date.
This is the R code:
pr1$dotvoranje <- paste(substr(pr1$dotvoranje, 1, 4), substr(pr1$dotvoranje, 6, 7), substr(pr1$dotvoranje, 9, 10), sep = "-")
pr1$dotvoranje <- as.Date(pr1$dotvoranje)
I need to do the following code in Python I found that I need to use .join() , but I have a column with strings that I need to convert to dates.
I started with this code (but I do not know how to use .join here). But this line only substracted the first four rows of that column. And I need to take that column and replace "."with "-".
depoziti['ddospevanje'] = depoziti['ddospevanje'].loc[0:4] + depoziti['ddospevanje'].loc[5:7] + depoziti['ddospevanje'].loc[8:10]
I'm going to assume that by "then to date" you mean you want a datetime object.
It is not necessary to do string.replace() in order to create the datetime object.
The datetime object uses syntax like %Y to refer to a year and so we will be using that with the function strptime() to ingest the date.
import datetime
date_as_string = "2022.10.31"
date_format = "%Y.%m.%d"
date = datetime.strptime(date_as_string, date_format)
Now you have a date time object.
If you want to then print out the date as a string, you can use strftime()
new_format = "%Y-%m-%d"
date_as_new_string = date.strftime(new_format)
print(date_as_new_string)
Further reference:
https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime
https://www.w3schools.com/python/python_datetime.asp -(This also contains a list of all the syntax for use with strftime() or strptime())
if you are getting your dates (as strings) in a list, then just put the above code into a for loop:
import datetime
date_format = "%Y.%m.%d"
date_as_string_list = ['2022.12.31', '2022.11.30']
date_list = []
for date_as_string in date_as_string_list:
date_list.append(datetime.strptime(date_as_string, date_format))
now you have a list of datetime objects, and you can loop through them in order to get them as strings as already shown.
Just putting it together
from datetime import datetime
dt1 = '2022.10.31'
dt2 = datetime.strptime(dt1, '%Y.%m.%d').date()
print ("type(dt2) :", type(dt2))
print ("dt2 :", dt2)
Output
type(dt2) : <class 'datetime.datetime'>
dt2 : 2022-10-31
Update : dt1 should be a series of string dates, not only one string... dt = ['2022.12.31', '2022.11.30'.....]
If it's a list, use list comprehension
dt1 = ['2022.10.31', '2022.10.1', '2022.9.1']
dt2 = [datetime.strptime(i, '%Y.%m.%d').date() for i in dt1]
dt2
If it's a column in pandas dataframe, this is one way of doing it
df = pd.DataFrame({'event' : ['x', 'y', 'z'],
'date' : ['2022.10.31', '2022.10.1', '2022.9.1']})
df['date1'] = df['date'].apply(lambda x : datetime.strptime(x, '%Y.%m.%d').date())
df

How to use parser on multiple time objects

I have an list:
list = ['2022-06-01', '2022-02-02']
Now am using parser to convert this to python date object. like this,
from dateutil import parser
def to_date(value):
for data in value:
return parser.parse(data)
Above one gives just one output, but i need the output for both the times and also need to convert that to an string like this:
From : June 1, 2022 | To: Feb. 28, 2022
Is that possible with parser ?
You only get one value back from your to_date function because you exit the function in the first loop iteration. You need to introduce an list storing your parsed dates temporary:
from dateutil import parser
def to_date(date_list):
parsed_date_list = []
for date in date_list:
parsed_date_list.append(parser.parse(date))
return parsed_date_list
date_list = ['2022-06-01', '2022-02-02']
res = to_date(date_list)
Or using a list comprehension to keep your code more concise:
from dateutil import parser
def to_date(date_list):
return [parser.parse(date) for date in date_list]
date_list = ['2022-06-01', '2022-02-02']
res = to_date(date_list)
And to format your string, simply use the strftime function as pointed out by kpie
in his comment:
# res = to_date(date_list)
date_format = "%b %d, %Y"
print(f"From: {res[0].strftime(date_format)} | To: {res[1].strftime(date_format)}")
Do not use list as a variable name. list is a data structure and therefore already in use by the class list.
You can use the standard datetime library to perform the parsing.
datetime.datetime.strptime allows you to convert a datetime string to a datetime object.
datetime.datetime.strftime allows you to convert a datetime object to the desired string.
dt_from, dt_to = [datetime.datetime.strptime(x, "%Y-%m-%d") for x in dt_list]
dt_str = f"From : {datetime.datetime.strftime('%b %d,%Y', dt_from)} | To: {datetime.datetime.strftime('%b %d,%Y', dt_to)}"]

How to get timedelta in seconds from a string? Example: +414 00:45:41.004000

I have data of timedeltas which looks like this:
time_delta = '+414 00:45:41.004000'
So, these values are strings and they are of the format ddd hh:mm:ss.f. I now want to get this deltas to seconds. I tried to use .total_seconds() but it did not work.
How could I achieve what I am trying to do?
If you always assume the same input format, you can build a function as below (result to be checked with a simple case) :
import datetime as dt
def parseTimeDelta(time_delta_str):
splitted = time_delta_str.split(' ')
day_part = int(splitted[0][1:])
time_part = dt.datetime.strptime(splitted[1], "%H:%M:%S.%f")
delta = dt.timedelta(days=day_part, hours=time_part.hour, minutes=time_part.minute, seconds=time_part.second,microseconds=time_part.microsecond)
return delta.total_seconds()
time_delta = '+414 00:45:41.004000'
parseTimeDelta(time_delta)
can do this with pandas library
import pandas as pd
# Create the Timedelta object
td = pd.Timedelta('3 days 06:05:01.000000111')
print(td)
print(td.seconds)
Unfortunately we can't create a timedelta with a formatted string directly, but we can get a similar effect with regex then unpack parsed values into a timedelta.
import re
import datetime
# Create parser for your time format with named groups that match timedelta kwargs
time_parser = re.compile(r"\+(?P<days>\d+)\s+(?P<hours>\d{2}):(?P<minutes>\d{2}):(?P<seconds>\d{2})\.(?P<microseconds>\d+)")
# Get the values from your example string
regex_match = time_parser.match("+414 00:45:41.004000")
time_dict = regex_match.groupdict()
# Convert the time values to integers from strings
timedelta_kwargs = {k: int(v) for k, v in time_dict.items()}
# Make a time delta object
delta = datetime.timedelta(**timedelta_kwargs)
# Get total seconds
delta_in_seconds = delta.total_seconds()
Organise that into some functions and you'll get the functionality you're looking for with standard python packages.

What is the quickest way to increment date string YYYY-MM-DD in Python?

In Pandas, I am using dates with string format YYYY-MM-DD
What is the quickest way to increment the date with the result in YYYY-MM-DD format?
d1 = '2018-02-10'
I want to increment it by 1 and get the result back as a string:
d1_inc = '2018-02-11'
Pure Python
You can use the datetime module, part of the standard library. There are 3 steps:
Convert string to datetime object via strptime.
Add a day via timedelta.
Convert resulting datetime object back to string via strftime.
Here's a demo:
from datetime import datetime, timedelta
x = '2017-05-15'
res = (datetime.strptime(x, '%Y-%m-%d') + timedelta(days=1)).strftime('%Y-%m-%d')
print(res)
# 2017-05-16
Pandas
The equivalent steps can be performed using 3rd party Pandas:
x = '2017-05-15'
# choose some combination of below methods
res = (pd.Timestamp(x) + pd.DateOffset(days=1)).strftime('%Y-%m-%d')
res = (pd.to_datetime(x) + pd.Timedelta('1 day')).strftime('%Y-%m-%d')
print(res)
# 2017-05-16
Using pd.to_datetime, pd.TimeDelta and strftime:
fmt = '%Y-%m-%d'
(pd.to_datetime(<your series or column>, format=fmt) + pd.Timedelta('1 days')).dt.strftime(date_format=fmt)
Example
df = pd.DataFrame({'date': ['2017-04-02', '2017-04-23']})
fmt = '%Y-%m-%d'
>>> (pd.to_datetime(df.date, format=fmt) + pd.Timedelta('1 days')).dt.strftime(date_format=fmt)
0 2017-04-03
1 2017-04-24
Name: date, dtype: object
You can perform arithmetic operations with datetime and timedelta objects.
from datetime import datetime, timedelta
d = datetime(year=2018, month=3, day=1)
t = timedelta(days=1)
d + t
# datetime.datetime(2018, 3, 2, 0, 0)
d + t + t
# datetime.datetime(2018, 3, 3, 0, 0)
for i in range(30):
d += 1
print(d)
# datetime.datetime(2018, 3, 31, 0, 0)
You mean like this
date = datetime.date(2015,5,15)
date += datetime.timedelta(days=1)
It depends on what you mean by quickest. I'm assuming you mean the quickest way to program this, not the shortest program execution time (which might be relevant when you're doing lots of these).
from datetime import datetime, timedelta
original_date = '2018-5-15'
print('The original date is {}, the date one day later is: {}'.
format(original_date, (datetime.strptime(original_date, '%Y-%m-%d') +
timedelta(days=1)).strftime('%Y-%m-%d')
Step by step version:
Create a datetime object from the string, note the string that shows python the formatting (see the documentation for more information)
dt = datetime.strptime(original_date, '%Y-%m-%d')
Add a day
dt += timedelta(days=1)
Reformat it back to the requested string
print(dt.strftime('%Y-%m-%d'))
That's all!

Split List based on index of value

Assuming I have a list of dates:
dates =['11/09/2013','12/09/2013','20/09/2013','27/09/2013','30/09/2013']
test = '21/09/2013'
I want to split the 'dates' list based on this 'test' variable
Desired output:
1. ['11/09/2013','12/09/2013','20/09/2013']
2. ['27/09/2013','30/09/2013']
If 'test' actually belongs(contains) in the list, it must be in list 1.
I'm trying everything still no luck.
You should convert the strings into datetime objects and then split the list using a date comparison:
from datetime import datetime
dates =['11/09/2013','12/09/2013','20/09/2013','27/09/2013','30/09/2013']
test = '21/09/2013'
# Convert to datetime objects
dd = [datetime.strptime(d,'%d/%m/%Y') for d in dates]
tt = datetime.strptime(test,'%d/%m/%Y')
# Split lists
dd_before = [d for d in dd if d < tt]
dd_after = [d for d in dd if d >= tt]
# Convert back to strings if needed
dates_before = [datetime.strftime(d,'%d/%m/%Y') for d in dd_before]
dates_after = [datetime.strftime(d,'%d/%m/%Y') for d in dd_after]
dates =['11/09/2013','12/09/2013','20/09/2013','27/09/2013','30/09/2013']
test = '21/09/2013'
dates.append(test)
dates.sort()
index = dates.index(test)
print dates[:index]
print dates[index + 1:]
output
['11/09/2013', '12/09/2013', '20/09/2013']
['27/09/2013', '30/09/2013']
You can get your desired result.
How about converting the items to datetime objects?
from datetime import datetime
# convert to datetime objects
datesdt = [datetime.strptime(date, '%d/%m/%Y') for date in dates]
testdt = datetime.strptime(test, '%d/%m/%Y')
# make separate datetime object lists for low and high dates
low_date_dt_list = [dt for dt in datesdt if dt <= testdt]
high_date_dt_list = [dt for dt in datesdt if dt > testdt]
# now print in desired format
low_date_list = [dt.strftime('%d/%m/%Y') for dt in low_date_dt_list]
high_date_list = [dt.strftime('%d/%m/%Y') for dt in high_date_dt_list]
Here's what I get for output when using the above code with your given dates and test values:
In [27]: low_date_list
Out[27]: ['11/09/2013', '12/09/2013', '20/09/2013']
In [28]: high_date_list
Out[28]: ['27/09/2013', '30/09/2013']
Documentation for datetime.strptime
Datetime Format Codes
Documentation for strftime

Categories

Resources