import re
import pandas as pd
from sqlalchemy import create_engine
Sleep Data
Load sleep data into PostgreSQL collected from the Sleep Cycle phone app
= "./data/sleepdata.csv" sleepdata
Database credentials
with open("../../postgres.txt") as f:
= (s.strip() for s in f.readlines()) user, pwd
Create DataFrame
= pd.read_csv(sleepdata, delimiter=";")
df df.dtypes
Start object
End object
Sleep Quality object
Regularity object
Mood float64
Heart rate (bpm) int64
Steps int64
Alarm mode object
Air Pressure (Pa) float64
City object
Movements per hour float64
Time in bed (seconds) float64
Time asleep (seconds) float64
Time before sleep (seconds) float64
Window start object
Window stop object
Did snore bool
Snore time float64
Weather temperature (°F) float64
Weather type object
Notes object
dtype: object
Create DB Safe Column Names
= re.compile(r"[^\w]")
nonword_pattern = re.compile(r"[_]{2,}")
spacing_pattern = re.compile(r"_$")
ending_pattern = [
df.columns
re.sub(
ending_pattern,"",
"_", re.sub(nonword_pattern, "_", col)),
re.sub(spacing_pattern,
).lower()for col in df.columns
] df.columns
Index(['start', 'end', 'sleep_quality', 'regularity', 'mood', 'heart_rate_bpm',
'steps', 'alarm_mode', 'air_pressure_pa', 'city', 'movements_per_hour',
'time_in_bed_seconds', 'time_asleep_seconds',
'time_before_sleep_seconds', 'window_start', 'window_stop', 'did_snore',
'snore_time', 'weather_temperature_f', 'weather_type', 'notes'],
dtype='object')
= "localhost"
db_host = "sleepdata"
db_table = "personal"
db_name
= create_engine(f"postgresql://{user}:{pwd}@{db_host}/{db_name}")
engine
f"{db_table}", engine, if_exists="replace", index=False) df.to_sql(
509