Python Forum

Full Version: What is the mechanism of numpy function returning pandas object?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Numpy's isnan() can generate the result as Series class of pandas, although numpy does not have dependency on pandas and Numpy's API reference does not explain this as below. Could anyone please tell me the mechanism of this? Unfortunately, I could not find isnan()'s definition in Numpy's source codes.

isnan()'s API Reference Wrote:Returns: y : ndarray or bool
True where x is NaN, false otherwise. This is a scalar if x is a scalar.

Example
s = pd.Series([0])
print(type(np.isnan(s)))
Output:
<class 'pandas.core.series.Series'>
You have the understanding the wrong way round - Series likely inherits from a NumPy class or implements the kind of interface* that NumPy expects.

* OK, you don't have interfaces in Python per se, but you can do it by implementing whatever methods you need (the term to look up is "duck typing").
Thank you so much for the reply, ndc85430.

If the mechanism is based on duck typing, does isnan() do below?

- First, isnan() checks if the argument object has all required methods as array
- if it does, it creates a new instance with the constructor of the argument’s class
- it puts the results into the new instance with those those methods and returns it

Are there any example codes?