Handling Date and Time Data in Python pandas

In the previous section, we ‘ve already handled any date issues in our dataset, and our dates are also in correct format. To practice some more, let’s perform a few more date operations.

  • From the LoanStartDate, we will extract month in a new column. This will give us some insights into the pattern on when people take new loans.

  • We already have a column called LoanDurationDays. Still, if this column was not present, we could create this column by taking the difference between LoanStartDate and LoanEndDate.

To extract the month from the LoanStartDate column and create a new column, and to calculate the loan duration in days if the LoanDurationDays column was not present, we can use the following code:

# Ensure 'LoanStartDate' and 'LoanEndDate' are in datetime format
loan_data_cleaned['LoanStartDate'] = pd.to_datetime(loan_data_cleaned['LoanStartDate'])
loan_data_cleaned['LoanEndDate'] = pd.to_datetime(loan_data_cleaned['LoanEndDate'])

# Extract the month from 'LoanStartDate' and create a new column
loan_data_cleaned['StartMonth'] = loan_data_cleaned['LoanStartDate'].dt.month

# If 'LoanDurationDays' was not present, calculate it as the difference between 'LoanEndDate' and 'LoanStartDate'
# Uncomment the following line if you want to create 'LoanDurationDays'

# loan_data_cleaned['LoanPeriod'] = (loan_data_cleaned['LoanEndDate'] - loan_data_cleaned['LoanStartDate']).dt.days

# Verify the changes
loan_data_cleaned.head()

This code will first ensure that both LoanStartDate and LoanEndDate are in the correct datetime format. It then extracts the month from LoanStartDate and creates a new column StartMonth. If you need to create the LoanDurationDays column, you can uncomment the relevant line in the code; this will calculate the duration in days as the difference between LoanEndDate and LoanStartDate. The .dt accessor is used to access datetime properties of the columns.

Related Downloads

Finance Train Premium
Accelerate your finance career with cutting-edge data skills.
Join Finance Train Premium for unlimited access to a growing library of ebooks, projects and code examples covering financial modeling, data analysis, data science, machine learning, algorithmic trading strategies, and more applied to real-world finance scenarios.
I WANT TO JOIN
JOIN 30,000 DATA PROFESSIONALS

Free Guides - Getting Started with R and Python

Enter your name and email address below and we will email you the guides for R programming and Python.

Saylient AI Logo

Accelerate your finance career with cutting-edge data skills.

Join Finance Train Premium for unlimited access to a growing library of ebooks, projects and code examples covering financial modeling, data analysis, data science, machine learning, algorithmic trading strategies, and more applied to real-world finance scenarios.