{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction: Get Date/Time Usage\n", "\n", "In this notebook, we will see how to use the `get_datetime_info` function to extract date and time information (month, week, day of week, hour, minute, fraction of day, etc.) from a datetime in a pandas dataframe. This function will provide us with 17 attributes we can use for modeling or to examine patterns." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2018-12-17T16:56:11.010168Z", "start_time": "2018-12-17T16:56:10.576973Z" } }, "outputs": [], "source": [ "# Standard data science libraries\n", "import pandas as pd\n", "import numpy as np\n", "\n", "# Options for pandas\n", "pd.options.display.max_columns = 25\n", "\n", "# Display all cell outputs\n", "from IPython.core.interactiveshell import InteractiveShell\n", "InteractiveShell.ast_node_interactivity = 'all'\n", "\n", "from get_datetime_info import get_datetime_info" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The data we are using is building energy data. It does not really matter what the data is so long as it has a datetime." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2018-12-17T16:56:11.105493Z", "start_time": "2018-12-17T16:56:11.012160Z" } }, "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", "
sensor3
measured_at
2017-12-14 06:00:00-05:006035.214040
2017-12-14 06:15:00-05:006182.405506
2017-12-14 06:30:00-05:006035.187942
2017-12-14 06:45:00-05:006035.192571
2017-12-14 07:00:00-05:006035.198581
\n", "
" ], "text/plain": [ "sensor 3\n", "measured_at \n", "2017-12-14 06:00:00-05:00 6035.214040\n", "2017-12-14 06:15:00-05:00 6182.405506\n", "2017-12-14 06:30:00-05:00 6035.187942\n", "2017-12-14 06:45:00-05:00 6035.192571\n", "2017-12-14 07:00:00-05:00 6035.198581" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = pd.read_csv('building_one_with_tz.csv', header = [0, 1], index_col = 0).loc[:, 'Energy']\n", "data.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will use the `index` here that includes a time zone. __As an important note, the function converts times into local time when calculating the attributes if a time zone is passed in.__ To disable this behavior, do not pass in a time zone." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function takes in a number of parameters:\n", "\n", "* `df`: The dataframe\n", "* `date_col`: a string with the name of the column containing the datetimes. This can also be \"index\" to use the index of the dataframe\n", "* `timezone`: string with the timezone of the building. If provided, the times are converted to local time\n", "* `drop`: boolean for whether the original column should be dropped form the dataframe\n", "\n", "The return is a new dataframe with the 17 columns containing the attributes." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2018-12-17T16:56:11.656744Z", "start_time": "2018-12-17T16:56:11.107185Z" } }, "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", " \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", " \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", " \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", "
sensor3utclocalmeasured_at_Yearmeasured_at_Monthmeasured_at_Weekmeasured_at_Daymeasured_at_Dayofweekmeasured_at_Dayofyearmeasured_at_Is_month_endmeasured_at_Is_month_startmeasured_at_Is_quarter_endmeasured_at_Is_quarter_startmeasured_at_Is_year_endmeasured_at_Is_year_startmeasured_at_Hourmeasured_at_Minutemeasured_at_Secondmeasured_at_FracDaymeasured_at_FracWeek
measured_at
2017-12-14 06:00:00-05:006035.2140402017-12-14 11:00:002017-12-14 06:00:0020171250143348FalseFalseFalseFalseFalseFalse6000.2500000.464286
2017-12-14 06:15:00-05:006182.4055062017-12-14 11:15:002017-12-14 06:15:0020171250143348FalseFalseFalseFalseFalseFalse61500.2604170.465774
2017-12-14 06:30:00-05:006035.1879422017-12-14 11:30:002017-12-14 06:30:0020171250143348FalseFalseFalseFalseFalseFalse63000.2708330.467262
2017-12-14 06:45:00-05:006035.1925712017-12-14 11:45:002017-12-14 06:45:0020171250143348FalseFalseFalseFalseFalseFalse64500.2812500.468750
2017-12-14 07:00:00-05:006035.1985812017-12-14 12:00:002017-12-14 07:00:0020171250143348FalseFalseFalseFalseFalseFalse7000.2916670.470238
\n", "
" ], "text/plain": [ "sensor 3 utc \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 6035.214040 2017-12-14 11:00:00 \n", "2017-12-14 06:15:00-05:00 6182.405506 2017-12-14 11:15:00 \n", "2017-12-14 06:30:00-05:00 6035.187942 2017-12-14 11:30:00 \n", "2017-12-14 06:45:00-05:00 6035.192571 2017-12-14 11:45:00 \n", "2017-12-14 07:00:00-05:00 6035.198581 2017-12-14 12:00:00 \n", "\n", "sensor local measured_at_Year \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 2017-12-14 06:00:00 2017 \n", "2017-12-14 06:15:00-05:00 2017-12-14 06:15:00 2017 \n", "2017-12-14 06:30:00-05:00 2017-12-14 06:30:00 2017 \n", "2017-12-14 06:45:00-05:00 2017-12-14 06:45:00 2017 \n", "2017-12-14 07:00:00-05:00 2017-12-14 07:00:00 2017 \n", "\n", "sensor measured_at_Month measured_at_Week \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 12 50 \n", "2017-12-14 06:15:00-05:00 12 50 \n", "2017-12-14 06:30:00-05:00 12 50 \n", "2017-12-14 06:45:00-05:00 12 50 \n", "2017-12-14 07:00:00-05:00 12 50 \n", "\n", "sensor measured_at_Day measured_at_Dayofweek \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 14 3 \n", "2017-12-14 06:15:00-05:00 14 3 \n", "2017-12-14 06:30:00-05:00 14 3 \n", "2017-12-14 06:45:00-05:00 14 3 \n", "2017-12-14 07:00:00-05:00 14 3 \n", "\n", "sensor measured_at_Dayofyear measured_at_Is_month_end \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 348 False \n", "2017-12-14 06:15:00-05:00 348 False \n", "2017-12-14 06:30:00-05:00 348 False \n", "2017-12-14 06:45:00-05:00 348 False \n", "2017-12-14 07:00:00-05:00 348 False \n", "\n", "sensor measured_at_Is_month_start \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 False \n", "2017-12-14 06:15:00-05:00 False \n", "2017-12-14 06:30:00-05:00 False \n", "2017-12-14 06:45:00-05:00 False \n", "2017-12-14 07:00:00-05:00 False \n", "\n", "sensor measured_at_Is_quarter_end \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 False \n", "2017-12-14 06:15:00-05:00 False \n", "2017-12-14 06:30:00-05:00 False \n", "2017-12-14 06:45:00-05:00 False \n", "2017-12-14 07:00:00-05:00 False \n", "\n", "sensor measured_at_Is_quarter_start \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 False \n", "2017-12-14 06:15:00-05:00 False \n", "2017-12-14 06:30:00-05:00 False \n", "2017-12-14 06:45:00-05:00 False \n", "2017-12-14 07:00:00-05:00 False \n", "\n", "sensor measured_at_Is_year_end measured_at_Is_year_start \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 False False \n", "2017-12-14 06:15:00-05:00 False False \n", "2017-12-14 06:30:00-05:00 False False \n", "2017-12-14 06:45:00-05:00 False False \n", "2017-12-14 07:00:00-05:00 False False \n", "\n", "sensor measured_at_Hour measured_at_Minute \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 6 0 \n", "2017-12-14 06:15:00-05:00 6 15 \n", "2017-12-14 06:30:00-05:00 6 30 \n", "2017-12-14 06:45:00-05:00 6 45 \n", "2017-12-14 07:00:00-05:00 7 0 \n", "\n", "sensor measured_at_Second measured_at_FracDay \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 0 0.250000 \n", "2017-12-14 06:15:00-05:00 0 0.260417 \n", "2017-12-14 06:30:00-05:00 0 0.270833 \n", "2017-12-14 06:45:00-05:00 0 0.281250 \n", "2017-12-14 07:00:00-05:00 0 0.291667 \n", "\n", "sensor measured_at_FracWeek \n", "measured_at \n", "2017-12-14 06:00:00-05:00 0.464286 \n", "2017-12-14 06:15:00-05:00 0.465774 \n", "2017-12-14 06:30:00-05:00 0.467262 \n", "2017-12-14 06:45:00-05:00 0.468750 \n", "2017-12-14 07:00:00-05:00 0.470238 " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_with_info = get_datetime_info(df=data, date_col='index', timezone='America/New_York', drop=False)\n", "data_with_info.head()" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2018-12-17T16:29:00.989468Z", "start_time": "2018-12-17T16:29:00.627458Z" } }, "source": [ "We can also run this without a timezone which means none of the times will be converted." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2018-12-17T16:56:12.115576Z", "start_time": "2018-12-17T16:56:11.659139Z" } }, "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", " \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", " \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", " \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", "
sensor3measured_at_Yearmeasured_at_Monthmeasured_at_Weekmeasured_at_Daymeasured_at_Dayofweekmeasured_at_Dayofyearmeasured_at_Is_month_endmeasured_at_Is_month_startmeasured_at_Is_quarter_endmeasured_at_Is_quarter_startmeasured_at_Is_year_endmeasured_at_Is_year_startmeasured_at_Hourmeasured_at_Minutemeasured_at_Secondmeasured_at_FracDaymeasured_at_FracWeek
measured_at
2017-12-14 06:00:00-05:006035.21404020171250143348FalseFalseFalseFalseFalseFalse11000.4583330.494048
2017-12-14 06:15:00-05:006182.40550620171250143348FalseFalseFalseFalseFalseFalse111500.4687500.495536
2017-12-14 06:30:00-05:006035.18794220171250143348FalseFalseFalseFalseFalseFalse113000.4791670.497024
2017-12-14 06:45:00-05:006035.19257120171250143348FalseFalseFalseFalseFalseFalse114500.4895830.498512
2017-12-14 07:00:00-05:006035.19858120171250143348FalseFalseFalseFalseFalseFalse12000.5000000.500000
\n", "
" ], "text/plain": [ "sensor 3 measured_at_Year measured_at_Month \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 6035.214040 2017 12 \n", "2017-12-14 06:15:00-05:00 6182.405506 2017 12 \n", "2017-12-14 06:30:00-05:00 6035.187942 2017 12 \n", "2017-12-14 06:45:00-05:00 6035.192571 2017 12 \n", "2017-12-14 07:00:00-05:00 6035.198581 2017 12 \n", "\n", "sensor measured_at_Week measured_at_Day \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 50 14 \n", "2017-12-14 06:15:00-05:00 50 14 \n", "2017-12-14 06:30:00-05:00 50 14 \n", "2017-12-14 06:45:00-05:00 50 14 \n", "2017-12-14 07:00:00-05:00 50 14 \n", "\n", "sensor measured_at_Dayofweek measured_at_Dayofyear \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 3 348 \n", "2017-12-14 06:15:00-05:00 3 348 \n", "2017-12-14 06:30:00-05:00 3 348 \n", "2017-12-14 06:45:00-05:00 3 348 \n", "2017-12-14 07:00:00-05:00 3 348 \n", "\n", "sensor measured_at_Is_month_end \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 False \n", "2017-12-14 06:15:00-05:00 False \n", "2017-12-14 06:30:00-05:00 False \n", "2017-12-14 06:45:00-05:00 False \n", "2017-12-14 07:00:00-05:00 False \n", "\n", "sensor measured_at_Is_month_start \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 False \n", "2017-12-14 06:15:00-05:00 False \n", "2017-12-14 06:30:00-05:00 False \n", "2017-12-14 06:45:00-05:00 False \n", "2017-12-14 07:00:00-05:00 False \n", "\n", "sensor measured_at_Is_quarter_end \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 False \n", "2017-12-14 06:15:00-05:00 False \n", "2017-12-14 06:30:00-05:00 False \n", "2017-12-14 06:45:00-05:00 False \n", "2017-12-14 07:00:00-05:00 False \n", "\n", "sensor measured_at_Is_quarter_start \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 False \n", "2017-12-14 06:15:00-05:00 False \n", "2017-12-14 06:30:00-05:00 False \n", "2017-12-14 06:45:00-05:00 False \n", "2017-12-14 07:00:00-05:00 False \n", "\n", "sensor measured_at_Is_year_end measured_at_Is_year_start \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 False False \n", "2017-12-14 06:15:00-05:00 False False \n", "2017-12-14 06:30:00-05:00 False False \n", "2017-12-14 06:45:00-05:00 False False \n", "2017-12-14 07:00:00-05:00 False False \n", "\n", "sensor measured_at_Hour measured_at_Minute \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 11 0 \n", "2017-12-14 06:15:00-05:00 11 15 \n", "2017-12-14 06:30:00-05:00 11 30 \n", "2017-12-14 06:45:00-05:00 11 45 \n", "2017-12-14 07:00:00-05:00 12 0 \n", "\n", "sensor measured_at_Second measured_at_FracDay \\\n", "measured_at \n", "2017-12-14 06:00:00-05:00 0 0.458333 \n", "2017-12-14 06:15:00-05:00 0 0.468750 \n", "2017-12-14 06:30:00-05:00 0 0.479167 \n", "2017-12-14 06:45:00-05:00 0 0.489583 \n", "2017-12-14 07:00:00-05:00 0 0.500000 \n", "\n", "sensor measured_at_FracWeek \n", "measured_at \n", "2017-12-14 06:00:00-05:00 0.494048 \n", "2017-12-14 06:15:00-05:00 0.495536 \n", "2017-12-14 06:30:00-05:00 0.497024 \n", "2017-12-14 06:45:00-05:00 0.498512 \n", "2017-12-14 07:00:00-05:00 0.500000 " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_without_tz = get_datetime_info(df=data, date_col='index', timezone=None, drop=False)\n", "data_without_tz.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All of the attributes are now given in utc time. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use a column (not the index) with the same effect." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2018-12-17T16:56:12.508435Z", "start_time": "2018-12-17T16:56:12.116782Z" } }, "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", " \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", " \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", " \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", "
sensormeasured_at3utclocalmeasured_at_Yearmeasured_at_Monthmeasured_at_Weekmeasured_at_Daymeasured_at_Dayofweekmeasured_at_Dayofyearmeasured_at_Is_month_endmeasured_at_Is_month_startmeasured_at_Is_quarter_endmeasured_at_Is_quarter_startmeasured_at_Is_year_endmeasured_at_Is_year_startmeasured_at_Hourmeasured_at_Minutemeasured_at_Secondmeasured_at_FracDaymeasured_at_FracWeek
02017-12-14 06:00:00-05:006035.2140402017-12-14 11:00:002017-12-14 06:00:0020171250143348FalseFalseFalseFalseFalseFalse6000.2500000.464286
12017-12-14 06:15:00-05:006182.4055062017-12-14 11:15:002017-12-14 06:15:0020171250143348FalseFalseFalseFalseFalseFalse61500.2604170.465774
22017-12-14 06:30:00-05:006035.1879422017-12-14 11:30:002017-12-14 06:30:0020171250143348FalseFalseFalseFalseFalseFalse63000.2708330.467262
32017-12-14 06:45:00-05:006035.1925712017-12-14 11:45:002017-12-14 06:45:0020171250143348FalseFalseFalseFalseFalseFalse64500.2812500.468750
42017-12-14 07:00:00-05:006035.1985812017-12-14 12:00:002017-12-14 07:00:0020171250143348FalseFalseFalseFalseFalseFalse7000.2916670.470238
\n", "
" ], "text/plain": [ "sensor measured_at 3 utc \\\n", "0 2017-12-14 06:00:00-05:00 6035.214040 2017-12-14 11:00:00 \n", "1 2017-12-14 06:15:00-05:00 6182.405506 2017-12-14 11:15:00 \n", "2 2017-12-14 06:30:00-05:00 6035.187942 2017-12-14 11:30:00 \n", "3 2017-12-14 06:45:00-05:00 6035.192571 2017-12-14 11:45:00 \n", "4 2017-12-14 07:00:00-05:00 6035.198581 2017-12-14 12:00:00 \n", "\n", "sensor local measured_at_Year measured_at_Month \\\n", "0 2017-12-14 06:00:00 2017 12 \n", "1 2017-12-14 06:15:00 2017 12 \n", "2 2017-12-14 06:30:00 2017 12 \n", "3 2017-12-14 06:45:00 2017 12 \n", "4 2017-12-14 07:00:00 2017 12 \n", "\n", "sensor measured_at_Week measured_at_Day measured_at_Dayofweek \\\n", "0 50 14 3 \n", "1 50 14 3 \n", "2 50 14 3 \n", "3 50 14 3 \n", "4 50 14 3 \n", "\n", "sensor measured_at_Dayofyear measured_at_Is_month_end \\\n", "0 348 False \n", "1 348 False \n", "2 348 False \n", "3 348 False \n", "4 348 False \n", "\n", "sensor measured_at_Is_month_start measured_at_Is_quarter_end \\\n", "0 False False \n", "1 False False \n", "2 False False \n", "3 False False \n", "4 False False \n", "\n", "sensor measured_at_Is_quarter_start measured_at_Is_year_end \\\n", "0 False False \n", "1 False False \n", "2 False False \n", "3 False False \n", "4 False False \n", "\n", "sensor measured_at_Is_year_start measured_at_Hour measured_at_Minute \\\n", "0 False 6 0 \n", "1 False 6 15 \n", "2 False 6 30 \n", "3 False 6 45 \n", "4 False 7 0 \n", "\n", "sensor measured_at_Second measured_at_FracDay measured_at_FracWeek \n", "0 0 0.250000 0.464286 \n", "1 0 0.260417 0.465774 \n", "2 0 0.270833 0.467262 \n", "3 0 0.281250 0.468750 \n", "4 0 0.291667 0.470238 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_with_info2 = get_datetime_info(data.reset_index(), date_col='measured_at', timezone='America/New_York', drop = False)\n", "data_with_info2.head()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2018-12-17T16:56:12.558293Z", "start_time": "2018-12-17T16:56:12.509716Z" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.all(np.equal(data_with_info.iloc[:, 3:].values, data_with_info2.iloc[:, 4:].values))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we can see, both methods return the same values. \n", "\n", "We now have a dataframe with many date and time attributes that we can use as needed. This function is useful for rapidly creating new features for fitting a model or finding trends in data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "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" }, "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 }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }