Date and time in python
In this blog, I will discuss how to convert date time column from string to pandas._libs.tslibs.timestamps.Timestamp format using pd.to_datetime or datetime.datetime format using datetime.datetime.strptime function. I will also discuss ways to change a datetime column into other formats.
At first, import pandas and datetime libraries
import datetimeimport pandas as pd
- Conversion of dates to standard format using pd.to_datetime
# Using pd.to_datetimesample_dates = ('03/11/2021', '03-Nov-2021', 'Nov 03 21', '11/03/2021')date1 = pd.to_datetime(sample_dates[0], format = '%d/%m/%Y')date2 = pd.to_datetime(sample_dates[1], format = '%d-%b-%Y')date3 = pd.to_datetime(sample_dates[2], format = '%b %d %y')date4 = pd.to_datetime(sample_dates[3], format = '%m/%d/%Y')print('date1 = ', date1, ', date2 = ', date2, ', date3 = ', date3, ' , date4 = ', date4)
Output:
date1 = 2021-11-03 00:00:00 , date2 = 2021-11-03 00:00:00 , date3 = 2021-11-03 00:00:00 , date4 = 2021-11-03 00:00:00
2. Conversion of dates to standard format using datetime.datetime.strptime
# Using datetime.datetime.strptimesample_dates = ('03/11/2021', '03-Nov-2021', 'Nov 03 21', '11/03/2021')date1 = datetime.datetime.strptime(sample_dates[0], '%d/%m/%Y')date2 = datetime.datetime.strptime(sample_dates[1], '%d-%b-%Y')date3 = datetime.datetime.strptime(sample_dates[2], '%b %d %y')date4 = datetime.datetime.strptime(sample_dates[3], '%m/%d/%Y')print('date1 = ', date1, ', date2 = ', date2, ', date3 = ', date3, ' , date4 = ', date4)
Output:
date1 = 2021-11-03 00:00:00 , date2 = 2021-11-03 00:00:00 , date3 = 2021-11-03 00:00:00 , date4 = 2021-11-03 00:00:00
3. Conversion of date time to standard format using strptime
The time is in 12 hrs format and AM/PM is used. Hence in the format argument I — for hours, M — for minutes and p — for AM/PM is used.
# Using I for 24 hrs and AM/PMstart_dt = '2/11/2022 4:00 AM'end_dt = '2/11/2022 4:30 PM'start_dt1 = datetime.datetime.strptime(start_dt, '%m/%d/%Y %I:%M %p')end_dt1 = datetime.datetime.strptime(end_dt, '%m/%d/%Y %I:%M %p')print('start time: ', start_dt1, ' , end time: ', end_dt1)
Output:
start time: 2022-02-11 04:00:00 , end time: 2022-02-11 16:30:00
The time is in 24 hrs format and AM/PM is not used. Hence in the format argument H — for hours and M — for minutes is used.
# Using H for 24 hrs and without AM/PMstart_dth = '2/11/2022 4:00'end_dth = '2/11/2022 16:30'start_dth1 = datetime.datetime.strptime(start_dth, '%m/%d/%Y %H:%M')end_dth1 = datetime.datetime.strptime(end_dth, '%m/%d/%Y %H:%M')print('start time: ', start_dt1, ' , end time: ', end_dt1)
Output:
start time: 2022-02-11 04:00:00 , end time: 2022-02-11 16:30:00
4. Changing date from one format to other
# Changing formats '03/11/2021' to '03-Nov-2021'date_a = '03/11/2021'date_b = datetime.datetime.strptime(date_a,'%d/%m/%Y').strftime('Day: %d-%b-%Y')print(date_b)
Output:
Day: 03-Nov-2021
Note: While changing the format we can add additional text based on our requirement.
5. Getting days after or before a specific date
# Getting dates a few days before or after todaytoday = datetime.date.today()yesterday = today - datetime.timedelta(days = 1)tomorrow = today + datetime.timedelta(days = 1)print("yesterday was: ",yesterday)print("tomorrow is:", tomorrow)print("Yesterday formatted: ",yesterday.strftime('%d-%b-%Y'))
Output:
yesterday was: 2022-06-17
tomorrow is: 2022-06-19
Yesterday formatted: 17-Jun-2022
6. Getting now in python and formatting it as per requirement
# now to add time of data updatenow_date = datetime.datetime.now().date().strftime('%Y-%m-%d')now_time = datetime.datetime.now().time().strftime('%H-%M-%S')now_datetime = datetime.datetime.now().strftime('Day: %Y-%m-%d Time: %H-%M-%S')print('now date = ', now_date, ' now time = ', now_time, ' now with formatting = ', now_datetime)
Output:
now date = 2022-06-18 now time = 05-15-12 now with formatting = Day: 2022-06-18 Time: 05-15-12
7. Calculating date difference
# Date differencesample_dt = ('03/11/2021', '03/12/2020')date_end = pd.to_datetime(sample_dt[0], format = '%d/%m/%Y')date_start = pd.to_datetime(sample_dt[1], format = '%d/%m/%Y')diff_dt = date_end - date_startdiff_dt
Output:
Timedelta('335 days 00:00:00')
8. Converting datetime column in a sample data frame
# Creating a dataframe with event and date time columns
time_dict = {"Event": ['A', 'B', 'C', 'D', 'E'], "date_time" : ['6/9/2022 10:42 PM', '6/8/2022 6:22 AM', '6/10/2022 7:15 AM', '6/8/2022 10:59 PM', '6/7/2022 11:13 PM']}dt_df = pd.DataFrame(time_dict)dt_df
Output:
# Extract datedt_df['date'] = pd.to_datetime(dt_df['date_time'], format='%m/%d/%Y %I:%M %p').dt.date# Extract time in 24 hrs formatdt_df['time_24'] = pd.to_datetime(dt_df['date_time'], format='%m/%d/%Y %I:%M %p').dt.time# Extract time in 12 hrs formatdt_df['time_12'] = pd.to_datetime(dt_df['date_time'], format='%m/%d/%Y %I:%M %p').dt.strftime('%I:%M %p')# Extract yeardt_df['year'] = pd.to_datetime(dt_df['date_time'], format='%m/%d/%Y %I:%M %p').dt.year# Extract weekdaydt_df['weekday'] = pd.to_datetime(dt_df['date_time'], format='%m/%d/%Y %I:%M %p').dt.weekday# Data frame after adding columns
dt_df
Output:
I have published the code at Date time in python (Static report) (jetbrains.com).
Please share the blog if you like it.
References: