<jupyter_start><jupyter_text>___ ___*Copyright <NAME> 2017**For more information, visit us at www.pieriandata.com*  Introduction to Time Series with PandasA lot of our financial data will have a datatime index, so let's learn how to deal with this sort of data with pandas!<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
# To illustrate the order of arguments
my_year = 2017
my_month = 1
my_day = 2
my_hour = 13
my_minute = 30
my_second = 15
# January 2nd, 2017
my_date = datetime(my_year,my_month, my_day)
# Defaults to 0:00
my_date 
# January 2nd, 2017 at 13:30:15
my_date_time = datetime(my_year, my_month, my_day, my_hour, my_minute, my_second)
my_date_time<jupyter_output><empty_output><jupyter_text>You can grab any part of the datetime object you want<jupyter_code>my_date.day
my_date_time.hour<jupyter_output><empty_output><jupyter_text>Pandas with Datetime IndexYou'll usually deal with time series as an index when working with pandas dataframes obtained from some sort of financial API. Fortunately pandas has a lot of functions and methods to work with time series!<jupyter_code># Create an example datetime list/array
first_two = [datetime(2016, 1, 1), datetime(2016, 1, 2)]
first_two
# Converted to an index
dt_ind = pd.DatetimeIndex(first_two)
dt_ind
# Attached to some random data
data = np.random.randn(2, 2)
print(data)
cols = ['A','B']
df = pd.DataFrame(data,dt_ind,cols)
df
df.index
# Latest Date Location
df.index.argmax()
df.index.max()
# Earliest Date Index Location
df.index.argmin()
df.index.min()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08214947498455837, 'fraction_numerical': 0.030265596046942556, 'mean_word_length': 5.982758620689655, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '27809', 'license': 'Apache-2.0', 'n_tokens_mistral': 592, 'n_tokens_neox': 541, 'n_words': 189, 'path': '05-Pandas-with-Time-Series/01 - Datetime Index.ipynb', 'repo_name': 'funeconomist/algorithmic-trading', 'size': '1625'}