
[Python-Financial] Net Present Value (NPV) method to calculate the net present value of investment projects
- Importing Libraries:
import pandas as pd
import datetime as dt
This part imports the necessary libraries for the code. `pandas` is imported as `pd` to provide functionality for data manipulation and analysis. `datetime` is imported as `dt` to work with dates and times.
2. Defining the Function:
def calculate_npv(investment, start_date, end_date, tax_rate, discount_rate, scrap_value, cash_flow):This is the definition of the `calculate_npv` function. It takes in several parameters: `investment`, `start_date`, `end_date`, `tax_rate`, `discount_rate`, `scrap_value`, and `cash_flow`. This function calculates the Net Present Value (NPV) of a project based on the provided inputs.
3. Calculating Project Life:
project_life = round((end_date - start_date).days / 365, 0)This line calculates the project life in years by subtracting the `start_date` from the `end_date` and dividing the number of days by 365. The result is rounded to the nearest whole number.
4. Creating a DataFrame:
df = pd.DataFrame(cash_flow)This line creates a DataFrame called `df` using the `cash_flow` dictionary. The DataFrame will have columns for ‘Date’, ‘Cash Inflow’, and ‘Cash Outflow’.
5. Calculating Period and Depreciation:
df['Period'] = round((df['Date'] - start_date).dt.days / 365, 2)
df['Depreciation'] = (investment - scrap_value) / project_life * (df['Period'] - df['Period'].shift(1).fillna(0))These lines calculate the period and depreciation for each entry in the DataFrame. The ‘Period’ column is calculated by subtracting the `start_date` from the ‘Date’ column and dividing the number of days by 365. The ‘Depreciation’ column is calculated based on the investment, scrap value, project life, and the difference in periods with consecutive rows
6. Calculating Pre-Tax Profit, Income Tax, Net Profit, and Net Cash Flow:
df['Pre-Tax Profit'] = df['Cash Inflow'] - df['Cash Outflow'] - df['Depreciation']
df['Income Tax'] = df['Pre-Tax Profit'] * tax_rate
df['Net Profit'] = df['Pre-Tax Profit'] - df['Income Tax']
df['Net Cash Flow'] = df['Net Profit'] + df['Depreciation']These lines calculate the pre-tax profit, income tax, net profit, and net cash flow for each entry in the DataFrame. The calculations are based on the cash inflow, cash outflow, depreciation, and tax rate
7. Calculating Discount Factor and Discounted Cash Flow:
df['Discount Factor'] = (1 + discount_rate) ** (df['Period'])
df['Discounted Cash Flow'] = df['Net Cash Flow'] / df['Discount Factor']These lines calculate the discount factor and discounted cash flow for each entry in the DataFrame. The discount factor is calculated by raising (1 + discount_rate) to the power of the period. The discounted cash flow is calculated by dividing the net cash flow by the discount factor
8. Calculating Net Present Value:
NPV = df['Discounted Cash Flow'].sum() - investmentThis line calculates the Net Present Value (NPV) of the project by summing the discounted cash flows from the DataFrame and subtracting the initial investment.
9. Printing the Result:
print("The Net Present Value of the project is:", npv)This line prints the calculated NPV of the project.
The code demonstrates how to use the calculate_npv function to calculate the NPV of a project based on the provided inputs. It utilizes a DataFrame to organize and perform calculations on the cash flow data.





