Skip to content

Instantly share code, notes, and snippets.

@eliorc
Last active February 27, 2019 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliorc/e847b0a8641d8613bcb43f2fe6daa33c to your computer and use it in GitHub Desktop.
Save eliorc/e847b0a8641d8613bcb43f2fe6daa33c to your computer and use it in GitHub Desktop.
Generate cyclic time features on pandas.DataFrame using a pandas.DateTime column
from itertools import count
import pandas as pd
import numpy as np
def cyclical_encoder(data: pd.DataFrame,
time_column: str,
time_unit: str,
normalize_val: float,
label_suffix: str) -> pd.DataFrame:
"""
Encodes a cyclical time unit using sinus and cosine
"""
# Get time values
time_values = getattr(data[time_column].dt, time_unit)
sin_time = np.sin(2*np.pi*time_values / normalize_val)
cos_time = np.cos(2*np.pi*time_values / normalize_val)
# Determine labels, check if already exists
sin_label = 'sin_{}_'.format(time_unit)
cos_label = 'cos_{}_'.format(time_unit)
if label_suffix:
sin_label += str(label_suffix)
cos_label += str(label_suffix)
else:
for n in count():
if not sin_label + str(n) in data.columns:
break
sin_label += str(n)
cos_label += str(n)
return pd.concat([data, pd.DataFrame(columns=[cos_label, sin_label], data=np.array([cos_time, sin_time]).T)], axis=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment