{"id":901,"date":"2022-07-06T15:46:14","date_gmt":"2022-07-06T15:46:14","guid":{"rendered":"https:\/\/ml-gis-service.com\/?p=901"},"modified":"2022-07-06T15:49:13","modified_gmt":"2022-07-06T15:49:13","slug":"toolbox-get-date-n-weeks-from-other-date-in-python","status":"publish","type":"post","link":"https:\/\/ml-gis-service.com\/index.php\/2022\/07\/06\/toolbox-get-date-n-weeks-from-other-date-in-python\/","title":{"rendered":"Toolbox: Get date n weeks from another date in Python"},"content":{"rendered":"\n<p>The function uses only basic Python modules (<code>datetime<\/code> and <code>timedelta<\/code>). If you want to:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>trim your weekly time series into training and test set,<\/li><li>or create or check the date n weeks ahead or back,<\/li><\/ul>\n\n\n\n<p>then this function is for you.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from datetime import datetime, timedelta\n\n\ndef get_date_n_weeks(fixed_date, weeks_diff, dateformat=None, direction='back'):\n    \"\"\"\n    Function returns specific date n weeks ahead or back.\n\n    INPUT:\n\n    :param fixed_date: (str or datetime) date from which other date will be calculated.\n    :param weeks_diff: number of weeks back or ahead from the fixed date.\n    :param dateformat: (str, optional) if given fixed_date has type string then parameter must be\n                       set to the date format.\n    :param direction: (str) 'back' or 'ahead'.\n\n    OUTPUT:\n\n    :returns: (datetime) other date n weeks ahead or back\n    \"\"\"\n\n    if isinstance(fixed_date, str):\n        date = datetime.strptime(fixed_date, dateformat)\n    else:\n        date = fixed_date\n\n    d = timedelta(days=weeks_diff * 7)  # 7 days in one week\n\n    if direction == 'back':\n        new_date = date - d\n    elif direction == 'ahead':\n        new_date = date + d\n    else:\n        raise TypeError('Wrong parameter: direction parameter could be only \"back\" or \"ahead\".')\n\n    return new_date<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you want to trim your weekly time series into training and test set, or create \/ check the date n weeks ahead or back then this function is for you!<\/p>\n","protected":false},"author":1,"featured_media":903,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,18,3,17,180],"tags":[211,207,210,215,7,209,184,208,214,212,213],"class_list":["post-901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-engineering","category-data-science","category-python","category-scripts","category-time-series","tag-date","tag-datetime","tag-get-date","tag-how-to","tag-python","tag-time","tag-time-series","tag-timedelta","tag-week","tag-weekly","tag-weeks"],"_links":{"self":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts\/901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/comments?post=901"}],"version-history":[{"count":3,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts\/901\/revisions"}],"predecessor-version":[{"id":905,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts\/901\/revisions\/905"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/media\/903"}],"wp:attachment":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/media?parent=901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/categories?post=901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/tags?post=901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}