简体   繁体   中英

How do I Plot the Duration between two Dates in Python?

I'm working with the following DataFrame:

First Bill Date Last Bill Date  Duration
0   2020-06-22  2021-01-06  0 Years 6 Months 14 Days
1   2020-07-06  2020-12-04  0 Years 4 Months 29 Days
2   2020-06-06  2020-10-01  0 Years 3 Months 25 Days
3   2020-07-21  2020-07-21  0 Years 0 Months 0 Days
4   2020-07-03  2020-09-16  0 Years 2 Months 13 Days

where First Bill Date and Last Bill Date are of type TIMESTAMP

, and Duration (type str ) is the difference between the First Bill Date and Last Bill Date

Duration is calculated from the following function:

def date_difference(d1, d2):
    d1 = d1.strftime("%Y-%m-%d")
    d2 = d2.strftime("%Y-%m-%d")
    d1 = datetime.strptime(d1, "%Y-%m-%d")
    d2 = datetime.strptime(d2, "%Y-%m-%d")
    
    difference = relativedelta.relativedelta(d1, d2)

    years = difference.years
    months = difference.months
    days = difference.days
    
    return f'{abs(years)} Years {abs(months)} Months {abs(days)} Days'

Is there a way to plot Duration using any visualization package?

Please Advise.

If you transform the duration into an integers of days, then it should be straight forward to plot it. So you could change your function to the following

def date_difference(d1, d2):
    return (d2-d1).days

and then plot with df['Duration'].plot()

EDIT: I am assuming that d2 is the later date, if that is not the case then you should reverse the ordering of course

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM