Skip to content

Instantly share code, notes, and snippets.

@yuu-ito
Created July 17, 2017 09:58
Show Gist options
  • Save yuu-ito/bfa37d3392d5ddc512f9e9edf2ef32fa to your computer and use it in GitHub Desktop.
Save yuu-ito/bfa37d3392d5ddc512f9e9edf2ef32fa to your computer and use it in GitHub Desktop.
import numpy as np
import pandas as pd
np.random.seed(1234)
start_date = "2017-07-01"
end_date = "2017-07-02"
_date_range = pd.date_range(start_date, end_date, freq="H")
_size = _date_range.shape[0]
sample_log = pd.DataFrame({
"created_at" : pd.date_range(start_date, end_date, freq="H"),
"click" : np.random.binomial(10, 0.1, size=_size)
}).query("click >0")
sample_log.index = np.arange(1, sample_log.shape[0]+1, 1)
print("[before]\n",sample_log.head(10))
start_at, end_at = sample_log.created_at.min(), sample_log.created_at.max()
sample_log.index = sample_log.created_at
res = sample_log.reindex(pd.date_range(start_at, end_at, freq="H"))
print("[after]\n",res.head(10))
res.created_at = res.index
res.click = res.click.fillna(0)
print("[after2]\n",res.head(10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment