I want to select data from a dataframe for a particular day of the year. Here is what I have so far as a minimal example.
import pandas as pd
from datetime import datetime
from datetime import timedelta
import numpy.random as npr
rng = pd.date_range('1/1/1990', periods=365*10, freq='D')
df1 = pd.DataFrame(npr.randn(len(rng)), index=rng)
print df1
That generates:
0
1990-01-01 -0.032601
1990-01-02 -0.496401
1990-01-03 0.444490
etc. Now I make a list of dates that I want to extract. I have used this before in pandas, but I suspect this is not the best way to get values for a particular date. Anyway,
td = timedelta(days=31)
dr = pd.date_range(datetime(1990,12,31)+td,datetime(2000,12,31),
freq=pd.DateOffset(months=12, days=0))
print dr
This, of course, generates:
DatetimeIndex(['1991-01-31', '1992-01-31', '1993-01-31', '1994-01-31',
'1995-01-31', '1996-01-31', '1997-01-31', '1998-01-31',
'1999-01-31', '2000-01-31'],
dtype='datetime64[ns]', freq='<DateOffset: kwds={'months': 12, 'days': 0}>', tz=None)
When I try to slice the dataframe by the list of dates, I generate an error:
monthly_df1 = df1[dr]
Output:
KeyError: "['1991-01-30T16:00:00.000000000-0800' '1992-01-30T16:00:00.000000000-0800'\n
'1993-01-30T16:00:00.000000000-0800' '1994-01-30T16:00:00.000000000-0800'\n
'1995-01-30T16:00:00.000000000-0800' '1996-01-30T16:00:00.000000000-0800'\n
'1997-01-30T16:00:00.000000000-0800' '1998-01-30T16:00:00.000000000-0800'\n
'1999-01-30T16:00:00.000000000-0800' '2000-01-30T16:00:00.000000000-0800']
not in index"
I think that I have two fundamental problems here: (1) there is a better way to extract yearly data for a particular date; and (2) the time series in the dataframe and date_range list are different. I would appreciate information on both problems. Thanks, community.