Skip to content

Instantly share code, notes, and snippets.

@GeorgeSeif
Created October 10, 2019 23:41
Show Gist options
  • Save GeorgeSeif/eca27bf41c561b332c5e5eb6b15c893e to your computer and use it in GitHub Desktop.
Save GeorgeSeif/eca27bf41c561b332c5e5eb6b15c893e to your computer and use it in GitHub Desktop.
from itertools import product
import pandas as pd
import numpy as np
col_names = ["Day", "Month", "Year"]
df = pd.DataFrame(list(product([10, 11, 12], [8, 9], [2018, 2019])),
columns=col_names)
df['data'] = np.random.randn(len(df))
df = df.sort_values(['Year', 'Month'], ascending=[True, True])
df.insert(loc=0, column="date", value=pd.to_datetime(df[col_names]))
df = df.drop(col_names, axis=1).squeeze()
print(df)
"""
date data
0 2018-08-10 -0.328973
4 2018-08-11 -0.670790
8 2018-08-12 -1.360565
2 2018-09-10 -0.401973
6 2018-09-11 -1.238754
10 2018-09-12 0.957695
1 2019-08-10 0.571126
5 2019-08-11 -1.320735
9 2019-08-12 0.196036
3 2019-09-10 -1.717800
7 2019-09-11 0.074606
11 2019-09-12 -0.643198
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment