Handling dates with Python’s maya package

python maya package


Background

In this package we’ll discuss Python’s maya package for parsing dates from strings. A previous article talked about the dateutil and dateparser libraries for finding dates in strings. maya is really great for standardizing variations in a field or list of dates.

maya can be installed using pip:

pip install maya

Standardizing dates with maya

Let’s start with a basic example. First, we just need to import maya. Next, we’ll use its parse method to convert the text into a MayaDT object. We can append the datetime method to this to get a datetime from the string.


import maya

maya.parse("march 1 2019").datetime()

python convert string to date


maya.parse("9th of february 2019").datetime()

python maya parse date


maya.parse("1/1/2020").datetime()

python's maya package

Below are several more examples of different date variations.


maya.parse("1-1-2020").datetime()

maya.parse("1 1 2020").datetime()

maya.parse("Jan 1 2020").datetime()

maya.parse("January 1 2020").datetime()

maya.parse("January 1st 2020").datetime()

maya.parse("1st of january 2020").datetime()


python's maya package standardize dates

All of the above comes in handy if you’re dealing with a list of date strings that you need to standardize. Suppose, for example, we have a list containing some of the variations above.


strings = ["1-1-2020", "1 1 2020", "Jan 1 2020", "January 1 2020",
           "January 1st 2020", "1st of january 2020"]


Now, we can convert each string to a properly-formed datetime using a list comprehension.


[maya.parse(text).datetime() for text in strings]

Other functionality

maya has several additional features as well. In the below snippet we can add time to a defined datetime variable. The rfc2822 method formats the result as seen in the last line below. This time, we’ll use the when method, which can be used to create a MayaDT instance from a string. Following that, we use the snap method to adjust a datetime by days, hours, seconds, etc.


temp = maya.when('March 1 2019 12:00 EST')

# add six hours
temp.snap('@d+6h').rfc2822()

# 'Fri, 01 Mar 2019 06:00:00 EST'

# add 5 days, 4 hours, 3 minutes, and 2 seconds
temp.snap('@d+5d+4h+3m+2s').rfc2822()

MayaDT objects also have several useful attributes. For example, let’s create a MayaDT object with the current timestamp.


now = maya.now()

Next, we can get attributes from this timestamp like below:


# get day of week (numerical)
now.day

# get week of year
now.week

# get day of week (numerical)
now.weekday

# get month (numerical)
now.month

That’s all for this post! Check out more on maya here.

Please click here to follow my blog on Twitter!.