Skip to content

Instantly share code, notes, and snippets.

@taldcroft
Created July 18, 2014 02:32
Show Gist options
  • Save taldcroft/23c0c6a002f5956a72ff to your computer and use it in GitHub Desktop.
Save taldcroft/23c0c6a002f5956a72ff to your computer and use it in GitHub Desktop.
Vectorized version of quarter binning
"""
Vectorized version of quarter_bin() function.
"""
import numpy as np
def quarter_bin(dates):
# print(Time(datetime(2014,1,31)).jyear)
# Jan31 = np.modf(Time(datetime(2014,1,31)).jyear)[0]
# Apr30 = np.modf(Time(datetime(2014,4,30)).jyear)[0]
# Jul31 = np.modf(Time(datetime(2014,7,31)).jyear)[0]
# Oct31 = np.modf(Time(datetime(2014,10,31)).jyear)[0]
jan31 = 0.082135523613942496
apr30 = 0.3258042436686992
jul31 = 0.57768651608489563
oct31 = 0.82956878850109206
bounds = np.array([jan31, apr30, jul31, oct31])
years = np.arange(1999, 2020).reshape(-1, 1)
quarter_bounds = (years + bounds).ravel()
indices = np.searchsorted(quarter_bounds, dates)
return quarter_bounds[indices - 1]
dates = np.arange(2000.0, 2014.5, 0.1)
binned_dates = quarter_bin(dates)
print(np.vstack([dates, binned_dates]).transpose())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment