Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 What is the mechanism of numpy function returning pandas object?
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.

s = pd.Series([0])
<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?

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  'numpy.ndarray' object is not callable ato_cr 1 217 May-12-2020, 08:12 AM
Last Post: anbu23
  numpy.float64 object is not iterable jcdr91 1 508 Apr-22-2020, 04:09 PM
Last Post: jefsummers
  how to apply user defined function to Pandas DataFrame evelynow 3 1,927 Aug-20-2019, 11:35 PM
Last Post: scidam
  numpy.copy / numpy.delete paul18fr 3 647 Jul-26-2019, 01:51 PM
Last Post: paul18fr
  Pandas to_csv in for loop AttributeError: 'tuple' object has no attribute 'to_csv' NSearch 9 5,244 Apr-22-2019, 05:05 PM
Last Post: Yoriz
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 696 Apr-09-2019, 04:54 PM
Last Post: PhysChem
  Function question using Pandas smw10c 7 3,227 Feb-12-2019, 06:52 PM
Last Post: Nathandsn
  Pandas and numpy gehrenfeld 7 2,500 Dec-26-2018, 07:37 AM
Last Post: ayaz786amd
  'function' object does not support item assignment Mahdi1994 2 3,447 Apr-04-2018, 12:16 PM
Last Post: Mahdi1994
  Convert indexing For Loop from MATLAB (uses numpy and pandas) bentaz 3 1,522 Mar-20-2018, 08:29 PM
Last Post: bentaz

Forum Jump:

Users browsing this thread: 1 Guest(s)