How to use str methods inside pandas query()

Viewed 2214

There appears to be a right and a wrong way to use str methods inside of pandas query. Why is the first query working as expected but the second one fails:

>>> import pandas
>>> data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
    ...         'year': [2012, 2012, 2013, 2014, 2014],
    ...         'coverage': [25, 94, 57, 62, 70]}
>>> df = pandas.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])
>>> print(df.query('name.str.slice(0,1)=="J"'))
              coverage   name  year
    Cochice         25  Jason  2012
    Maricopa        62   Jake  2014
>>> 
>>> print(df.query('name.str.startswith("J")'))
<lines omitted>
    TypeError: 'Series' objects are mutable, thus they cannot be hashed
1 Answers
Related