# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Working with Text Data in pandas

# +
import pandas as pd

time_sentences = ["Monday: Soccer is at 2:45pm.", 
                  "Tuesday: Appointment is at 11:30 am.",
                  "Wednesday: At 7:00pm, there is a python MOOC!",
                  "Thursday: Beer at 11:15 pm at the latest.",
                  "Friday: Starts at 08:10 am, ends at 09:00am."]

df = pd.DataFrame(time_sentences, columns=['text'])
df
# -

# find the number of chars for each string in df['text']
df['text'].str.len()

# find the number of tokens for each string in df['text']
df['text'].str.split().str.len()

df['text'].str.contains('Appointment')

df['text'].str.count(r'\d')

# find all digits
df['text'].str.findall(r'\d')

# group and find the hours and minutes
df['text'].str.findall(r'(\d?\d):(\d\d)')

# replace weekdays with '???'
df['text'].str.replace(r'\w+day\b', '???')

# replace weekdays with 3 letter abbrevations
df['text'].str.replace(r'(\w+day\b)', lambda x: x.groups()[0][:3])

# create new columns from first match of extracted groups
df['text'].str.extract(r'(\d?\d):(\d\d)', expand=True)

# extract the entire time, the hours, the minutes, and the period
df['text'].str.extractall(r'((\d?\d):(\d\d) ?([ap]m))')

# extract the entire time, the hours, the minutes, and the period with group names
df['text'].str.extractall(r'(?P<time>(?P<hour>\d?\d):(?P<minute>\d\d) ?(?P<period>[ap]m))')

# # Links
#
# - [Coursera: Applied Text Mining in Python](https://www.coursera.org/learn/python-text-mining/home/welcome)
# - [Working with Text Data in Pandas](https://pandas.pydata.org/pandas-docs/stable/text.html)

StarcoderdataJupyterScriptsDedupFiltered