Python pandas计算日期之间的距离

时间:2018-03-01 14:29:05

标签: python pandas dataframe

我有两个数据帧。 一旦常规数据帧:

DF

Build Artifacts

和包含假日日期的Dataframe。例如,如果 DF 的日期范围是2014-2015,那么DF-Holiday将有2014年和2015年的假期:

DFHolidays

Datum                   
...
2014-12-30 23:00:00 
2014-12-30 23:15:00 
2014-12-30 23:30:00 
2014-12-30 23:45:00 
2014-12-31 00:00:00 
...
2015-01-01 00:00:00 
2015-01-02 00:00:00 
2015-01-03 00:00:00 
2015-01-04 00:00:00 
2015-01-04 00:00:00 
2015-01-05 00:00:00 
...

Dataframe" DF"现在应该为每个假期都有一个新列,它计算给定行的每年假日的天数。

示例:

           DATE
NAME    
Neujahr 2014-01-01
Heilige Drei Könige 2014-01-06
Karfreitag  2014-04-18
Ostersonntag    2014-04-20
Ostermontag 2014-04-21
1. Mai  2014-05-01
...
Erster Weihnachtsfeiertag   2015-12-25
Zweiter Weihnachtsfeiertag  2015-12-26
...

我写了以下代码。

                        Neujahr Heilige Drei Könige    Karfreitag      ...
...
2014-01-01 23:00:00       0               value             value
2014-01-02 23:15:00       1               value             value
2014-01-03 23:30:00       2               value             value
2014-01-04 23:45:00       3               value             value
2014-01-05 00:00:00       4               value             value
... 

这有效,但速度很慢。我怎么能更优雅地解决这个问题?

