Python for Excel users · Lesson 11 of 14

Excel date functions in Python with pandas

YEAR, MONTH, EOMONTH, EDATE, NETWORKDAYS, DATEDIF and Excel date serial numbers in pandas, with fixed dates so results never change.

About 17 minutes · code runs in your browser, nothing to install

Dates in Excel are numbers with a date format. In pandas they are a real date type, datetime64. Once a column has that type, .dt gives you the date functions, the same way .str gives you the text functions.

Text to real dates

pd.to_datetime turns text into dates. Always give the format, so 03/04/2026 cannot be read as 4 March in one file and 3 April in another. %d is the day, %m the month and %Y the four-digit year.

Pythoneditable
import pandas as pd

df = pd.DataFrame({"Date": ["02/03/2026", "15/03/2026", "31/03/2026"]})
df["Date"] = pd.to_datetime(df["Date"], format="%d/%m/%Y")
df["Year"] = df["Date"].dt.year
df["Month"] = df["Date"].dt.month
df["Weekday"] = df["Date"].dt.day_name()
print(df)
Output
        Date  Year  Month  Weekday
0 2026-03-02  2026      3   Monday
1 2026-03-15  2026      3   Sunday
2 2026-03-31  2026      3  Tuesday

=YEAR(A2), =MONTH(A2) and =TEXT(A2, "dddd") as columns.

Days between dates and DATEDIF

Subtracting two dates gives a time span; .dt.days turns it into a number of days, like =B2-A2. For complete years, as in =DATEDIF(A2, B2, "Y"), count the year difference and take one off if the anniversary has not come yet. Instead of =TODAY(), set the report date once in a variable such as as_of: a report that uses today's date gives a different answer every day and is hard to check.

Pythoneditable
import pandas as pd

as_of = pd.Timestamp("2026-03-31")
df = pd.DataFrame({
    "Name": ["Ana", "Ben", "Chloe"],
    "Joined": pd.to_datetime(["2019-04-15", "2023-03-31", "2025-12-01"]),
})
df["Days"] = (as_of - df["Joined"]).dt.days

def full_years(start, end):
    early = (end.month, end.day) < (start.month, start.day)
    return end.year - start.year - early

df["Years"] = df["Joined"].apply(lambda d: full_years(d, as_of))
print(df)
Output
    Name     Joined  Days  Years
0    Ana 2019-04-15  2542      6
1    Ben 2023-03-31  1096      3
2  Chloe 2025-12-01   120      0

early is True (counted as 1) when the anniversary has not been reached yet in the end year.

EOMONTH and EDATE

=EOMONTH(A2, 0), the last day of the month, is + pd.offsets.MonthEnd(0). =EDATE(A2, 3), the same day three months later, is + pd.DateOffset(months=3). Like Excel, 31 January plus one month gives the last day of February.

Pythoneditable
import pandas as pd

d = pd.Series(pd.to_datetime(["2026-01-31", "2026-02-10", "2026-11-20"]))
print((d + pd.offsets.MonthEnd(0)).dt.strftime("%Y-%m-%d").tolist())
print((d + pd.DateOffset(months=1)).dt.strftime("%Y-%m-%d").tolist())
print((d + pd.DateOffset(months=1) + pd.offsets.MonthEnd(0)).dt.strftime("%Y-%m-%d").tolist())
Output
['2026-01-31', '2026-02-28', '2026-11-30']
['2026-02-28', '2026-03-10', '2026-12-20']
['2026-02-28', '2026-03-31', '2026-12-31']

The last line is =EOMONTH(A2, 1): move one month on, then go to the end of that month.

NETWORKDAYS and WORKDAY

NumPy's np.busday_count(start, end) counts Monday-to-Friday days. Excel's NETWORKDAYS counts both the start and the end date, NumPy leaves the end date out, so add one day to the end. Holidays go in a list, like NETWORKDAYS' third argument.

Pythoneditable
import numpy as np

days = np.busday_count("2026-03-02", "2026-04-01")
print("Working days in March:", int(days))
holidays = ["2026-03-25"]
days = np.busday_count("2026-03-02", "2026-04-01", holidays=holidays)
print("Without the holiday:", int(days))
due = np.busday_offset("2026-03-27", 5, roll="forward")
print("WORKDAY + 5:", due)
Output
Working days in March: 22
Without the holiday: 21
WORKDAY + 5: 2026-04-03

np.busday_offset is =WORKDAY(A2, 5). pd.bdate_range("2026-03-01", "2026-03-31") lists the working days themselves if you need them.

Excel date serial numbers

Sometimes a file gives you 46082 instead of a date: that is Excel's day number. Count the days from Excel's starting point, 30 December 1899, to get the real date.

Pythoneditable
import pandas as pd

serials = pd.Series([46082, 46112, 46113])
dates = pd.to_datetime(serials, unit="D", origin="1899-12-30")
print(dates.dt.strftime("%d.%m.%Y").tolist())
Output
['01.03.2026', '31.03.2026', '01.04.2026']

strftime formats a date as text, like =TEXT(A2, "dd.mm.yyyy").

✎ Exercise

For each invoice, add Due (the invoice date plus 30 days), MonthEnd (the last day of the invoice month) and DaysLate (how many days past Due the date as_of is; can be negative). Print the table.

Hint: pd.to_timedelta(30, unit="D") is 30 days to add; pd.offsets.MonthEnd(0) moves to the month end; (as_of - df["Due"]).dt.days gives the difference.

Your codewrite your answer, then Run
import pandas as pd

as_of = pd.Timestamp("2026-04-15")
df = pd.DataFrame({
    "Invoice": ["A1", "A2", "A3"],
    "Date": pd.to_datetime(["2026-02-20", "2026-03-10", "2026-03-28"]),
})
# your code here

Expected output:

  Invoice       Date        Due   MonthEnd  DaysLate
0      A1 2026-02-20 2026-03-22 2026-02-28        24
1      A2 2026-03-10 2026-04-09 2026-03-31         6
2      A3 2026-03-28 2026-04-27 2026-03-31       -12
Show a solution
Solutioneditable
import pandas as pd

as_of = pd.Timestamp("2026-04-15")
df = pd.DataFrame({
    "Invoice": ["A1", "A2", "A3"],
    "Date": pd.to_datetime(["2026-02-20", "2026-03-10", "2026-03-28"]),
})
df["Due"] = df["Date"] + pd.to_timedelta(30, unit="D")
df["MonthEnd"] = df["Date"] + pd.offsets.MonthEnd(0)
df["DaysLate"] = (as_of - df["Due"]).dt.days
print(df)
Output
  Invoice       Date        Due   MonthEnd  DaysLate
0      A1 2026-02-20 2026-03-22 2026-02-28        24
1      A2 2026-03-10 2026-04-09 2026-03-31         6
2      A3 2026-03-28 2026-04-27 2026-03-31       -12

Check yourself

1. Why give format= to pd.to_datetime?

2. What is =EOMONTH(A2, 0) in pandas?

3. np.busday_count("2026-03-02", "2026-03-06") gives 4, while =NETWORKDAYS gives 5 for the same dates. Why?

Spotted a mistake on this page? Tell us