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

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book includes PDFs, explanations, instructions, data files, and R code for all examples.

Get the Bundle for $39 (Regular $57)
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.

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book comes with PDFs, detailed explanations, step-by-step instructions, data files, and complete downloadable R code for all examples.