# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Introduction: Get Date/Time Usage
#
# 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.

# +
# Standard data science libraries
import pandas as pd
import numpy as np

# Options for pandas
pd.options.display.max_columns = 25

# Display all cell outputs
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'

from get_datetime_info import get_datetime_info
# -

# 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.

data = pd.read_csv('building_one_with_tz.csv', header = [0, 1], index_col = 0).loc[:, 'Energy']
data.head()

# 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.

# The function takes in a number of parameters:
#
# * `df`: The dataframe
# * `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
# * `timezone`: string with the timezone of the building. If provided, the times are converted to local time
# * `drop`: boolean for whether the original column should be dropped form the dataframe
#
# The return is a new dataframe with the 17 columns containing the attributes.

data_with_info = get_datetime_info(df=data, date_col='index', timezone='America/New_York', drop=False)
data_with_info.head()

# We can also run this without a timezone which means none of the times will be converted.

data_without_tz = get_datetime_info(df=data, date_col='index', timezone=None, drop=False)
data_without_tz.head()

# All of the attributes are now given in utc time. 

# We can also use a column (not the index) with the same effect.

data_with_info2 = get_datetime_info(data.reset_index(), date_col='measured_at', timezone='America/New_York', drop = False)
data_with_info2.head()

np.all(np.equal(data_with_info.iloc[:, 3:].values, data_with_info2.iloc[:, 4:].values))

# As we can see, both methods return the same values. 
#
# 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.



StarcoderdataJupyterScriptsDedupFiltered