calculated streaks from habit checks

This commit is contained in:
Anish 2020-01-18 04:43:13 +11:00
parent 46bbc71751
commit 1b262f9f88

View file

@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -11,18 +11,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 59, "execution_count": 6,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"colNames = ['date', 'check']\n", "colNames = ['date', 'value']\n",
"meditateChecks = pd.read_csv('LoopHabits/002 Meditate/Checkmarks.csv', names=colNames, header=None)\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)" "meditateScore = pd.read_csv('LoopHabits/002 Meditate/Scores.csv', names=colNames, header=None)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 87, "execution_count": 67,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -33,16 +33,45 @@
" return df\n", " return df\n",
"\n", "\n",
"def onlyMarch(df):\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['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(df[df['drop'] == 1].index)\n",
" df = df.drop(columns=['drop'])\n", " df = df.drop(columns=['drop'])\n",
" return df\n", " return df\n",
"\n", "\n",
"def longestZero(df):\n", "def longestZero(df):\n",
" pass\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", "\n",
"def longestStreak(df):\n", "def longestStreak(df):\n",
" pass\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", "\n",
"def bestMonth(df):\n", "def bestMonth(df):\n",
" pass\n", " pass\n",
@ -50,425 +79,73 @@
"def worstMonth(df):\n", "def worstMonth(df):\n",
" pass\n", " pass\n",
"\n", "\n",
"def averagePerMonth(df):\n",
" pass\n",
"\n",
"def plotScore(df):\n", "def plotScore(df):\n",
" pass" " 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", "cell_type": "code",
"execution_count": 88, "execution_count": 69,
"metadata": {}, "metadata": {},
"outputs": [ "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": { "data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>date</th>\n",
" <th>check</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>2019-12-31</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>2019-12-30</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>2019-12-29</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>2019-12-28</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>2019-12-27</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>2019-12-26</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>2019-12-25</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>2019-12-24</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25</th>\n",
" <td>2019-12-23</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>2019-12-22</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>27</th>\n",
" <td>2019-12-21</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>28</th>\n",
" <td>2019-12-20</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>29</th>\n",
" <td>2019-12-19</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>30</th>\n",
" <td>2019-12-18</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>31</th>\n",
" <td>2019-12-17</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>32</th>\n",
" <td>2019-12-16</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>33</th>\n",
" <td>2019-12-15</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34</th>\n",
" <td>2019-12-14</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>35</th>\n",
" <td>2019-12-13</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>36</th>\n",
" <td>2019-12-12</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>37</th>\n",
" <td>2019-12-11</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>38</th>\n",
" <td>2019-12-10</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>39</th>\n",
" <td>2019-12-09</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>40</th>\n",
" <td>2019-12-08</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>41</th>\n",
" <td>2019-12-07</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>42</th>\n",
" <td>2019-12-06</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>43</th>\n",
" <td>2019-12-05</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>44</th>\n",
" <td>2019-12-04</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>45</th>\n",
" <td>2019-12-03</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>46</th>\n",
" <td>2019-12-02</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>293</th>\n",
" <td>2019-03-30</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>294</th>\n",
" <td>2019-03-29</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>295</th>\n",
" <td>2019-03-28</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>296</th>\n",
" <td>2019-03-27</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>297</th>\n",
" <td>2019-03-26</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>298</th>\n",
" <td>2019-03-25</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>299</th>\n",
" <td>2019-03-24</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>300</th>\n",
" <td>2019-03-23</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>301</th>\n",
" <td>2019-03-22</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>302</th>\n",
" <td>2019-03-21</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>303</th>\n",
" <td>2019-03-20</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>304</th>\n",
" <td>2019-03-19</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>305</th>\n",
" <td>2019-03-18</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>306</th>\n",
" <td>2019-03-17</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>307</th>\n",
" <td>2019-03-16</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>308</th>\n",
" <td>2019-03-15</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>309</th>\n",
" <td>2019-03-14</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>310</th>\n",
" <td>2019-03-13</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>311</th>\n",
" <td>2019-03-12</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>312</th>\n",
" <td>2019-03-11</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>313</th>\n",
" <td>2019-03-10</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>314</th>\n",
" <td>2019-03-09</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>315</th>\n",
" <td>2019-03-08</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>316</th>\n",
" <td>2019-03-07</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>317</th>\n",
" <td>2019-03-06</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>318</th>\n",
" <td>2019-03-05</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>319</th>\n",
" <td>2019-03-04</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>320</th>\n",
" <td>2019-03-03</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>321</th>\n",
" <td>2019-03-02</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>322</th>\n",
" <td>2019-03-01</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>306 rows × 2 columns</p>\n",
"</div>"
],
"text/plain": [ "text/plain": [
" date check\n", "299"
"17 2019-12-31 0\n",
"18 2019-12-30 0\n",
"19 2019-12-29 0\n",
"20 2019-12-28 0\n",
"21 2019-12-27 0\n",
"22 2019-12-26 0\n",
"23 2019-12-25 0\n",
"24 2019-12-24 2\n",
"25 2019-12-23 1\n",
"26 2019-12-22 1\n",
"27 2019-12-21 2\n",
"28 2019-12-20 2\n",
"29 2019-12-19 0\n",
"30 2019-12-18 0\n",
"31 2019-12-17 0\n",
"32 2019-12-16 2\n",
"33 2019-12-15 0\n",
"34 2019-12-14 0\n",
"35 2019-12-13 2\n",
"36 2019-12-12 0\n",
"37 2019-12-11 0\n",
"38 2019-12-10 0\n",
"39 2019-12-09 0\n",
"40 2019-12-08 0\n",
"41 2019-12-07 0\n",
"42 2019-12-06 0\n",
"43 2019-12-05 0\n",
"44 2019-12-04 0\n",
"45 2019-12-03 0\n",
"46 2019-12-02 0\n",
".. ... ...\n",
"293 2019-03-30 1\n",
"294 2019-03-29 1\n",
"295 2019-03-28 2\n",
"296 2019-03-27 2\n",
"297 2019-03-26 0\n",
"298 2019-03-25 0\n",
"299 2019-03-24 2\n",
"300 2019-03-23 0\n",
"301 2019-03-22 1\n",
"302 2019-03-21 2\n",
"303 2019-03-20 0\n",
"304 2019-03-19 2\n",
"305 2019-03-18 0\n",
"306 2019-03-17 0\n",
"307 2019-03-16 2\n",
"308 2019-03-15 0\n",
"309 2019-03-14 1\n",
"310 2019-03-13 2\n",
"311 2019-03-12 0\n",
"312 2019-03-11 2\n",
"313 2019-03-10 0\n",
"314 2019-03-09 0\n",
"315 2019-03-08 0\n",
"316 2019-03-07 0\n",
"317 2019-03-06 0\n",
"318 2019-03-05 0\n",
"319 2019-03-04 0\n",
"320 2019-03-03 0\n",
"321 2019-03-02 0\n",
"322 2019-03-01 0\n",
"\n",
"[306 rows x 2 columns]"
] ]
}, },
"execution_count": 88, "execution_count": 71,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"onlyMarch(only2019(meditateChecks))" "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??"
] ]
} }
], ],