In Python, Pandas. How to subset dataframe by WOM - 'Week of the Month'? -
i want able subset df week of month, similar can day of week or month.
sample = df[df.index.month == 12]
so there way this??
sample = df[df.index.wom == 1]
i know if type line above attributeerror: 'index' object has no attribute 'wom', reference understand want do.
thanks
you can @ value of .weekofyear
, same value @ beginning of month , difference of these 2 should give week of month; example:
>>> days = ['2014-02-01', '2014-06-10', '2014-08-30', '2014-11-22'] >>> idx = pd.to_datetime(days) >>> idx.weekofyear array([ 5, 24, 35, 47], dtype=int32)
for beginning of month can subtract .day
index itself:
>>> mon = idx - pd.to_timedelta(idx.day - 1, unit='d') >>> mon.weekofyear array([ 5, 22, 31, 44], dtype=int32)
and week of month be:
>>> 1 + idx.weekofyear - mon.weekofyear array([1, 3, 5, 4], dtype=int32)
Comments
Post a Comment