1. Overview

In this quick guide, we're going to look at today's date in Pandas. How to get today with or without time, how to subtract days or date from it.

2. Get today's date in Pandas

To get today's date as datetime in Pandas we can use the method to_datetime() and pass parameter - today.

Below we are creating new column with Timestamp of today:

pd.to_datetime("today")
df['today'] = pd.to_datetime("today")

The result is Timestamp of today's date and new column with the same date:

Timestamp('2022-02-15 12:00:42.371569')

3. Pandas get today's date without time

Depending on the final data type there are several options how to extract dates in Pandas:

  • string - strftime()
  • datetime - method date()

3.1 Get Today's Date in Pandas - strftime() and format %m/%d/%Y

If you like to get today as a string in custom date format then we can use method - strftime():

pd.to_datetime("today").strftime("%m/%d/%Y")

the result is a string with the current date:

'02/15/2022'

3.2 Get Today's Date in Pandas - method date()

Pandas offer method date() which returns datetime from a given date. For today we can chain the methods in order to get today's date:

pd.to_datetime("today").date()

The result is:

datetime.date(2022, 2, 15)

4. Today date minus days or date in Pandas

There are two main scenarious for dates and subtraction:

  • date minus days with result in new date
  • date minus another date - result in timedelta

4.1 Today date minus N days

To subtract days from todays date in Pandas we can use the following format - n * obj.freq. We need to provide two parameters:

  • n - number of days. In this example is 10
  • unit - the frequency of the object - D for days
pd.to_datetime("today") - pd.Timedelta(10, unit='D')

The result is a date with difference 10 days from today:

Timestamp('2022-02-05 13:23:10.382430')
Note:

The subtracting from integer or float is deprecated and will raise error:

TypeError: Addition/subtraction of integers and integer-arrays with Timestamp is no longer supported. Instead of adding/subtracting n, use n * obj.freq

4.2 Today date minus another date

In pandas is possible to perform subtraction of two dates with minus operator:

td = pd.to_datetime("today") - pd.to_datetime("02/10/2022")

The result will be timedelta:

Timedelta('5 days 12:47:35.699206')

Days can be extracted by using attribute - days from the result- td.days - 5.

5. Conclusion

In this article, we looked at different solutions for getting today's date in Pandas. We covered most popular questions related to the dates and today in Pandas: remove time, subtract days or date.