You are currently looking at version 1.1 of this notebook. To download notebooks and datafiles, as well as get help on Jupyter notebooks in the Coursera platform, visit the Jupyter Notebook FAQ course resource.
In this assignment, you'll be working with messy medical data and using regex to extract relevant infromation from the data.
Each line of the dates.txt
file corresponds to a medical note. Each note has a date that needs to be extracted, but each date is encoded in one of many formats.
The goal of this assignment is to correctly identify all of the different date variants encoded in this dataset and to properly normalize and sort the dates.
Here is a list of some of the variants you might encounter in this dataset:
Once you have extracted these date patterns from the text, the next step is to sort them in ascending chronological order accoring to the following rules:
With these rules in mind, find the correct date in each note and return a pandas Series in chronological order of the original Series' indices.
For example if the original series was this:
0 1999
1 2010
2 1978
3 2015
4 1985
Your function should return this:
0 2
1 4
2 0
3 1
4 3
Your score will be calculated using Kendall's tau, a correlation measure for ordinal data.
This function should return a Series of length 500 and dtype int.
import pandas as pd
import re
doc = []
with open('dates.txt') as file:
for line in file:
doc.append(line)
df = pd.Series(doc)
df
month_abbr = {"Jan" : 1, "Feb" : 2, "Mar" : 3, "Apr" : 4,
"May" : 5, "Jun" : 6, "Jul" : 7, "Aug" : 8,
"Sep" : 9, "Oct" : 10, "Nov" : 11, "Dec" : 12}
def date_sorter():
data = df.to_frame("Text")
data["Length"] = df.str.len()
days = []
months = []
years = []
for id in list(range(len(data))) :
text = data.iloc[id, 0]
#0 - 124
#Of type mm/dd/yyyy.
if re.search(r'\d{1,2}[/-]\d{1,2}[/-]\d{4}', text) :
date = (re.search(r'\d{1,2}[/-]\d{1,2}[/-]\d{4}', text)).group()
md = int(date.find(r'/'))
dy = int(date.rfind(r'/'))
day = int(date[md + 1 : dy])
month = int(date[0 : md])
year = int(date[-4 :])
#Of type (m)m/(d)d/yy.
elif re.search(r'\d{1,2}/\d{1,2}/\d{2}', text) :
date = re.search(r'\d{1,2}/\d{1,2}/\d{2}', text).group()
md = int(date.find('/'))
dy = int(date.rfind('/'))
day = int(date[md + 1 : dy])
month = int(date[0 : md])
year = 1900 + int(date[-2 :])
#Of type (m)m-(d)d-yy.
elif re.search(r'\d{1,2}-\d{1,2}-\d{2}', text) :
date = re.search(r'\d{1,2}-\d{1,2}-\d{2}', text).group()
md = int(date.find('-'))
dy = int(date.rfind('-'))
day = int(date[md + 1 : dy])
month = int(date[0 : md])
year = 1900 + int(date[-2 :])
#125 - 193
#Of type dd (Mo, Mo., Month)(,) yyyy.
elif re.search(r'\d{1,2}\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*.?\s\d{4}', text) :
date = re.search(r'\d{1,2}\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*.?\s\d{4}', text).group()
dm = int(date.find(' '))
my = int(date.rfind(' '))
day = int(date[0 : dm])
month = month_abbr[date[dm + 1 : dm + 4]]
year = int(date[-4 :])
#194 - 227
#Of type (Mo, Month) dd, yyyy.
elif re.search(r'(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*.?\s\d{2},?\s\d{4}', text) :
date = re.search(r'(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*.?\s\d{2},?\s\d{4}', text).group()
md = int(date.find(' '))
dy = int(date.rfind(' '))
day = int(date[md + 1 : dy - 1])
month = month_abbr[date[0 : 3]]
year = int(date[-4 :])
#228 - 342
#Of type Mo yyyy.
elif re.search(r'(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*,?\s\d{4}', text) :
date = re.search(r'(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*,?\s\d{4}', text).group()
day = 1
month = month_abbr[date[0:3]]
year = int(date[-4:])
#340 - 454
#Of type mm/yyyy.
elif re.search(r'\d{1,2}/\d{4}', text) :
date = re.search(r'\d{1,2}/\d{4}', text).group()
my = int(date.rfind('/'))
day = 1
month = int(date[0 : my])
year = int(date[-4 :])
#Of type mm-yyyy.
elif re.search(r'\d{1,2}-\d{4}', text) :
date = re.search(r'\d{1,2}-\d{4}', text).group()
my = int(date.rfind('-'))
day = 1
month = int(date[0 : my])
year = int(date[-4 :])
#455 - 499
#Of type yyyy.
elif re.search(r'[12]\d{3}', text) :
date = re.search(r'[12]\d{3}', text).group()
day = 1
month = 1
year = int(date)
days.append(day)
months.append(month)
years.append(year)
data["Day"] = days
data["Month"] = months
data["Year"] = years
data.sort_values(by = ["Year", "Month", "Day"], inplace = True)
data.reset_index(inplace = True)
ordered_series = pd.Series(data["index"])
return ordered_series
date_sorter()