python-datetime

Split a list of dates into subsets of consecutive dates

我们两清 提交于 2020-08-27 07:16:05
问题 I've got an array of dates that can contain multiple date ranges in it. dates = [ '2020-01-01', '2020-01-02', '2020-01-03', '2020-01-06', '2020-01-07', '2020-01-08' ] In this example, the list contains 2 separate consecutive date ranges (2020-01-01 to 2020-01-03 & 2020-01-06 to 2020-01-08) I'm attempting to figure out how I would loop through this list and find all the consecutive date ranges. One of the articles I'm looking at (How to detect if dates are consecutive in Python?) seems to have

Datetime strptime issue with a timezone offset with colons

笑着哭i 提交于 2020-07-07 06:04:08
问题 Currently we are working on an application which is trying to calculate the travel time of a route from a to b, using the 2.db.transport.rest API. Unfortunatly we are receiving timestamps with a timezone offset from that API, such as +01:00 . What we need is a timezone offset without the : , so +0100 . The following example gives an error on Linux, using Python 3.6.7: from datetime import datetime datetimestring = "2019-01-19T15:13:00.000+01:00" datetime.strptime(datetimestring, '%Y-%m-%dT%H:

Datetime strptime issue with a timezone offset with colons

不羁岁月 提交于 2020-07-07 06:03:19
问题 Currently we are working on an application which is trying to calculate the travel time of a route from a to b, using the 2.db.transport.rest API. Unfortunatly we are receiving timestamps with a timezone offset from that API, such as +01:00 . What we need is a timezone offset without the : , so +0100 . The following example gives an error on Linux, using Python 3.6.7: from datetime import datetime datetimestring = "2019-01-19T15:13:00.000+01:00" datetime.strptime(datetimestring, '%Y-%m-%dT%H:

Elegant way to shift multiple date columns - Pandas

邮差的信 提交于 2020-06-26 05:53:05
问题 I have a dataframe like as shown below df = pd.DataFrame({'person_id': [11,11,11,21,21], 'offset' :['-131 days','29 days','142 days','20 days','-200 days'], 'date_1': ['05/29/2017', '01/21/1997', '7/27/1989','01/01/2013','12/31/2016'], 'dis_date': ['05/29/2017', '01/24/1999', '7/22/1999','01/01/2015','12/31/1991'], 'vis_date':['05/29/2018', '01/27/1994', '7/29/2011','01/01/2018','12/31/2014']}) df['date_1'] = pd.to_datetime(df['date_1']) df['dis_date'] = pd.to_datetime(df['dis_date']) df['vis

no of days to previous and next year - Pandas

…衆ロ難τιáo~ 提交于 2020-06-23 09:28:42
问题 I have a data frame like as shown below df1 = pd.DataFrame({'person_id': [11, 21, 31, 41, 51], 'date_1': ['12/30/1961', '05/29/1967', '02/03/1957', '7/27/1959', '01/13/1971'], 'date_2': ['07/23/2017','05/29/2017','02/03/2015',np.nan,np.nan]}) df1 = df1.melt('person_id', value_name='dates') I would like to get the number of days to the previous and next year . I am able to get the previous and next year using the below code df1['cur_year'] = pd.DatetimeIndex(df1['dates']).year df1['prev_year']

Convert a list of DateTime objects to string in python [duplicate]

一个人想着一个人 提交于 2020-06-01 07:13:05
问题 This question already has answers here : How to print a date in a regular format? (22 answers) Closed last year . I have a list of Dates returning Mondays between two dates mondays = [datetime.date(2019, 2, 14), datetime.date(2019, 2, 21)] How do I format this list to %Y%m%d format? Expecting output list to be in the below format: ['20190214','20190221'] is there a way? 回答1: the following will work [date_obj.strftime('%Y%m%d') for date_obj in mondays] Output ['20190214', '20190221'] 来源: https

Subclassing datetime.datetime [duplicate]

北城余情 提交于 2020-05-15 11:10:31
问题 This question already has answers here : Why can't I subclass datetime.date? (6 answers) Closed 5 years ago . I'm looking into creating a python datetime-subclass which provides a default timezone when created. For the sake of keeping this question simple, let's assume I always want to hard-code my datetimes to be in UTC. I can't figure out why the following works: import datetime, dateutil.tz def foo(*args, **kwargs): kwargs['tzinfo'] = dateutil.tz.tzutc() return datetime.datetime(*args, *

Pandas to_datetime changes year unexpectedly

安稳与你 提交于 2020-04-16 03:08:12
问题 I have a date column which after using pandas read_csv is represented as the object type. I'm trying to convert it to pandas datetime object but when using pd.to_datetime() it returns incorrect datetime object. for example, I have dates in this format 01-06-68 , where 01 is the day, 06 is the month and 68 is the year. Applying pandas to_datetime() to this string returns 2068-06-01 but should return 1968-06-01 , where 06 is a month and 01 is the day. I tried every possible solution using

How to determine whether dateparser.search.search_dates() returns dates, times, or datetimes

≡放荡痞女 提交于 2020-03-02 19:14:12
问题 Trying to answer another question I stumbled over the following problem: dateparser.search.search_dates() returns a list of tuples, each tuple consisting of a text segment recognized as carrying date and/or time information, and the corresponding datetime (plus an optional language code, if requested). If the segment was of date type, only the date fields of the datetime are set. The others are taken from the current time, or from a base datetime that can be supplied via the settings

datetime.fromtimestamp vs datetime.utcfromtimestamp, which one is safer to use?

房东的猫 提交于 2020-02-26 15:52:56
问题 I'm collecting some data from sensors and I get the timestamp from it like this: "time": { "seconds": 40, "year": 115, "month": 5, "hours": 7, "time": 1434549820776, "date": 17, "minutes": 3, "day": 3, "timezoneOffset": 420 }, I have a python script that processes the data coming from the sensors (incoming data is json format), I take the value of time and converts into readable time format. I used datetime.fromtimestamp(1434549820776/1000).strftime('%Y-%m-%d %H:%M:%S') and that returned