Compute daily climatology using pandas python

后端 未结 3 1621
孤街浪徒
孤街浪徒 2020-12-10 17:34

I am trying to use pandas to compute daily climatology. My code is:

import pandas as pd

dates      = pd.date_range(\'1950-01-01\', \'1953-12-31\', freq=\'D\         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 18:25

    @joris. Thanks. Your answer was just what I needed to use pandas to calculate daily climatologies, but you stopped short of the final step. Re-mapping the month,day index back to an index of day of the year for all years, including leap years, i.e. 1 thru 366. So I thought I'd share my solution for other users. 1950 thru 1953 is 4 years with one leap year, 1952. Note since random values are used each run will give different results.

    ...   
    from datetime import date
    doy = []
    doy_mean = []
    doy_size = []
    for name, group in cum_data.groupby([cum_data.index.month, cum_data.index.day]):
      (mo, dy) = name
      # Note: can use any leap year here.
      yrday = (date(1952, mo, dy)).timetuple().tm_yday
      doy.append(yrday)
      doy_mean.append(group.mean())
      doy_size.append(group.count())
      # Note: useful climatology stats are also available via group.describe() returned as dict
      #desc = group.describe()
      # desc["mean"], desc["min"], desc["max"], std,quartiles, etc.
    
    # we lose the counts here.
    new_cum_data  = pd.Series(doy_mean, index=doy)
    print new_cum_data.ix[366]
    >> 634.5
    
    pd_dict = {}
    pd_dict["mean"] = doy_mean
    pd_dict["size"] = doy_size
    cum_data_df = pd.DataFrame(data=pd_dict, index=doy)
    
    print cum_data_df.ix[366]
    >> mean    634.5
    >> size      4.0
    >> Name: 366, dtype: float64
    # and just to check Feb 29
    print cum_data_df.ix[60]
    >> mean    343
    >> size      1
    >> Name: 60, dtype: float64
    

提交回复
热议问题