@创建于:20211117
1、背景
经常遇到两个日期之间相差多个年,多少月,多少日。
方式1:使用datetime
datetime只能计算日期间隔了多少天,换成年,需要除以常数365.25;换成月则需要除以30.42。这样计算不太准确。
方式3:使用dateutil的relativedelta
dateutil则可以计算日期间隔了几年几月几日,是一种比较准确的方法。
2、代码示例
2.1 代码
"""
文件说明:
日期时间,做差,算日期相差几年几月几日
"""
import numpy as np
import pandas as pd
from datetime import datetime
from dateutil.relativedelta import relativedelta
# 显示所有列
pd.set_option('display.max_columns', None)
def year_month_day():
# today = datetime.today()
today = datetime(2021, 4, 3)
someday = datetime(2021, 2, 1)
print("today is {}, someday is {}".format(today, someday))
delta = relativedelta(dt1=today, dt2=someday)
years = delta.years
months = delta.months
days = delta.days
print('Time delta is {}'.format(delta))
print('years={}, months={}, days={}'.format(years, months, days))
def df_year_month_day():
t1 = ['2017-01-01', '2017-01-01', '2017-02-03']
t2 = ['2018-12-31', '2019-01-01', '2021-01-31']
df = pd.DataFrame(np.array([t1, t2]).T, columns=['a', 'b'])
print("原始数据:\n", df)
df = df.astype('datetime64')
df['c'] = df.apply(lambda x: relativedelta(x['b'], x['a']), axis=1)
df['cy'] = df['c'].apply(lambda x: x.years)
df['cm'] = df['c'].apply(lambda x: x.months)
df['cd'] = df['c'].apply(lambda x: x.days)
df['np_y'] = pd.to_datetime(df['b']) - pd.to_datetime(df['a'])
df['np_y'] = df['np_y'].map(lambda x: x / np.timedelta64(1, 'Y'))
print("计算两列日期差别:\n", df)
if __name__=="__main__":
year_month_day()
print('\n')
df_year_month_day()
2.1 输出结果
today is 2021-04-03 00:00:00, someday is 2021-02-01 00:00:00
Time delta is relativedelta(months=+2, days=+2)
years=0, months=2, days=2
原始数据:
a b
0 2017-01-01 2018-12-31
1 2017-01-01 2019-01-01
2 2017-02-03 2021-01-31
计算两列日期差别:
a b c cy \
0 2017-01-01 2018-12-31 relativedelta(years=+1, months=+11, days=+30) 1
1 2017-01-01 2019-01-01 relativedelta(years=+2) 2
2 2017-02-03 2021-01-31 relativedelta(years=+3, months=+11, days=+28) 3
cm cd np_y
0 11 30 1.995934
1 0 0 1.998672
2 11 28 3.991868
3、结果讨论
在结果中可以看到,dateutil的relativedelta 比 np.timedelta64()更合理。