Flag Daylight Saving Time (DST) Hours in Pandas Date-Time Column

前端 未结 4 691
执笔经年
执笔经年 2021-01-20 04:50

I created an hourly dates dataframe, and now I would like to create a column that flags whether each row (hour) is in Daylight Saving Time or not. For example, in summer hou

4条回答
  •  一个人的身影
    2021-01-20 05:00

    the following vectorized way seem to work fine. The idea behind is the same as Nick Klavoht's idea : find the difference between the current time in your timezone and the utc time.

    # Localized dates dataframe
    df = pd.DataFrame(data=pd.date_range('2018-1-1', '2019-1-1', freq='h', tz='America/Denver'), columns=['date_time'])
    
    df['utc_offset'] = df['date_time'].dt.strftime('%z').str[0:3].astype(float)
    df['utc_offset_shifted'] = df['utc_offset'].shift(-1)
    df['dst'] = df['utc_offset'] - df['utc_offset_shifted']
    df_dst = df[(df['dst'] != 0) & (df['dst'])]
    df_dst = df_dst.drop(['utc_offset', 'utc_offset_shifted'], axis=1).reset_index(drop=True)
    
    print(df_dst)
    

    This outputs :

                      date_time  dst
    0 2018-03-11 01:00:00-07:00 -1.0
    1 2018-11-04 01:00:00-06:00  1.0
    

提交回复
热议问题