Fundamentals 6 min read

Master Python Date Parsing: From dateparser to Arrow for Complex Time Ranges

This article introduces Python's built‑in time modules, explains why third‑party libraries like DateParser, Dateutil, and Arrow are needed for unconventional date formats, and demonstrates how to use them—including code examples—to split time ranges by month or week for robust data export tasks.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Python Date Parsing: From dateparser to Arrow for Complex Time Ranges

Python's standard time and datetime modules cover most everyday scenarios, but they struggle with unusual date formats. For those cases, third‑party libraries such as DateParser and Dateutil can convert arbitrary strings into standard timestamps.

DateParser and Dateutil handle many quirky formats. Example:

# -*- coding: utf-8 -*-
# @Time : 2020-12-05 16:46
import time
import dateparser
from dateutil import parser

print(dateparser.parse("2020/11/7 5:01:08"))
print(parser.parse("2020"))

When exporting large datasets, platforms often limit the amount of data per request. To avoid failures, it is useful to split the export period into smaller intervals (e.g., monthly or weekly).

While searching for Python tools to split time ranges, the Arrow library stands out. Arrow wraps datetime and provides a fluent API for manipulation and range generation.

Basic Arrow usage examples:

# -*- coding: utf-8 -*-
# @Time : 2020-12-05 16:46
i = arrow.now()

# i.replace(day=1)   # set day to 1
# i.shift(months=-1) # go back one month
# i.format('YYYY-MM-DD') # format output
print(i.shift(days=-30).format('YYYY-MM-DD'))
print(i.shift(months=-3).format('YYYY-MM-DD'))
print(i.replace(day=1).shift(months=-1).format('YYYY-MM-DD'))
print(i.replace(day=1).shift(months=-1).format('YYYY-MM-DD HH:mm:ss'))

Arrow also offers span_range and range methods to generate sequences of dates with a chosen granularity. The following functions illustrate how to obtain monthly, weekly, or custom formatted ranges over a 120‑day period:

# -*- coding: utf-8 -*-
# @Time : 2020-12-05 16:46
import arrow
import datetime

end = datetime.datetime.now()
start = end + datetime.timedelta(days=-120)

def timeYmdRange(start=start, end=end):
    tRange = []
    for r in arrow.Arrow.span_range('months', start, end):
        qTime = [i.format('YYYY-MM-DD') for i in r]
        tRange.append(qTime)
    return tRange

def timeWeekRange(start=start, end=end):
    tRange = []
    for r in arrow.Arrow.span_range('weeks', start, end):
        qTime = [i.format('YYYY-MM-DD') for i in r]
        tRange.append(qTime)
    return tRange

def timeMonthRange(start=start, end=end):
    tRange = []
    for r in arrow.Arrow.range('months', start, end):
        qTime = r.format('YYYYMM')
        tRange.append(qTime)
    return tRange

print(timeWeekRange())

Using Arrow's range capabilities satisfies the need for precise time slicing without resorting to multiple complex libraries, keeping the solution simple and maintainable.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonTutorialdateutilDate ParsingArrowtime range
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.