Skip to content

Instantly share code, notes, and snippets.

@spestana
Last active May 16, 2023 17:46
Show Gist options
  • Save spestana/93682fecb659a8013e6c853b2630c15e to your computer and use it in GitHub Desktop.
Save spestana/93682fecb659a8013e6c853b2630c15e to your computer and use it in GitHub Desktop.
Compute the mean, median, modes, counts from an xarray DataArrayResample object, returns a pandas dataframe
# requires: numpy, pandas, scipy.stats, xarray
def compute_modes(resampled, n=0):
'''Given a resampled xarray object (xarray.core.resample.DataArrayResample),
compute the modes ( rounding values with np.round(_,n) ),
return a pandas dataframe with the modes, counts, and groups (datetime)'''
# Compute modes
resampled_ModeResults = [stats.mode(np.round(x[1],n)) for x in resampled]
# Get all the modes
resampled_modes = [x.mode for x in resampled_ModeResults]
# Reshape result into 1D array
resampled_modes = np.array(resampled_modes).reshape(len(resampled))
# Get the count for each mode
resampled_counts = [x.count for x in resampled_ModeResults]
# Reshape result into 1D array
resampled_counts = np.array(resampled_counts).reshape(len(resampled))
# Get the group (datetime) for each mode
resampled_groups = np.array( list(resampled.groups.keys()) )
# Create pandas dataframe
d = {'modes': resampled_modes, 'counts': resampled_counts}
df = pd.DataFrame(data=d, index=resampled_groups)
return df
def resampled_stats(resampled, n=0, q=(.25,.75)):
'''Given a resampled xarray object (xarray.core.resample.DataArrayResample),
return a pandas dataframe with the mean, median, modes, counts, and groups (datetime)'''
# Compute modes
resampled_modes = compute_modes(resampled, n)
# Compute and add mean, medians, modes, counts to a dataframe
d = {'means': resampled.mean().values,
'medians': resampled.median().values,
'modes': resampled_modes.modes.values,
'counts': resampled_modes.counts.values,
'maxs': resampled.max().values,
'mins': resampled.min().values,
'ranges': resampled.max().values - resampled.min().values,
'stds': resampled.std().values,
'qUpper': resampled.quantile(q[1]).values,
'qLower': resampled.quantile(q[0]).values,}
df = pd.DataFrame(data=d, index=resampled_modes.index)
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment