Parsing DateTime strings containing nanoseconds in Python
Most of the applications require a precision of up to seconds but there are also some critical applications that require nanosecond precision, especially the ones which can perform extremely fast computations. It can help provide insights on certain factors related to time space for the application. Let us see how can we parse DateTime strings that have nanoseconds in them. Python has a list of directives that can be used in order to parse the strings as datetime objects. Let us have a look at some of them that we will be using in our codes.
Directive Description Example %Y Year 2021 %m Month Number 7 %d Date of the month 5 %H 24 Hour format 16 %M Minute 51 %f Microsecond 234567
Image Demonstration of DateTime object:
Let us take the default Python timestamp format: “2021-08-05 15:25:56.792554” as an example to work on.
Method 1: Using DateTime module
In this example, we will see that the nanoseconds value is 792554. “%f” directive is used to parse the nanoseconds. The same thing is cross-verified by converting the “%f” directive to the string value of its representation for the datetime object using the strftime() method.
Method 2: Using Pandas library
Here we will use pandas.to_datetime() methods to parsing DateTime strings containing nanoseconds.
pandas.to_datetime(arg, errors=’raise’, dayfirst=False, yearfirst=False, utc=None, box=True, format=None, exact=True, unit=None, infer_datetime_format=False, origin=’unix’, cache=False)
arg: An integer, string, float, list or dict object to convert in to Date time object.
dayfirst: Boolean value, places day first if True.
yearfirst: Boolean value, places year first if True.
utc: Boolean value, Returns time in UTC if True.
format: String input to tell position of day, month and year.
The above example is similar to the earlier one except for the fact that we have used pandas library instead of the datetime module. This can prove to be handy when we are working with pandas dataframes. One beautiful thing about this library is that we might not need to provide the format manually. The parameter infer_datetime_format in the pandas.to_datetime() method can take care of that automatically if provided as True. In cases, it can increase the parsing speed by ~5-10x. Below is an example of the same.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course