How to write DataFrame compatible features

I recently switched from using numpy ndarrayto pandas' DataFramefor my data analysis needs. I noticed that numpy functions seem to accept DataFrameobjects instead ndarraywithout any problems. However, when I try to use many of my existing functions written to work with ndarray, they often fail when performing indexing, translation, etc. operations, and I am forced to transfer the base one ndarrayusing df.values.

Is there a standard way or set of recommendations for ensuring function compatibility with DataFrame? How to execute numpy functions for both types?

+4
source share
1 answer

I resorted to digging in the numpy source code and found that many functions simply convert the input to ndarray, first using functions such as asarrayor asanyarray.

def numpyFunction(x, *args, **kwargs):
    x = np.asanyarray(x)
    ...
+1
source

Source: https://habr.com/ru/post/1679727/


All Articles