Home » excel » python – Pandas: excel days360 equivalent

python – Pandas: excel days360 equivalent

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am working on converting some excel models to python and some of the calculations are using the days360 function in excel, which calc the number of days between two days based on a 360-day year.

I have been looking over pandas.tseries.offsets and I am not seeing anything.

For example in excel,

d1 = 2/2/2015
d2 = 1/1/2018

=days(d1, d2) # 1064
=days360(d1, d2) # 1049
How to&Answers:

There’s a Reddit thread on days360 for Pandas – here’s the function that’s posted there (seems to work with your example input):

def days360(start_date, end_date, method_eu=False):
    start_day = start_date.day
    start_month = start_date.month
    start_year = start_date.year
    end_day = end_date.day
    end_month = end_date.month
    end_year = end_date.year

    if (
        start_day == 31 or
        (
            method_eu is False and
            start_month == 2 and (
                start_day == 29 or (
                    start_day == 28 and
                    start_date.is_leap_year is False
                )
            )
        )
    ):
        start_day = 30

    if end_day == 31:
        if method_eu is False and start_day != 30:
            end_day = 1

            if end_month == 12:
                end_year += 1
                end_month = 1
            else:
                end_month += 1
        else:
            end_day = 30

    return (
        end_day + end_month * 30 + end_year * 360 -
        start_day - start_month * 30 - start_year * 360)

Testing on OP data:

d1 = pd.to_datetime("2/2/2015")
d2 = pd.to_datetime("1/1/2018")

days360(d1, d2) # 1049