3jane / numpy_ext Goto Github PK
View Code? Open in Web Editor NEWAn extension library for NumPy that implements common array operations not present in NumPy
Home Page: https://3jane.github.io/numpy_ext/
License: MIT License
An extension library for NumPy that implements common array operations not present in NumPy
Home Page: https://3jane.github.io/numpy_ext/
License: MIT License
numpy>=1.20.1,<=1.23.1
joblib>=1.0.1,<1.1.0
Please remove the qualifications for these version numbers。
When used rolling_apply with multyple arrays are had different types i got error from title.
The error raised because the method nans creates np.empty(shape) with the default dtype.
...
arr = np.empty(shape) # <- There is creates nans array with default (float64) dtype
arr.fill(np.nan)
return arr
I suggest following changes to fix it:
def prepend_na(array: np.ndarray, n: int) -> np.ndarray:
"""
Return a copy of array with nans inserted at the beginning.
Parameters
----------
array : np.ndarray
Input array.
n : int
Number of elements to insert.
Returns
-------
np.ndarray
New array with nans added at the beginning.
Examples
--------
>>> prepend_na(np.array([1, 2]), 2)
array([nan, nan, 1., 2.])
"""
return np.hstack((nans(n, array[0].dtype), array)) #<- here
def nans(shape: Union[int, Tuple[int]], dtype) -> np.ndarray: #<- here
"""
Return a new array of a given shape and type, filled with np.nan values.
Parameters
----------
shape : int or tuple of ints
Shape of the new array, e.g., (2, 3) or 2.
Returns
-------
np.ndarray
Array of np.nans of the given shape.
Examples
--------
>>> nans(3)
array([nan, nan, nan])
>>> nans((2, 2))
array([[nan, nan],
[nan, nan]])
"""
arr = np.empty(shape, dtype = dtype) # <- here
arr.fill(np.nan)
return arr
How to reproduce
dt_arr = np.array(['2007-07-13', '2006-01-13', '2010-08-13', '2007-07-14', '2006-01-15', '2010-08-16', ], dtype='datetime64')
dt_2arr = np.arange(6)
winsize = 2
def delegate(index, data, external_counters, payload_func):
it = external_counters[0]
it+=1
print ('>>>', index[0],'__', data[0], it, end = '->')
res = payload_func(index, data, external_counters)
external_counters[0] = it
print ('>>>', res, index.shape[0])
#if it < index.shape[0]: return np.nan # this is ugly workaround
return res
df_sts_meta = pd.DataFrame()
payload_func = lambda index, data, external_counters: index[0]
rolling_apply(delegate, winsize, dt_arr, dt_2arr, external_counters=[0,0],payload_func = payload_func, n_jobs = 1)
TypeError: The DTypes <class 'numpy.dtype[datetime64]'> and <class 'numpy.dtype[float64]'> do not have a common DType. For example they cannot be stored in a single array unless the dtype is
object.
Hi
This is somewhat related to issue #9
I'm using rolling_apply on a function that returns multiple values.
Something like this:
import numpy as np
import numpy_ext as npext
def min_max(arr):
return np.min(arr), np.max(arr)
a = np.arange(1000)
b = npext.rolling_apply(min_max, 5, a)
However I'm getting this error:
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)
The problem is that, when the NaN are prepended, a 1D vector of NaN is prepended.
Here:
return np.hstack(
(
nans(n, array[0].dtype) if len(array) and hasattr(array[0], 'dtype') else nans(n),
array
)
)
I noticed that the nans function is ready to get the shape argument but the prepend only send n, instead of n and len(array[0])
I'd propose making this change on prepend_na:
return np.vstack(
(
nans((n, len(array[0])), array[0].dtype) if len(array) and hasattr(array[0], 'dtype') else nans((n, len(array[0]))),
array
)
)
Note also the change from hstack to vstack
IMPORTANT: this breaks the implementation for 1D arrays if array[0] is a number and doesn't have len()
Regards
Numpy version 1.2.4 does not support np.float.
def nans(shape: Union[int, Tuple[int, ...]], dtype=np.float64) -> np.ndarray:
if np.issubdtype(dtype, np.integer):
dtype = np.float
arr = np.empty(shape, dtype=dtype)
arr.fill(np.nan)
return arr
I'm not sure whether this is in the documentation somewhere and I missed it, but I am trying to replicate the behavior of pandas
rolling
function:
.rolling(3, center=True, min_periods=0)
From the minimal example in the docs:
import numpy as np
import numpy_ext as npext
a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
window = 3
npext.rolling_apply(np.sum, window, a, prepend_nans=True)
array([nan, nan, 3., 6., 9., 12., 15., 18., 21., 24.])
npext.rolling_apply(np.sum, window, a, prepend_nans=False)
array([ 3, 6, 9, 12, 15, 18, 21, 24])
Is there a way to get the result to match this pandas
version?
pd.DataFrame(a).rolling(3, center=True, min_periods=0).apply(np.sum).values.flatten()
array([ 1., 3., 6., 9., 12., 15., 18., 21., 24., 17.])
Hi there,
the requirements.txt has dependencies on very specific versions of numpy and job lib
numpy==1.20.1
joblib==1.0.1
Are these specific versions crucial to the execution code, or can they be relaxed to be more compatible . i.e.
numpy>=1.20.1,<1.21
joblib>=1.0.1,<1.1.0
I am having issues installing this package into a project of mine that has a different numpy dependency.
Thanks in advance.
Platform: Windows 10
Anaconda environment with python 3.10 installed.
Numpy version: 1.22
When I try to install numpy_ext it downgrades Numpy to version 1.20.3 which isn't supported to run under Python 3.10 and then numpy_ext fails.
Part of the error message:
Collecting numpy_ext Using cached numpy_ext-0.9.6-py3-none-any.whl (6.9 kB) Collecting numpy<1.21,>=1.20.1 Using cached numpy-1.20.3.zip (7.8 MB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Collecting joblib<1.1.0,>=1.0.1 Using cached joblib-1.0.1-py3-none-any.whl (303 kB) Building wheels for collected packages: numpy Building wheel for numpy (pyproject.toml) ... error ERROR: Command errored out with exit status 1: command: 'C:\Users\Michael\Anaconda3\envs\python310\python.exe' 'C:\Users\Michael\Anaconda3\envs\python310\lib\site-packages\pip\_vendor\pep517\in_process\_in_process.py' build_wheel 'C:\Users\Michael\AppData\Local\Temp\tmpdayu8j06' cwd: C:\Users\Michael\AppData\Local\Temp\pip-install-5dirr115\numpy_5153723f3f2b4e6ba2137681641308a2 Complete output (844 lines): setup.py:66: RuntimeWarning: NumPy 1.20.3 may not yet support Python 3.10. warnings.warn( Running from numpy source directory.
The complete error message is in the attached txt.
numpy_ext error.txt
Is it correct that numpy_ext can't run under python 3.10?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.