Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- def addEmpty(data, start_date, end_date, from_loc, to_loc, day=True):
- dates = pd.DataFrame(0, index=pd.date_range(start=start_date, end=end_date), columns=['__'])
- if day:
- dates.index = pd.MultiIndex.from_tuples([(a.year, a.month, a.day) for a in dates.index], names=['year', 'month', 'day'])
- else:
- dates.index = pd.MultiIndex.from_tuples([(a.year, a.month) for a in dates.index], names=['year', 'month'])
- dates.reset_index(inplace=True)
- d = dict([(loc, 0) for loc in to_loc])
- d['from'] = from_loc
- countries = pd.DataFrame(d)
- countries['__'] = 0
- empty = dates.merge(countries, how='outer')
- del empty['__']
- empty.set_index(list(data.index.names), inplace=True)
- result = pd.concat([data, empty[~empty.index.isin(data.index)]], axis=0, sort=False)
- result.sort_index(inplace=True)
- return result.fillna(0)
- data = pd.DataFrame({'year':[2018, 2018], 'month':[1, 1], 'day':[1, 2], 'from': ['a', 'b'], 'x': [1, 0], 'y': [2, 1]})
- data.set_index(['year', 'month', 'day', 'from'], inplace=True)
- print(data)
- allData = addEmpty(data, '1/1/2018', '1/3/2018', from_loc=['a', 'b', 'c'], to_loc=['x', 'y', 'z'])
- print(allData)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement