βŒ› Easy date math in Python


Welcome back to Pandas Daily! Master Python in just 5 minutes a day.

​

In case you missed Friday's issue on "datetime" you can take a quick peek here. Today we go one step ahead - understand how Python handles dates (πŸ˜‰). Mini exercise at the end!

​

πŸš— "timedelta" - to do any changes in date or time

Here we add 7 days

In:
from datetime import date, timedelta
d = date(2025, 8, 22)
new_d = d + timedelta(days=7)
print(d)
print(new_d)
Out:
2025-08-22
2025-08-29

​

πŸ”™ Going back in time? "timedelta" again

In:
prev = d - timedelta(days=7)
print(prev)
Out:
2025-08-15

​

πŸ§— Can't way for days? So jump weeks!

In:
new_d = d + timedelta(weeks=2)
print(new_d)
Out:
2025-09-05

​

⏰ Adding hours to a timestamp

In:
dt = datetime(2025, 8, 22, 10, 0)
new_dt = dt + timedelta(hours=5)
print(dt)
print(new_dt)
Out:
2025-08-22 10:00:00
2025-08-22 15:00:00

Can you add minutes? Do reply with your code

​

πŸ›³οΈ Difference between dates: Number of days since I last ate chocolate - ZERO

In:
start = date(2025, 7, 1)
end = date(2025, 8, 22)
diff = end - start
print(diff.days)
Out:
52

​

πŸ” Mid point of two dates - If your project needs to be done in 2 phases?

In:
start = date(2025, 7, 1)
end = date(2025, 8, 22)
mid = start + ((end - start) // 2)
print(mid)
Out:
2025-07-27

​

πŸ’€ Did you miss the deadline? Oh! Yes.

In:
deadline = date(2025, 8, 15)
today = date(2025, 8, 22)
print(today > deadline)
Out:
True

​

πŸ’ƒπŸ’ƒ Making the deadline miss more obvious

Using our "if-else" knowledge!!

In:
if (deadline - today).days < 0:
    print("Deadline Passed")
else:
    print(f"You have {(deadline - today).days} days left")
Out:
Deadline Passed

πŸ‹οΈ Mini Exercise

​

Today is 2025-08-25.
πŸ‘‰ Find the date 30 days earlier and print how many days are left until 2025-12-31.

β­πŸ“£ That's it for today! If you liked it, please share it with anyone who will find it useful and share your feedback below 🐼

​

Pandas Daily

Beginner to Expert in Python in just 5 minutes

Read more from Pandas Daily

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all agree that dashboards, emails, logs should read like English. Meaning 1.2 million beats 1234567 and 3.1M is more readable than 3145728. Stakeholders scan - and they get pissed if they have to parse messy numbers 😭😭 So today, we explore a third-party humanize library. Format numbers, file sizes, and dates/times into human-readable text. Mini exercise at the end. πŸ₯‡ Ready, Get, Set, Go! Install and...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Amazon reviews, chats and social media text (posts, comments, stories etc) are full of emojis πŸ”₯πŸ”₯ Great for expression! But give a hard time for text interpretation (Amazon, Meta would be going crazy). That is where Python's demoji library comes in. From a given text you can: Extract emojis Remove them if needed Replace with textual meaning Understand sentiment and what not... Let's begin!! Install demoji...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all know Python’s basic container data types β€” lists, dictionaries, tuples, sets. "Container" is just a data type that holds multiple items instead of single values. However, many times basics are not enough. Like if you want count of different items in the list you will run a loop like below. In: fruits = ["apple", "banana", "apple", "cherry"] counts = {} for item in fruits: counts[item] =...