Getting Guide From Documentation When Writing Code

by bldrkamal in Teachers > Coding

148 Views, 0 Favorites, 0 Comments

Getting Guide From Documentation When Writing Code

pandas colab.PNG

Thousands of people across the world are learning how to program. A lot of information and resources are available on the internet to get started. This includes millions of blog posts and thousands of youtube uploads daily. As this information increases daily, many learners start getting distracted on what to focus on,

it is expected that using documentation can help developers stay focused, utilize their time and effort efficiently and learn the bigger picture in programming.

This article is on how to visit the official documentation page of any python library and understand how to use any class and its method or function in your program

We will be using pandas.isna() function documentation from the official pandas library website as our case study.

Supplies

pandas doc.PNG
def isna.PNG
  1. we are using the python pandas data science library.
  2. We will be using google colab and running the code from our browser

Visit Pandas Documentation Website

pandas documentation.PNG

The pandas library documentation is divided into four major parts:


Getting started

if you are new to pandas library, this section is for you as it contains tutorials that are simple to understand the basic concept of the framework


User guide

this section provides information on some pandas key concepts.

Developer guide

if you want to improve on some existing functionalities of the library, then this section is for you


API reference

This page gives an overview of all public pandas objects, parameters, return types, functions and methods. All classes and functions are exposed in pandas.* namespace is public. This is the section we will be using for this article.

Navigate to the API Reference and Read About the Parameters the Function Pandas.isna() Can Take

pandas para.PNG

Most functions, methods and classes take either zero, single or many parameters that you can pass to them as an argument when calling such functions or creating objects from classes. it is expected that it is specified in all documentation

In this function pandas.isna(obj), from the documentation shown above, the function can take only a parameter which is a scalar or array-like object.

Read About the Pandas.isna() Function Return Type

pandas return.PNG

it is expected that any function return type will also be specified in the documentation.

This function returns a boolean or array-like boolean type. For scalar input, it returns a scalar boolean. For array input, it returns an array of boolean indicating whether each corresponding element is missing.

Lets Apply What We Have Read From the Documentation

pandas colab.PNG

Assuming we have an array containing both numeric, string and missing values, let's write a program that will detect values in the array and count the number of their occurrences and also count the number of missing or empty values, display the number of their occurrence as well. we are using the None type in the array to represent empty values In our array.

You can run the code by following this link


# import the pandas libraryimport pandas as pd#lets define our array objectalist=[1,2,3,5,None,4,None,4,6,7,3,None,"instructables"] # you can play with this list check your output#this returns a boolean arraypd.isna(alist)#we store the return boolean values in bool_arraybool_array = pd.isna(alist)# we define the counter for keeping track of None present in our array objectNone_counter=0# we define the counter for keepiing track of both numeeric and string values present our array objectvalue_counter=0# we loop theough our return boolean arrayfor i in bool_array:    if i:     #if None exist in our return boolean array    #increase our None_counter by 1    None_counter=None_counter+1  else:    # else, if string or numeric value exist, increase our value_counter by 1    value_counter=value_counter+1# we print the number of None present in our alist array objectprint("Number of occurence of None in our list object is: ", None_counter)#finally, we display the number of both our numeric and string, present in our alist array objectprint("Number of numeric or string values in our list object is: ", value_counter)

Conclusion

pandas result.PNG

It is expected that when reading documentation about a class, method and function, we should look at two important things:

  1. The number and type of parameters
  2. The return type of the function or method which you can use the return value for further computation

I hope this helps you appreciate the power of documentation