top of page

25% Discount For All Pricing Plans "welcome"

Working with Dates and Times in Python




I. Introduction to Date and Time Representation


Think of date and time as ingredients in a recipe. For any recipe, you need to know what ingredients you have on hand and how to use them effectively. Similarly, understanding how Python represents dates and times is essential for many data analysis tasks. Let's consider a specific example, it's like noting the exact time you put a cake in the oven and the precise time you take it out. Without this, you might end up with an undercooked or burnt cake.


Python provides a module named datetime that gives us the tools to work with dates and times.


Example of a date and time together:

from datetime import datetime
print(datetime.now())

Output:

2023-08-02 13:37:12.756859


II. Creating a Datetime Object


Working with datetime objects in Python is similar to working with a toolkit for a DIY project. You need to know what tools you have and how to use them effectively to achieve your desired outcome.


To create a datetime object, we need to import the datetime class from the datetime module. Then we can create a new instance of datetime and populate the fields with specific values.

from datetime import datetime
dt = datetime(2023, 8, 2, 13, 37)
print(dt)

Output:

2023-08-02 13:37:00


III. Microseconds in Datetime


Think of microseconds as the tiny crumbs in a loaf of bread. Each second is made up of 1,000,000 microseconds, just as a loaf of bread contains countless crumbs. Python allows us to express a time down to microseconds.


To add microseconds to a datetime, we include them as an additional parameter when creating the datetime.

from datetime import datetime
dt = datetime(2023, 8, 2, 13, 37, 0, 123456)
print(dt)

Output:

2023-08-02 13:37:00.123456


IV. Using Named Arguments in Datetime


Named arguments in Python can be compared to labels on switches in a complex control panel. Using named arguments, we know exactly what each switch does without having to remember their positions.


When creating a datetime object, we can use named arguments to make the code more readable.

from datetime import datetime
dt = datetime(year=2023, month=8, day=2, hour=13, minute=37, second=0, microsecond=123456)
print(dt)

Output:

2023-08-02 13:37:00.123456


V. Replacing Parts of a Datetime


Replacing parts of a datetime object is like swapping out parts of a Lego structure. We can take out one piece and replace it with another to get a new structure without affecting the rest.


We can create new datetimes from existing ones using the replace() method. Let's say we want to round a datetime down to the start of the hour.

dt_start_of_hour = dt.replace(minute=0, second=0, microsecond=0)
print(dt_start_of_hour)

Output:

2023-08-02 13:00:00


Practical Application: Analysis of Shared Bike Program Data


Let's now dive into an interesting and practical scenario. We will analyze a data set from a shared bike program, a real-world example that many cities around the world have implemented.


I. Introduction to the Shared Bike Program


Imagine a shared bike program as a city-wide book library, but for bikes. People can pick up a bike from any station, ride it wherever they need to go, and drop it off at any other station. Just as the dates and times when a book is borrowed and returned can provide valuable insights into user behaviour, so too can data from a shared bike program.


For our data analysis, let's assume we have a data set that records the start and end times of each bike ride.


II. Understanding Datetime Objects


In the context of our shared bike program, datetime objects are like the

timestamps on surveillance footage. They allow us to track exactly when each event (in this case, each bike ride) started and ended.


By creating datetime objects for these start and end times, we can perform various analyses such as finding the most popular time for bike rides or the average length of a ride.


To illustrate, let's use a simplified example where we have the start and end times of a single bike ride. In a real scenario, we would read this data from a file or a database.

from datetime import datetime

ride_start = datetime(2023, 8, 2, 7, 30)  # The ride started at 7:30
ride_end = datetime(2023, 8, 2, 8, 15)  # The ride ended at 8:15

# We can now use these datetime objects in our analysis


Printing and Parsing Datetimes


Knowing how to print and parse datetimes is like knowing how to read and write in a specific language. We need to understand this language to communicate effectively with our data.


I. Printing Datetimes


Just as we can write in different styles (handwriting, typing, calligraphy), we can also print datetimes in various formats using the strftime() function, where "f" stands for "format".

