Author avatar

Chhaya Wagmi

Explore Python Libraries: Arrow

Chhaya Wagmi

  • Jun 12, 2020
  • 6 Min read
  • Jun 12, 2020
  • 6 Min read
Data Analytics
Machine Learning


As a Python user, you may have faced a lot of trouble dealing with date and time formats like converting one date-time format into another time zone format, using manually built functions to bring change in days, hours, minutes, etc. You may end up using a lot of libraries like datetime, time, dateutil, etc. that still require a lot of extra code to be written. Instead, what if you learn one single library that presents all major library features and, above all, provides additional features to make you code less?

In this guide you will learn about the Arrow library by exploring these topics:

  • Building data
  • Manipulating data
  • Human-friendly data
  • Extracting data
  • Language conversion

Before delving into these topics, you should install the Arrow library using the command pip install arrow. All the code examples in this guide assume that you have imported the library using the command import arrow.

Building Data

If you want to fetch the timestamp of a timezone, mention its expression inside the now() function. Expression could be US/Pacific, US/Mountain, Europe/Berlin, etc. If you don't mention the timezone inside the now() function, you will be receiving the data of your timezone. Here's an example to fetch the timestamp and also to extract its individual components like hour, day, second, etc.

1arw ='Europe/Berlin')
3# 2020-06-08T23:03:01.688314+02:00
5print(, arw.month, arw.year, arw.hour, arw.minute, arw.second )
6# (8, 6, 2020, 23, 6, 56)

Next, try to convert a timestamp value to an arrow timestamp using the get() function. The original timestamp could be either int or float data type.

1arw = arrow.get(1592901654)
3# 2020-06-23T08:40:54+00:00

Manipulating Data

Sometimes you may get faulty data or you may want to completely update the timestamp. Whatever the case is, you can perform such manipulations directly using the Arrow's replace() and shift() functions.

Here are few interesting examples to explain both of these functions.

Your previous marathon database has the best record timing as 2:19:10. Now, a new runner breaks the record and sets it to 2:09:10. How will you update the new record in the database? Before you proceed, notice that both the hour and seconds data is the same in both the records except the minute. So you just need to update the minute data:

1arw = arrow.get(1592705950)
2print(arw, '|', arw.replace(minute=9))
3# 2020-06-21T02:19:10+00:00 | 2020-06-21T02:09:10+00:00

Consider a start of the rainy season from today and its probability to continue till next two weeks. You have the timestamp for today, how will you update it to next two weeks? Well, you can use the shift() function to do the calculations for you as shown in this example:

1arw = arrow.get(1592705950)
2print(arw, '|', arw.shift(weeks=2))
3# 2020-06-21T02:19:10+00:00 | 2020-07-05T02:19:10+00:00

Human-Friendly Data

Have you ever wondered if you can receive time and date in a format say "in two days" rather than "2020-05-25T14:45:53.516031-07:00"? You can do a lot more than this small example. Arrow library provides you the benefit of retaining date and time by combining a language (not just English!) and numbers. Here are a few examples that explain how the humanize() function can save your time in reading date and time:

1arw ='US/Pacific')
3# just now
5arw = arw.shift(weeks=-2)
8# 2 weeks ago
10arw = arw.replace(minute=30)
11print(arw.humanize()) # No change
12# 2 weeks ago
14arw = arw.shift(years=+4)
15print(arw.humanize(granularity=["year", "day", "hour", "minute"]))
16# in 3 years 351 days 5 hours and 47 minutes

As you can observe in the last line of the code, you can select exactly with which time frame the result should be formed. By default, it will be the last granularity that you have changed otherwise it will follow the granularity mentioned in the function.

Extracting Data

Assume you have a sentence that constitutes either a date or time value which needs to be extracted. Usually, you might write a regular expressions to match the record and fetch the data. However, the Arrow library takes care of that for you. Use the basic get() function along with the format and you will receive the data as shown:

1sentence = "I went to picnic with my college friends on 2020-01-28. We all spent a good time together"
2arw = arrow.get(sentence, "YYYY-MM-DD")
4# 2020-01-28T00:00:00+00:00

For more complicated data, you can still rely on regular expressions to extract the data as shown:

1sentence = "I went to picnic with my college friends on 2020      Jan 28       8 o'clock. We all spent a good time together"
2fmt = r"YYYY[\s+]MMM[\s+]DD[\s+]H"
3print(arrow.get(sentence, fmt))
4# 2020-01-28T08:00:00+00:00

In the above example, the data is separated with random spaces and still we're able to extract it. Make sure you use proper tokens. Refer here for supported tokens.

Language Conversion

The Arrow library has a dedicated module that support a plethora of languages to display the final result. In this example, French and Greek have been chosen to display that it's working:

1arw = arrow.utcnow()
2print(arw.shift(years=5, months=8, weeks=1).humanize(granularity=['year', 'day','month','minute','second'], locale='en'))
3# in 5 years 8 months 7 days 1080 minutes and 0 seconds
5arw = arrow.utcnow()
6print(arw.shift(years=5, months=8, weeks=1).humanize(granularity=['year', 'day','month','minute','second'], locale='fr'))
7# dans 5 ans 8 mois 7 jours 1080 minutes et 0 quelques secondes
9arw = arrow.utcnow()
10print(arw.shift(years=5, months=8, weeks=1).humanize(granularity=['year', 'day','month','minute','second'], locale='el'))
11# σε 5 χρόνια 8 μήνες 7 μέρες 1080 λεπτά και 0 δευτερόλεπτα


The Arrow library provides you with a one-stop solution to all the problems related to date and time data. It combines features from various libraries, and on top of that it provides an additional module, locale, to display results as per your language requirement.