Convert string list to date list Python 3.6 - python

I have a list of dates that python recognizes as str. I need to be able to work this as dates, to be able to add and subtract days. The dates are of the form "29-Jun-2017"
How can I have a date list instead of a String list?

datetime.datetime.strptime converts string to datetime object:
>>> datetime.datetime.strptime('29-Jun-2017', '%d-%b-%Y')
datetime.datetime(2017, 6, 29, 0, 0)
Then, datetime.datetime.date() will return date object:
>>> datetime.datetime.strptime('29-Jun-2017', '%d-%b-%Y').date()
datetime.date(2017, 6, 29)
See strftime() and strptime() Behavior for other format codes.

The arrow module makes this type of calculation with dates especially easy.
Here I create a short list of date strings which I convert to arrow dates. Having displayed these dates I use the replace method to subtract two days from each date, creating a new list. Then I format this result for display.
>>> import arrow
>>> data = [ '01-Jan-2017', '31-Dec-2017' ]
>>> data_as_dates = [ arrow.get(_, 'DD-MMM-YYYY') for _ in data]
>>> data_as_dates
[<Arrow [2017-01-01T00:00:00+00:00]>, <Arrow [2017-12-31T00:00:00+00:00]>]
>>> two_days_earlier = [ _.replace(days=-2) for _ in data_as_dates ]
>>> [_.format('YYYY MMMM DD') for _ in two_days_earlier]
['2016 December 30', '2017 December 29']
The dates contained in arrow objects are available as conventional Python dates suitable for use with datetime, for instance.

Related

how to convert string list to date and time list in python?

I have a string list containing date and time. I want to convert it to date and time list.
lstDateTime = [datetime.strptime(x,'%Y-%m-%d %H:%M:%S.%f') for x in time]
print("lstDateTime:",lstDateTime)
It gives absurd list as
datetime.datetime(2017, 1, 1, 10, 12, 13), datetime.datetime(2017, 1, 1, 10, 12, 14)
while actual string list is like:-
'2017-01-01 16:59:25.000', '2017-01-01 16:59:26.000'
Cleanest way is to use pandas, for example with the following test_list as strings:
import pandas as pd
dates = pd.to_datetime(list, format='%Y-%m-%d %H:%M:%S.%f')
This will return a series object. In case you need it as list, convert it:
lstDateTime = date.to_list()
Try this:
lstDateTime = [str(datetime.strptime(x,'%Y-%m-%d %H:%M:%S.%f').isoformat(timespec='milliseconds')) for x in time]
print(f'lstDateTime: {", ".join(lstDateTime)}')
You are printing the list directly, which causes the objects not to be displayed as formatted strings as you want them to. You can use strftime() to get the desired output. If you want to use the same format as the one you used to convert the strings to datetime objects, you can use this:
lstDateTime = [datetime.strptime(x,'%Y-%m-%d %H:%M:%S.%f') for x in time]
lstDateTime = [x.strftime('%Y-%m-%d %H:%M:%S.%f') for x in lstDateTime]
print("lstDateTime:",lstDateTime)

How to convert strings to TimeStamps for compare?

I have strings like:
first = '2018-09-16 15:00:00'
second = '1900-01-01 09:45:55.500597'
I want to compare them.
All methods I found like Convert string date to timestamp in Python requires to know the format of the string.
I don't know the format of the strings (see differences between first and second) all I know is that they can be converted to timestamps.
How can I convert them in order to compare them?
Edit:
The "largest" string that I can get is:
1900-01-01 09:45:55.500597
but I can also get:
1900-01-01
1900-01-01 09:45
1900-01-01 09:45:55
etc..
It's always YYYY-MM-DD HH-MM....
You can use pandas.to_datetime. It offers a lot of flexibility in the string timestamp format, and you can use it on single strings or list/series/tuples of timestamps.
>>> import pandas as pd
>>> day = pd.to_datetime('1900-01-01')
>>> minute = pd.to_datetime('1900-01-01 09:45')
>>> second = pd.to_datetime('1900-01-01 09:45:55')
>>> subsecond = pd.to_datetime('1900-01-01 09:45:55.500597')
>>> assert subsecond > second
>>> assert minute < second
>>> assert day < minute
You can use the dateutil module (pip install python-dateutil):
>>> from dateutil.parser import parse
>>> parse('2018-09-16 15:00:00')
datetime.datetime(2018, 9, 16, 15, 0)
>>> parse('1900-01-01 09:45:55.500597')
datetime.datetime(1900, 1, 1, 9, 45, 55, 500597)
From the list of its features:
Generic parsing of dates in almost any string format;
Once you have the datetime objects, you can compare them directly, there's no need to calculate the timestamps.

Subtract "n" days from a list of format [month, day]

I have a this list in the Python.
dates= [6,15],[8,24]
I want to subtract values from this list. For example [6,15] is [month,day] so I want to subtract 15 to 10. I want to get [6,5], well this operation will repeat after that I want to get [5, 26] like this. How can I do this code?
You probably want to use the builtin datetime and 3rd-party dateutil modules for this. Note you will need to specify a year, since some years have months of differing lengths (i.e leap years) -parse will assume the current year:
import datetime.date as dt
from dateutil.parser import parse
from dateutil.relativedelta import relativedelta
print(parse('6/15') - relativedelta(days=10))
You should be using inbuilt datetime.datetime and datetime.timedelta objects to achieve this in simplified way as:
>>> from datetime import datetime, timedelta
>>> my_date_list = [6, 15] # your current list in "[month, day]" format
# create `datetime` object using above values
# since you don't care about year, using 2018 for demonstration. But you need this.
>>> datetime_obj = datetime(month=my_date_list[0], day=my_date_list[1], year=2018)
# timedelta object for the number of days you want the diff
>>> diff = timedelta(days=20)
# New datetime object
>>> new_datetime_obj = datetime_obj - diff
>>> new_datetime_obj
datetime.datetime(2018, 5, 26, 0, 0)
# You desired format list of [month, day]
>>> [new_datetime_obj.month, new_datetime_obj.day]
[5, 26]
PS: You shouldn't be even storing your initial and final list as "[Month, Day]" format. Simply store the list of datetime objects, and use it where ever you need. new_datetime_obj.month yields the month and new_datetime_obj.day yields the day
Note: You must consider about the year in your code. It is necessary in doing your computation and calculating the days. For example, the calculation for February for leap year and non-leap years yields different results.

