{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2021-06-29T05:20:26.267026Z", "start_time": "2021-06-29T05:20:26.259203Z" } }, "outputs": [], "source": [ "from datetime import datetime, date\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2021-06-29T05:10:11.339694Z", "start_time": "2021-06-29T05:10:11.315592Z" } }, "outputs": [ { "data": { "text/plain": [ "'1.3.2'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## inline date elements" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2021-06-29T05:20:52.097514Z", "start_time": "2021-06-29T05:20:52.067371Z" } }, "outputs": [], "source": [ "from datetime import date\n", "import pandas as pd\n", "\n", "df = pd.DataFrame({\n", " \"name\": [\"alice\", \"bob\", \"charlie\"],\n", " \"date_of_birth\": [date(1999,4,1), date(2001,12,15), date(1985,4,24)] \n", "})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## pandas timestamp now" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2021-06-29T05:10:12.191183Z", "start_time": "2021-06-29T05:10:12.118037Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
nameage
0alice12
1bob43
2charlie22
3david34
\n", "
" ], "text/plain": [ " name age\n", "0 alice 12\n", "1 bob 43\n", "2 charlie 22\n", "3 david 34" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame({\n", " \"name\":[\"alice\",\"bob\",\"charlie\", \"david\"],\n", " \"age\":[12,43,22,34]\n", "})\n", "df" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2021-06-29T05:10:31.467561Z", "start_time": "2021-06-29T05:10:31.445164Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
nameagetime
0alice122022-05-05 00:46:43.273572
1bob432022-05-05 00:46:43.273572
2charlie222022-05-05 00:46:43.273572
3david342022-05-05 00:46:43.273572
\n", "
" ], "text/plain": [ " name age time\n", "0 alice 12 2022-05-05 00:46:43.273572\n", "1 bob 43 2022-05-05 00:46:43.273572\n", "2 charlie 22 2022-05-05 00:46:43.273572\n", "3 david 34 2022-05-05 00:46:43.273572" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[\"time\"] = pd.Timestamp(datetime.now())\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## timestamp to string" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2021-06-29T05:14:12.468050Z", "start_time": "2021-06-29T05:14:12.439949Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
nameagetimestamp_col
0alice122022-05-05 00:46:43.285148
1bob432022-05-05 00:46:43.285148
2charlie222022-05-05 00:46:43.285148
3david342022-05-05 00:46:43.285148
\n", "
" ], "text/plain": [ " name age timestamp_col\n", "0 alice 12 2022-05-05 00:46:43.285148\n", "1 bob 43 2022-05-05 00:46:43.285148\n", "2 charlie 22 2022-05-05 00:46:43.285148\n", "3 david 34 2022-05-05 00:46:43.285148" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame({\n", " \"name\":[\"alice\",\"bob\",\"charlie\", \"david\"],\n", " \"age\":[12,43,22,34]\n", "})\n", "\n", "df[\"timestamp_col\"] = pd.Timestamp(datetime.now())\n", "df" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2021-06-29T05:14:12.704240Z", "start_time": "2021-06-29T05:14:12.583792Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
nameagetimestamp_colformatted_col
0alice122022-05-05 00:46:43.28514805-05-2022
1bob432022-05-05 00:46:43.28514805-05-2022
2charlie222022-05-05 00:46:43.28514805-05-2022
3david342022-05-05 00:46:43.28514805-05-2022
\n", "
" ], "text/plain": [ " name age timestamp_col formatted_col\n", "0 alice 12 2022-05-05 00:46:43.285148 05-05-2022\n", "1 bob 43 2022-05-05 00:46:43.285148 05-05-2022\n", "2 charlie 22 2022-05-05 00:46:43.285148 05-05-2022\n", "3 david 34 2022-05-05 00:46:43.285148 05-05-2022" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[\"formatted_col\"] = df[\"timestamp_col\"].map(lambda ts: ts.strftime(\"%d-%m-%Y\"))\n", "\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## group by year\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
year_of_birthcount
019981
119992
220011
\n", "
" ], "text/plain": [ " year_of_birth count\n", "0 1998 1\n", "1 1999 2\n", "2 2001 1" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "df = pd.DataFrame({\n", " 'name': ['alice','bob','charlie', 'david'],\n", " 'date_of_birth': ['2001-05-27','1999-02-16','1998-09-25', '1999-01-01']\n", "})\n", "\n", "df['date_of_birth'] = pd.to_datetime(df['date_of_birth'])\n", "\n", "# step 1: create a 'year' column\n", "df['year_of_birth'] = df['date_of_birth'].map(lambda dt: dt.strftime('%Y'))\n", "\n", "# step 2: group by the created columns\n", "grouped_df = df.groupby('year_of_birth').size()\n", "\n", "grouped_df.to_frame('count').reset_index()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## group by start of week" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
purchase_start_of_weekcount
02001-01-153
12001-01-222
22001-01-291
\n", "
" ], "text/plain": [ " purchase_start_of_week count\n", "0 2001-01-15 3\n", "1 2001-01-22 2\n", "2 2001-01-29 1" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from datetime import timedelta\n", "\n", "import pandas as pd\n", "\n", "df = pd.DataFrame({\n", " 'item': ['a', 'b', 'c', 'd', 'e', 'f'],\n", " 'purchase_date': ['2001-01-15', '2001-01-18','2001-01-21','2001-01-24', '2001-01-27', '2001-01-30']\n", "})\n", "\n", "# convert values to datetime type\n", "df['purchase_date'] = pd.to_datetime(df['purchase_date'])\n", "\n", "\n", "df['purchase_start_of_week'] = df['purchase_date'].map(lambda dt: dt - timedelta(days=dt.weekday()))\n", "\n", "grouped_df = df.groupby('purchase_start_of_week').size()\n", "\n", "grouped_df.to_frame('count').reset_index()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }