The following post introduces the petpy
package and its methods for interacting with the Petfinder API. The goal of the petpy
library is to enable other users to interact with the rich data available in the Petfinder database with an easy-to-use and straightforward Python interface. Methods for coercing the often messy JSON and XML API outputs into pandas DataFrame are also available to help facilitate users who are more interested in using the API for data analysis. More information on the Petfinder API itself can be found on the API documentation page.
Installation¶
If not already installed, install petpy
using pip
:
pip install petpy
Then, import the package.
import petpy
The Petfinder API requires an API key to authenticate access. To receive an API key, register with Petfinder on their developer page: https://www.petfinder.com/developers/api-key
The API key received from Petfinder is then used to authenticate the Petfinder
class in petpy
.
The API key is first stored as an environment variable and then loaded using the os
library. Storing your keys received from APIs and other sensitive information in a secure file or as an environment variable is considered best practice to avoid any potential malicious activity.
import os
key = os.getenv('PETFINDER_KEY')
pf = petpy.Petfinder(key)
The pf
variable is the initialized Petfinder class with our given API key. We can now use this instance to interact with and extract data from the Petfinder API.
Examples using the petpy
package¶
Pet Methods¶
The following examples demonstrate some simple usage of using petpy
to interact with and pull data from the Petfinder database. petpy
contains methods for coercing the returned API results into a pandas DataFrame for easier data analysis and exporting the results into more common formats such as .csv or Excel. More examples of how to use petpy
in conjunction with the Python scientific computing stack (Scipy, Numpy, pandas, scikit-learn, etc.) to analyze the results can be found in the later chapters of this introduction.
Getting Animal Breeds¶
Pulling the list of animal breeds from the Petfinder database is straightforward with petpy
. Let's say we are interested in finding the available breeds of cats:
cats = pf.breed_list('cat')
The default return format is JSON, but can be changed to XML by setting the default parameter outputformat
to 'xml'.
cats
The return_df
parameter can also be set to True to coerce the results into a pandas DataFrame.
cats_df = pf.breed_list('cat', return_df=True)
cats_df.head()
Please note the coercion to a pandas DataFrame removes the metadata returned in the JSON format to make the conversion process more efficient and straightforward.
According to Petfinder's API documentation, the available animals to search are ['barnyard', 'bird', 'cat', 'dog', 'horse', 'reptile', 'smallfurry']. Searching for an animal not available in the Petfinder database will return a JSON object with a message stating 'invalid arguments'.
pf.breed_list('zebra')
Returning random Petfinder pet records¶
The petpy
method pet_getRandom()
provides a wrapper for the Petfinder pet.getRandom
method. The potential results can be filtered to a subset by the method parameters, otherwise the method can be called simply as:
pf.pet_getRandom()
The default record output contains only the pet record's ID and the call's JSON metadata. If we wish to return a more complete random pet record, we can set the parameter output
to basic
(name, age, animal, breed, shelterID) or full
(complete record with description).
pf.pet_getRandom(output='full')
We can also pull a specified number of pet records from the API by setting the records
parameter and return the collected results as a pandas DataFrame by setting return_df
to True
.
random_pet_df = pf.pet_getRandom(records=5, return_df=True)
random_pet_df
Return a pet record associated with a specific petId¶
The pet_get
method can be used to extract a full record from the Petfinder database. We use the pet ID retrieved from the previous call to pet_getRandom
to illustrate.
pet = pf.pet_get('26417898')
pet
The record can also be returned as a DataFrame
.
pf.pet_get('39801731', return_df=True)
The pets_get()
method accepts a list or tuple and returns the records associated with each pet ID in the passed variable.
petids = random_pet_df['id'].tolist() # get the pet IDs from the previous call by turning the id column into a list
pf.pets_get(petids, return_df=True)
The pets_get()
method is essentially a convenience wrapper of pet_get()
. The same results can be obtained by passing the variable to pet_get()
.
pf.pet_get(petids, return_df=True)
Finding pet records matching particular search criteria¶
The pet.find()
method returns a collection of pet records that match the input search criteria. The available search criteria are listed in the petpy API documentation.
For example, let's say we are interested in finding female cats in Washington state and we want the results returned in a tidy pandas DataFrame.
cats = pf.pet_find(location='WA', animal='cat', sex='F', return_df=True)
cats.head()
The default amount of records returned is 25, which can be changed by setting the count
parameter. For large queries, it is recommended to set the pages
parameter with a smaller count
value. For example, if we wanted to return 1000 results, we could set the pages
parameter to 10 and the count
parameter to 100. Please note the Petfinder API places a hard cap of 2,000 results returned per query.
Shelter Methods¶
Shelter methods are quite similar to the pet methods explored previously but return information on the animal welfare organizations available in Petfinder's database.
Finding animal welfare organizations in a certain area¶
The shelter_find()
method can be used to return shelter records matching the input search criteria. Let's say we want to find 10 shelters listed in the Petfinder database located in Washington State as a pandas DataFrame.
wa_shelters = pf.shelter_find(location='WA', count=10, return_df=True)
wa_shelters
Returning specific shelter information¶
The shelter_get()
method returns the available information in the Petfinder database matching the given shelter ID. Shelter IDs can be found using the shelter_find()
method used earlier. For example, let's use the method to return the record matching the first shelter ID in the result set obtained in the previous example.
shelter_list = wa_shelters['id'].tolist()
pf.shelter_get(shelter_list[0])
The shelter_get()
method can also accept a list or tuple of shelter IDs. Internally, this calls a convenience wrapper method shelters_get()
.
pf.shelters_get(shelter_list, return_df=True)
The result obtained would be the same if one were to use the shelter_get()
method and passed the same variable.
Extracting pet records from a particular shelter¶
The shelter.getPets()
method returns the pet records that belong to a particular shelter ID. For example, let's say we want to return the pet records from the first shelter in our list as a DataFrame.
pf.shelter_getPets(shelter_list[0], return_df=True)
Finding shelters that have records matching a particular animal breed¶
The shelter_listByBreeds()
method allows the user to find shelters that match pet records of the input animal breed. This method is best used in conjunction with the breed_list()
method to find the available animal breeds in the Petfinder database.
We already extracted the available cat breeds earlier in the introduction, which we can use to select a cat breed listed in the Petfinder database.
cats_df.head()
The Abyssinian is a beautiful breed of cat, let's find some shelters that have pet records matching an Abyssinian breed and return it as a DataFrame.
aby = cats_df['cat breeds'].tolist()[0]
pf.shelter_listByBreed('cat', aby, return_df=True)