Skip to content

Instantly share code, notes, and snippets.

@TomAugspurger
Created November 13, 2013 23:04
Show Gist options
  • Select an option

  • Save TomAugspurger/7458121 to your computer and use it in GitHub Desktop.

Select an option

Save TomAugspurger/7458121 to your computer and use it in GitHub Desktop.
helpers for HDF bug
def sane_names(df):
names = _gen_items()
if isinstance(df, pd.Panel):
return df.rename(items=names, minor_axis=names)
return df.rename(columns=names)
def get_useful(df, strict=True):
df = sane_names(df)
# TODO: grab cols from convert_names.keys()
# or both from a common function.
cols = _gen_items().values()
no_history = list(set(cols) - {'unemployed_history', 'nonemployed_history',
'either_history', 'flow'})
if isinstance(df, pd.Panel):
try:
res = df.loc[cols]
except KeyError:
if strict:
raise KeyError
else:
res = df.loc[no_history]
else:
try:
res = df[cols]
except KeyError:
if strict:
raise KeyError
else:
res = df[no_history]
return res
def fix_timestamp(wp_original):
wp = wp_original.copy()
wp = get_useful(wp)
years = wp['year'].dropna()
months = wp['month'].dropna()
ts = wp['timestamp'].dropna()
ncols = len(years.columns)
idx = range(1, ncols + 1)
stamp = df_unique(ts, index=idx)
years = df_unique(years, index=idx)
months = df_unique(months, index=idx)
expected = pd.to_datetime(years.astype(int).astype(str) + '-' +
months.astype(int).astype(str) + '-01')
needfix = expected[stamp != expected].index
if needfix is not None:
for month in needfix:
wp_original['timestamp'][month] = expected[month]
return wp_original
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment