Encoding continuous temporal variable into cyclical components.
data -- dataframe with any temporal column to be converted.
scol -- variable name to be converted.
ifreq -- frequency of the temporal variable to be converted. For example, for "dow" ifreq should 7.
return -- input dataframe with the new cyclical components included.
NOTE: it is not robust with NaN values.
Source: https://ianlondon.github.io/blog/encoding-cyclical-features-24hour-time/
## encoding continuous temporal variable into cyclical components
def tocyclical(data:'dataframe',scol:str,ifreq:int)-> 'dataframe':
"""
Encoding continuous temporal variable into cyclical components.
data -- dataframe with any temporal column to be converted.
scol -- variable name to be converted.
ifreq -- frequency of the temporal variable to be converted. For example, for "dow" ifreq should 7.
return -- input dataframe with the new cyclical components included.
NOTE: it is not robust with NaN values.
"""
from numpy import pi
# compute the cyclical components
data['%s_sin'%scol] = np.sin(2*pi*data[scol]/ifreq)
data['%s_cos'%scol] = np.cos(2*pi*data[scol]/ifreq)
# return
return data
if __name__ == '__main__':
data = tocyclical(data,'dow',7)
data = tocyclical(data,'month',12)