Python

Handling dates with Python’s 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()


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


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

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()


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!.

Andrew Treadway

Recent Posts

Software Engineering for Data Scientists (New book!)

Very excited to announce the early-access preview (MEAP) of my upcoming book, Software Engineering for…

2 years ago

How to stop long-running code in Python

Ever had long-running code that you don't know when it's going to finish running? If…

3 years ago

Faster alternatives to pandas

Background If you've done any type of data analysis in Python, chances are you've probably…

3 years ago

Automated EDA with Python

In this post, we will investigate the pandas_profiling and sweetviz packages, which can be used…

3 years ago

How to plot XGBoost trees in R

In this post, we're going to cover how to plot XGBoost trees in R. XGBoost…

4 years ago

Python collections tutorial

In this post, we'll discuss the underrated Python collections package, which is part of the…

4 years ago