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
Related
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
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']]
I need to convert a variable i created into a timestamp from a datetime.
I need it in a timestamp format to perform a lambda function against my pandas series, which is stored as a datetime64.
The lambda function should find the difference in months between startDate and the entire pandas series. Please help?
I've tried using relativedelta to calculate the difference in months but I'm not sure how to implement it with a pandas series.
from datetime import datetime
import pandas as pd
from dateutil.relativedelta import relativedelta as rd
#open the data set and store in the series ('df')
file = pd.read_csv("test_data.csv")
df = pd.DataFrame(file)
#extract column "AccountOpenedDate into a data frame"
open_date_data = pd.Series.to_datetime(df['AccountOpenedDate'], format = '%Y/%m/%d')
#set the variable startDate
dateformat = '%Y/%m/%d %H:%M:%S'
set_date = datetime.strptime('2017/07/01 00:00:00',dateformat)
startDate = datetime.timestamp(set_date)
#This function calculates the difference in months between two dates: ignore
def month_delta(start_date, end_date):
delta = rd(end_date, start_date)
# >>> relativedelta(years=+2, months=+3, days=+28)
return 12 * delta.years + delta.months
d1 = datetime(2017, 7, 1)
d2 = datetime(2019, 10, 29)
total_months = month_delta(d1, d2)
# Apply a lambda function to each row by adding 5 to each value in each column
dfobj = open_date_data.apply(lambda x: x + startDate)
print(dfobj)
I'm only using a single column from the loaded data set. It's a date column in the following format ("%Y/%m/%d %H:%M:%S"). I want to find the difference in months between startDate and all the dates in the series.
As I don't have your original csv, I've made up some sample data and hopefully managed to shorten your code quite a bit:
open_date_data = pd.Series(pd.date_range('2017/07/01', periods=10, freq='M'))
startDate = pd.Timestamp("2017/07/01")
Then, with help from this answer to get the appropriate month_diff formula:
def month_diff(a, b):
return 12 * (a.year - b.year) + (a.month - b.month)
open_date_data.apply(lambda x: month_diff(x, startDate))
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!
I have a list of dates (mm-yyyy), having only the fields of months and years:
d = ['09-2007', '10-2007', '03-2011', '05-2011']
And I need to convert them into JSON date strings..like 1154476800000.. will Python's datetime or simplejson module help in this?
In [30]: import datetime as DT
In [31]: import time
In [32]: d = ['09-2007', '10-2007', '03-2011', '05-2011']
In [33]: [time.mktime(DT.datetime.strptime(dstr, '%m-%Y').timetuple())*1000 for dstr in d]
Out[33]: [1188619200000.0, 1191211200000.0, 1298955600000.0, 1304222400000.0]
You need to convert those months to date objects, then to time values:
from datetime import datetime
import time
def to_jstimestamp(dt):
dt = datetime.strptime(dt, '%m-%Y')
return time.mktime(dt.timetuple()) * 1000
d = [to_jstimestamp(dt) for dt in d]
JSON does not have a standard for representing datetime values; what you are describing are JavaScript timestamps instead.
Demo:
>>> json.dumps([to_jstimestamp(dt) for dt in d])
'[1188601200000.0, 1191193200000.0, 1298937600000.0, 1304204400000.0]'