How to Get the Dates of Yesterday, Today, and Tomorrow in NumPy?
Introduction
In NumPy, we can retrieve today's date using numpy.datetime64('today', 'D'), and if we want a date prior to today, we can deduct the number of dates from today using np.timedelta64(). If we want a date beyond today, we will employ np.timedelta64() to add the number of dates from today.
So, it's pretty straightforward, yeah? At first look, it may appear challenging. Don't worry, I've got your back. In this blog, we will go through the data type numpy.datetime64() in detail.
numpy.datetime64()
Before we go any further, consider why we need specific functions to work with dates. Isn't it possible to just utilize strings say '09-07-2022' and be done with it? Without further ado, let's comprehend this.
Working with dates and times is tricky, so we can't 'simply use strings'. Assume we need to discover vital facts such as which day of the week our website has the most views, which hour the most transactions are performed, or which hour the most crypto / stocks are exchanged, and so on. Finding answers to all such sorts of questions using strings would be rather challenging. Dates and times, clearly, require their own functions.
Although we cannot "simply use strings" to express dates and times, we will use strings as inputs to the data type with which we will be working i.e. : np.datetime64 ().
Note :
The 64 indicates that the numerals are 64 bits long.
Basic Datetimes
The simplest approach to generate datetimes is to use strings in ISO 8601 date or datetime format. We may either insert a space between the date and time '2022-07-09 21:14' or a capital T between them '2022-07-09T21:14' to enter these strings into NumPy. Because it is easier to read, the first way is preferable. However, when you print any np.datetime64() functions, NumPy always inserts a T. The internal storage unit is automatically chosen from the string's shape and can be either a date unit or a time unit. Years ('Y'), months ('M'), weeks ('W'), and days ('D') are the date units, while hours ('h'), minutes ('m'), seconds ('s'), and milliseconds('ms') quantities are the time units. For a "Not A Time" value, the datetime64 data type accepts the string "NaT" in any combination of lowercase/uppercase letters. Let me illustrate this with an example.
Note : ISO 8601 is the international standard for writing dates and times. According to ISO 8601 standards,
- Dates are formatted as YYYY-MM-DD, thus 2022-09-06 is the sixth of September 2022.
- For timings, use hh:mm:ss, so 21:14:48 is 21:14 (9.14 pm) and 48 seconds.
- Use a full stop for milliseconds, 21:14:48.9. (adding on 9 milliseconds).
When we perform arithmetic with datetime objects later on, this will be crucial.
Code :
Output :
Datetime and Timedelta Arithmetic
NumPy lacks a physical calendar arithmetic system, the timedelta64 data type was designed to supplement datetime64. np.timedelta64 is used in any arithmetic that is more complicated than adding or subtracting integers. The result of the equation is a np.timedelta64 object. np.timedelta64 takes two parameters: a number to indicate the number of units, and a date/time unit, such as (D)ay, (M)onth, (Y)ear, (h)ours, (m)inutes, or (s)econds . Consider the following example.
Code :
Output :
Note : timedelta units ('Y', years, and 'M', months) are treated differently since the amount of time they indicate differs based on when they are employed. While a timedelta day unit is equal to 24hrs, there is no method to translate a month unit into days since the number of days in each month varies (some have 30-days, but some have 31-days, and for month of Feb it varies whether the year is a leap year or not).
Code :
Output :
Some Special Functions
Using the tools listed above, we may create any list of datetimes. However, there are certain things we can do to make our lives simpler.
Businesses are generally mainly concerned with what happens during their working hours. As a result, it would be useful to have a collection of methods to deal with this. If we had to incorporate Sat and Sun in trade data research, there would be a lot of extra data points.
Fortunately, NumPy tackles this by utilizing the idea of business days through the np.busday() function. There are a couple of them. So, let's start with the most crucial ones: np.is_busday(), np.busday_count(), and np.busday_offset().
Note : It should be noted that the input to the np.busday methods is a np.datetime64 object rather than a string.
numpy.is_busday() :
Syntax :
Employ np.is_busday() to determine whether a datetime64 object is a legitimate day. Returns a boolean result indicating whether the day is a business day or not.
Code :
Output :
numpy.busday_count() :
Syntax :
"np.busday_count()" returns the number of valid days in a given range of datetime64 dates.
Code :
Output :
Note : You can do this if you have a list of datetime64 objects and want to know how many of them are legitimate dates.
Code :
Output :
numpy.busday_offset() :
Syntax :
Returns a legitimate business day and the number of days to offset based on the roll rule (how to handle dates that do not occur on a legitimate day). This will be used to locate the nearest business day to any date we send to this function.
For example, imagine we wish to know the stock market trends on July 9, 2022. This is unlikely because it is a weekend and the stock exchange is closed. We'll use this method to get the nearest np.datetime64 object that is a business day.
The parameters are as follows: np.datetime64 object, number of days to offset, and 'roll'. To identify the nearest business day, we shall simply use offset=0 (i.e. no offset). We'll also set roll='forward'. As a result, if we enter a weekend, it should return, Monday (2022-07-11). We would get Friday (2022-07-08) if we used roll='backward' (last working day before weekend).
Code :
Output :
Python Datetime Vs. NumPy Datetime
- In terms of efficiency and speed, NumPy is often regarded as a superior Python implementation. In this situation, too, NumPy datetime64 outperforms Python datetime objects.
- To store date-time properties, Native Python datetime & NumPy datetime64 use distinct formats. For each item of information, the Python datetime format saves it as a set of numbers. It means that each year, month, day, minute, hour, and second has an integer representation. Surprisingly, NumPy datetime64 uses an offset representation based on the Unix epoch. A signed int64 array is used to hold this data.
- The native Python datetime range has been inbuilt between 1 - 9999 Years. It is done thus because the Python integer can accommodate very high numbers, which are now unattainable. The range of NumPy datetime64 is versatile and somewhat dynamic. The range is determined by the int64 range. Smaller units have a narrower range, as does the datetime64 range.
- Operator overloading of addition & subtraction for integers is offered by the NumPy datetime64 object. Python datetimes, on the other hand, does not provide operator overloading of addition and subtraction for integers. Python datetimes only enable addition and subtraction using the timedelta() method (included in the Python datetime library).
These are some of the main distinctions between a Python datetime object and a NumPy datetime64 object.
This takes us to the conclusion of our blog; this was only the tip of the iceberg in terms of what the NumPy datetime64 object can provide; if you want to learn more, please see the official Python documentation.
Conclusion
We learned from this blog:
- To work with ISO 8601 standards date/time in numpy, utilize numpy.datetime64 objects.
- To check for dates from yesterday, today, and tomorrow, we may utilize the numpy.timedelta64 data type, which was meant to supplement the numpy.datetime64 object.
- We may use certain specific functions such as np.is_busday(), np.busday count(), and np.busday offset() to determine whether a particular day is a business day or not.
- The primary distinction between a native Python datetime object and a NumPy datetime64 object