dt = datetime(2023, 8, 2, 13, 37)
formatted = dt.strftime('%Y-%m-%d %H:%M')
print(formatted)

Output:

2023-08-02 13:37

The format codes %Y, %m, %d, %H, and %M correspond to the year, month, day, hour, and minute, respectively. We can create more complex formatting strings by combining these and other format codes.


II. ISO 8601 Format


ISO 8601 is a standard format for date and time, much like the standard rules for driving on a particular side of the road. It helps to avoid confusion and maintain consistency across different regions.


We can write a datetime in ISO 8601 format using the isoformat() method.

iso_format = dt.isoformat()
print(iso_format)

Output:

2023-08-02T13:37:00


III. Parsing Datetimes with strptime()


Parsing datetimes from strings is like translating a foreign language into our native language. We use specific rules (in this case, the format codes) to understand the meaning.


The strptime() function, where "p" stands for "parse", allows us to parse dates from strings using these format codes.

date_string = '2023-08-02 13:37'
dt = datetime.strptime(date_string, '%Y-%m-%d %H:%M')
print(dt)

Output:

2023-08-02 13:37:00

It's important to note that the format codes must match the string exactly, just as the rules of a language must be followed precisely to understand the meaning correctly.


IV. Unix Timestamps


Unix timestamps are like universal translators for date and time. They represent the number of seconds that have passed since the "Unix epoch": 00:00:00 on January 1, 1970.


We can read Unix timestamps using the fromtimestamp() method.

unix_timestamp = 1693825020
dt = datetime.fromtimestamp(unix_timestamp)
print(dt)

Output:

2023-08-02 13:37:00


Working with Durations


The ability to work with durations (intervals between two datetimes) is akin to being able to measure distances on a map. Just as knowing the distance between two cities allows us to plan our journey, knowing the duration between two datetimes allows us to analyze our data more effectively.


I. Arithmetic Operations on Datetimes


The process of comparing and subtracting datetimes is like measuring the time it takes for a runner to finish a race. We simply mark the start and end times, then calculate the difference.

from datetime import datetime

start = datetime(2023, 8, 2, 7, 30)  # The race started at 7:30
end = datetime(2023, 8, 2, 8, 15)  # The race ended at 8:15

duration = end - start
print(duration)

Output:

0:45:00

Similarly, adding an interval (a timedelta object, as we'll see soon) to a datetime is like scheduling a meeting a certain amount of time after another event.


II. Timedelta and Durations


The concept of duration in Python is represented by the timedelta class. Think of timedelta as the yardstick we use to measure the "distance" between two points in time.


The total_seconds() method returns the total duration represented by a timedelta object in seconds.

total_seconds = duration.total_seconds()
print(total_seconds)

Output:

2700.0

In this case, the output is 2700.0, which is the number of seconds in 45 minutes.


III. Creating Timedeltas


Creating timedelta instances is like setting a timer on your phone. You specify the duration, and Python counts the time accordingly.

from datetime import timedelta

break_time = timedelta(minutes=15)  # We're setting a 15-minute timer

You can add this timedelta to a datetime to find out when this interval will end, given a starting point.

next_meeting = end + break_time
print(next_meeting)

Output:

2023-08-02 08:30:00


IV. Negative Timedeltas


Creating a negative timedelta is like going back in time. If we subtract a positive timedelta from a datetime, we'll get an earlier datetime.

previous_meeting = end - break_time
print(previous_meeting)

Output:

2023-08-02 08:00:00

In this example, the previous meeting was 15 minutes before the end of the race.


Conclusion


We've covered a lot of ground in this tutorial. We started with the basics of how Python represents date and time, then we went on to apply these concepts in a real-world data analysis scenario. We looked at how to print and parse datetimes in different formats, and we explored the concept of durations, or timedeltas, and how to perform arithmetic operations with them.


The ability to work with date and time is a fundamental skill in data science. It allows us to track events, analyze trends over time, and make predictions for the future. It's like having a time machine that lets us explore the past, understand the present, and peek into the future.


As with any skill, the best way to become proficient at working with date and time in Python is through practice. So why not find a data set and start exploring? The insights you uncover might surprise you.

Comments


bottom of page