python - Pandas Dataframe label selection -
can tell me why df['2005-5-31'] trigger keyerror exception?
rng = pd.date_range('2005', '2012', freq='m') df = pd.dataframe(randn(len(rng), 3), rng, ['x', 'y', 'z']) df.head() # works df.ix['2005-5-31'] df['2005-5-31':'2005-5-31'] # gives keyerror: u'no item named 2005-5-31' df['2005-5-31']
follow code using df['2000-01-01'] works.
#multiple rows on single date rng = pd.date_range('2000-01-01', '2000-01-3', freq='8h') df = pd.dataframe(randn(len(rng), 3), rng, ['x', 'y', 'z']) # works df['2000-01-01'] x y z 2000-01-01 00:00:00 -0.227981 1.927932 -0.518947 2000-01-01 08:00:00 0.486063 -1.255186 0.375075 2000-01-01 16:00:00 -2.313950 0.654384 1.111493
df['2005-5-31']
returns column named: 2005-5-31. columns named x,y,z. , because don't have date column, giving error!
now .ix[]
method works because takes 2 inputs, first input being row index. have row index '2005-5-31'
works!
Comments
Post a Comment