<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv('walmart_stock.csv')<jupyter_output><empty_output><jupyter_text>Other way (less customizable but easy)```df = pd.read_csv('walmart_stock.csv', index_col='Date', parse_dates=True)```<jupyter_code>df.head()
df.info() #in the info we see that date column is a string and not a datetime object
df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d') #YYYYY-MM-DD
#overwriting df['Date'] as coverted datetime by using pd.to_datetime()
df.head()
df.info() #confirming that date column is a datetime object<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 1258 entries, 0 to 1257
Data columns (total 7 columns):
 #   Column     Non-Null Count  Dtype         
---  ------     --------------  -----         
 0   Date       1258 non-null   datetime64[ns]
 1   Open       1258 non-null   float64       
 2   High       1258 non-null   float64       
 3   Low        1258 non-null   float64       
 4   Close      1258 non-null   float64       
 5   Volume     1258 non-null   int64         
 6   Adj Close  1258 non-null   float64       
dtypes: datetime64[ns](1), float64(5), int64(1)
memory usage: 68.9 KB<jupyter_text>Other way ```df['Date'] = df['Date'].apply(pd.to_datetime)```<jupyter_code>df.set_index('Date', inplace=True)
df.head()
df_new = pd.read_csv('walmart_stock.csv', index_col='Date', parse_dates=True)
df_new.index<jupyter_output><empty_output><jupyter_text>Resampling just works like groupby for time series objects. It groups the data by the rule specified as argument```example_df.resample(rule='rule').aggregate_function()```This groups the data in the dataframe by the specified rule (eg monthly, quarterly, yearly) and applies the specified aggregate function on it<jupyter_code>df.resample(rule='A').mean() #gives mean of each year in the data 
#here A = Annual (starts counting by the end of each year.... also visible in the output [2012-12-31] etc)
# B = business day end etc etc
df.resample(rule='BQ').mean() #quarterly mean
# we can apply our own functions also 
def first_day(entry):
    return entry[0]
df.resample(rule='A').apply(first_day)
df['Close'].resample('M').mean().plot(kind ='bar',figsize=(16,6))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13258232235701906, 'fraction_numerical': 0.03596187175043328, 'mean_word_length': 3.9549356223175964, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '473119', 'license': 'MIT', 'n_tokens_mistral': 819, 'n_tokens_neox': 762, 'n_words': 220, 'path': 'Udemy/Python for Financial Analysis by Jose Portilla/time_resampling.ipynb', 'repo_name': 'aayush1036/OnlineCourses', 'size': '2308'}