Author avatar

Chhaya Wagmi

Explore Python Libraries: Arrow

Chhaya Wagmi

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

Introduction

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.

1
2
3
4
5
6
arw = arrow.now('Europe/Berlin')
print(arw)
# 2020-06-08T23:03:01.688314+02:00

print( arw.day, arw.month, arw.year, arw.hour, arw.minute, arw.second )
# (8, 6, 2020, 23, 6, 56)
python

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.

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

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:

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

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:

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

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
arw = arrow.now('US/Pacific')
print(arw.humanize())
# just now

arw = arw.shift(weeks=-2)
print(arw)
print(arw.humanize())
# 2 weeks ago

arw = arw.replace(minute=30)
print(arw.humanize()) # No change
# 2 weeks ago

arw = arw.shift(years=+4)
print(arw.humanize(granularity=["year", "day", "hour", "minute"]))
# in 3 years 351 days 5 hours and 47 minutes
python

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:

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

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

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

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 locales.py 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:

1
2
3
4
5
6
7
8
9
10
11
arw = arrow.utcnow()
print(arw.shift(years=5, months=8, weeks=1).humanize(granularity=['year', 'day','month','minute','second'], locale='en'))
# in 5 years 8 months 7 days 1080 minutes and 0 seconds

arw = arrow.utcnow()
print(arw.shift(years=5, months=8, weeks=1).humanize(granularity=['year', 'day','month','minute','second'], locale='fr'))
# dans 5 ans 8 mois 7 jours 1080 minutes et 0 quelques secondes

arw = arrow.utcnow()
print(arw.shift(years=5, months=8, weeks=1).humanize(granularity=['year', 'day','month','minute','second'], locale='el'))
# σε 5 χρόνια 8 μήνες 7 μέρες 1080 λεπτά και 0 δευτερόλεπτα
python

Conclusion

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.

8