Pandas – 两个日期之间的月数
在本文中,我们将使用Python查找 pandas 中两个日期之间的月数。
示例 1:
我们将获取一个数据框,并为我们想要获得差异的日期提供两列。使用 df.dates1-df.dates2 求两个日期的差,然后将结果转换为月份的形式。转换为“int”数据类型,否则结果将采用浮点形式。
Python3
# Importing required libraries
import pandas as pd
import numpy as np
import datetime
# Making a dataframe which will have two
# columns two store different dates
df = pd.DataFrame({'dates1': np.array(
[datetime.datetime(2000, 10, 19), datetime.datetime(2021, 1, 8)]),
'dates2': np.array(
[datetime.datetime(1998, 6, 20),
datetime.datetime(2012, 10, 18)])})
# Used to convert the difference in terms of months
df['nb_months'] = ((df.dates1 - df.dates2)/np.timedelta64(1, 'M'))
df['nb_months'] = df['nb_months'].astype(int)
print(df)
Python3
# Importing required libraries
import pandas as pd
import numpy as np
import datetime
# Making a dataframe which will have
# two columns two store different dates
df = pd.DataFrame({'dates1': np.array(
[datetime.datetime(2000, 10, 19), datetime.datetime(2021, 1, 8)]),
'dates2': np.array(
[datetime.datetime(1998, 6, 20),
datetime.datetime(2012, 10, 18)])})
# Used to convert the difference in terms of days
df['Number_of_days'] = ((df.dates1 - df.dates2)/np.timedelta64(1, 'D'))
df['Number_of_days'] = df['Number_of_days'].astype(int)
print(df)
Python3
# Importing required libraries
import pandas as pd
import numpy as np
import datetime
# Making a dataframe which will
# have two columns two store different dates
df= pd.DataFrame({'dates1': np.array(
[datetime.datetime(2000, 10, 19), datetime.datetime(2021, 1, 8)]),
'dates2': np.array(
[datetime.datetime(1998, 6, 20),
datetime.datetime(2012, 10, 18)] )})
# Used to convert the difference in terms of weeks
df['Number_of_weeks'] = ((df.dates1 - df.dates2)/np.timedelta64(1, 'W'))
df['Number_of_weeks'] = df['Number_of_weeks'].astype(int)
print(df)
Python3
# Importing required libraries
import pandas as pd
import numpy as np
import datetime
# Making a dataframe which will
# have two columns two store different dates
df = pd.DataFrame({'dates1': np.array(
[datetime.datetime(2000, 10, 19), datetime.datetime(2021, 1, 8)]),
'dates2': np.array(
[datetime.datetime(1998, 6, 20),
datetime.datetime(2012, 10, 18)])})
# Used to convert the difference in terms of years
df['Number_of_years'] = ((df.dates1 - df.dates2)/np.timedelta64(1, 'Y'))
df['Number_of_years'] = df['Number_of_years'].astype(int)
print(df)
输出:
示例 2:
我们还可以通过对代码稍作修改来获取两个日期之间的天数。如下图所示:
Python3
# Importing required libraries
import pandas as pd
import numpy as np
import datetime
# Making a dataframe which will have
# two columns two store different dates
df = pd.DataFrame({'dates1': np.array(
[datetime.datetime(2000, 10, 19), datetime.datetime(2021, 1, 8)]),
'dates2': np.array(
[datetime.datetime(1998, 6, 20),
datetime.datetime(2012, 10, 18)])})
# Used to convert the difference in terms of days
df['Number_of_days'] = ((df.dates1 - df.dates2)/np.timedelta64(1, 'D'))
df['Number_of_days'] = df['Number_of_days'].astype(int)
print(df)
输出:
示例 3:
以类似的方式,我们也可以在周数方面得到两个日期之间的差异。
Python3
# Importing required libraries
import pandas as pd
import numpy as np
import datetime
# Making a dataframe which will
# have two columns two store different dates
df= pd.DataFrame({'dates1': np.array(
[datetime.datetime(2000, 10, 19), datetime.datetime(2021, 1, 8)]),
'dates2': np.array(
[datetime.datetime(1998, 6, 20),
datetime.datetime(2012, 10, 18)] )})
# Used to convert the difference in terms of weeks
df['Number_of_weeks'] = ((df.dates1 - df.dates2)/np.timedelta64(1, 'W'))
df['Number_of_weeks'] = df['Number_of_weeks'].astype(int)
print(df)
输出:
示例 4:
以类似的方式,我们也可以在年份方面获得两个日期之间的差异。
Python3
# Importing required libraries
import pandas as pd
import numpy as np
import datetime
# Making a dataframe which will
# have two columns two store different dates
df = pd.DataFrame({'dates1': np.array(
[datetime.datetime(2000, 10, 19), datetime.datetime(2021, 1, 8)]),
'dates2': np.array(
[datetime.datetime(1998, 6, 20),
datetime.datetime(2012, 10, 18)])})
# Used to convert the difference in terms of years
df['Number_of_years'] = ((df.dates1 - df.dates2)/np.timedelta64(1, 'Y'))
df['Number_of_years'] = df['Number_of_years'].astype(int)
print(df)
输出: