datalife-2019/notebook.ipynb
2020-01-18 04:43:13 +11:00

174 lines
4.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"colNames = ['date', 'value']\n",
"meditateChecks = pd.read_csv('LoopHabits/002 Meditate/Checkmarks.csv', names=colNames, header=None)\n",
"meditateScore = pd.read_csv('LoopHabits/002 Meditate/Scores.csv', names=colNames, header=None)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [],
"source": [
"def only2019(df):\n",
" df['drop'] = df['date'].apply(lambda x: 0 if x[0:4] == '2019' else 1)\n",
" df = df.drop(df[df['drop'] == 1].index)\n",
" df = df.drop(columns=['drop'])\n",
" return df\n",
"\n",
"def onlyMarch(df):\n",
" # For habits only, lost my phone in late Febuary, didn't have a recent backup\n",
" df['drop'] = df['date'].apply(lambda x: 1 if x[5:7] == '01' or x[5:7] == '02' else 0)\n",
" df = df.drop(df[df['drop'] == 1].index)\n",
" df = df.drop(columns=['drop'])\n",
" return df\n",
"\n",
"def longestZero(df):\n",
" # reverse the series so dates are ascending (increasing?)\n",
" # pad out the series with 0s, then diff it to track total, use that total to calculate highest streak\n",
" # streak increases when sum is 0\n",
" diffStreak = pd.concat([pd.Series([0]) , df[::-1].value, pd.Series([0])]).diff().tolist()\n",
" runningSum = 0\n",
" streak = 0\n",
" streakList = []\n",
" for e in diffStreak[1:-1]:\n",
" runningSum += e\n",
" if runningSum == 0:\n",
" streak += 1\n",
" else:\n",
" streakList.append(streak)\n",
" streak = 0\n",
" return sum(streakList)\n",
"\n",
"def longestStreak(df):\n",
" # reverse the series so dates are ascending (increasing?)\n",
" # pad out the series with 0s, then diff it to track total, use that total to calculate highest streak\n",
" # streak increases when sum is non-zero\n",
" diffStreak = pd.concat([pd.Series([0]) , df[::-1].value, pd.Series([0])]).diff().tolist()\n",
" runningSum = 0\n",
" streak = 0\n",
" streakList = []\n",
" for e in diffStreak[1:-1]:\n",
" runningSum += e\n",
" if runningSum != 0:\n",
" streak += 1\n",
" else:\n",
" streakList.append(streak)\n",
" streak = 0\n",
" return sum(streakList)\n",
"\n",
"def bestMonth(df):\n",
" pass\n",
"\n",
"def worstMonth(df):\n",
" pass\n",
"\n",
"def averagePerMonth(df):\n",
" pass\n",
"\n",
"def plotScore(df):\n",
" pass\n",
"\n",
"def countZeros(df):\n",
" return (df.value == 0).astype(int).sum()\n",
"\n",
"def countValue(countValue, df):\n",
" return (df.value == countValue).astype(int).sum()\n"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Days done: 72\n",
"Non-streak days: 195\n",
"Streak days: 39\n"
]
}
],
"source": [
"print(\"Days done: \" + str(countValue(2, onlyMarch(only2019(meditateChecks)))))\n",
"print(\"Non-streak days: \" + str(countZeros(onlyMarch(only2019(meditateChecks)))))\n",
"print(\"Streak days: \" + str(countValue(1, onlyMarch(only2019(meditateChecks)))))"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"306\n"
]
},
{
"data": {
"text/plain": [
"299"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(72 + 195 + 39)\n",
"longestStreak(onlyMarch(only2019(meditateChecks))) + longestZero(onlyMarch(only2019(meditateChecks)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Why don't the above two numbers match??"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}