Date Time Formats in Python

What are these date-time formats? I need to convert them to the same format, to check if they are the same. These are just two coming from a separate data source, so I need to find a way to make them the same format. Any ideas?
2013-07-12T07:00:00Z
2013-07-10T11:00:00.000Z
Thanks in advance
That extra .000 is micro seconds.
This will convert a date string of a format to datetime object.
import datetime
d1 = datetime.datetime.strptime("2013-07-12T07:00:00Z","%Y-%m-%dT%H:%M:%SZ")
d2 = datetime.datetime.strptime("2013-07-10T11:00:00.000Z","%Y-%m-%dT%H:%M:%S.%fZ")
Then convert them into any format depending on your requirement, by using:
new_format = "%Y-%m-%d"
d1.strftime(new_format)
perhaps use .isoformat()
string in ISO 8601 format, YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM]
>>> import datetime
>>> datetime.datetime.utcnow().isoformat() + "Z"
'2013-07-11T22:26:51.564000Z'
>>>
Z specifies "zulu" time or UTC.
You can also add the timezone component by making your datetime object timezone aware by applying the appropriate tzinfo object. With the tzinfo applied the .isoformat() method will include the appropriate utc offset in the output:
>>> d = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
>>> d.isoformat()
'2019-11-11T00:52:43.349356+00:00'
You can remove the microseconds by change the microseconds value to 0:
>>> no_ms = d.replace(microsecond=0)
>>> no_ms.isoformat()
'2019-11-11T00:52:43+00:00'
Also, as of python 3.7 the .fromisoformat() method is available to load an iso formatted datetime string into a python datetime object:
>>> datetime.datetime.fromisoformat('2019-11-11T00:52:43+00:00')
datetime.datetime(2019, 11, 11, 0, 52, 43, tzinfo=datetime.timezone.utc)
http://www.ietf.org/rfc/rfc3339.txt
you can try to trim the string
data = "2019-10-22T00:00:00.000-05:00"
result1 = datetime.datetime.strptime(data[0:19],"%Y-%m-%dT%H:%M:%S")
result2 = datetime.datetime.strptime(data[0:23],"%Y-%m-%dT%H:%M:%S.%f")
result3 = datetime.datetime.strptime(data[0:9], "%Y-%m-%d")
use datetime module.
For a variable
import datetime
def convertDate(d):
new_date = datetime.datetime.strptime(d,"%Y-%m-%dT%H:%M:%S.%fZ")
return new_date.date()
convertDate("2019-12-23T00:00:00.000Z")
you can change the ".date()" to ".year", ".month", ".day" etc...
Output: # is now a datetime object
datetime.date(2019, 12, 23)
For a DataFrame column, use apply()
df['new_column'] = df['date_column'].apply(convertDate)
* Short and best way:
str(datetime.datetime.now()).replace(' ','T')
or
str(datetime.datetime.now()).replace(' ','T') + "Z"

Slicing and Replacing Unicode String Characters

I have the following loop I'm trying to use to replace characters in a unicode string. The data I'm getting for this loop is in the following format: YYYY-MM-DD HH:MM:SS
This data is apparently stored in UTC, so when I grab it and append these times & dates to my list appts_list its 4 hours ahead.
I've gotten as far as slicing the unicode string and doing the math on these characters and getting what would be the correct hour I need, but I'm having a problem getting that back into a string so I can write it to my list appts_list.
I'm getting TypeError when I try to write the integer for the correct hour time_slice_int back into the original string. I decided to try to put the entire string into a list and change them there, but that isn't working either.
Ideally I want an appointment for '2013-06-28 15:30:00' to be entered into my appts_list as '2013-06-28 11:30:00'.
The print statements are there for me to debug as I ran it. They are not necessary for the final version.
Anyone have any suggestions or solutions?
for appt in todays_appts:
time = appt['apptdateourtime_c']
time_slice = time[11:13]
time_slice_int = int(time_slice)
time_slice_int -= 4
print(time_slice_int)
appt_time = list(time)
print(appt_time)
print(appt_time[11:13])
#appt_time[11:13] = time_slice_int
#appts_list.append()
print('AppointmentScheduled')
#print(appt['apptdateourtime_c'])
#print(time)
print('')
You should use the datetime module here:
>>> from datetime import datetime, timedelta
>>> strs = '2013-06-28 15:30:00'
>>> d = datetime.strptime(strs, "%Y-%m-%d %H:%M:%S")
datetime.strptime returns a datetime object:
>>> d
datetime.datetime(2013, 6, 28, 15, 30)
>>> d.hour
15
>>> d.month
6
Now decrease 4 hours from the above datetime object(d) using timedelta and assign the new object to a variable:
>>> d1 = d - timedelta(hours = 4)
Now use datetime.strftime to get a string of required format:
>>> datetime.strftime(d1,"%Y-%m-%d %H:%M:%S")
'2013-06-28 11:30:00'

Categories

Resources