Reputation: 31
Can anybody tell me why df['2005-5-31'] trigger the 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 a 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
Upvotes: 1
Views: 266
Reputation: 93924
df['2005-5-31']
is to select by column, but you don't have a column name 2005-5-31
.
df['X']
works since you have a columns name x
The reason df['2005-5-31':'2005-5-31']
is to select by index but not column is because it doesn't make sense to make a slicing selection on columns.
For more information, take a look at here
Upvotes: 1
Reputation: 17869
df['2005-5-31']
returns the column that is named: 2005-5-31. Your columns are named X,Y,Z. And because you don't have the date column, it is giving you an error!
Now the .ix[]
method works because it takes up to two inputs, the first input being the row index. You have a row with the index '2005-5-31'
so it works!
Upvotes: 1