Close Navigation
Learn more about IBKR accounts
Handling Dates with Python’s maya Package

Handling Dates with Python’s maya Package

Posted February 8, 2023
Andrew Treadway
TheAutomatic.net

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()
 class=
maya.parse("9th of february 2019").datetime()
Handling Dates with Python’s maya Package - TheAutomatic.net
maya.parse("1/1/2020").datetime()
 class=

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

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:

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

Originally posted on TheAutomatic.net Blog.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from TheAutomatic.net and is being posted with its permission. The views expressed in this material are solely those of the author and/or TheAutomatic.net and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.