Skip to content

Commit

Permalink
testing type hinting and char limits for docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tktran11 committed Nov 2, 2023
1 parent b3a8bce commit e38ee8e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 246 deletions.
264 changes: 41 additions & 223 deletions 00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
"from nbdev.showdoc import *"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| exporti\n",
"\n",
"# allows for type hinting annotations without breaking functionality\n",
"from __future__ import annotations"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -77,23 +89,23 @@
"outputs": [],
"source": [
"#| export\n",
"def file_loader(data_source: str):\n",
"def file_loader(data_source: str|pd.DataFrame) -> pd.DataFrame:\n",
" \"\"\"\n",
" Flexible file loader able to read a single file path or folder path.\n",
" Accepts .csv and .json file format loading.\n",
" \n",
" Parameters\n",
" ----------\n",
" data_source\n",
" String file or folder path. Single .json or .csv paths create\n",
" a pd.DataFrame. Folder paths with files matching the input \n",
" pattern are read together into a single pd.DataFrame.\n",
" String file or folder path. Single .json or .csv paths create a pd.DataFrame. \n",
" Folder paths with files matching the input pattern are read together into a single pd.DataFrame.\n",
" Existing dataframes are read as is.\n",
" \n",
" \n",
" Returns\n",
" -------\n",
" df\n",
" A single dataframe consisting of all data matching the provided\n",
" file or folder path.\n",
" A single dataframe consisting of all data matching the provided file or folder path.\n",
" \n",
" \"\"\"\n",
" if isinstance(data_source, str):\n",
Expand Down Expand Up @@ -376,21 +388,30 @@
"outputs": [],
"source": [
"#| export\n",
"def find_date(data_source, h = 4, date_col = 5):\n",
"def find_date(data_source: str|pd.DataFrame, h:int = 4, date_col:int = 5) -> pd.Series:\n",
" \"\"\"\n",
" Description:\\n\n",
" Extract date information from a column and shift each date in the column by h hours. (Day starts h hours early if h is negative and h hours late if h is positive)\\n\n",
" \n",
" Input:\\n\n",
" - data_source(str, pandas df): input path, file in pickle, csv or pandas dataframe format\\n\n",
" - h(int) : hours to shift the date. For example, when h = 4, everyday starts and ends 4 hours later than normal.\n",
" \n",
" Return:\\n\n",
" - a pandas series represents the date extracted from col.\\n\n",
"\n",
" Requirements:\\n\n",
" Elements in col should be pd.datetime objects\n",
" \n",
" Extracts date from a datetime column and after shifting datetime by 'h' hours.\n",
" (A day starts 'h' hours early if 'h' is negative, or 'h' hours later if 'h' is\n",
" positive.)\n",
" \n",
" Parameters\n",
" ----------\n",
" data_source\n",
" String file or folder path. Single .json or .csv paths create a pd.DataFrame. \n",
" Folder paths with files matching the input pattern are read together into a single pd.DataFrame. Existing\n",
" dataframes are read as is.\n",
" h\n",
" Number of hours to shift the definition for 'date' by. For example, h = 4 would shift days so that time membership\n",
" to each date starts at 4:00 AM and ends at 3:59:59 AM the next calendar day.\n",
" date_col\n",
" Column number for existing datetime column in provided data source. Default column\n",
" number is defined by the accompanying HOWTO document for TREETS.\n",
" \n",
" \n",
" Returns\n",
" -------\n",
" date\n",
" Series of dates.\n",
" \"\"\"\n",
" df = file_loader(data_source)\n",
" # fifth column of food log dataframes should represent date/time in a 24 hour system\n",
Expand All @@ -409,209 +430,6 @@
" return df[col].apply(find_date, args=([h]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"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>ID</th>\n",
" <th>unique_code</th>\n",
" <th>research_info_id</th>\n",
" <th>desc_text</th>\n",
" <th>food_type</th>\n",
" <th>original_logtime</th>\n",
" <th>foodimage_file_name</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>7572733</td>\n",
" <td>alqt14018795225</td>\n",
" <td>150</td>\n",
" <td>Water</td>\n",
" <td>w</td>\n",
" <td>2017-12-08 17:30:00+00:00</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>411111</td>\n",
" <td>alqt14018795225</td>\n",
" <td>150</td>\n",
" <td>Coffee White</td>\n",
" <td>b</td>\n",
" <td>2017-12-09 00:01:00+00:00</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>8409118</td>\n",
" <td>alqt14018795225</td>\n",
" <td>150</td>\n",
" <td>Salad</td>\n",
" <td>f</td>\n",
" <td>2017-12-09 00:58:00+00:00</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>9615131</td>\n",
" <td>alqt14018795225</td>\n",
" <td>150</td>\n",
" <td>Tea Black</td>\n",
" <td>b</td>\n",
" <td>2018-02-22 21:52:00+00:00</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>8100781</td>\n",
" <td>alqt14018795225</td>\n",
" <td>150</td>\n",
" <td>Soup Vegetable</td>\n",
" <td>f</td>\n",
" <td>2018-02-22 22:53:00+00:00</td>\n",
" <td>alqt171220325_f_2018-02-19_12-53-09.jpg</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6849</th>\n",
" <td>1221048</td>\n",
" <td>alqt62359040167</td>\n",
" <td>150</td>\n",
" <td>Rice, Cheese, Corn Tortilla, Mushrooms, Beans</td>\n",
" <td>f</td>\n",
" <td>2020-07-28 08:00:00+00:00</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6850</th>\n",
" <td>5114874</td>\n",
" <td>alqt62359040167</td>\n",
" <td>150</td>\n",
" <td>Eggs, Cheese, Mushrooms</td>\n",
" <td>f</td>\n",
" <td>2020-07-28 13:30:00+00:00</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6851</th>\n",
" <td>5432126</td>\n",
" <td>alqt62359040167</td>\n",
" <td>150</td>\n",
" <td>Orange, Chocolate, Banana Pancakes</td>\n",
" <td>f</td>\n",
" <td>2020-07-29 03:36:00+00:00</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6852</th>\n",
" <td>3810969</td>\n",
" <td>alqt62359040167</td>\n",
" <td>150</td>\n",
" <td>Cheese, Corn Tortilla</td>\n",
" <td>f</td>\n",
" <td>2020-07-29 14:47:00+00:00</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6853</th>\n",
" <td>4341467</td>\n",
" <td>alqt62359040167</td>\n",
" <td>150</td>\n",
" <td>Ramen</td>\n",
" <td>f</td>\n",
" <td>2020-07-29 08:00:00+00:00</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>6854 rows × 7 columns</p>\n",
"</div>"
],
"text/plain": [
" ID unique_code research_info_id \\\n",
"0 7572733 alqt14018795225 150 \n",
"1 411111 alqt14018795225 150 \n",
"2 8409118 alqt14018795225 150 \n",
"3 9615131 alqt14018795225 150 \n",
"4 8100781 alqt14018795225 150 \n",
"... ... ... ... \n",
"6849 1221048 alqt62359040167 150 \n",
"6850 5114874 alqt62359040167 150 \n",
"6851 5432126 alqt62359040167 150 \n",
"6852 3810969 alqt62359040167 150 \n",
"6853 4341467 alqt62359040167 150 \n",
"\n",
" desc_text food_type \\\n",
"0 Water w \n",
"1 Coffee White b \n",
"2 Salad f \n",
"3 Tea Black b \n",
"4 Soup Vegetable f \n",
"... ... ... \n",
"6849 Rice, Cheese, Corn Tortilla, Mushrooms, Beans f \n",
"6850 Eggs, Cheese, Mushrooms f \n",
"6851 Orange, Chocolate, Banana Pancakes f \n",
"6852 Cheese, Corn Tortilla f \n",
"6853 Ramen f \n",
"\n",
" original_logtime foodimage_file_name \n",
"0 2017-12-08 17:30:00+00:00 NaN \n",
"1 2017-12-09 00:01:00+00:00 NaN \n",
"2 2017-12-09 00:58:00+00:00 NaN \n",
"3 2018-02-22 21:52:00+00:00 NaN \n",
"4 2018-02-22 22:53:00+00:00 alqt171220325_f_2018-02-19_12-53-09.jpg \n",
"... ... ... \n",
"6849 2020-07-28 08:00:00+00:00 NaN \n",
"6850 2020-07-28 13:30:00+00:00 NaN \n",
"6851 2020-07-29 03:36:00+00:00 NaN \n",
"6852 2020-07-29 14:47:00+00:00 NaN \n",
"6853 2020-07-29 08:00:00+00:00 NaN \n",
"\n",
"[6854 rows x 7 columns]"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"file_loader('data/test_food_details.csv')"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
Loading

0 comments on commit e38ee8e

Please sign in to comment.