<jupyter_start><jupyter_text>Investor - Flow of Funds - US  Introduction:Special thanks to: https://github.com/rgrp for sharing the dataset. Step 1. Import the necessary libraries<jupyter_code>import pandas as pd
import numpy as np<jupyter_output><empty_output><jupyter_text>Step 2. Import the dataset from this [address](https://raw.githubusercontent.com/datasets/investor-flow-of-funds-us/master/data/weekly.csv).   Step 3. Assign it to a variable called<jupyter_code>url = 'https://raw.githubusercontent.com/datasets/investor-flow-of-funds-us/master/data/weekly.csv'
df = pd.read_csv(url)
df.head()<jupyter_output><empty_output><jupyter_text>Step 4.  What is the frequency of the dataset?<jupyter_code>_ = pd.to_datetime(df['Date'])
pd.Series([(_.iat[i+1]-_.iat[i]).days for i in range(df.shape[0]-1)]).value_counts()
# frequency seems to be weekly but a few exceptions<jupyter_output><empty_output><jupyter_text>Step 5. Set the column Date as the index.<jupyter_code>df.set_index(df['Date'], drop=True, inplace=True)
df.drop(columns=['Date'], inplace=True)
df.head()<jupyter_output><empty_output><jupyter_text>Step 6. What is the type of the index?<jupyter_code>df.index.dtype, type(df.index)
# -> type 'object'<jupyter_output><empty_output><jupyter_text>Step 7. Set the index to a DatetimeIndex type<jupyter_code>df.index = pd.to_datetime(df.index)
df.index.dtype, type(df.index)<jupyter_output><empty_output><jupyter_text>Step 8.  Change the frequency to monthly, sum the values and assign it to monthly.<jupyter_code># df.resample('M').agg(np.sum)
monthly = df.resample('M').sum()
monthly<jupyter_output><empty_output><jupyter_text>Step 9. You will notice that it filled the dataFrame with months that don't have any data with NaN. Let's drop these rows.<jupyter_code>monthly = monthly[monthly.sum(axis=1)!=0]
monthly<jupyter_output><empty_output><jupyter_text>Step 10. Good, now we have the monthly data. Now change the frequency to year.<jupyter_code>monthly.resample('AS-JAN').sum()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup