cabavyras
cabavyras

Reputation: 59

Why .loc doesn't work in Python Pandas Series?

I get the needed row of date '2013-09-12' with filter. Is it because label 'date' is a label of an indecies and indecies are '2013-09-12' dates. Where '.loc', I believe, is looking at column and row label values. Is my understanding correct?

mean_altitudes_perday = grouped_bydates.altitude.mean()

print(mean_altitudes_perday)
print(mean_altitudes_perday.loc[mean_altitudes_perday['date']] == '2013-09-12') # Why loc is not working here?


# Q: What is the mean altitude of the birds on 2013-09-12?
# A: print(mean_altitudes_perday.filter(like='2013-09-12', axis=0))

Error Message:

date
2013-08-15    134.092000
2013-08-16    134.839506
2013-08-17    147.439024
2013-08-18    129.608163
2013-08-19    180.174797
                 ...    
2014-04-26     15.118012
2014-04-27     23.897297
2014-04-28     37.716867
2014-04-29     19.244792
2014-04-30     13.954545
Name: altitude, Length: 259, dtype: float64
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4410             try:
-> 4411                 return libindex.get_value_at(s, key)
   4412             except IndexError:

# Had to delete a lot of error code due to posting requirements

KeyError: 'date'

Upvotes: 1

Views: 891

Answers (1)

Valdi_Bo
Valdi_Bo

Reputation: 30991

It looks like mean_altitudes_perday is a Series, with the index named date.

Probably some DataFrame contained date and altitude columns and you created mean_altitudes_perday executing something like:

mean_altitudes_perday = df.set_index('date').altitude

Note Name: altitude if the final row of your printout. This is just a name of the Series, inherited from the original column name. The index is also inherited, with its original name.

When you execute somenthing like mean_altitudes_perday['date'], Pandas attempts to find 'date' (a string) in the index and return the corresponding element of this Series.

As the index has no element == 'date', an exception (KeyError) is raised.

Maybe a more meaningful name of this exception would be IndexError (no corresponding value in the index), but we can't do anything about it.

But if you execute e.g. mean_altitudes_perday['2013-08-19'], you will get a value (180.174797).

You can also run mean_altitudes_perday.loc['2013-08-19'] (more generally, passing any existing index value), with the same result.

Upvotes: 1

Related Questions