NLP | Using dateutil to parse dates.
Last Updated :
18 Jun, 2019
The parser module can parse datetime strings in many more formats. There can be no better library than dateutil to parse dates and times in Python. To lookup the timezones, the tz module provides everything. When these modules are combined, they make it very easy to parse strings into timezone-aware datetime objects.
Installation :
dateutil can be installed using pip or easy_install, that is, sudo pip install dateutil==2.0 or sudo easy_install dateutil==2.0. 2.0 version for Python 3 compatibility is required. The complete documentation can be found at http://labix.org/python-dateutil.
Code: Parsing Examples
from dateutil import parser
print (parser.parse( 'Thu Sep 25 10:36:28 2010' ))
print (parser.parse( 'Thursday, 25. September 2010 10:36AM' ))
print (parser.parse( '9 / 25 / 2010 10:36:28' ))
print (parser.parse( '9 / 25 / 2010' ))
print (parser.parse( '2010-09-25T10:36:28Z' ))
|
Output :
datetime.datetime(2010, 9, 25, 10, 36, 28)
datetime.datetime(2010, 9, 25, 10, 36)
datetime.datetime(2010, 9, 25, 10, 36, 28)
datetime.datetime(2010, 9, 25, 0, 0)
datetime.datetime(2010, 9, 25, 10, 36, 28, tzinfo=tzutc())
All it takes is importing the parser module and calling the parse() function with a datetime string. The parser can return a sensible datetime object, but it cannot parse the string, it will raise a ValueError.
How it works :
- The parser instead of looking for recognizable tokens, guess what those tokens refer to. It doesn’t use regular expressions.
- The order of these tokens matters as it uses a date format that looks like Month/Day/Year (the default order), while others use a Day/Month/Year format.
- The parse() function takes an optional keyword argument, dayfirst, which defaults to False to deal with this problem.
- It can correctly parse dates in the latter format if it is set to True.
parser.parse( '16 / 6/2019' , dayfirst = True )
|
Output :
datetime.datetime(2016, 6, 16, 0, 0)
Another ordering issue can occur with two-digit years. but ’11-6-19′ is an ambiguous date format. Since dateutil defaults to the Month-Day-Year format, ’11-6-19′ is parsed to the year 2019. But if yearfirst = True is passed into parse(), it can be parsed to the year 2011.
print (parser.parse( '11-6-19' ))
print (parser.parse( '10-6-25' , yearfirst = True ))
|
Output :
datetime.datetime(2019, 11, 6, 0, 0)
datetime.datetime(2011, 6, 19, 0, 0)
dateutil parser can also do fuzzy parsing and allows to ignore extraneous characters in a datetime string. parse() will raise a ValueError with the default value of False, when it encounters unknown tokens. A datetime object can usually be returned, if fuzzy = True.
Similar Reads
How to Parse a Time String Containing Milliseconds in Python?
In many applications, precise timekeeping is crucial, and that often includes parsing time strings down to the millisecond. Python offers robust tools for handling date and time data, making this task straightforward. This article will guide us through the process of parsing time strings containing
3 min read
Convert the column type from string to datetime format in Pandas dataframe
To perform time-series operations, dates should be in the correct format. Let's learn how to convert a Pandas DataFrame column of strings to datetime format. Pandas Convert Column To DateTime using pd.to_datetime()pd.to_datetime() function in Pandas is the most effective way to handle this conversio
5 min read
Introduction to Python Dateutil Package
The Dateutil is a Python package that enhances Python's built-in datetime module and makes it more flexible and user-friendly when dealing with dates and times. In this article, we will learn about the basics of the python-dateutil package, including its installation, key features, and practical exa
3 min read
Perl - Extracting Date from a String using Regex
In Perl generally, we have to read CSV (Comma Separated Values) files to extract the required data. Sometimes there are dates in the file name like sample 2014-02-12T11:10:10.csv or there could be a column in a file that has a date in it. These dates can be of any pattern like YYYY-MM-DDThh:mm:ss or
5 min read
PHP | date_parse_from_format() Function
The date_parse_from_format() is an inbuilt function in PHP which is used to get information about given date formatted according to the specified format. The date_parse_from_format() function accepts two parameters and returns associative array with detailed information about given date. Syntax: arr
3 min read
How to Convert String to Date Time in Scala?
In this article, we will learn how to convert String to DateTime in Scala. Converting String to DateTime consists of parsing a textual representation of date and time into a structured DateTime object for further manipulation and processing in Scala programs. Table of Content Using SimpleDateFormatU
4 min read
Python - Extract date in String
Given a string, the task is to write a Python program to extract date from it. Input : test_str = "gfg at 2021-01-04" Output : 2021-01-04 Explanation : Date format string found. Input : test_str = "2021-01-04 for gfg" Output : 2021-01-04 Explanation : Date format string found. Method #1 : Using re.s
4 min read
JSTL Formatting <fmt:parseDate> Tag
In JSTL, the formatting <fmt:parseDate> tag is used to parse the date strings into proper java.util.Date objects. This is useful if we need to represent a string in a formatted Date object. Using the tag the tasks of date parsing and formatting can be easily done in Java Server Pages applicati
3 min read
Working with Date and Time in Julia
Julia provides a library to use Dates for dealing with date and time. The Dates module comes inbuilt with the Julia we just need to import it in order to use it. Now we need to prefix every function with an explicit type Dates, e.g Dates.Date. If you donât want to prefix on each function just add us
5 min read
Split Date-Time column into Date and Time variables in R
R programming language provides a variety of ways for dealing with both date and date/time data. The builtin framework as.Date function is responsible for the handling of dates alone, the library chron in R handles both dates and times, without any support for time zones; whereas the POSIXct and POS
4 min read