编辑:完整的DFHolidays(在这个例子中只有两年。真正的数据大约是10年以上。

import datetime
from datetime import datetime
def computeDiff(x,y):
    x = pd.to_datetime(x).date()
    print("x: ",x)
    mask = mask = (y.dt.year == x.year)
    y = y.loc[mask]
    y = y.get_value(0,0)
    print("y: ",y)
    y = pd.to_datetime(y).date()
    return (y - x).days

for holiday in list(DFHolidays.index):
    day = DFHolidays.loc[holiday, 'DATE']
    df[holiday] = df['Datum'].apply(computeDiff, args=(day,))

1 个答案:

答案 0 :(得分:1)

以下内容如何:

import pandas as pd

df = pd.DataFrame({'Date': ['2014-12-30 23:00:00','2014-12-30 23:15:00','2014-12-30 23:30:00','2014-12-30 23:45:00','2014-12-31 00:00:00']})

DFHolidays = pd.DataFrame({'NAME': ['Neujahr', 'Heilige Drei Könige', 'Karfreitag', 'Ostersonntag', 'Ostermontag', '1. Mai', 'Erster Weihnachtsfeiertag', 'Zweiter Weihnachtsfeiertag'],
                           'DATE': ['2014-01-01','2014-01-06','2014-04-18','2014-04-20','2014-04-21','2014-05-01','2015-12-25','2015-12-26']})

# Ensure all dates are actually dates
df['Date'] = pd.to_datetime(df['Date'])
DFHolidays['DATE'] = pd.to_datetime(DFHolidays['DATE'])
DFHolidays.set_index('NAME', inplace=True)

# Loop over each holiday, apply the calculation
for holiday_name, date in DFHolidays['DATE'].to_dict().items():
    df[holiday_name] = date - df['Date']

返回,给出样本数据:

                 Date             Neujahr Heilige Drei Könige  \
0 2014-12-30 23:00:00 -364 days +01:00:00 -359 days +01:00:00   
1 2014-12-30 23:15:00 -364 days +00:45:00 -359 days +00:45:00   
2 2014-12-30 23:30:00 -364 days +00:30:00 -359 days +00:30:00   
3 2014-12-30 23:45:00 -364 days +00:15:00 -359 days +00:15:00   
4 2014-12-31 00:00:00 -364 days +00:00:00 -359 days +00:00:00   

           Karfreitag        Ostersonntag         Ostermontag  \
0 -257 days +01:00:00 -255 days +01:00:00 -254 days +01:00:00   
1 -257 days +00:45:00 -255 days +00:45:00 -254 days +00:45:00   
2 -257 days +00:30:00 -255 days +00:30:00 -254 days +00:30:00   
3 -257 days +00:15:00 -255 days +00:15:00 -254 days +00:15:00   
4 -257 days +00:00:00 -255 days +00:00:00 -254 days +00:00:00   

               1. Mai Erster Weihnachtsfeiertag Zweiter Weihnachtsfeiertag  
0 -244 days +01:00:00         359 days 01:00:00          360 days 01:00:00  
1 -244 days +00:45:00         359 days 00:45:00          360 days 00:45:00  
2 -244 days +00:30:00         359 days 00:30:00          360 days 00:30:00  
3 -244 days +00:15:00         359 days 00:15:00          360 days 00:15:00  
4 -244 days +00:00:00         359 days 00:00:00          360 days 00:00:00   

更改样本数据

了解数据年份非常重要,我们可以执行以下操作:

import pandas as pd

df = pd.DataFrame({'Date': ['2016-02-15 23:00:00','2016-03-05 23:15:00','2016-12-30 23:30:00','2017-08-10 23:45:00','2017-09-01 00:00:00']})

DFHolidays = pd.DataFrame({'NAME': ['Neujahr','Karfreitag','Ostersonntag', 'Ostermontag', '1. Mai','Christi Himmelfahrt','Pfingstsonntag','Pfingstmontag', 'Tag der deutschen Einheit', 'Erster Weihnachtsfeiertag', 'Zweiter Weihnachtsfeiertag','Neujahr','Karfreitag','Ostersonntag',  'Ostermontag','1. Mai','Christi Himmelfahrt','Pfingstsonntag','Pfingstmontag', 'Tag der deutschen Einheit', 'Erster Weihnachtsfeiertag', 'Zweiter Weihnachtsfeiertag'],
                           'DATE': ['2016-01-01','2016-03-25','2016-03-27','2016-03-28','2016-05-01','2016-05-05','2016-05-15','2016-05-16','2016-10-03','2016-12-25','2016-12-26','2017-01-01','2017-04-14','2017-04-16','2017-04-17','2017-05-01','2017-05-25','2017-06-04','2017-06-05','2017-10-03','2017-12-25','2017-12-26']})

# Ensure all dates are actually dates
df['Date'] = pd.to_datetime(df['Date'])
DFHolidays['DATE'] = pd.to_datetime(DFHolidays['DATE'])

# Set up a year column in both dataframes ot join on shortly
DFHolidays['Year'] = DFHolidays['DATE'].dt.year
df['Year'] = df['Date'].dt.year

# Work out what all the holiday names are
holiday_names = DFHolidays['NAME'].unique()
DFHolidays = DFHolidays.pivot(index='Year', columns='NAME', values='DATE') \
                       .reset_index()

# Merge the frames
df = df.merge(DFHolidays, on='Year')

# Calculate the difference
for holiday in holiday_names:
    df[holiday] = df[holiday] - df['Date']

这给了我们:

                 Date  Year              1. Mai Christi Himmelfahrt  \
0 2016-02-15 23:00:00  2016    75 days 01:00:00    79 days 01:00:00   
1 2016-03-05 23:15:00  2016    56 days 00:45:00    60 days 00:45:00   
2 2016-12-30 23:30:00  2016 -244 days +00:30:00 -240 days +00:30:00   
3 2017-08-10 23:45:00  2017 -102 days +00:15:00  -78 days +00:15:00   
4 2017-09-01 00:00:00  2017 -123 days +00:00:00  -99 days +00:00:00   

  Erster Weihnachtsfeiertag          Karfreitag             Neujahr  \
0         313 days 01:00:00    38 days 01:00:00  -46 days +01:00:00   
1         294 days 00:45:00    19 days 00:45:00  -65 days +00:45:00   
2         -6 days +00:30:00 -281 days +00:30:00 -365 days +00:30:00   
3         136 days 00:15:00 -119 days +00:15:00 -222 days +00:15:00   
4         115 days 00:00:00 -140 days +00:00:00 -243 days +00:00:00   

          Ostermontag        Ostersonntag       Pfingstmontag  \
0    41 days 01:00:00    40 days 01:00:00    90 days 01:00:00   
1    22 days 00:45:00    21 days 00:45:00    71 days 00:45:00   
2 -278 days +00:30:00 -279 days +00:30:00 -229 days +00:30:00   
3 -116 days +00:15:00 -117 days +00:15:00  -67 days +00:15:00   
4 -137 days +00:00:00 -138 days +00:00:00  -88 days +00:00:00   

       Pfingstsonntag Tag der deutschen Einheit Zweiter Weihnachtsfeiertag  
0    89 days 01:00:00         230 days 01:00:00          314 days 01:00:00  
1    70 days 00:45:00         211 days 00:45:00          295 days 00:45:00  
2 -230 days +00:30:00        -89 days +00:30:00          -5 days +00:30:00  
3  -68 days +00:15:00          53 days 00:15:00          137 days 00:15:00  
4  -89 days +00:00:00          32 days 00:00:00          116 days 00:00:00  

要获得天数,请将最后一行更改为

df[holiday] = df[holiday].dt.date - df['Date'].dt.date