Majority of the data is in date-time series
Datetime function
from datetime import datetime
import pandas as pd
my_date_time = datetime(2023, 01, 05, 12,35,49)
# can access month, year, etc
my_date_time.month # output is 01
# Pandas datetime columns
date_time_indx = pd.DatetimeIndex(col_name)
# In time series analysis, our index is time, and we can do bunch of analysis on it
# Max index
df.index.argmax()
# Max value
df.index.max()
Time Resampling
df.resample(rule='A').mean() # A here represents year end frequency
df.resample(rule='Q').max() # Quarterly max
# Make our own customization functions
def first_day(entry):
return entry[0]
# Apply the function to our resampled df
df.resample('A').apply(first_day)
Time Shifts
# Time shift example -- Shift everything down by 1
df.shift(periods = 1)
# To shift backwards, you just set the periods to negative
df.shift(periods=-1)
# T shift
df.tshift(freq='M') # Shift all index dates to the end of the month
df.tshift(freq='A') # Shift all index dates to the end of the month
Pandas Rolling and Expanding — Creating Simple Moving Averages
df.rolling(7).mean()
# Give us the moving average after every 7 days
# Can also change it to different days or do it on specific columns
df['Open'].rolling(30).mean() # Moving average for opening price for 30 days
df['Close'].expanding(min_periods=1).mean()
# At each paticular point on the x axis, it just means that it takes
# into account everything before it
Bullenger Bands — One technical indicator
# Close 20 day moving average stock price
df['Close: 20 Day moving Average'] = df['Close'].rolling(20).mean()
# upper = 20MA + 2std(20)
df['Upper'] = df['Close: 20 Day moving Average'] + 2*df['Close'].rolling(20).std()
# lower = 20MA - 2std(20)
df['Lower'] = df['Close: 20 Day moving Average'] - 2*df['Close'].rolling(20).std()
# Close
df['Close']
# plot them all in 1 plot
df[['Close', 'Upper', 'Lower', 'Close: 20 Day moving Average']].plot(figsize(16,6))
Agenda: