Skip to content

Instantly share code, notes, and snippets.

@guziy
Last active August 29, 2015 14:16
Show Gist options
  • Save guziy/15a6c9f31cc0a0f58ba1 to your computer and use it in GitHub Desktop.
Save guziy/15a6c9f31cc0a0f58ba1 to your computer and use it in GitHub Desktop.
# Example done with Kamel Chikhar on 9/03/2015
# frame1 - a frame from a file that does not contain levels and can have different number of levels for different dates
# and different number of dates for the same levels
# Create all possible multiindices
all_dates_unique = [start + i * day for i in range(ndays)]
all_levels_unique = list(reversed(np.unique(frame1["X.6"])))
all_hours_unique = [i * dt.seconds // 3600 for i in range(24 * 3600 // dt.seconds)]
all_dates_unique_formatted = [int(d.strftime("%Y%m%d")) for d in all_dates_unique]
# Generate a multi index from a product of lists
mi_all = pd.MultiIndex.from_product([all_dates_unique_formatted, all_hours_unique, all_levels_unique],
names=["date", "time", "level"])
frame_with_nans = frame1.reindex(mi_all)
frame_with_nans.sortlevel(inplace=True) # Really important, without it you won't be able to use slicing with .loc
print 20 * "+"
print frame_with_nans.loc[(slice(None),[0,12],100000),:]
print 20 * "+"
print frame_with_nans[:10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment