{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport polars as pl\nimport datetime\nfrom matplotlib import pyplot as plt\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.712887,"end_time":"2023-11-09T15:31:17.624861","exception":false,"start_time":"2023-11-09T15:31:16.911974","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-09T16:17:24.583955Z","iopub.execute_input":"2023-11-09T16:17:24.584323Z","iopub.status.idle":"2023-11-09T16:17:24.591905Z","shell.execute_reply.started":"2023-11-09T16:17:24.584295Z","shell.execute_reply":"2023-11-09T16:17:24.590843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df_submission = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/sample_submission.csv')\n#df_pq_test = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet')\n#df_pq_train = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet')\ndf_train_events = pl.read_csv(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv\")","metadata":{"papermill":{"duration":0.203303,"end_time":"2023-11-09T15:31:17.834011","exception":false,"start_time":"2023-11-09T15:31:17.630708","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-09T16:17:24.593407Z","iopub.execute_input":"2023-11-09T16:17:24.593694Z","iopub.status.idle":"2023-11-09T16:17:24.607420Z","shell.execute_reply.started":"2023-11-09T16:17:24.593669Z","shell.execute_reply":"2023-11-09T16:17:24.606014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_events = df_train_events.with_columns(\n    pl.col(\"timestamp\").apply(lambda x: x[-5:]).alias(\"timezone_offset\"),\n    pl.col(\"timestamp\").apply(lambda x: datetime.datetime.strptime(str(x), '%Y-%m-%dT%H:%M:%S%z')).alias(\"timestamp\"),\n)","metadata":{"papermill":{"duration":0.342846,"end_time":"2023-11-09T15:31:18.180532","exception":false,"start_time":"2023-11-09T15:31:17.837686","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-09T16:17:24.610966Z","iopub.execute_input":"2023-11-09T16:17:24.611300Z","iopub.status.idle":"2023-11-09T16:17:24.767809Z","shell.execute_reply.started":"2023-11-09T16:17:24.611271Z","shell.execute_reply":"2023-11-09T16:17:24.767049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_df(df, num=3, tail=True):\n    print(df.shape)\n    display(df.head(num))\n    if tail:\n        display(df.tail(num))","metadata":{"execution":{"iopub.status.busy":"2023-11-09T16:17:24.769781Z","iopub.execute_input":"2023-11-09T16:17:24.770084Z","iopub.status.idle":"2023-11-09T16:17:24.776337Z","shell.execute_reply.started":"2023-11-09T16:17:24.770059Z","shell.execute_reply":"2023-11-09T16:17:24.774822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_events = df_train_events.with_columns(\n    df_train_events['timestamp'].apply(lambda x: x.year).alias(\"year\"),\n    df_train_events['timestamp'].apply(lambda x: x.month).alias(\"month\"),\n    df_train_events['timestamp'].apply(lambda x: x.day).alias(\"day\"),\n    df_train_events['timestamp'].apply(lambda x: x.hour).alias(\"hour\"),\n    df_train_events['timestamp'].apply(lambda x: x.minute).alias(\"minute\"),\n    df_train_events['timestamp'].apply(lambda x: x.second).alias(\"second\"),\n)\n\nshow_df(df_train_events)\n\n\n\n\n\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-09T16:17:24.777950Z","iopub.execute_input":"2023-11-09T16:17:24.778330Z","iopub.status.idle":"2023-11-09T16:17:24.876207Z","shell.execute_reply.started":"2023-11-09T16:17:24.778298Z","shell.execute_reply":"2023-11-09T16:17:24.874880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\n\nplt.rcParams[\"figure.figsize\"] = [12.00, 5]\nplt.rcParams[\"figure.autolayout\"] = True\n\ndf = pd.DataFrame(dict(day=np.array(df_train_events[\"day\"].take_every(12*60))))\n\nax = sns.countplot(x=\"day\", data=df)\n\nfor p in ax.patches:\n   ax.annotate('{:.1f}'.format(p.get_height()), (p.get_x()+0.25, p.get_height()+0.01))\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-09T16:17:24.877972Z","iopub.execute_input":"2023-11-09T16:17:24.878792Z","iopub.status.idle":"2023-11-09T16:17:25.185281Z","shell.execute_reply.started":"2023-11-09T16:17:24.878493Z","shell.execute_reply":"2023-11-09T16:17:25.183340Z"},"trusted":true},"execution_count":null,"outputs":[]}]}