Datasets:
Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/__pycache__/__init__.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/__pycache__/arraylike.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/__pycache__/config_init.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/__pycache__/roperator.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/__pycache__/sorting.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/__init__.py +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/__pycache__/__init__.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/__pycache__/executor.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/executor.py +239 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/extensions.py +585 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/kernels/__init__.py +27 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/kernels/mean_.py +196 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/kernels/min_max_.py +125 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/kernels/shared.py +29 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/kernels/sum_.py +244 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/kernels/var_.py +245 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__init__.py +9 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/__init__.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/datetimelike_accumulations.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/masked_accumulations.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/masked_reductions.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/putmask.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/quantile.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/replace.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/take.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/transforms.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/datetimelike_accumulations.py +67 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/masked_accumulations.py +90 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/masked_reductions.py +201 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/putmask.py +149 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/quantile.py +226 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/replace.py +154 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/take.py +594 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/transforms.py +50 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__init__.py +43 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/__init__.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/_arrow_string_mixins.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/_mixins.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/_ranges.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/_utils.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/base.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/boolean.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/categorical.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/datetimelike.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/datetimes.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/floating.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/integer.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/interval.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/masked.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/numeric.cpython-310.pyc +0 -0
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (222 Bytes). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/__pycache__/arraylike.cpython-310.pyc
ADDED
|
Binary file (14.5 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/__pycache__/config_init.cpython-310.pyc
ADDED
|
Binary file (21.1 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/__pycache__/roperator.cpython-310.pyc
ADDED
|
Binary file (1.85 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/__pycache__/sorting.cpython-310.pyc
ADDED
|
Binary file (19.9 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/__init__.py
ADDED
|
File without changes
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (229 Bytes). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/__pycache__/executor.cpython-310.pyc
ADDED
|
Binary file (5.14 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/executor.py
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
| 2 |
+
|
| 3 |
+
import functools
|
| 4 |
+
from typing import (
|
| 5 |
+
TYPE_CHECKING,
|
| 6 |
+
Any,
|
| 7 |
+
Callable,
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
if TYPE_CHECKING:
|
| 11 |
+
from pandas._typing import Scalar
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
from pandas.compat._optional import import_optional_dependency
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@functools.cache
|
| 19 |
+
def generate_apply_looper(func, nopython=True, nogil=True, parallel=False):
|
| 20 |
+
if TYPE_CHECKING:
|
| 21 |
+
import numba
|
| 22 |
+
else:
|
| 23 |
+
numba = import_optional_dependency("numba")
|
| 24 |
+
nb_compat_func = numba.extending.register_jitable(func)
|
| 25 |
+
|
| 26 |
+
@numba.jit(nopython=nopython, nogil=nogil, parallel=parallel)
|
| 27 |
+
def nb_looper(values, axis):
|
| 28 |
+
# Operate on the first row/col in order to get
|
| 29 |
+
# the output shape
|
| 30 |
+
if axis == 0:
|
| 31 |
+
first_elem = values[:, 0]
|
| 32 |
+
dim0 = values.shape[1]
|
| 33 |
+
else:
|
| 34 |
+
first_elem = values[0]
|
| 35 |
+
dim0 = values.shape[0]
|
| 36 |
+
res0 = nb_compat_func(first_elem)
|
| 37 |
+
# Use np.asarray to get shape for
|
| 38 |
+
# https://github.com/numba/numba/issues/4202#issuecomment-1185981507
|
| 39 |
+
buf_shape = (dim0,) + np.atleast_1d(np.asarray(res0)).shape
|
| 40 |
+
if axis == 0:
|
| 41 |
+
buf_shape = buf_shape[::-1]
|
| 42 |
+
buff = np.empty(buf_shape)
|
| 43 |
+
|
| 44 |
+
if axis == 1:
|
| 45 |
+
buff[0] = res0
|
| 46 |
+
for i in numba.prange(1, values.shape[0]):
|
| 47 |
+
buff[i] = nb_compat_func(values[i])
|
| 48 |
+
else:
|
| 49 |
+
buff[:, 0] = res0
|
| 50 |
+
for j in numba.prange(1, values.shape[1]):
|
| 51 |
+
buff[:, j] = nb_compat_func(values[:, j])
|
| 52 |
+
return buff
|
| 53 |
+
|
| 54 |
+
return nb_looper
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
@functools.cache
|
| 58 |
+
def make_looper(func, result_dtype, is_grouped_kernel, nopython, nogil, parallel):
|
| 59 |
+
if TYPE_CHECKING:
|
| 60 |
+
import numba
|
| 61 |
+
else:
|
| 62 |
+
numba = import_optional_dependency("numba")
|
| 63 |
+
|
| 64 |
+
if is_grouped_kernel:
|
| 65 |
+
|
| 66 |
+
@numba.jit(nopython=nopython, nogil=nogil, parallel=parallel)
|
| 67 |
+
def column_looper(
|
| 68 |
+
values: np.ndarray,
|
| 69 |
+
labels: np.ndarray,
|
| 70 |
+
ngroups: int,
|
| 71 |
+
min_periods: int,
|
| 72 |
+
*args,
|
| 73 |
+
):
|
| 74 |
+
result = np.empty((values.shape[0], ngroups), dtype=result_dtype)
|
| 75 |
+
na_positions = {}
|
| 76 |
+
for i in numba.prange(values.shape[0]):
|
| 77 |
+
output, na_pos = func(
|
| 78 |
+
values[i], result_dtype, labels, ngroups, min_periods, *args
|
| 79 |
+
)
|
| 80 |
+
result[i] = output
|
| 81 |
+
if len(na_pos) > 0:
|
| 82 |
+
na_positions[i] = np.array(na_pos)
|
| 83 |
+
return result, na_positions
|
| 84 |
+
|
| 85 |
+
else:
|
| 86 |
+
|
| 87 |
+
@numba.jit(nopython=nopython, nogil=nogil, parallel=parallel)
|
| 88 |
+
def column_looper(
|
| 89 |
+
values: np.ndarray,
|
| 90 |
+
start: np.ndarray,
|
| 91 |
+
end: np.ndarray,
|
| 92 |
+
min_periods: int,
|
| 93 |
+
*args,
|
| 94 |
+
):
|
| 95 |
+
result = np.empty((values.shape[0], len(start)), dtype=result_dtype)
|
| 96 |
+
na_positions = {}
|
| 97 |
+
for i in numba.prange(values.shape[0]):
|
| 98 |
+
output, na_pos = func(
|
| 99 |
+
values[i], result_dtype, start, end, min_periods, *args
|
| 100 |
+
)
|
| 101 |
+
result[i] = output
|
| 102 |
+
if len(na_pos) > 0:
|
| 103 |
+
na_positions[i] = np.array(na_pos)
|
| 104 |
+
return result, na_positions
|
| 105 |
+
|
| 106 |
+
return column_looper
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
default_dtype_mapping: dict[np.dtype, Any] = {
|
| 110 |
+
np.dtype("int8"): np.int64,
|
| 111 |
+
np.dtype("int16"): np.int64,
|
| 112 |
+
np.dtype("int32"): np.int64,
|
| 113 |
+
np.dtype("int64"): np.int64,
|
| 114 |
+
np.dtype("uint8"): np.uint64,
|
| 115 |
+
np.dtype("uint16"): np.uint64,
|
| 116 |
+
np.dtype("uint32"): np.uint64,
|
| 117 |
+
np.dtype("uint64"): np.uint64,
|
| 118 |
+
np.dtype("float32"): np.float64,
|
| 119 |
+
np.dtype("float64"): np.float64,
|
| 120 |
+
np.dtype("complex64"): np.complex128,
|
| 121 |
+
np.dtype("complex128"): np.complex128,
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
# TODO: Preserve complex dtypes
|
| 126 |
+
|
| 127 |
+
float_dtype_mapping: dict[np.dtype, Any] = {
|
| 128 |
+
np.dtype("int8"): np.float64,
|
| 129 |
+
np.dtype("int16"): np.float64,
|
| 130 |
+
np.dtype("int32"): np.float64,
|
| 131 |
+
np.dtype("int64"): np.float64,
|
| 132 |
+
np.dtype("uint8"): np.float64,
|
| 133 |
+
np.dtype("uint16"): np.float64,
|
| 134 |
+
np.dtype("uint32"): np.float64,
|
| 135 |
+
np.dtype("uint64"): np.float64,
|
| 136 |
+
np.dtype("float32"): np.float64,
|
| 137 |
+
np.dtype("float64"): np.float64,
|
| 138 |
+
np.dtype("complex64"): np.float64,
|
| 139 |
+
np.dtype("complex128"): np.float64,
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
identity_dtype_mapping: dict[np.dtype, Any] = {
|
| 143 |
+
np.dtype("int8"): np.int8,
|
| 144 |
+
np.dtype("int16"): np.int16,
|
| 145 |
+
np.dtype("int32"): np.int32,
|
| 146 |
+
np.dtype("int64"): np.int64,
|
| 147 |
+
np.dtype("uint8"): np.uint8,
|
| 148 |
+
np.dtype("uint16"): np.uint16,
|
| 149 |
+
np.dtype("uint32"): np.uint32,
|
| 150 |
+
np.dtype("uint64"): np.uint64,
|
| 151 |
+
np.dtype("float32"): np.float32,
|
| 152 |
+
np.dtype("float64"): np.float64,
|
| 153 |
+
np.dtype("complex64"): np.complex64,
|
| 154 |
+
np.dtype("complex128"): np.complex128,
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def generate_shared_aggregator(
|
| 159 |
+
func: Callable[..., Scalar],
|
| 160 |
+
dtype_mapping: dict[np.dtype, np.dtype],
|
| 161 |
+
is_grouped_kernel: bool,
|
| 162 |
+
nopython: bool,
|
| 163 |
+
nogil: bool,
|
| 164 |
+
parallel: bool,
|
| 165 |
+
):
|
| 166 |
+
"""
|
| 167 |
+
Generate a Numba function that loops over the columns 2D object and applies
|
| 168 |
+
a 1D numba kernel over each column.
|
| 169 |
+
|
| 170 |
+
Parameters
|
| 171 |
+
----------
|
| 172 |
+
func : function
|
| 173 |
+
aggregation function to be applied to each column
|
| 174 |
+
dtype_mapping: dict or None
|
| 175 |
+
If not None, maps a dtype to a result dtype.
|
| 176 |
+
Otherwise, will fall back to default mapping.
|
| 177 |
+
is_grouped_kernel: bool, default False
|
| 178 |
+
Whether func operates using the group labels (True)
|
| 179 |
+
or using starts/ends arrays
|
| 180 |
+
|
| 181 |
+
If true, you also need to pass the number of groups to this function
|
| 182 |
+
nopython : bool
|
| 183 |
+
nopython to be passed into numba.jit
|
| 184 |
+
nogil : bool
|
| 185 |
+
nogil to be passed into numba.jit
|
| 186 |
+
parallel : bool
|
| 187 |
+
parallel to be passed into numba.jit
|
| 188 |
+
|
| 189 |
+
Returns
|
| 190 |
+
-------
|
| 191 |
+
Numba function
|
| 192 |
+
"""
|
| 193 |
+
|
| 194 |
+
# A wrapper around the looper function,
|
| 195 |
+
# to dispatch based on dtype since numba is unable to do that in nopython mode
|
| 196 |
+
|
| 197 |
+
# It also post-processes the values by inserting nans where number of observations
|
| 198 |
+
# is less than min_periods
|
| 199 |
+
# Cannot do this in numba nopython mode
|
| 200 |
+
# (you'll run into type-unification error when you cast int -> float)
|
| 201 |
+
def looper_wrapper(
|
| 202 |
+
values,
|
| 203 |
+
start=None,
|
| 204 |
+
end=None,
|
| 205 |
+
labels=None,
|
| 206 |
+
ngroups=None,
|
| 207 |
+
min_periods: int = 0,
|
| 208 |
+
**kwargs,
|
| 209 |
+
):
|
| 210 |
+
result_dtype = dtype_mapping[values.dtype]
|
| 211 |
+
column_looper = make_looper(
|
| 212 |
+
func, result_dtype, is_grouped_kernel, nopython, nogil, parallel
|
| 213 |
+
)
|
| 214 |
+
# Need to unpack kwargs since numba only supports *args
|
| 215 |
+
if is_grouped_kernel:
|
| 216 |
+
result, na_positions = column_looper(
|
| 217 |
+
values, labels, ngroups, min_periods, *kwargs.values()
|
| 218 |
+
)
|
| 219 |
+
else:
|
| 220 |
+
result, na_positions = column_looper(
|
| 221 |
+
values, start, end, min_periods, *kwargs.values()
|
| 222 |
+
)
|
| 223 |
+
if result.dtype.kind == "i":
|
| 224 |
+
# Look if na_positions is not empty
|
| 225 |
+
# If so, convert the whole block
|
| 226 |
+
# This is OK since int dtype cannot hold nan,
|
| 227 |
+
# so if min_periods not satisfied for 1 col, it is not satisfied for
|
| 228 |
+
# all columns at that index
|
| 229 |
+
for na_pos in na_positions.values():
|
| 230 |
+
if len(na_pos) > 0:
|
| 231 |
+
result = result.astype("float64")
|
| 232 |
+
break
|
| 233 |
+
# TODO: Optimize this
|
| 234 |
+
for i, na_pos in na_positions.items():
|
| 235 |
+
if len(na_pos) > 0:
|
| 236 |
+
result[i, na_pos] = np.nan
|
| 237 |
+
return result
|
| 238 |
+
|
| 239 |
+
return looper_wrapper
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Disable type checking for this module since numba's internals
|
| 2 |
+
# are not typed, and we use numba's internals via its extension API
|
| 3 |
+
# mypy: ignore-errors
|
| 4 |
+
"""
|
| 5 |
+
Utility classes/functions to let numba recognize
|
| 6 |
+
pandas Index/Series/DataFrame
|
| 7 |
+
|
| 8 |
+
Mostly vendored from https://github.com/numba/numba/blob/main/numba/tests/pdlike_usecase.py
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
from __future__ import annotations
|
| 12 |
+
|
| 13 |
+
from contextlib import contextmanager
|
| 14 |
+
import operator
|
| 15 |
+
|
| 16 |
+
import numba
|
| 17 |
+
from numba import types
|
| 18 |
+
from numba.core import cgutils
|
| 19 |
+
from numba.core.datamodel import models
|
| 20 |
+
from numba.core.extending import (
|
| 21 |
+
NativeValue,
|
| 22 |
+
box,
|
| 23 |
+
lower_builtin,
|
| 24 |
+
make_attribute_wrapper,
|
| 25 |
+
overload,
|
| 26 |
+
overload_attribute,
|
| 27 |
+
overload_method,
|
| 28 |
+
register_model,
|
| 29 |
+
type_callable,
|
| 30 |
+
typeof_impl,
|
| 31 |
+
unbox,
|
| 32 |
+
)
|
| 33 |
+
from numba.core.imputils import impl_ret_borrowed
|
| 34 |
+
import numpy as np
|
| 35 |
+
|
| 36 |
+
from pandas._libs import lib
|
| 37 |
+
|
| 38 |
+
from pandas.core.indexes.base import Index
|
| 39 |
+
from pandas.core.indexing import _iLocIndexer
|
| 40 |
+
from pandas.core.internals import SingleBlockManager
|
| 41 |
+
from pandas.core.series import Series
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# Helper function to hack around fact that Index casts numpy string dtype to object
|
| 45 |
+
#
|
| 46 |
+
# Idea is to set an attribute on a Index called _numba_data
|
| 47 |
+
# that is the original data, or the object data casted to numpy string dtype,
|
| 48 |
+
# with a context manager that is unset afterwards
|
| 49 |
+
@contextmanager
|
| 50 |
+
def set_numba_data(index: Index):
|
| 51 |
+
numba_data = index._data
|
| 52 |
+
if numba_data.dtype in (object, "string"):
|
| 53 |
+
numba_data = np.asarray(numba_data)
|
| 54 |
+
if not lib.is_string_array(numba_data):
|
| 55 |
+
raise ValueError(
|
| 56 |
+
"The numba engine only supports using string or numeric column names"
|
| 57 |
+
)
|
| 58 |
+
numba_data = numba_data.astype("U")
|
| 59 |
+
try:
|
| 60 |
+
index._numba_data = numba_data
|
| 61 |
+
yield index
|
| 62 |
+
finally:
|
| 63 |
+
del index._numba_data
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# TODO: Range index support
|
| 67 |
+
# (this currently lowers OK, but does not round-trip)
|
| 68 |
+
class IndexType(types.Type):
|
| 69 |
+
"""
|
| 70 |
+
The type class for Index objects.
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
def __init__(self, dtype, layout, pyclass: any) -> None:
|
| 74 |
+
self.pyclass = pyclass
|
| 75 |
+
name = f"index({dtype}, {layout})"
|
| 76 |
+
self.dtype = dtype
|
| 77 |
+
self.layout = layout
|
| 78 |
+
super().__init__(name)
|
| 79 |
+
|
| 80 |
+
@property
|
| 81 |
+
def key(self):
|
| 82 |
+
return self.pyclass, self.dtype, self.layout
|
| 83 |
+
|
| 84 |
+
@property
|
| 85 |
+
def as_array(self):
|
| 86 |
+
return types.Array(self.dtype, 1, self.layout)
|
| 87 |
+
|
| 88 |
+
def copy(self, dtype=None, ndim: int = 1, layout=None):
|
| 89 |
+
assert ndim == 1
|
| 90 |
+
if dtype is None:
|
| 91 |
+
dtype = self.dtype
|
| 92 |
+
layout = layout or self.layout
|
| 93 |
+
return type(self)(dtype, layout, self.pyclass)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
class SeriesType(types.Type):
|
| 97 |
+
"""
|
| 98 |
+
The type class for Series objects.
|
| 99 |
+
"""
|
| 100 |
+
|
| 101 |
+
def __init__(self, dtype, index, namety) -> None:
|
| 102 |
+
assert isinstance(index, IndexType)
|
| 103 |
+
self.dtype = dtype
|
| 104 |
+
self.index = index
|
| 105 |
+
self.values = types.Array(self.dtype, 1, "C")
|
| 106 |
+
self.namety = namety
|
| 107 |
+
name = f"series({dtype}, {index}, {namety})"
|
| 108 |
+
super().__init__(name)
|
| 109 |
+
|
| 110 |
+
@property
|
| 111 |
+
def key(self):
|
| 112 |
+
return self.dtype, self.index, self.namety
|
| 113 |
+
|
| 114 |
+
@property
|
| 115 |
+
def as_array(self):
|
| 116 |
+
return self.values
|
| 117 |
+
|
| 118 |
+
def copy(self, dtype=None, ndim: int = 1, layout: str = "C"):
|
| 119 |
+
assert ndim == 1
|
| 120 |
+
assert layout == "C"
|
| 121 |
+
if dtype is None:
|
| 122 |
+
dtype = self.dtype
|
| 123 |
+
return type(self)(dtype, self.index, self.namety)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
@typeof_impl.register(Index)
|
| 127 |
+
def typeof_index(val, c):
|
| 128 |
+
"""
|
| 129 |
+
This will assume that only strings are in object dtype
|
| 130 |
+
index.
|
| 131 |
+
(you should check this before this gets lowered down to numba)
|
| 132 |
+
"""
|
| 133 |
+
# arrty = typeof_impl(val._data, c)
|
| 134 |
+
arrty = typeof_impl(val._numba_data, c)
|
| 135 |
+
assert arrty.ndim == 1
|
| 136 |
+
return IndexType(arrty.dtype, arrty.layout, type(val))
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
@typeof_impl.register(Series)
|
| 140 |
+
def typeof_series(val, c):
|
| 141 |
+
index = typeof_impl(val.index, c)
|
| 142 |
+
arrty = typeof_impl(val.values, c)
|
| 143 |
+
namety = typeof_impl(val.name, c)
|
| 144 |
+
assert arrty.ndim == 1
|
| 145 |
+
assert arrty.layout == "C"
|
| 146 |
+
return SeriesType(arrty.dtype, index, namety)
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
@type_callable(Series)
|
| 150 |
+
def type_series_constructor(context):
|
| 151 |
+
def typer(data, index, name=None):
|
| 152 |
+
if isinstance(index, IndexType) and isinstance(data, types.Array):
|
| 153 |
+
assert data.ndim == 1
|
| 154 |
+
if name is None:
|
| 155 |
+
name = types.intp
|
| 156 |
+
return SeriesType(data.dtype, index, name)
|
| 157 |
+
|
| 158 |
+
return typer
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
@type_callable(Index)
|
| 162 |
+
def type_index_constructor(context):
|
| 163 |
+
def typer(data, hashmap=None):
|
| 164 |
+
if isinstance(data, types.Array):
|
| 165 |
+
assert data.layout == "C"
|
| 166 |
+
assert data.ndim == 1
|
| 167 |
+
assert hashmap is None or isinstance(hashmap, types.DictType)
|
| 168 |
+
return IndexType(data.dtype, layout=data.layout, pyclass=Index)
|
| 169 |
+
|
| 170 |
+
return typer
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
# Backend extensions for Index and Series and Frame
|
| 174 |
+
@register_model(IndexType)
|
| 175 |
+
class IndexModel(models.StructModel):
|
| 176 |
+
def __init__(self, dmm, fe_type) -> None:
|
| 177 |
+
# We don't want the numpy string scalar type in our hashmap
|
| 178 |
+
members = [
|
| 179 |
+
("data", fe_type.as_array),
|
| 180 |
+
# This is an attempt to emulate our hashtable code with a numba
|
| 181 |
+
# typed dict
|
| 182 |
+
# It maps from values in the index to their integer positions in the array
|
| 183 |
+
("hashmap", types.DictType(fe_type.dtype, types.intp)),
|
| 184 |
+
# Pointer to the Index object this was created from, or that it
|
| 185 |
+
# boxes to
|
| 186 |
+
# https://numba.discourse.group/t/qst-how-to-cache-the-boxing-of-an-object/2128/2?u=lithomas1
|
| 187 |
+
("parent", types.pyobject),
|
| 188 |
+
]
|
| 189 |
+
models.StructModel.__init__(self, dmm, fe_type, members)
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
@register_model(SeriesType)
|
| 193 |
+
class SeriesModel(models.StructModel):
|
| 194 |
+
def __init__(self, dmm, fe_type) -> None:
|
| 195 |
+
members = [
|
| 196 |
+
("index", fe_type.index),
|
| 197 |
+
("values", fe_type.as_array),
|
| 198 |
+
("name", fe_type.namety),
|
| 199 |
+
]
|
| 200 |
+
models.StructModel.__init__(self, dmm, fe_type, members)
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
make_attribute_wrapper(IndexType, "data", "_data")
|
| 204 |
+
make_attribute_wrapper(IndexType, "hashmap", "hashmap")
|
| 205 |
+
|
| 206 |
+
make_attribute_wrapper(SeriesType, "index", "index")
|
| 207 |
+
make_attribute_wrapper(SeriesType, "values", "values")
|
| 208 |
+
make_attribute_wrapper(SeriesType, "name", "name")
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
@lower_builtin(Series, types.Array, IndexType)
|
| 212 |
+
def pdseries_constructor(context, builder, sig, args):
|
| 213 |
+
data, index = args
|
| 214 |
+
series = cgutils.create_struct_proxy(sig.return_type)(context, builder)
|
| 215 |
+
series.index = index
|
| 216 |
+
series.values = data
|
| 217 |
+
series.name = context.get_constant(types.intp, 0)
|
| 218 |
+
return impl_ret_borrowed(context, builder, sig.return_type, series._getvalue())
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
@lower_builtin(Series, types.Array, IndexType, types.intp)
|
| 222 |
+
@lower_builtin(Series, types.Array, IndexType, types.float64)
|
| 223 |
+
@lower_builtin(Series, types.Array, IndexType, types.unicode_type)
|
| 224 |
+
def pdseries_constructor_with_name(context, builder, sig, args):
|
| 225 |
+
data, index, name = args
|
| 226 |
+
series = cgutils.create_struct_proxy(sig.return_type)(context, builder)
|
| 227 |
+
series.index = index
|
| 228 |
+
series.values = data
|
| 229 |
+
series.name = name
|
| 230 |
+
return impl_ret_borrowed(context, builder, sig.return_type, series._getvalue())
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
@lower_builtin(Index, types.Array, types.DictType, types.pyobject)
|
| 234 |
+
def index_constructor_2arg(context, builder, sig, args):
|
| 235 |
+
(data, hashmap, parent) = args
|
| 236 |
+
index = cgutils.create_struct_proxy(sig.return_type)(context, builder)
|
| 237 |
+
|
| 238 |
+
index.data = data
|
| 239 |
+
index.hashmap = hashmap
|
| 240 |
+
index.parent = parent
|
| 241 |
+
return impl_ret_borrowed(context, builder, sig.return_type, index._getvalue())
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
@lower_builtin(Index, types.Array, types.DictType)
|
| 245 |
+
def index_constructor_2arg_parent(context, builder, sig, args):
|
| 246 |
+
# Basically same as index_constructor_1arg, but also lets you specify the
|
| 247 |
+
# parent object
|
| 248 |
+
(data, hashmap) = args
|
| 249 |
+
index = cgutils.create_struct_proxy(sig.return_type)(context, builder)
|
| 250 |
+
|
| 251 |
+
index.data = data
|
| 252 |
+
index.hashmap = hashmap
|
| 253 |
+
return impl_ret_borrowed(context, builder, sig.return_type, index._getvalue())
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
@lower_builtin(Index, types.Array)
|
| 257 |
+
def index_constructor_1arg(context, builder, sig, args):
|
| 258 |
+
from numba.typed import Dict
|
| 259 |
+
|
| 260 |
+
key_type = sig.return_type.dtype
|
| 261 |
+
value_type = types.intp
|
| 262 |
+
|
| 263 |
+
def index_impl(data):
|
| 264 |
+
return Index(data, Dict.empty(key_type, value_type))
|
| 265 |
+
|
| 266 |
+
return context.compile_internal(builder, index_impl, sig, args)
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
# Helper to convert the unicodecharseq (numpy string scalar) into a unicode_type
|
| 270 |
+
# (regular string)
|
| 271 |
+
def maybe_cast_str(x):
|
| 272 |
+
# Dummy function that numba can overload
|
| 273 |
+
pass
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
@overload(maybe_cast_str)
|
| 277 |
+
def maybe_cast_str_impl(x):
|
| 278 |
+
"""Converts numba UnicodeCharSeq (numpy string scalar) -> unicode type (string).
|
| 279 |
+
Is a no-op for other types."""
|
| 280 |
+
if isinstance(x, types.UnicodeCharSeq):
|
| 281 |
+
return lambda x: str(x)
|
| 282 |
+
else:
|
| 283 |
+
return lambda x: x
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
@unbox(IndexType)
|
| 287 |
+
def unbox_index(typ, obj, c):
|
| 288 |
+
"""
|
| 289 |
+
Convert a Index object to a native structure.
|
| 290 |
+
|
| 291 |
+
Note: Object dtype is not allowed here
|
| 292 |
+
"""
|
| 293 |
+
data_obj = c.pyapi.object_getattr_string(obj, "_numba_data")
|
| 294 |
+
index = cgutils.create_struct_proxy(typ)(c.context, c.builder)
|
| 295 |
+
# If we see an object array, assume its been validated as only containing strings
|
| 296 |
+
# We still need to do the conversion though
|
| 297 |
+
index.data = c.unbox(typ.as_array, data_obj).value
|
| 298 |
+
typed_dict_obj = c.pyapi.unserialize(c.pyapi.serialize_object(numba.typed.Dict))
|
| 299 |
+
# Create an empty typed dict in numba for the hashmap for indexing
|
| 300 |
+
# equiv of numba.typed.Dict.empty(typ.dtype, types.intp)
|
| 301 |
+
arr_type_obj = c.pyapi.unserialize(c.pyapi.serialize_object(typ.dtype))
|
| 302 |
+
intp_type_obj = c.pyapi.unserialize(c.pyapi.serialize_object(types.intp))
|
| 303 |
+
hashmap_obj = c.pyapi.call_method(
|
| 304 |
+
typed_dict_obj, "empty", (arr_type_obj, intp_type_obj)
|
| 305 |
+
)
|
| 306 |
+
index.hashmap = c.unbox(types.DictType(typ.dtype, types.intp), hashmap_obj).value
|
| 307 |
+
# Set the parent for speedy boxing.
|
| 308 |
+
index.parent = obj
|
| 309 |
+
|
| 310 |
+
# Decrefs
|
| 311 |
+
c.pyapi.decref(data_obj)
|
| 312 |
+
c.pyapi.decref(arr_type_obj)
|
| 313 |
+
c.pyapi.decref(intp_type_obj)
|
| 314 |
+
c.pyapi.decref(typed_dict_obj)
|
| 315 |
+
|
| 316 |
+
return NativeValue(index._getvalue())
|
| 317 |
+
|
| 318 |
+
|
| 319 |
+
@unbox(SeriesType)
|
| 320 |
+
def unbox_series(typ, obj, c):
|
| 321 |
+
"""
|
| 322 |
+
Convert a Series object to a native structure.
|
| 323 |
+
"""
|
| 324 |
+
index_obj = c.pyapi.object_getattr_string(obj, "index")
|
| 325 |
+
values_obj = c.pyapi.object_getattr_string(obj, "values")
|
| 326 |
+
name_obj = c.pyapi.object_getattr_string(obj, "name")
|
| 327 |
+
|
| 328 |
+
series = cgutils.create_struct_proxy(typ)(c.context, c.builder)
|
| 329 |
+
series.index = c.unbox(typ.index, index_obj).value
|
| 330 |
+
series.values = c.unbox(typ.values, values_obj).value
|
| 331 |
+
series.name = c.unbox(typ.namety, name_obj).value
|
| 332 |
+
|
| 333 |
+
# Decrefs
|
| 334 |
+
c.pyapi.decref(index_obj)
|
| 335 |
+
c.pyapi.decref(values_obj)
|
| 336 |
+
c.pyapi.decref(name_obj)
|
| 337 |
+
|
| 338 |
+
return NativeValue(series._getvalue())
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
@box(IndexType)
|
| 342 |
+
def box_index(typ, val, c):
|
| 343 |
+
"""
|
| 344 |
+
Convert a native index structure to a Index object.
|
| 345 |
+
|
| 346 |
+
If our native index is of a numpy string dtype, we'll cast it to
|
| 347 |
+
object.
|
| 348 |
+
"""
|
| 349 |
+
# First build a Numpy array object, then wrap it in a Index
|
| 350 |
+
index = cgutils.create_struct_proxy(typ)(c.context, c.builder, value=val)
|
| 351 |
+
|
| 352 |
+
res = cgutils.alloca_once_value(c.builder, index.parent)
|
| 353 |
+
|
| 354 |
+
# Does parent exist?
|
| 355 |
+
# (it means already boxed once, or Index same as original df.index or df.columns)
|
| 356 |
+
# xref https://github.com/numba/numba/blob/596e8a55334cc46854e3192766e643767bd7c934/numba/core/boxing.py#L593C17-L593C17
|
| 357 |
+
with c.builder.if_else(cgutils.is_not_null(c.builder, index.parent)) as (
|
| 358 |
+
has_parent,
|
| 359 |
+
otherwise,
|
| 360 |
+
):
|
| 361 |
+
with has_parent:
|
| 362 |
+
c.pyapi.incref(index.parent)
|
| 363 |
+
with otherwise:
|
| 364 |
+
# TODO: preserve the original class for the index
|
| 365 |
+
# Also need preserve the name of the Index
|
| 366 |
+
# class_obj = c.pyapi.unserialize(c.pyapi.serialize_object(typ.pyclass))
|
| 367 |
+
class_obj = c.pyapi.unserialize(c.pyapi.serialize_object(Index))
|
| 368 |
+
array_obj = c.box(typ.as_array, index.data)
|
| 369 |
+
if isinstance(typ.dtype, types.UnicodeCharSeq):
|
| 370 |
+
# We converted to numpy string dtype, convert back
|
| 371 |
+
# to object since _simple_new won't do that for uss
|
| 372 |
+
object_str_obj = c.pyapi.unserialize(c.pyapi.serialize_object("object"))
|
| 373 |
+
array_obj = c.pyapi.call_method(array_obj, "astype", (object_str_obj,))
|
| 374 |
+
c.pyapi.decref(object_str_obj)
|
| 375 |
+
# this is basically Index._simple_new(array_obj, name_obj) in python
|
| 376 |
+
index_obj = c.pyapi.call_method(class_obj, "_simple_new", (array_obj,))
|
| 377 |
+
index.parent = index_obj
|
| 378 |
+
c.builder.store(index_obj, res)
|
| 379 |
+
|
| 380 |
+
# Decrefs
|
| 381 |
+
c.pyapi.decref(class_obj)
|
| 382 |
+
c.pyapi.decref(array_obj)
|
| 383 |
+
return c.builder.load(res)
|
| 384 |
+
|
| 385 |
+
|
| 386 |
+
@box(SeriesType)
|
| 387 |
+
def box_series(typ, val, c):
|
| 388 |
+
"""
|
| 389 |
+
Convert a native series structure to a Series object.
|
| 390 |
+
"""
|
| 391 |
+
series = cgutils.create_struct_proxy(typ)(c.context, c.builder, value=val)
|
| 392 |
+
series_const_obj = c.pyapi.unserialize(c.pyapi.serialize_object(Series._from_mgr))
|
| 393 |
+
mgr_const_obj = c.pyapi.unserialize(
|
| 394 |
+
c.pyapi.serialize_object(SingleBlockManager.from_array)
|
| 395 |
+
)
|
| 396 |
+
index_obj = c.box(typ.index, series.index)
|
| 397 |
+
array_obj = c.box(typ.as_array, series.values)
|
| 398 |
+
name_obj = c.box(typ.namety, series.name)
|
| 399 |
+
# This is basically equivalent of
|
| 400 |
+
# pd.Series(data=array_obj, index=index_obj)
|
| 401 |
+
# To improve perf, we will construct the Series from a manager
|
| 402 |
+
# object to avoid checks.
|
| 403 |
+
# We'll also set the name attribute manually to avoid validation
|
| 404 |
+
mgr_obj = c.pyapi.call_function_objargs(
|
| 405 |
+
mgr_const_obj,
|
| 406 |
+
(
|
| 407 |
+
array_obj,
|
| 408 |
+
index_obj,
|
| 409 |
+
),
|
| 410 |
+
)
|
| 411 |
+
mgr_axes_obj = c.pyapi.object_getattr_string(mgr_obj, "axes")
|
| 412 |
+
# Series._constructor_from_mgr(mgr, axes)
|
| 413 |
+
series_obj = c.pyapi.call_function_objargs(
|
| 414 |
+
series_const_obj, (mgr_obj, mgr_axes_obj)
|
| 415 |
+
)
|
| 416 |
+
c.pyapi.object_setattr_string(series_obj, "_name", name_obj)
|
| 417 |
+
|
| 418 |
+
# Decrefs
|
| 419 |
+
c.pyapi.decref(series_const_obj)
|
| 420 |
+
c.pyapi.decref(mgr_axes_obj)
|
| 421 |
+
c.pyapi.decref(mgr_obj)
|
| 422 |
+
c.pyapi.decref(mgr_const_obj)
|
| 423 |
+
c.pyapi.decref(index_obj)
|
| 424 |
+
c.pyapi.decref(array_obj)
|
| 425 |
+
c.pyapi.decref(name_obj)
|
| 426 |
+
|
| 427 |
+
return series_obj
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
# Add common series reductions (e.g. mean, sum),
|
| 431 |
+
# and also add common binops (e.g. add, sub, mul, div)
|
| 432 |
+
def generate_series_reduction(ser_reduction, ser_method):
|
| 433 |
+
@overload_method(SeriesType, ser_reduction)
|
| 434 |
+
def series_reduction(series):
|
| 435 |
+
def series_reduction_impl(series):
|
| 436 |
+
return ser_method(series.values)
|
| 437 |
+
|
| 438 |
+
return series_reduction_impl
|
| 439 |
+
|
| 440 |
+
return series_reduction
|
| 441 |
+
|
| 442 |
+
|
| 443 |
+
def generate_series_binop(binop):
|
| 444 |
+
@overload(binop)
|
| 445 |
+
def series_binop(series1, value):
|
| 446 |
+
if isinstance(series1, SeriesType):
|
| 447 |
+
if isinstance(value, SeriesType):
|
| 448 |
+
|
| 449 |
+
def series_binop_impl(series1, series2):
|
| 450 |
+
# TODO: Check index matching?
|
| 451 |
+
return Series(
|
| 452 |
+
binop(series1.values, series2.values),
|
| 453 |
+
series1.index,
|
| 454 |
+
series1.name,
|
| 455 |
+
)
|
| 456 |
+
|
| 457 |
+
return series_binop_impl
|
| 458 |
+
else:
|
| 459 |
+
|
| 460 |
+
def series_binop_impl(series1, value):
|
| 461 |
+
return Series(
|
| 462 |
+
binop(series1.values, value), series1.index, series1.name
|
| 463 |
+
)
|
| 464 |
+
|
| 465 |
+
return series_binop_impl
|
| 466 |
+
|
| 467 |
+
return series_binop
|
| 468 |
+
|
| 469 |
+
|
| 470 |
+
series_reductions = [
|
| 471 |
+
("sum", np.sum),
|
| 472 |
+
("mean", np.mean),
|
| 473 |
+
# Disabled due to discrepancies between numba std. dev
|
| 474 |
+
# and pandas std. dev (no way to specify dof)
|
| 475 |
+
# ("std", np.std),
|
| 476 |
+
# ("var", np.var),
|
| 477 |
+
("min", np.min),
|
| 478 |
+
("max", np.max),
|
| 479 |
+
]
|
| 480 |
+
for reduction, reduction_method in series_reductions:
|
| 481 |
+
generate_series_reduction(reduction, reduction_method)
|
| 482 |
+
|
| 483 |
+
series_binops = [operator.add, operator.sub, operator.mul, operator.truediv]
|
| 484 |
+
|
| 485 |
+
for ser_binop in series_binops:
|
| 486 |
+
generate_series_binop(ser_binop)
|
| 487 |
+
|
| 488 |
+
|
| 489 |
+
# get_loc on Index
|
| 490 |
+
@overload_method(IndexType, "get_loc")
|
| 491 |
+
def index_get_loc(index, item):
|
| 492 |
+
def index_get_loc_impl(index, item):
|
| 493 |
+
# Initialize the hash table if not initialized
|
| 494 |
+
if len(index.hashmap) == 0:
|
| 495 |
+
for i, val in enumerate(index._data):
|
| 496 |
+
index.hashmap[val] = i
|
| 497 |
+
return index.hashmap[item]
|
| 498 |
+
|
| 499 |
+
return index_get_loc_impl
|
| 500 |
+
|
| 501 |
+
|
| 502 |
+
# Indexing for Series/Index
|
| 503 |
+
@overload(operator.getitem)
|
| 504 |
+
def series_indexing(series, item):
|
| 505 |
+
if isinstance(series, SeriesType):
|
| 506 |
+
|
| 507 |
+
def series_getitem(series, item):
|
| 508 |
+
loc = series.index.get_loc(item)
|
| 509 |
+
return series.iloc[loc]
|
| 510 |
+
|
| 511 |
+
return series_getitem
|
| 512 |
+
|
| 513 |
+
|
| 514 |
+
@overload(operator.getitem)
|
| 515 |
+
def index_indexing(index, idx):
|
| 516 |
+
if isinstance(index, IndexType):
|
| 517 |
+
|
| 518 |
+
def index_getitem(index, idx):
|
| 519 |
+
return index._data[idx]
|
| 520 |
+
|
| 521 |
+
return index_getitem
|
| 522 |
+
|
| 523 |
+
|
| 524 |
+
class IlocType(types.Type):
|
| 525 |
+
def __init__(self, obj_type) -> None:
|
| 526 |
+
self.obj_type = obj_type
|
| 527 |
+
name = f"iLocIndexer({obj_type})"
|
| 528 |
+
super().__init__(name=name)
|
| 529 |
+
|
| 530 |
+
@property
|
| 531 |
+
def key(self):
|
| 532 |
+
return self.obj_type
|
| 533 |
+
|
| 534 |
+
|
| 535 |
+
@typeof_impl.register(_iLocIndexer)
|
| 536 |
+
def typeof_iloc(val, c):
|
| 537 |
+
objtype = typeof_impl(val.obj, c)
|
| 538 |
+
return IlocType(objtype)
|
| 539 |
+
|
| 540 |
+
|
| 541 |
+
@type_callable(_iLocIndexer)
|
| 542 |
+
def type_iloc_constructor(context):
|
| 543 |
+
def typer(obj):
|
| 544 |
+
if isinstance(obj, SeriesType):
|
| 545 |
+
return IlocType(obj)
|
| 546 |
+
|
| 547 |
+
return typer
|
| 548 |
+
|
| 549 |
+
|
| 550 |
+
@lower_builtin(_iLocIndexer, SeriesType)
|
| 551 |
+
def iloc_constructor(context, builder, sig, args):
|
| 552 |
+
(obj,) = args
|
| 553 |
+
iloc_indexer = cgutils.create_struct_proxy(sig.return_type)(context, builder)
|
| 554 |
+
iloc_indexer.obj = obj
|
| 555 |
+
return impl_ret_borrowed(
|
| 556 |
+
context, builder, sig.return_type, iloc_indexer._getvalue()
|
| 557 |
+
)
|
| 558 |
+
|
| 559 |
+
|
| 560 |
+
@register_model(IlocType)
|
| 561 |
+
class ILocModel(models.StructModel):
|
| 562 |
+
def __init__(self, dmm, fe_type) -> None:
|
| 563 |
+
members = [("obj", fe_type.obj_type)]
|
| 564 |
+
models.StructModel.__init__(self, dmm, fe_type, members)
|
| 565 |
+
|
| 566 |
+
|
| 567 |
+
make_attribute_wrapper(IlocType, "obj", "obj")
|
| 568 |
+
|
| 569 |
+
|
| 570 |
+
@overload_attribute(SeriesType, "iloc")
|
| 571 |
+
def series_iloc(series):
|
| 572 |
+
def get(series):
|
| 573 |
+
return _iLocIndexer(series)
|
| 574 |
+
|
| 575 |
+
return get
|
| 576 |
+
|
| 577 |
+
|
| 578 |
+
@overload(operator.getitem)
|
| 579 |
+
def iloc_getitem(iloc_indexer, i):
|
| 580 |
+
if isinstance(iloc_indexer, IlocType):
|
| 581 |
+
|
| 582 |
+
def getitem_impl(iloc_indexer, i):
|
| 583 |
+
return iloc_indexer.obj.values[i]
|
| 584 |
+
|
| 585 |
+
return getitem_impl
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/kernels/__init__.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pandas.core._numba.kernels.mean_ import (
|
| 2 |
+
grouped_mean,
|
| 3 |
+
sliding_mean,
|
| 4 |
+
)
|
| 5 |
+
from pandas.core._numba.kernels.min_max_ import (
|
| 6 |
+
grouped_min_max,
|
| 7 |
+
sliding_min_max,
|
| 8 |
+
)
|
| 9 |
+
from pandas.core._numba.kernels.sum_ import (
|
| 10 |
+
grouped_sum,
|
| 11 |
+
sliding_sum,
|
| 12 |
+
)
|
| 13 |
+
from pandas.core._numba.kernels.var_ import (
|
| 14 |
+
grouped_var,
|
| 15 |
+
sliding_var,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
__all__ = [
|
| 19 |
+
"sliding_mean",
|
| 20 |
+
"grouped_mean",
|
| 21 |
+
"sliding_sum",
|
| 22 |
+
"grouped_sum",
|
| 23 |
+
"sliding_var",
|
| 24 |
+
"grouped_var",
|
| 25 |
+
"sliding_min_max",
|
| 26 |
+
"grouped_min_max",
|
| 27 |
+
]
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/kernels/mean_.py
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
+
* rolling / expanding
|
| 6 |
+
|
| 7 |
+
Mirrors pandas/_libs/window/aggregation.pyx
|
| 8 |
+
"""
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
from typing import TYPE_CHECKING
|
| 12 |
+
|
| 13 |
+
import numba
|
| 14 |
+
import numpy as np
|
| 15 |
+
|
| 16 |
+
from pandas.core._numba.kernels.shared import is_monotonic_increasing
|
| 17 |
+
from pandas.core._numba.kernels.sum_ import grouped_kahan_sum
|
| 18 |
+
|
| 19 |
+
if TYPE_CHECKING:
|
| 20 |
+
from pandas._typing import npt
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@numba.jit(nopython=True, nogil=True, parallel=False)
|
| 24 |
+
def add_mean(
|
| 25 |
+
val: float,
|
| 26 |
+
nobs: int,
|
| 27 |
+
sum_x: float,
|
| 28 |
+
neg_ct: int,
|
| 29 |
+
compensation: float,
|
| 30 |
+
num_consecutive_same_value: int,
|
| 31 |
+
prev_value: float,
|
| 32 |
+
) -> tuple[int, float, int, float, int, float]:
|
| 33 |
+
if not np.isnan(val):
|
| 34 |
+
nobs += 1
|
| 35 |
+
y = val - compensation
|
| 36 |
+
t = sum_x + y
|
| 37 |
+
compensation = t - sum_x - y
|
| 38 |
+
sum_x = t
|
| 39 |
+
if val < 0:
|
| 40 |
+
neg_ct += 1
|
| 41 |
+
|
| 42 |
+
if val == prev_value:
|
| 43 |
+
num_consecutive_same_value += 1
|
| 44 |
+
else:
|
| 45 |
+
num_consecutive_same_value = 1
|
| 46 |
+
prev_value = val
|
| 47 |
+
|
| 48 |
+
return nobs, sum_x, neg_ct, compensation, num_consecutive_same_value, prev_value
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@numba.jit(nopython=True, nogil=True, parallel=False)
|
| 52 |
+
def remove_mean(
|
| 53 |
+
val: float, nobs: int, sum_x: float, neg_ct: int, compensation: float
|
| 54 |
+
) -> tuple[int, float, int, float]:
|
| 55 |
+
if not np.isnan(val):
|
| 56 |
+
nobs -= 1
|
| 57 |
+
y = -val - compensation
|
| 58 |
+
t = sum_x + y
|
| 59 |
+
compensation = t - sum_x - y
|
| 60 |
+
sum_x = t
|
| 61 |
+
if val < 0:
|
| 62 |
+
neg_ct -= 1
|
| 63 |
+
return nobs, sum_x, neg_ct, compensation
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
@numba.jit(nopython=True, nogil=True, parallel=False)
|
| 67 |
+
def sliding_mean(
|
| 68 |
+
values: np.ndarray,
|
| 69 |
+
result_dtype: np.dtype,
|
| 70 |
+
start: np.ndarray,
|
| 71 |
+
end: np.ndarray,
|
| 72 |
+
min_periods: int,
|
| 73 |
+
) -> tuple[np.ndarray, list[int]]:
|
| 74 |
+
N = len(start)
|
| 75 |
+
nobs = 0
|
| 76 |
+
sum_x = 0.0
|
| 77 |
+
neg_ct = 0
|
| 78 |
+
compensation_add = 0.0
|
| 79 |
+
compensation_remove = 0.0
|
| 80 |
+
|
| 81 |
+
is_monotonic_increasing_bounds = is_monotonic_increasing(
|
| 82 |
+
start
|
| 83 |
+
) and is_monotonic_increasing(end)
|
| 84 |
+
|
| 85 |
+
output = np.empty(N, dtype=result_dtype)
|
| 86 |
+
|
| 87 |
+
for i in range(N):
|
| 88 |
+
s = start[i]
|
| 89 |
+
e = end[i]
|
| 90 |
+
if i == 0 or not is_monotonic_increasing_bounds:
|
| 91 |
+
prev_value = values[s]
|
| 92 |
+
num_consecutive_same_value = 0
|
| 93 |
+
|
| 94 |
+
for j in range(s, e):
|
| 95 |
+
val = values[j]
|
| 96 |
+
(
|
| 97 |
+
nobs,
|
| 98 |
+
sum_x,
|
| 99 |
+
neg_ct,
|
| 100 |
+
compensation_add,
|
| 101 |
+
num_consecutive_same_value,
|
| 102 |
+
prev_value,
|
| 103 |
+
) = add_mean(
|
| 104 |
+
val,
|
| 105 |
+
nobs,
|
| 106 |
+
sum_x,
|
| 107 |
+
neg_ct,
|
| 108 |
+
compensation_add,
|
| 109 |
+
num_consecutive_same_value,
|
| 110 |
+
prev_value, # pyright: ignore[reportGeneralTypeIssues]
|
| 111 |
+
)
|
| 112 |
+
else:
|
| 113 |
+
for j in range(start[i - 1], s):
|
| 114 |
+
val = values[j]
|
| 115 |
+
nobs, sum_x, neg_ct, compensation_remove = remove_mean(
|
| 116 |
+
val, nobs, sum_x, neg_ct, compensation_remove
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
for j in range(end[i - 1], e):
|
| 120 |
+
val = values[j]
|
| 121 |
+
(
|
| 122 |
+
nobs,
|
| 123 |
+
sum_x,
|
| 124 |
+
neg_ct,
|
| 125 |
+
compensation_add,
|
| 126 |
+
num_consecutive_same_value,
|
| 127 |
+
prev_value,
|
| 128 |
+
) = add_mean(
|
| 129 |
+
val,
|
| 130 |
+
nobs,
|
| 131 |
+
sum_x,
|
| 132 |
+
neg_ct,
|
| 133 |
+
compensation_add,
|
| 134 |
+
num_consecutive_same_value,
|
| 135 |
+
prev_value, # pyright: ignore[reportGeneralTypeIssues]
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
if nobs >= min_periods and nobs > 0:
|
| 139 |
+
result = sum_x / nobs
|
| 140 |
+
if num_consecutive_same_value >= nobs:
|
| 141 |
+
result = prev_value
|
| 142 |
+
elif neg_ct == 0 and result < 0:
|
| 143 |
+
result = 0
|
| 144 |
+
elif neg_ct == nobs and result > 0:
|
| 145 |
+
result = 0
|
| 146 |
+
else:
|
| 147 |
+
result = np.nan
|
| 148 |
+
|
| 149 |
+
output[i] = result
|
| 150 |
+
|
| 151 |
+
if not is_monotonic_increasing_bounds:
|
| 152 |
+
nobs = 0
|
| 153 |
+
sum_x = 0.0
|
| 154 |
+
neg_ct = 0
|
| 155 |
+
compensation_remove = 0.0
|
| 156 |
+
|
| 157 |
+
# na_position is empty list since float64 can already hold nans
|
| 158 |
+
# Do list comprehension, since numba cannot figure out that na_pos is
|
| 159 |
+
# empty list of ints on its own
|
| 160 |
+
na_pos = [0 for i in range(0)]
|
| 161 |
+
return output, na_pos
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
@numba.jit(nopython=True, nogil=True, parallel=False)
|
| 165 |
+
def grouped_mean(
|
| 166 |
+
values: np.ndarray,
|
| 167 |
+
result_dtype: np.dtype,
|
| 168 |
+
labels: npt.NDArray[np.intp],
|
| 169 |
+
ngroups: int,
|
| 170 |
+
min_periods: int,
|
| 171 |
+
) -> tuple[np.ndarray, list[int]]:
|
| 172 |
+
output, nobs_arr, comp_arr, consecutive_counts, prev_vals = grouped_kahan_sum(
|
| 173 |
+
values, result_dtype, labels, ngroups
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
# Post-processing, replace sums that don't satisfy min_periods
|
| 177 |
+
for lab in range(ngroups):
|
| 178 |
+
nobs = nobs_arr[lab]
|
| 179 |
+
num_consecutive_same_value = consecutive_counts[lab]
|
| 180 |
+
prev_value = prev_vals[lab]
|
| 181 |
+
sum_x = output[lab]
|
| 182 |
+
if nobs >= min_periods:
|
| 183 |
+
if num_consecutive_same_value >= nobs:
|
| 184 |
+
result = prev_value * nobs
|
| 185 |
+
else:
|
| 186 |
+
result = sum_x
|
| 187 |
+
else:
|
| 188 |
+
result = np.nan
|
| 189 |
+
result /= nobs
|
| 190 |
+
output[lab] = result
|
| 191 |
+
|
| 192 |
+
# na_position is empty list since float64 can already hold nans
|
| 193 |
+
# Do list comprehension, since numba cannot figure out that na_pos is
|
| 194 |
+
# empty list of ints on its own
|
| 195 |
+
na_pos = [0 for i in range(0)]
|
| 196 |
+
return output, na_pos
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/kernels/min_max_.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Numba 1D min/max kernels that can be shared by
|
| 3 |
+
* Dataframe / Series
|
| 4 |
+
* groupby
|
| 5 |
+
* rolling / expanding
|
| 6 |
+
|
| 7 |
+
Mirrors pandas/_libs/window/aggregation.pyx
|
| 8 |
+
"""
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
from typing import TYPE_CHECKING
|
| 12 |
+
|
| 13 |
+
import numba
|
| 14 |
+
import numpy as np
|
| 15 |
+
|
| 16 |
+
if TYPE_CHECKING:
|
| 17 |
+
from pandas._typing import npt
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@numba.jit(nopython=True, nogil=True, parallel=False)
|
| 21 |
+
def sliding_min_max(
|
| 22 |
+
values: np.ndarray,
|
| 23 |
+
result_dtype: np.dtype,
|
| 24 |
+
start: np.ndarray,
|
| 25 |
+
end: np.ndarray,
|
| 26 |
+
min_periods: int,
|
| 27 |
+
is_max: bool,
|
| 28 |
+
) -> tuple[np.ndarray, list[int]]:
|
| 29 |
+
N = len(start)
|
| 30 |
+
nobs = 0
|
| 31 |
+
output = np.empty(N, dtype=result_dtype)
|
| 32 |
+
na_pos = []
|
| 33 |
+
# Use deque once numba supports it
|
| 34 |
+
# https://github.com/numba/numba/issues/7417
|
| 35 |
+
Q: list = []
|
| 36 |
+
W: list = []
|
| 37 |
+
for i in range(N):
|
| 38 |
+
curr_win_size = end[i] - start[i]
|
| 39 |
+
if i == 0:
|
| 40 |
+
st = start[i]
|
| 41 |
+
else:
|
| 42 |
+
st = end[i - 1]
|
| 43 |
+
|
| 44 |
+
for k in range(st, end[i]):
|
| 45 |
+
ai = values[k]
|
| 46 |
+
if not np.isnan(ai):
|
| 47 |
+
nobs += 1
|
| 48 |
+
elif is_max:
|
| 49 |
+
ai = -np.inf
|
| 50 |
+
else:
|
| 51 |
+
ai = np.inf
|
| 52 |
+
# Discard previous entries if we find new min or max
|
| 53 |
+
if is_max:
|
| 54 |
+
while Q and ((ai >= values[Q[-1]]) or values[Q[-1]] != values[Q[-1]]):
|
| 55 |
+
Q.pop()
|
| 56 |
+
else:
|
| 57 |
+
while Q and ((ai <= values[Q[-1]]) or values[Q[-1]] != values[Q[-1]]):
|
| 58 |
+
Q.pop()
|
| 59 |
+
Q.append(k)
|
| 60 |
+
W.append(k)
|
| 61 |
+
|
| 62 |
+
# Discard entries outside and left of current window
|
| 63 |
+
while Q and Q[0] <= start[i] - 1:
|
| 64 |
+
Q.pop(0)
|
| 65 |
+
while W and W[0] <= start[i] - 1:
|
| 66 |
+
if not np.isnan(values[W[0]]):
|
| 67 |
+
nobs -= 1
|
| 68 |
+
W.pop(0)
|
| 69 |
+
|
| 70 |
+
# Save output based on index in input value array
|
| 71 |
+
if Q and curr_win_size > 0 and nobs >= min_periods:
|
| 72 |
+
output[i] = values[Q[0]]
|
| 73 |
+
else:
|
| 74 |
+
if values.dtype.kind != "i":
|
| 75 |
+
output[i] = np.nan
|
| 76 |
+
else:
|
| 77 |
+
na_pos.append(i)
|
| 78 |
+
|
| 79 |
+
return output, na_pos
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
@numba.jit(nopython=True, nogil=True, parallel=False)
|
| 83 |
+
def grouped_min_max(
|
| 84 |
+
values: np.ndarray,
|
| 85 |
+
result_dtype: np.dtype,
|
| 86 |
+
labels: npt.NDArray[np.intp],
|
| 87 |
+
ngroups: int,
|
| 88 |
+
min_periods: int,
|
| 89 |
+
is_max: bool,
|
| 90 |
+
) -> tuple[np.ndarray, list[int]]:
|
| 91 |
+
N = len(labels)
|
| 92 |
+
nobs = np.zeros(ngroups, dtype=np.int64)
|
| 93 |
+
na_pos = []
|
| 94 |
+
output = np.empty(ngroups, dtype=result_dtype)
|
| 95 |
+
|
| 96 |
+
for i in range(N):
|
| 97 |
+
lab = labels[i]
|
| 98 |
+
val = values[i]
|
| 99 |
+
if lab < 0:
|
| 100 |
+
continue
|
| 101 |
+
|
| 102 |
+
if values.dtype.kind == "i" or not np.isnan(val):
|
| 103 |
+
nobs[lab] += 1
|
| 104 |
+
else:
|
| 105 |
+
# NaN value cannot be a min/max value
|
| 106 |
+
continue
|
| 107 |
+
|
| 108 |
+
if nobs[lab] == 1:
|
| 109 |
+
# First element in group, set output equal to this
|
| 110 |
+
output[lab] = val
|
| 111 |
+
continue
|
| 112 |
+
|
| 113 |
+
if is_max:
|
| 114 |
+
if val > output[lab]:
|
| 115 |
+
output[lab] = val
|
| 116 |
+
else:
|
| 117 |
+
if val < output[lab]:
|
| 118 |
+
output[lab] = val
|
| 119 |
+
|
| 120 |
+
# Set labels that don't satisfy min_periods as np.nan
|
| 121 |
+
for lab, count in enumerate(nobs):
|
| 122 |
+
if count < min_periods:
|
| 123 |
+
na_pos.append(lab)
|
| 124 |
+
|
| 125 |
+
return output, na_pos
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/kernels/shared.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import TYPE_CHECKING
|
| 4 |
+
|
| 5 |
+
import numba
|
| 6 |
+
|
| 7 |
+
if TYPE_CHECKING:
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@numba.jit(
|
| 12 |
+
# error: Any? not callable
|
| 13 |
+
numba.boolean(numba.int64[:]), # type: ignore[misc]
|
| 14 |
+
nopython=True,
|
| 15 |
+
nogil=True,
|
| 16 |
+
parallel=False,
|
| 17 |
+
)
|
| 18 |
+
def is_monotonic_increasing(bounds: np.ndarray) -> bool:
|
| 19 |
+
"""Check if int64 values are monotonically increasing."""
|
| 20 |
+
n = len(bounds)
|
| 21 |
+
if n < 2:
|
| 22 |
+
return True
|
| 23 |
+
prev = bounds[0]
|
| 24 |
+
for i in range(1, n):
|
| 25 |
+
cur = bounds[i]
|
| 26 |
+
if cur < prev:
|
| 27 |
+
return False
|
| 28 |
+
prev = cur
|
| 29 |
+
return True
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/_numba/kernels/sum_.py
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Numba 1D sum kernels that can be shared by
|
| 3 |
+
* Dataframe / Series
|
| 4 |
+
* groupby
|
| 5 |
+
* rolling / expanding
|
| 6 |
+
|
| 7 |
+
Mirrors pandas/_libs/window/aggregation.pyx
|
| 8 |
+
"""
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
from typing import (
|
| 12 |
+
TYPE_CHECKING,
|
| 13 |
+
Any,
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
import numba
|
| 17 |
+
from numba.extending import register_jitable
|
| 18 |
+
import numpy as np
|
| 19 |
+
|
| 20 |
+
if TYPE_CHECKING:
|
| 21 |
+
from pandas._typing import npt
|
| 22 |
+
|
| 23 |
+
from pandas.core._numba.kernels.shared import is_monotonic_increasing
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@numba.jit(nopython=True, nogil=True, parallel=False)
|
| 27 |
+
def add_sum(
|
| 28 |
+
val: Any,
|
| 29 |
+
nobs: int,
|
| 30 |
+
sum_x: Any,
|
| 31 |
+
compensation: Any,
|
| 32 |
+
num_consecutive_same_value: int,
|
| 33 |
+
prev_value: Any,
|
| 34 |
+
) -> tuple[int, Any, Any, int, Any]:
|
| 35 |
+
if not np.isnan(val):
|
| 36 |
+
nobs += 1
|
| 37 |
+
y = val - compensation
|
| 38 |
+
t = sum_x + y
|
| 39 |
+
compensation = t - sum_x - y
|
| 40 |
+
sum_x = t
|
| 41 |
+
|
| 42 |
+
if val == prev_value:
|
| 43 |
+
num_consecutive_same_value += 1
|
| 44 |
+
else:
|
| 45 |
+
num_consecutive_same_value = 1
|
| 46 |
+
prev_value = val
|
| 47 |
+
|
| 48 |
+
return nobs, sum_x, compensation, num_consecutive_same_value, prev_value
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@numba.jit(nopython=True, nogil=True, parallel=False)
|
| 52 |
+
def remove_sum(
|
| 53 |
+
val: Any, nobs: int, sum_x: Any, compensation: Any
|
| 54 |
+
) -> tuple[int, Any, Any]:
|
| 55 |
+
if not np.isnan(val):
|
| 56 |
+
nobs -= 1
|
| 57 |
+
y = -val - compensation
|
| 58 |
+
t = sum_x + y
|
| 59 |
+
compensation = t - sum_x - y
|
| 60 |
+
sum_x = t
|
| 61 |
+
return nobs, sum_x, compensation
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
@numba.jit(nopython=True, nogil=True, parallel=False)
|
| 65 |
+
def sliding_sum(
|
| 66 |
+
values: np.ndarray,
|
| 67 |
+
result_dtype: np.dtype,
|
| 68 |
+
start: np.ndarray,
|
| 69 |
+
end: np.ndarray,
|
| 70 |
+
min_periods: int,
|
| 71 |
+
) -> tuple[np.ndarray, list[int]]:
|
| 72 |
+
dtype = values.dtype
|
| 73 |
+
|
| 74 |
+
na_val: object = np.nan
|
| 75 |
+
if dtype.kind == "i":
|
| 76 |
+
na_val = 0
|
| 77 |
+
|
| 78 |
+
N = len(start)
|
| 79 |
+
nobs = 0
|
| 80 |
+
sum_x = 0
|
| 81 |
+
compensation_add = 0
|
| 82 |
+
compensation_remove = 0
|
| 83 |
+
na_pos = []
|
| 84 |
+
|
| 85 |
+
is_monotonic_increasing_bounds = is_monotonic_increasing(
|
| 86 |
+
start
|
| 87 |
+
) and is_monotonic_increasing(end)
|
| 88 |
+
|
| 89 |
+
output = np.empty(N, dtype=result_dtype)
|
| 90 |
+
|
| 91 |
+
for i in range(N):
|
| 92 |
+
s = start[i]
|
| 93 |
+
e = end[i]
|
| 94 |
+
if i == 0 or not is_monotonic_increasing_bounds:
|
| 95 |
+
prev_value = values[s]
|
| 96 |
+
num_consecutive_same_value = 0
|
| 97 |
+
|
| 98 |
+
for j in range(s, e):
|
| 99 |
+
val = values[j]
|
| 100 |
+
(
|
| 101 |
+
nobs,
|
| 102 |
+
sum_x,
|
| 103 |
+
compensation_add,
|
| 104 |
+
num_consecutive_same_value,
|
| 105 |
+
prev_value,
|
| 106 |
+
) = add_sum(
|
| 107 |
+
val,
|
| 108 |
+
nobs,
|
| 109 |
+
sum_x,
|
| 110 |
+
compensation_add,
|
| 111 |
+
num_consecutive_same_value,
|
| 112 |
+
prev_value,
|
| 113 |
+
)
|
| 114 |
+
else:
|
| 115 |
+
for j in range(start[i - 1], s):
|
| 116 |
+
val = values[j]
|
| 117 |
+
nobs, sum_x, compensation_remove = remove_sum(
|
| 118 |
+
val, nobs, sum_x, compensation_remove
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
for j in range(end[i - 1], e):
|
| 122 |
+
val = values[j]
|
| 123 |
+
(
|
| 124 |
+
nobs,
|
| 125 |
+
sum_x,
|
| 126 |
+
compensation_add,
|
| 127 |
+
num_consecutive_same_value,
|
| 128 |
+
prev_value,
|
| 129 |
+
) = add_sum(
|
| 130 |
+
val,
|
| 131 |
+
nobs,
|
| 132 |
+
sum_x,
|
| 133 |
+
compensation_add,
|
| 134 |
+
num_consecutive_same_value,
|
| 135 |
+
prev_value,
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
if nobs == 0 == min_periods:
|
| 139 |
+
result: object = 0
|
| 140 |
+
elif nobs >= min_periods:
|
| 141 |
+
if num_consecutive_same_value >= nobs:
|
| 142 |
+
result = prev_value * nobs
|
| 143 |
+
else:
|
| 144 |
+
result = sum_x
|
| 145 |
+
else:
|
| 146 |
+
result = na_val
|
| 147 |
+
if dtype.kind == "i":
|
| 148 |
+
na_pos.append(i)
|
| 149 |
+
|
| 150 |
+
output[i] = result
|
| 151 |
+
|
| 152 |
+
if not is_monotonic_increasing_bounds:
|
| 153 |
+
nobs = 0
|
| 154 |
+
sum_x = 0
|
| 155 |
+
compensation_remove = 0
|
| 156 |
+
|
| 157 |
+
return output, na_pos
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
# Mypy/pyright don't like the fact that the decorator is untyped
|
| 161 |
+
@register_jitable # type: ignore[misc]
|
| 162 |
+
def grouped_kahan_sum(
|
| 163 |
+
values: np.ndarray,
|
| 164 |
+
result_dtype: np.dtype,
|
| 165 |
+
labels: npt.NDArray[np.intp],
|
| 166 |
+
ngroups: int,
|
| 167 |
+
) -> tuple[
|
| 168 |
+
np.ndarray, npt.NDArray[np.int64], np.ndarray, npt.NDArray[np.int64], np.ndarray
|
| 169 |
+
]:
|
| 170 |
+
N = len(labels)
|
| 171 |
+
|
| 172 |
+
nobs_arr = np.zeros(ngroups, dtype=np.int64)
|
| 173 |
+
comp_arr = np.zeros(ngroups, dtype=values.dtype)
|
| 174 |
+
consecutive_counts = np.zeros(ngroups, dtype=np.int64)
|
| 175 |
+
prev_vals = np.zeros(ngroups, dtype=values.dtype)
|
| 176 |
+
output = np.zeros(ngroups, dtype=result_dtype)
|
| 177 |
+
|
| 178 |
+
for i in range(N):
|
| 179 |
+
lab = labels[i]
|
| 180 |
+
val = values[i]
|
| 181 |
+
|
| 182 |
+
if lab < 0:
|
| 183 |
+
continue
|
| 184 |
+
|
| 185 |
+
sum_x = output[lab]
|
| 186 |
+
nobs = nobs_arr[lab]
|
| 187 |
+
compensation_add = comp_arr[lab]
|
| 188 |
+
num_consecutive_same_value = consecutive_counts[lab]
|
| 189 |
+
prev_value = prev_vals[lab]
|
| 190 |
+
|
| 191 |
+
(
|
| 192 |
+
nobs,
|
| 193 |
+
sum_x,
|
| 194 |
+
compensation_add,
|
| 195 |
+
num_consecutive_same_value,
|
| 196 |
+
prev_value,
|
| 197 |
+
) = add_sum(
|
| 198 |
+
val,
|
| 199 |
+
nobs,
|
| 200 |
+
sum_x,
|
| 201 |
+
compensation_add,
|
| 202 |
+
num_consecutive_same_value,
|
| 203 |
+
prev_value,
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
output[lab] = sum_x
|
| 207 |
+
consecutive_counts[lab] = num_consecutive_same_value
|
| 208 |
+
prev_vals[lab] = prev_value
|
| 209 |
+
comp_arr[lab] = compensation_add
|
| 210 |
+
nobs_arr[lab] = nobs
|
| 211 |
+
return output, nobs_arr, comp_arr, consecutive_counts, prev_vals
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
@numba.jit(nopython=True, nogil=True, parallel=False)
|
| 215 |
+
def grouped_sum(
|
| 216 |
+
values: np.ndarray,
|
| 217 |
+
result_dtype: np.dtype,
|
| 218 |
+
labels: npt.NDArray[np.intp],
|
| 219 |
+
ngroups: int,
|
| 220 |
+
min_periods: int,
|
| 221 |
+
) -> tuple[np.ndarray, list[int]]:
|
| 222 |
+
na_pos = []
|
| 223 |
+
|
| 224 |
+
output, nobs_arr, comp_arr, consecutive_counts, prev_vals = grouped_kahan_sum(
|
| 225 |
+
values, result_dtype, labels, ngroups
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
# Post-processing, replace sums that don't satisfy min_periods
|
| 229 |
+
for lab in range(ngroups):
|
| 230 |
+
nobs = nobs_arr[lab]
|
| 231 |
+
num_consecutive_same_value = consecutive_counts[lab]
|
| 232 |
+
prev_value = prev_vals[lab]
|
| 233 |
+
sum_x = output[lab]
|
| 234 |
+
if nobs >= min_periods:
|
| 235 |
+
if num_consecutive_same_value >= nobs:
|
| 236 |
+
result = prev_value * nobs
|
| 237 |
+
else:
|
| 238 |
+
result = sum_x
|
| 239 |
+
else:
|
| 240 |
+
result = sum_x # Don't change val, will be replaced by nan later
|
| 241 |
+
na_pos.append(lab)
|
| 242 |
+
output[lab] = result
|
| 243 |
+
|
| 244 |
+
return output, na_pos
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Numba 1D var kernels that can be shared by
|
| 3 |
+
* Dataframe / Series
|
| 4 |
+
* groupby
|
| 5 |
+
* rolling / expanding
|
| 6 |
+
|
| 7 |
+
Mirrors pandas/_libs/window/aggregation.pyx
|
| 8 |
+
"""
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
from typing import TYPE_CHECKING
|
| 12 |
+
|
| 13 |
+
import numba
|
| 14 |
+
import numpy as np
|
| 15 |
+
|
| 16 |
+
if TYPE_CHECKING:
|
| 17 |
+
from pandas._typing import npt
|
| 18 |
+
|
| 19 |
+
from pandas.core._numba.kernels.shared import is_monotonic_increasing
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@numba.jit(nopython=True, nogil=True, parallel=False)
|
| 23 |
+
def add_var(
|
| 24 |
+
val: float,
|
| 25 |
+
nobs: int,
|
| 26 |
+
mean_x: float,
|
| 27 |
+
ssqdm_x: float,
|
| 28 |
+
compensation: float,
|
| 29 |
+
num_consecutive_same_value: int,
|
| 30 |
+
prev_value: float,
|
| 31 |
+
) -> tuple[int, float, float, float, int, float]:
|
| 32 |
+
if not np.isnan(val):
|
| 33 |
+
if val == prev_value:
|
| 34 |
+
num_consecutive_same_value += 1
|
| 35 |
+
else:
|
| 36 |
+
num_consecutive_same_value = 1
|
| 37 |
+
prev_value = val
|
| 38 |
+
|
| 39 |
+
nobs += 1
|
| 40 |
+
prev_mean = mean_x - compensation
|
| 41 |
+
y = val - compensation
|
| 42 |
+
t = y - mean_x
|
| 43 |
+
compensation = t + mean_x - y
|
| 44 |
+
delta = t
|
| 45 |
+
if nobs:
|
| 46 |
+
mean_x += delta / nobs
|
| 47 |
+
else:
|
| 48 |
+
mean_x = 0
|
| 49 |
+
ssqdm_x += (val - prev_mean) * (val - mean_x)
|
| 50 |
+
return nobs, mean_x, ssqdm_x, compensation, num_consecutive_same_value, prev_value
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
@numba.jit(nopython=True, nogil=True, parallel=False)
|
| 54 |
+
def remove_var(
|
| 55 |
+
val: float, nobs: int, mean_x: float, ssqdm_x: float, compensation: float
|
| 56 |
+
) -> tuple[int, float, float, float]:
|
| 57 |
+
if not np.isnan(val):
|
| 58 |
+
nobs -= 1
|
| 59 |
+
if nobs:
|
| 60 |
+
prev_mean = mean_x - compensation
|
| 61 |
+
y = val - compensation
|
| 62 |
+
t = y - mean_x
|
| 63 |
+
compensation = t + mean_x - y
|
| 64 |
+
delta = t
|
| 65 |
+
mean_x -= delta / nobs
|
| 66 |
+
ssqdm_x -= (val - prev_mean) * (val - mean_x)
|
| 67 |
+
else:
|
| 68 |
+
mean_x = 0
|
| 69 |
+
ssqdm_x = 0
|
| 70 |
+
return nobs, mean_x, ssqdm_x, compensation
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
@numba.jit(nopython=True, nogil=True, parallel=False)
|
| 74 |
+
def sliding_var(
|
| 75 |
+
values: np.ndarray,
|
| 76 |
+
result_dtype: np.dtype,
|
| 77 |
+
start: np.ndarray,
|
| 78 |
+
end: np.ndarray,
|
| 79 |
+
min_periods: int,
|
| 80 |
+
ddof: int = 1,
|
| 81 |
+
) -> tuple[np.ndarray, list[int]]:
|
| 82 |
+
N = len(start)
|
| 83 |
+
nobs = 0
|
| 84 |
+
mean_x = 0.0
|
| 85 |
+
ssqdm_x = 0.0
|
| 86 |
+
compensation_add = 0.0
|
| 87 |
+
compensation_remove = 0.0
|
| 88 |
+
|
| 89 |
+
min_periods = max(min_periods, 1)
|
| 90 |
+
is_monotonic_increasing_bounds = is_monotonic_increasing(
|
| 91 |
+
start
|
| 92 |
+
) and is_monotonic_increasing(end)
|
| 93 |
+
|
| 94 |
+
output = np.empty(N, dtype=result_dtype)
|
| 95 |
+
|
| 96 |
+
for i in range(N):
|
| 97 |
+
s = start[i]
|
| 98 |
+
e = end[i]
|
| 99 |
+
if i == 0 or not is_monotonic_increasing_bounds:
|
| 100 |
+
prev_value = values[s]
|
| 101 |
+
num_consecutive_same_value = 0
|
| 102 |
+
|
| 103 |
+
for j in range(s, e):
|
| 104 |
+
val = values[j]
|
| 105 |
+
(
|
| 106 |
+
nobs,
|
| 107 |
+
mean_x,
|
| 108 |
+
ssqdm_x,
|
| 109 |
+
compensation_add,
|
| 110 |
+
num_consecutive_same_value,
|
| 111 |
+
prev_value,
|
| 112 |
+
) = add_var(
|
| 113 |
+
val,
|
| 114 |
+
nobs,
|
| 115 |
+
mean_x,
|
| 116 |
+
ssqdm_x,
|
| 117 |
+
compensation_add,
|
| 118 |
+
num_consecutive_same_value,
|
| 119 |
+
prev_value,
|
| 120 |
+
)
|
| 121 |
+
else:
|
| 122 |
+
for j in range(start[i - 1], s):
|
| 123 |
+
val = values[j]
|
| 124 |
+
nobs, mean_x, ssqdm_x, compensation_remove = remove_var(
|
| 125 |
+
val, nobs, mean_x, ssqdm_x, compensation_remove
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
for j in range(end[i - 1], e):
|
| 129 |
+
val = values[j]
|
| 130 |
+
(
|
| 131 |
+
nobs,
|
| 132 |
+
mean_x,
|
| 133 |
+
ssqdm_x,
|
| 134 |
+
compensation_add,
|
| 135 |
+
num_consecutive_same_value,
|
| 136 |
+
prev_value,
|
| 137 |
+
) = add_var(
|
| 138 |
+
val,
|
| 139 |
+
nobs,
|
| 140 |
+
mean_x,
|
| 141 |
+
ssqdm_x,
|
| 142 |
+
compensation_add,
|
| 143 |
+
num_consecutive_same_value,
|
| 144 |
+
prev_value,
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
if nobs >= min_periods and nobs > ddof:
|
| 148 |
+
if nobs == 1 or num_consecutive_same_value >= nobs:
|
| 149 |
+
result = 0.0
|
| 150 |
+
else:
|
| 151 |
+
result = ssqdm_x / (nobs - ddof)
|
| 152 |
+
else:
|
| 153 |
+
result = np.nan
|
| 154 |
+
|
| 155 |
+
output[i] = result
|
| 156 |
+
|
| 157 |
+
if not is_monotonic_increasing_bounds:
|
| 158 |
+
nobs = 0
|
| 159 |
+
mean_x = 0.0
|
| 160 |
+
ssqdm_x = 0.0
|
| 161 |
+
compensation_remove = 0.0
|
| 162 |
+
|
| 163 |
+
# na_position is empty list since float64 can already hold nans
|
| 164 |
+
# Do list comprehension, since numba cannot figure out that na_pos is
|
| 165 |
+
# empty list of ints on its own
|
| 166 |
+
na_pos = [0 for i in range(0)]
|
| 167 |
+
return output, na_pos
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
@numba.jit(nopython=True, nogil=True, parallel=False)
|
| 171 |
+
def grouped_var(
|
| 172 |
+
values: np.ndarray,
|
| 173 |
+
result_dtype: np.dtype,
|
| 174 |
+
labels: npt.NDArray[np.intp],
|
| 175 |
+
ngroups: int,
|
| 176 |
+
min_periods: int,
|
| 177 |
+
ddof: int = 1,
|
| 178 |
+
) -> tuple[np.ndarray, list[int]]:
|
| 179 |
+
N = len(labels)
|
| 180 |
+
|
| 181 |
+
nobs_arr = np.zeros(ngroups, dtype=np.int64)
|
| 182 |
+
comp_arr = np.zeros(ngroups, dtype=values.dtype)
|
| 183 |
+
consecutive_counts = np.zeros(ngroups, dtype=np.int64)
|
| 184 |
+
prev_vals = np.zeros(ngroups, dtype=values.dtype)
|
| 185 |
+
output = np.zeros(ngroups, dtype=result_dtype)
|
| 186 |
+
means = np.zeros(ngroups, dtype=result_dtype)
|
| 187 |
+
|
| 188 |
+
for i in range(N):
|
| 189 |
+
lab = labels[i]
|
| 190 |
+
val = values[i]
|
| 191 |
+
|
| 192 |
+
if lab < 0:
|
| 193 |
+
continue
|
| 194 |
+
|
| 195 |
+
mean_x = means[lab]
|
| 196 |
+
ssqdm_x = output[lab]
|
| 197 |
+
nobs = nobs_arr[lab]
|
| 198 |
+
compensation_add = comp_arr[lab]
|
| 199 |
+
num_consecutive_same_value = consecutive_counts[lab]
|
| 200 |
+
prev_value = prev_vals[lab]
|
| 201 |
+
|
| 202 |
+
(
|
| 203 |
+
nobs,
|
| 204 |
+
mean_x,
|
| 205 |
+
ssqdm_x,
|
| 206 |
+
compensation_add,
|
| 207 |
+
num_consecutive_same_value,
|
| 208 |
+
prev_value,
|
| 209 |
+
) = add_var(
|
| 210 |
+
val,
|
| 211 |
+
nobs,
|
| 212 |
+
mean_x,
|
| 213 |
+
ssqdm_x,
|
| 214 |
+
compensation_add,
|
| 215 |
+
num_consecutive_same_value,
|
| 216 |
+
prev_value,
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
output[lab] = ssqdm_x
|
| 220 |
+
means[lab] = mean_x
|
| 221 |
+
consecutive_counts[lab] = num_consecutive_same_value
|
| 222 |
+
prev_vals[lab] = prev_value
|
| 223 |
+
comp_arr[lab] = compensation_add
|
| 224 |
+
nobs_arr[lab] = nobs
|
| 225 |
+
|
| 226 |
+
# Post-processing, replace vars that don't satisfy min_periods
|
| 227 |
+
for lab in range(ngroups):
|
| 228 |
+
nobs = nobs_arr[lab]
|
| 229 |
+
num_consecutive_same_value = consecutive_counts[lab]
|
| 230 |
+
ssqdm_x = output[lab]
|
| 231 |
+
if nobs >= min_periods and nobs > ddof:
|
| 232 |
+
if nobs == 1 or num_consecutive_same_value >= nobs:
|
| 233 |
+
result = 0.0
|
| 234 |
+
else:
|
| 235 |
+
result = ssqdm_x / (nobs - ddof)
|
| 236 |
+
else:
|
| 237 |
+
result = np.nan
|
| 238 |
+
output[lab] = result
|
| 239 |
+
|
| 240 |
+
# Second pass to get the std.dev
|
| 241 |
+
# na_position is empty list since float64 can already hold nans
|
| 242 |
+
# Do list comprehension, since numba cannot figure out that na_pos is
|
| 243 |
+
# empty list of ints on its own
|
| 244 |
+
na_pos = [0 for i in range(0)]
|
| 245 |
+
return output, na_pos
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__init__.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
core.array_algos is for algorithms that operate on ndarray and ExtensionArray.
|
| 3 |
+
These should:
|
| 4 |
+
|
| 5 |
+
- Assume that any Index, Series, or DataFrame objects have already been unwrapped.
|
| 6 |
+
- Assume that any list arguments have already been cast to ndarray/EA.
|
| 7 |
+
- Not depend on Index, Series, or DataFrame, nor import any of these.
|
| 8 |
+
- May dispatch to ExtensionArray methods, but should not import from core.arrays.
|
| 9 |
+
"""
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (650 Bytes). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/datetimelike_accumulations.cpython-310.pyc
ADDED
|
Binary file (2.03 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/masked_accumulations.cpython-310.pyc
ADDED
|
Binary file (2.41 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/masked_reductions.cpython-310.pyc
ADDED
|
Binary file (4.09 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/putmask.cpython-310.pyc
ADDED
|
Binary file (3.48 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/quantile.cpython-310.pyc
ADDED
|
Binary file (4.67 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/replace.cpython-310.pyc
ADDED
|
Binary file (3.92 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/take.cpython-310.pyc
ADDED
|
Binary file (13.4 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/__pycache__/transforms.cpython-310.pyc
ADDED
|
Binary file (1.05 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/datetimelike_accumulations.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
datetimelke_accumulations.py is for accumulations of datetimelike extension arrays
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from __future__ import annotations
|
| 6 |
+
|
| 7 |
+
from typing import Callable
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
from pandas._libs import iNaT
|
| 12 |
+
|
| 13 |
+
from pandas.core.dtypes.missing import isna
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def _cum_func(
|
| 17 |
+
func: Callable,
|
| 18 |
+
values: np.ndarray,
|
| 19 |
+
*,
|
| 20 |
+
skipna: bool = True,
|
| 21 |
+
):
|
| 22 |
+
"""
|
| 23 |
+
Accumulations for 1D datetimelike arrays.
|
| 24 |
+
|
| 25 |
+
Parameters
|
| 26 |
+
----------
|
| 27 |
+
func : np.cumsum, np.maximum.accumulate, np.minimum.accumulate
|
| 28 |
+
values : np.ndarray
|
| 29 |
+
Numpy array with the values (can be of any dtype that support the
|
| 30 |
+
operation). Values is changed is modified inplace.
|
| 31 |
+
skipna : bool, default True
|
| 32 |
+
Whether to skip NA.
|
| 33 |
+
"""
|
| 34 |
+
try:
|
| 35 |
+
fill_value = {
|
| 36 |
+
np.maximum.accumulate: np.iinfo(np.int64).min,
|
| 37 |
+
np.cumsum: 0,
|
| 38 |
+
np.minimum.accumulate: np.iinfo(np.int64).max,
|
| 39 |
+
}[func]
|
| 40 |
+
except KeyError:
|
| 41 |
+
raise ValueError(f"No accumulation for {func} implemented on BaseMaskedArray")
|
| 42 |
+
|
| 43 |
+
mask = isna(values)
|
| 44 |
+
y = values.view("i8")
|
| 45 |
+
y[mask] = fill_value
|
| 46 |
+
|
| 47 |
+
if not skipna:
|
| 48 |
+
mask = np.maximum.accumulate(mask)
|
| 49 |
+
|
| 50 |
+
result = func(y)
|
| 51 |
+
result[mask] = iNaT
|
| 52 |
+
|
| 53 |
+
if values.dtype.kind in "mM":
|
| 54 |
+
return result.view(values.dtype.base)
|
| 55 |
+
return result
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def cumsum(values: np.ndarray, *, skipna: bool = True) -> np.ndarray:
|
| 59 |
+
return _cum_func(np.cumsum, values, skipna=skipna)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def cummin(values: np.ndarray, *, skipna: bool = True):
|
| 63 |
+
return _cum_func(np.minimum.accumulate, values, skipna=skipna)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def cummax(values: np.ndarray, *, skipna: bool = True):
|
| 67 |
+
return _cum_func(np.maximum.accumulate, values, skipna=skipna)
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/masked_accumulations.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
masked_accumulations.py is for accumulation algorithms using a mask-based approach
|
| 3 |
+
for missing values.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from __future__ import annotations
|
| 7 |
+
|
| 8 |
+
from typing import (
|
| 9 |
+
TYPE_CHECKING,
|
| 10 |
+
Callable,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
if TYPE_CHECKING:
|
| 16 |
+
from pandas._typing import npt
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _cum_func(
|
| 20 |
+
func: Callable,
|
| 21 |
+
values: np.ndarray,
|
| 22 |
+
mask: npt.NDArray[np.bool_],
|
| 23 |
+
*,
|
| 24 |
+
skipna: bool = True,
|
| 25 |
+
):
|
| 26 |
+
"""
|
| 27 |
+
Accumulations for 1D masked array.
|
| 28 |
+
|
| 29 |
+
We will modify values in place to replace NAs with the appropriate fill value.
|
| 30 |
+
|
| 31 |
+
Parameters
|
| 32 |
+
----------
|
| 33 |
+
func : np.cumsum, np.cumprod, np.maximum.accumulate, np.minimum.accumulate
|
| 34 |
+
values : np.ndarray
|
| 35 |
+
Numpy array with the values (can be of any dtype that support the
|
| 36 |
+
operation).
|
| 37 |
+
mask : np.ndarray
|
| 38 |
+
Boolean numpy array (True values indicate missing values).
|
| 39 |
+
skipna : bool, default True
|
| 40 |
+
Whether to skip NA.
|
| 41 |
+
"""
|
| 42 |
+
dtype_info: np.iinfo | np.finfo
|
| 43 |
+
if values.dtype.kind == "f":
|
| 44 |
+
dtype_info = np.finfo(values.dtype.type)
|
| 45 |
+
elif values.dtype.kind in "iu":
|
| 46 |
+
dtype_info = np.iinfo(values.dtype.type)
|
| 47 |
+
elif values.dtype.kind == "b":
|
| 48 |
+
# Max value of bool is 1, but since we are setting into a boolean
|
| 49 |
+
# array, 255 is fine as well. Min value has to be 0 when setting
|
| 50 |
+
# into the boolean array.
|
| 51 |
+
dtype_info = np.iinfo(np.uint8)
|
| 52 |
+
else:
|
| 53 |
+
raise NotImplementedError(
|
| 54 |
+
f"No masked accumulation defined for dtype {values.dtype.type}"
|
| 55 |
+
)
|
| 56 |
+
try:
|
| 57 |
+
fill_value = {
|
| 58 |
+
np.cumprod: 1,
|
| 59 |
+
np.maximum.accumulate: dtype_info.min,
|
| 60 |
+
np.cumsum: 0,
|
| 61 |
+
np.minimum.accumulate: dtype_info.max,
|
| 62 |
+
}[func]
|
| 63 |
+
except KeyError:
|
| 64 |
+
raise NotImplementedError(
|
| 65 |
+
f"No accumulation for {func} implemented on BaseMaskedArray"
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
values[mask] = fill_value
|
| 69 |
+
|
| 70 |
+
if not skipna:
|
| 71 |
+
mask = np.maximum.accumulate(mask)
|
| 72 |
+
|
| 73 |
+
values = func(values)
|
| 74 |
+
return values, mask
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def cumsum(values: np.ndarray, mask: npt.NDArray[np.bool_], *, skipna: bool = True):
|
| 78 |
+
return _cum_func(np.cumsum, values, mask, skipna=skipna)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def cumprod(values: np.ndarray, mask: npt.NDArray[np.bool_], *, skipna: bool = True):
|
| 82 |
+
return _cum_func(np.cumprod, values, mask, skipna=skipna)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def cummin(values: np.ndarray, mask: npt.NDArray[np.bool_], *, skipna: bool = True):
|
| 86 |
+
return _cum_func(np.minimum.accumulate, values, mask, skipna=skipna)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def cummax(values: np.ndarray, mask: npt.NDArray[np.bool_], *, skipna: bool = True):
|
| 90 |
+
return _cum_func(np.maximum.accumulate, values, mask, skipna=skipna)
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/masked_reductions.py
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
masked_reductions.py is for reduction algorithms using a mask-based approach
|
| 3 |
+
for missing values.
|
| 4 |
+
"""
|
| 5 |
+
from __future__ import annotations
|
| 6 |
+
|
| 7 |
+
from typing import (
|
| 8 |
+
TYPE_CHECKING,
|
| 9 |
+
Callable,
|
| 10 |
+
)
|
| 11 |
+
import warnings
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
from pandas._libs import missing as libmissing
|
| 16 |
+
|
| 17 |
+
from pandas.core.nanops import check_below_min_count
|
| 18 |
+
|
| 19 |
+
if TYPE_CHECKING:
|
| 20 |
+
from pandas._typing import (
|
| 21 |
+
AxisInt,
|
| 22 |
+
npt,
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def _reductions(
|
| 27 |
+
func: Callable,
|
| 28 |
+
values: np.ndarray,
|
| 29 |
+
mask: npt.NDArray[np.bool_],
|
| 30 |
+
*,
|
| 31 |
+
skipna: bool = True,
|
| 32 |
+
min_count: int = 0,
|
| 33 |
+
axis: AxisInt | None = None,
|
| 34 |
+
**kwargs,
|
| 35 |
+
):
|
| 36 |
+
"""
|
| 37 |
+
Sum, mean or product for 1D masked array.
|
| 38 |
+
|
| 39 |
+
Parameters
|
| 40 |
+
----------
|
| 41 |
+
func : np.sum or np.prod
|
| 42 |
+
values : np.ndarray
|
| 43 |
+
Numpy array with the values (can be of any dtype that support the
|
| 44 |
+
operation).
|
| 45 |
+
mask : np.ndarray[bool]
|
| 46 |
+
Boolean numpy array (True values indicate missing values).
|
| 47 |
+
skipna : bool, default True
|
| 48 |
+
Whether to skip NA.
|
| 49 |
+
min_count : int, default 0
|
| 50 |
+
The required number of valid values to perform the operation. If fewer than
|
| 51 |
+
``min_count`` non-NA values are present the result will be NA.
|
| 52 |
+
axis : int, optional, default None
|
| 53 |
+
"""
|
| 54 |
+
if not skipna:
|
| 55 |
+
if mask.any() or check_below_min_count(values.shape, None, min_count):
|
| 56 |
+
return libmissing.NA
|
| 57 |
+
else:
|
| 58 |
+
return func(values, axis=axis, **kwargs)
|
| 59 |
+
else:
|
| 60 |
+
if check_below_min_count(values.shape, mask, min_count) and (
|
| 61 |
+
axis is None or values.ndim == 1
|
| 62 |
+
):
|
| 63 |
+
return libmissing.NA
|
| 64 |
+
|
| 65 |
+
if values.dtype == np.dtype(object):
|
| 66 |
+
# object dtype does not support `where` without passing an initial
|
| 67 |
+
values = values[~mask]
|
| 68 |
+
return func(values, axis=axis, **kwargs)
|
| 69 |
+
return func(values, where=~mask, axis=axis, **kwargs)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def sum(
|
| 73 |
+
values: np.ndarray,
|
| 74 |
+
mask: npt.NDArray[np.bool_],
|
| 75 |
+
*,
|
| 76 |
+
skipna: bool = True,
|
| 77 |
+
min_count: int = 0,
|
| 78 |
+
axis: AxisInt | None = None,
|
| 79 |
+
):
|
| 80 |
+
return _reductions(
|
| 81 |
+
np.sum, values=values, mask=mask, skipna=skipna, min_count=min_count, axis=axis
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def prod(
|
| 86 |
+
values: np.ndarray,
|
| 87 |
+
mask: npt.NDArray[np.bool_],
|
| 88 |
+
*,
|
| 89 |
+
skipna: bool = True,
|
| 90 |
+
min_count: int = 0,
|
| 91 |
+
axis: AxisInt | None = None,
|
| 92 |
+
):
|
| 93 |
+
return _reductions(
|
| 94 |
+
np.prod, values=values, mask=mask, skipna=skipna, min_count=min_count, axis=axis
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def _minmax(
|
| 99 |
+
func: Callable,
|
| 100 |
+
values: np.ndarray,
|
| 101 |
+
mask: npt.NDArray[np.bool_],
|
| 102 |
+
*,
|
| 103 |
+
skipna: bool = True,
|
| 104 |
+
axis: AxisInt | None = None,
|
| 105 |
+
):
|
| 106 |
+
"""
|
| 107 |
+
Reduction for 1D masked array.
|
| 108 |
+
|
| 109 |
+
Parameters
|
| 110 |
+
----------
|
| 111 |
+
func : np.min or np.max
|
| 112 |
+
values : np.ndarray
|
| 113 |
+
Numpy array with the values (can be of any dtype that support the
|
| 114 |
+
operation).
|
| 115 |
+
mask : np.ndarray[bool]
|
| 116 |
+
Boolean numpy array (True values indicate missing values).
|
| 117 |
+
skipna : bool, default True
|
| 118 |
+
Whether to skip NA.
|
| 119 |
+
axis : int, optional, default None
|
| 120 |
+
"""
|
| 121 |
+
if not skipna:
|
| 122 |
+
if mask.any() or not values.size:
|
| 123 |
+
# min/max with empty array raise in numpy, pandas returns NA
|
| 124 |
+
return libmissing.NA
|
| 125 |
+
else:
|
| 126 |
+
return func(values, axis=axis)
|
| 127 |
+
else:
|
| 128 |
+
subset = values[~mask]
|
| 129 |
+
if subset.size:
|
| 130 |
+
return func(subset, axis=axis)
|
| 131 |
+
else:
|
| 132 |
+
# min/max with empty array raise in numpy, pandas returns NA
|
| 133 |
+
return libmissing.NA
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
def min(
|
| 137 |
+
values: np.ndarray,
|
| 138 |
+
mask: npt.NDArray[np.bool_],
|
| 139 |
+
*,
|
| 140 |
+
skipna: bool = True,
|
| 141 |
+
axis: AxisInt | None = None,
|
| 142 |
+
):
|
| 143 |
+
return _minmax(np.min, values=values, mask=mask, skipna=skipna, axis=axis)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def max(
|
| 147 |
+
values: np.ndarray,
|
| 148 |
+
mask: npt.NDArray[np.bool_],
|
| 149 |
+
*,
|
| 150 |
+
skipna: bool = True,
|
| 151 |
+
axis: AxisInt | None = None,
|
| 152 |
+
):
|
| 153 |
+
return _minmax(np.max, values=values, mask=mask, skipna=skipna, axis=axis)
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def mean(
|
| 157 |
+
values: np.ndarray,
|
| 158 |
+
mask: npt.NDArray[np.bool_],
|
| 159 |
+
*,
|
| 160 |
+
skipna: bool = True,
|
| 161 |
+
axis: AxisInt | None = None,
|
| 162 |
+
):
|
| 163 |
+
if not values.size or mask.all():
|
| 164 |
+
return libmissing.NA
|
| 165 |
+
return _reductions(np.mean, values=values, mask=mask, skipna=skipna, axis=axis)
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def var(
|
| 169 |
+
values: np.ndarray,
|
| 170 |
+
mask: npt.NDArray[np.bool_],
|
| 171 |
+
*,
|
| 172 |
+
skipna: bool = True,
|
| 173 |
+
axis: AxisInt | None = None,
|
| 174 |
+
ddof: int = 1,
|
| 175 |
+
):
|
| 176 |
+
if not values.size or mask.all():
|
| 177 |
+
return libmissing.NA
|
| 178 |
+
|
| 179 |
+
with warnings.catch_warnings():
|
| 180 |
+
warnings.simplefilter("ignore", RuntimeWarning)
|
| 181 |
+
return _reductions(
|
| 182 |
+
np.var, values=values, mask=mask, skipna=skipna, axis=axis, ddof=ddof
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def std(
|
| 187 |
+
values: np.ndarray,
|
| 188 |
+
mask: npt.NDArray[np.bool_],
|
| 189 |
+
*,
|
| 190 |
+
skipna: bool = True,
|
| 191 |
+
axis: AxisInt | None = None,
|
| 192 |
+
ddof: int = 1,
|
| 193 |
+
):
|
| 194 |
+
if not values.size or mask.all():
|
| 195 |
+
return libmissing.NA
|
| 196 |
+
|
| 197 |
+
with warnings.catch_warnings():
|
| 198 |
+
warnings.simplefilter("ignore", RuntimeWarning)
|
| 199 |
+
return _reductions(
|
| 200 |
+
np.std, values=values, mask=mask, skipna=skipna, axis=axis, ddof=ddof
|
| 201 |
+
)
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python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 5 |
+
|
| 6 |
+
from typing import (
|
| 7 |
+
TYPE_CHECKING,
|
| 8 |
+
Any,
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
from pandas._libs import lib
|
| 14 |
+
|
| 15 |
+
from pandas.core.dtypes.cast import infer_dtype_from
|
| 16 |
+
from pandas.core.dtypes.common import is_list_like
|
| 17 |
+
|
| 18 |
+
from pandas.core.arrays import ExtensionArray
|
| 19 |
+
|
| 20 |
+
if TYPE_CHECKING:
|
| 21 |
+
from pandas._typing import (
|
| 22 |
+
ArrayLike,
|
| 23 |
+
npt,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
from pandas import MultiIndex
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def putmask_inplace(values: ArrayLike, mask: npt.NDArray[np.bool_], value: Any) -> None:
|
| 30 |
+
"""
|
| 31 |
+
ExtensionArray-compatible implementation of np.putmask. The main
|
| 32 |
+
difference is we do not handle repeating or truncating like numpy.
|
| 33 |
+
|
| 34 |
+
Parameters
|
| 35 |
+
----------
|
| 36 |
+
values: np.ndarray or ExtensionArray
|
| 37 |
+
mask : np.ndarray[bool]
|
| 38 |
+
We assume extract_bool_array has already been called.
|
| 39 |
+
value : Any
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
if (
|
| 43 |
+
not isinstance(values, np.ndarray)
|
| 44 |
+
or (values.dtype == object and not lib.is_scalar(value))
|
| 45 |
+
# GH#43424: np.putmask raises TypeError if we cannot cast between types with
|
| 46 |
+
# rule = "safe", a stricter guarantee we may not have here
|
| 47 |
+
or (
|
| 48 |
+
isinstance(value, np.ndarray) and not np.can_cast(value.dtype, values.dtype)
|
| 49 |
+
)
|
| 50 |
+
):
|
| 51 |
+
# GH#19266 using np.putmask gives unexpected results with listlike value
|
| 52 |
+
# along with object dtype
|
| 53 |
+
if is_list_like(value) and len(value) == len(values):
|
| 54 |
+
values[mask] = value[mask]
|
| 55 |
+
else:
|
| 56 |
+
values[mask] = value
|
| 57 |
+
else:
|
| 58 |
+
# GH#37833 np.putmask is more performant than __setitem__
|
| 59 |
+
np.putmask(values, mask, value)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def putmask_without_repeat(
|
| 63 |
+
values: np.ndarray, mask: npt.NDArray[np.bool_], new: Any
|
| 64 |
+
) -> None:
|
| 65 |
+
"""
|
| 66 |
+
np.putmask will truncate or repeat if `new` is a listlike with
|
| 67 |
+
len(new) != len(values). We require an exact match.
|
| 68 |
+
|
| 69 |
+
Parameters
|
| 70 |
+
----------
|
| 71 |
+
values : np.ndarray
|
| 72 |
+
mask : np.ndarray[bool]
|
| 73 |
+
new : Any
|
| 74 |
+
"""
|
| 75 |
+
if getattr(new, "ndim", 0) >= 1:
|
| 76 |
+
new = new.astype(values.dtype, copy=False)
|
| 77 |
+
|
| 78 |
+
# TODO: this prob needs some better checking for 2D cases
|
| 79 |
+
nlocs = mask.sum()
|
| 80 |
+
if nlocs > 0 and is_list_like(new) and getattr(new, "ndim", 1) == 1:
|
| 81 |
+
shape = np.shape(new)
|
| 82 |
+
# np.shape compat for if setitem_datetimelike_compat
|
| 83 |
+
# changed arraylike to list e.g. test_where_dt64_2d
|
| 84 |
+
if nlocs == shape[-1]:
|
| 85 |
+
# GH#30567
|
| 86 |
+
# If length of ``new`` is less than the length of ``values``,
|
| 87 |
+
# `np.putmask` would first repeat the ``new`` array and then
|
| 88 |
+
# assign the masked values hence produces incorrect result.
|
| 89 |
+
# `np.place` on the other hand uses the ``new`` values at it is
|
| 90 |
+
# to place in the masked locations of ``values``
|
| 91 |
+
np.place(values, mask, new)
|
| 92 |
+
# i.e. values[mask] = new
|
| 93 |
+
elif mask.shape[-1] == shape[-1] or shape[-1] == 1:
|
| 94 |
+
np.putmask(values, mask, new)
|
| 95 |
+
else:
|
| 96 |
+
raise ValueError("cannot assign mismatch length to masked array")
|
| 97 |
+
else:
|
| 98 |
+
np.putmask(values, mask, new)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def validate_putmask(
|
| 102 |
+
values: ArrayLike | MultiIndex, mask: np.ndarray
|
| 103 |
+
) -> tuple[npt.NDArray[np.bool_], bool]:
|
| 104 |
+
"""
|
| 105 |
+
Validate mask and check if this putmask operation is a no-op.
|
| 106 |
+
"""
|
| 107 |
+
mask = extract_bool_array(mask)
|
| 108 |
+
if mask.shape != values.shape:
|
| 109 |
+
raise ValueError("putmask: mask and data must be the same size")
|
| 110 |
+
|
| 111 |
+
noop = not mask.any()
|
| 112 |
+
return mask, noop
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def extract_bool_array(mask: ArrayLike) -> npt.NDArray[np.bool_]:
|
| 116 |
+
"""
|
| 117 |
+
If we have a SparseArray or BooleanArray, convert it to ndarray[bool].
|
| 118 |
+
"""
|
| 119 |
+
if isinstance(mask, ExtensionArray):
|
| 120 |
+
# We could have BooleanArray, Sparse[bool], ...
|
| 121 |
+
# Except for BooleanArray, this is equivalent to just
|
| 122 |
+
# np.asarray(mask, dtype=bool)
|
| 123 |
+
mask = mask.to_numpy(dtype=bool, na_value=False)
|
| 124 |
+
|
| 125 |
+
mask = np.asarray(mask, dtype=bool)
|
| 126 |
+
return mask
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def setitem_datetimelike_compat(values: np.ndarray, num_set: int, other):
|
| 130 |
+
"""
|
| 131 |
+
Parameters
|
| 132 |
+
----------
|
| 133 |
+
values : np.ndarray
|
| 134 |
+
num_set : int
|
| 135 |
+
For putmask, this is mask.sum()
|
| 136 |
+
other : Any
|
| 137 |
+
"""
|
| 138 |
+
if values.dtype == object:
|
| 139 |
+
dtype, _ = infer_dtype_from(other)
|
| 140 |
+
|
| 141 |
+
if lib.is_np_dtype(dtype, "mM"):
|
| 142 |
+
# https://github.com/numpy/numpy/issues/12550
|
| 143 |
+
# timedelta64 will incorrectly cast to int
|
| 144 |
+
if not is_list_like(other):
|
| 145 |
+
other = [other] * num_set
|
| 146 |
+
else:
|
| 147 |
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import TYPE_CHECKING
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
from pandas.core.dtypes.missing import (
|
| 8 |
+
isna,
|
| 9 |
+
na_value_for_dtype,
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
if TYPE_CHECKING:
|
| 13 |
+
from pandas._typing import (
|
| 14 |
+
ArrayLike,
|
| 15 |
+
Scalar,
|
| 16 |
+
npt,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def quantile_compat(
|
| 21 |
+
values: ArrayLike, qs: npt.NDArray[np.float64], interpolation: str
|
| 22 |
+
) -> ArrayLike:
|
| 23 |
+
"""
|
| 24 |
+
Compute the quantiles of the given values for each quantile in `qs`.
|
| 25 |
+
|
| 26 |
+
Parameters
|
| 27 |
+
----------
|
| 28 |
+
values : np.ndarray or ExtensionArray
|
| 29 |
+
qs : np.ndarray[float64]
|
| 30 |
+
interpolation : str
|
| 31 |
+
|
| 32 |
+
Returns
|
| 33 |
+
-------
|
| 34 |
+
np.ndarray or ExtensionArray
|
| 35 |
+
"""
|
| 36 |
+
if isinstance(values, np.ndarray):
|
| 37 |
+
fill_value = na_value_for_dtype(values.dtype, compat=False)
|
| 38 |
+
mask = isna(values)
|
| 39 |
+
return quantile_with_mask(values, mask, fill_value, qs, interpolation)
|
| 40 |
+
else:
|
| 41 |
+
return values._quantile(qs, interpolation)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def quantile_with_mask(
|
| 45 |
+
values: np.ndarray,
|
| 46 |
+
mask: npt.NDArray[np.bool_],
|
| 47 |
+
fill_value,
|
| 48 |
+
qs: npt.NDArray[np.float64],
|
| 49 |
+
interpolation: str,
|
| 50 |
+
) -> np.ndarray:
|
| 51 |
+
"""
|
| 52 |
+
Compute the quantiles of the given values for each quantile in `qs`.
|
| 53 |
+
|
| 54 |
+
Parameters
|
| 55 |
+
----------
|
| 56 |
+
values : np.ndarray
|
| 57 |
+
For ExtensionArray, this is _values_for_factorize()[0]
|
| 58 |
+
mask : np.ndarray[bool]
|
| 59 |
+
mask = isna(values)
|
| 60 |
+
For ExtensionArray, this is computed before calling _value_for_factorize
|
| 61 |
+
fill_value : Scalar
|
| 62 |
+
The value to interpret fill NA entries with
|
| 63 |
+
For ExtensionArray, this is _values_for_factorize()[1]
|
| 64 |
+
qs : np.ndarray[float64]
|
| 65 |
+
interpolation : str
|
| 66 |
+
Type of interpolation
|
| 67 |
+
|
| 68 |
+
Returns
|
| 69 |
+
-------
|
| 70 |
+
np.ndarray
|
| 71 |
+
|
| 72 |
+
Notes
|
| 73 |
+
-----
|
| 74 |
+
Assumes values is already 2D. For ExtensionArray this means np.atleast_2d
|
| 75 |
+
has been called on _values_for_factorize()[0]
|
| 76 |
+
|
| 77 |
+
Quantile is computed along axis=1.
|
| 78 |
+
"""
|
| 79 |
+
assert values.shape == mask.shape
|
| 80 |
+
if values.ndim == 1:
|
| 81 |
+
# unsqueeze, operate, re-squeeze
|
| 82 |
+
values = np.atleast_2d(values)
|
| 83 |
+
mask = np.atleast_2d(mask)
|
| 84 |
+
res_values = quantile_with_mask(values, mask, fill_value, qs, interpolation)
|
| 85 |
+
return res_values[0]
|
| 86 |
+
|
| 87 |
+
assert values.ndim == 2
|
| 88 |
+
|
| 89 |
+
is_empty = values.shape[1] == 0
|
| 90 |
+
|
| 91 |
+
if is_empty:
|
| 92 |
+
# create the array of na_values
|
| 93 |
+
# 2d len(values) * len(qs)
|
| 94 |
+
flat = np.array([fill_value] * len(qs))
|
| 95 |
+
result = np.repeat(flat, len(values)).reshape(len(values), len(qs))
|
| 96 |
+
else:
|
| 97 |
+
result = _nanpercentile(
|
| 98 |
+
values,
|
| 99 |
+
qs * 100.0,
|
| 100 |
+
na_value=fill_value,
|
| 101 |
+
mask=mask,
|
| 102 |
+
interpolation=interpolation,
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
result = np.asarray(result)
|
| 106 |
+
result = result.T
|
| 107 |
+
|
| 108 |
+
return result
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def _nanpercentile_1d(
|
| 112 |
+
values: np.ndarray,
|
| 113 |
+
mask: npt.NDArray[np.bool_],
|
| 114 |
+
qs: npt.NDArray[np.float64],
|
| 115 |
+
na_value: Scalar,
|
| 116 |
+
interpolation: str,
|
| 117 |
+
) -> Scalar | np.ndarray:
|
| 118 |
+
"""
|
| 119 |
+
Wrapper for np.percentile that skips missing values, specialized to
|
| 120 |
+
1-dimensional case.
|
| 121 |
+
|
| 122 |
+
Parameters
|
| 123 |
+
----------
|
| 124 |
+
values : array over which to find quantiles
|
| 125 |
+
mask : ndarray[bool]
|
| 126 |
+
locations in values that should be considered missing
|
| 127 |
+
qs : np.ndarray[float64] of quantile indices to find
|
| 128 |
+
na_value : scalar
|
| 129 |
+
value to return for empty or all-null values
|
| 130 |
+
interpolation : str
|
| 131 |
+
|
| 132 |
+
Returns
|
| 133 |
+
-------
|
| 134 |
+
quantiles : scalar or array
|
| 135 |
+
"""
|
| 136 |
+
# mask is Union[ExtensionArray, ndarray]
|
| 137 |
+
values = values[~mask]
|
| 138 |
+
|
| 139 |
+
if len(values) == 0:
|
| 140 |
+
# Can't pass dtype=values.dtype here bc we might have na_value=np.nan
|
| 141 |
+
# with values.dtype=int64 see test_quantile_empty
|
| 142 |
+
# equiv: 'np.array([na_value] * len(qs))' but much faster
|
| 143 |
+
return np.full(len(qs), na_value)
|
| 144 |
+
|
| 145 |
+
return np.percentile(
|
| 146 |
+
values,
|
| 147 |
+
qs,
|
| 148 |
+
# error: No overload variant of "percentile" matches argument
|
| 149 |
+
# types "ndarray[Any, Any]", "ndarray[Any, dtype[floating[_64Bit]]]"
|
| 150 |
+
# , "Dict[str, str]" [call-overload]
|
| 151 |
+
method=interpolation, # type: ignore[call-overload]
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def _nanpercentile(
|
| 156 |
+
values: np.ndarray,
|
| 157 |
+
qs: npt.NDArray[np.float64],
|
| 158 |
+
*,
|
| 159 |
+
na_value,
|
| 160 |
+
mask: npt.NDArray[np.bool_],
|
| 161 |
+
interpolation: str,
|
| 162 |
+
):
|
| 163 |
+
"""
|
| 164 |
+
Wrapper for np.percentile that skips missing values.
|
| 165 |
+
|
| 166 |
+
Parameters
|
| 167 |
+
----------
|
| 168 |
+
values : np.ndarray[ndim=2] over which to find quantiles
|
| 169 |
+
qs : np.ndarray[float64] of quantile indices to find
|
| 170 |
+
na_value : scalar
|
| 171 |
+
value to return for empty or all-null values
|
| 172 |
+
mask : np.ndarray[bool]
|
| 173 |
+
locations in values that should be considered missing
|
| 174 |
+
interpolation : str
|
| 175 |
+
|
| 176 |
+
Returns
|
| 177 |
+
-------
|
| 178 |
+
quantiles : scalar or array
|
| 179 |
+
"""
|
| 180 |
+
|
| 181 |
+
if values.dtype.kind in "mM":
|
| 182 |
+
# need to cast to integer to avoid rounding errors in numpy
|
| 183 |
+
result = _nanpercentile(
|
| 184 |
+
values.view("i8"),
|
| 185 |
+
qs=qs,
|
| 186 |
+
na_value=na_value.view("i8"),
|
| 187 |
+
mask=mask,
|
| 188 |
+
interpolation=interpolation,
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
# Note: we have to do `astype` and not view because in general we
|
| 192 |
+
# have float result at this point, not i8
|
| 193 |
+
return result.astype(values.dtype)
|
| 194 |
+
|
| 195 |
+
if mask.any():
|
| 196 |
+
# Caller is responsible for ensuring mask shape match
|
| 197 |
+
assert mask.shape == values.shape
|
| 198 |
+
result = [
|
| 199 |
+
_nanpercentile_1d(val, m, qs, na_value, interpolation=interpolation)
|
| 200 |
+
for (val, m) in zip(list(values), list(mask))
|
| 201 |
+
]
|
| 202 |
+
if values.dtype.kind == "f":
|
| 203 |
+
# preserve itemsize
|
| 204 |
+
result = np.asarray(result, dtype=values.dtype).T
|
| 205 |
+
else:
|
| 206 |
+
result = np.asarray(result).T
|
| 207 |
+
if (
|
| 208 |
+
result.dtype != values.dtype
|
| 209 |
+
and not mask.all()
|
| 210 |
+
and (result == result.astype(values.dtype, copy=False)).all()
|
| 211 |
+
):
|
| 212 |
+
# mask.all() will never get cast back to int
|
| 213 |
+
# e.g. values id integer dtype and result is floating dtype,
|
| 214 |
+
# only cast back to integer dtype if result values are all-integer.
|
| 215 |
+
result = result.astype(values.dtype, copy=False)
|
| 216 |
+
return result
|
| 217 |
+
else:
|
| 218 |
+
return np.percentile(
|
| 219 |
+
values,
|
| 220 |
+
qs,
|
| 221 |
+
axis=1,
|
| 222 |
+
# error: No overload variant of "percentile" matches argument types
|
| 223 |
+
# "ndarray[Any, Any]", "ndarray[Any, dtype[floating[_64Bit]]]",
|
| 224 |
+
# "int", "Dict[str, str]" [call-overload]
|
| 225 |
+
method=interpolation, # type: ignore[call-overload]
|
| 226 |
+
)
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/replace.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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| 9 |
+
from typing import (
|
| 10 |
+
TYPE_CHECKING,
|
| 11 |
+
Any,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
import numpy as np
|
| 15 |
+
|
| 16 |
+
from pandas.core.dtypes.common import (
|
| 17 |
+
is_bool,
|
| 18 |
+
is_re,
|
| 19 |
+
is_re_compilable,
|
| 20 |
+
)
|
| 21 |
+
from pandas.core.dtypes.missing import isna
|
| 22 |
+
|
| 23 |
+
if TYPE_CHECKING:
|
| 24 |
+
from pandas._typing import (
|
| 25 |
+
ArrayLike,
|
| 26 |
+
Scalar,
|
| 27 |
+
npt,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def should_use_regex(regex: bool, to_replace: Any) -> bool:
|
| 32 |
+
"""
|
| 33 |
+
Decide whether to treat `to_replace` as a regular expression.
|
| 34 |
+
"""
|
| 35 |
+
if is_re(to_replace):
|
| 36 |
+
regex = True
|
| 37 |
+
|
| 38 |
+
regex = regex and is_re_compilable(to_replace)
|
| 39 |
+
|
| 40 |
+
# Don't use regex if the pattern is empty.
|
| 41 |
+
regex = regex and re.compile(to_replace).pattern != ""
|
| 42 |
+
return regex
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def compare_or_regex_search(
|
| 46 |
+
a: ArrayLike, b: Scalar | Pattern, regex: bool, mask: npt.NDArray[np.bool_]
|
| 47 |
+
) -> ArrayLike:
|
| 48 |
+
"""
|
| 49 |
+
Compare two array-like inputs of the same shape or two scalar values
|
| 50 |
+
|
| 51 |
+
Calls operator.eq or re.search, depending on regex argument. If regex is
|
| 52 |
+
True, perform an element-wise regex matching.
|
| 53 |
+
|
| 54 |
+
Parameters
|
| 55 |
+
----------
|
| 56 |
+
a : array-like
|
| 57 |
+
b : scalar or regex pattern
|
| 58 |
+
regex : bool
|
| 59 |
+
mask : np.ndarray[bool]
|
| 60 |
+
|
| 61 |
+
Returns
|
| 62 |
+
-------
|
| 63 |
+
mask : array-like of bool
|
| 64 |
+
"""
|
| 65 |
+
if isna(b):
|
| 66 |
+
return ~mask
|
| 67 |
+
|
| 68 |
+
def _check_comparison_types(
|
| 69 |
+
result: ArrayLike | bool, a: ArrayLike, b: Scalar | Pattern
|
| 70 |
+
):
|
| 71 |
+
"""
|
| 72 |
+
Raises an error if the two arrays (a,b) cannot be compared.
|
| 73 |
+
Otherwise, returns the comparison result as expected.
|
| 74 |
+
"""
|
| 75 |
+
if is_bool(result) and isinstance(a, np.ndarray):
|
| 76 |
+
type_names = [type(a).__name__, type(b).__name__]
|
| 77 |
+
|
| 78 |
+
type_names[0] = f"ndarray(dtype={a.dtype})"
|
| 79 |
+
|
| 80 |
+
raise TypeError(
|
| 81 |
+
f"Cannot compare types {repr(type_names[0])} and {repr(type_names[1])}"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
if not regex or not should_use_regex(regex, b):
|
| 85 |
+
# TODO: should use missing.mask_missing?
|
| 86 |
+
op = lambda x: operator.eq(x, b)
|
| 87 |
+
else:
|
| 88 |
+
op = np.vectorize(
|
| 89 |
+
lambda x: bool(re.search(b, x))
|
| 90 |
+
if isinstance(x, str) and isinstance(b, (str, Pattern))
|
| 91 |
+
else False
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# GH#32621 use mask to avoid comparing to NAs
|
| 95 |
+
if isinstance(a, np.ndarray):
|
| 96 |
+
a = a[mask]
|
| 97 |
+
|
| 98 |
+
result = op(a)
|
| 99 |
+
|
| 100 |
+
if isinstance(result, np.ndarray) and mask is not None:
|
| 101 |
+
# The shape of the mask can differ to that of the result
|
| 102 |
+
# since we may compare only a subset of a's or b's elements
|
| 103 |
+
tmp = np.zeros(mask.shape, dtype=np.bool_)
|
| 104 |
+
np.place(tmp, mask, result)
|
| 105 |
+
result = tmp
|
| 106 |
+
|
| 107 |
+
_check_comparison_types(result, a, b)
|
| 108 |
+
return result
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def replace_regex(
|
| 112 |
+
values: ArrayLike, rx: re.Pattern, value, mask: npt.NDArray[np.bool_] | None
|
| 113 |
+
) -> None:
|
| 114 |
+
"""
|
| 115 |
+
Parameters
|
| 116 |
+
----------
|
| 117 |
+
values : ArrayLike
|
| 118 |
+
Object dtype.
|
| 119 |
+
rx : re.Pattern
|
| 120 |
+
value : Any
|
| 121 |
+
mask : np.ndarray[bool], optional
|
| 122 |
+
|
| 123 |
+
Notes
|
| 124 |
+
-----
|
| 125 |
+
Alters values in-place.
|
| 126 |
+
"""
|
| 127 |
+
|
| 128 |
+
# deal with replacing values with objects (strings) that match but
|
| 129 |
+
# whose replacement is not a string (numeric, nan, object)
|
| 130 |
+
if isna(value) or not isinstance(value, str):
|
| 131 |
+
|
| 132 |
+
def re_replacer(s):
|
| 133 |
+
if is_re(rx) and isinstance(s, str):
|
| 134 |
+
return value if rx.search(s) is not None else s
|
| 135 |
+
else:
|
| 136 |
+
return s
|
| 137 |
+
|
| 138 |
+
else:
|
| 139 |
+
# value is guaranteed to be a string here, s can be either a string
|
| 140 |
+
# or null if it's null it gets returned
|
| 141 |
+
def re_replacer(s):
|
| 142 |
+
if is_re(rx) and isinstance(s, str):
|
| 143 |
+
return rx.sub(value, s)
|
| 144 |
+
else:
|
| 145 |
+
return s
|
| 146 |
+
|
| 147 |
+
f = np.vectorize(re_replacer, otypes=[np.object_])
|
| 148 |
+
|
| 149 |
+
if mask is None:
|
| 150 |
+
values[:] = f(values)
|
| 151 |
+
else:
|
| 152 |
+
if values.ndim != mask.ndim:
|
| 153 |
+
mask = np.broadcast_to(mask, values.shape)
|
| 154 |
+
values[mask] = f(values[mask])
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/take.py
ADDED
|
@@ -0,0 +1,594 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import functools
|
| 4 |
+
from typing import (
|
| 5 |
+
TYPE_CHECKING,
|
| 6 |
+
cast,
|
| 7 |
+
overload,
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
from pandas._libs import (
|
| 13 |
+
algos as libalgos,
|
| 14 |
+
lib,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
from pandas.core.dtypes.cast import maybe_promote
|
| 18 |
+
from pandas.core.dtypes.common import (
|
| 19 |
+
ensure_platform_int,
|
| 20 |
+
is_1d_only_ea_dtype,
|
| 21 |
+
)
|
| 22 |
+
from pandas.core.dtypes.missing import na_value_for_dtype
|
| 23 |
+
|
| 24 |
+
from pandas.core.construction import ensure_wrapped_if_datetimelike
|
| 25 |
+
|
| 26 |
+
if TYPE_CHECKING:
|
| 27 |
+
from pandas._typing import (
|
| 28 |
+
ArrayLike,
|
| 29 |
+
AxisInt,
|
| 30 |
+
npt,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray
|
| 34 |
+
from pandas.core.arrays.base import ExtensionArray
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@overload
|
| 38 |
+
def take_nd(
|
| 39 |
+
arr: np.ndarray,
|
| 40 |
+
indexer,
|
| 41 |
+
axis: AxisInt = ...,
|
| 42 |
+
fill_value=...,
|
| 43 |
+
allow_fill: bool = ...,
|
| 44 |
+
) -> np.ndarray:
|
| 45 |
+
...
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
@overload
|
| 49 |
+
def take_nd(
|
| 50 |
+
arr: ExtensionArray,
|
| 51 |
+
indexer,
|
| 52 |
+
axis: AxisInt = ...,
|
| 53 |
+
fill_value=...,
|
| 54 |
+
allow_fill: bool = ...,
|
| 55 |
+
) -> ArrayLike:
|
| 56 |
+
...
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def take_nd(
|
| 60 |
+
arr: ArrayLike,
|
| 61 |
+
indexer,
|
| 62 |
+
axis: AxisInt = 0,
|
| 63 |
+
fill_value=lib.no_default,
|
| 64 |
+
allow_fill: bool = True,
|
| 65 |
+
) -> ArrayLike:
|
| 66 |
+
"""
|
| 67 |
+
Specialized Cython take which sets NaN values in one pass
|
| 68 |
+
|
| 69 |
+
This dispatches to ``take`` defined on ExtensionArrays.
|
| 70 |
+
|
| 71 |
+
Note: this function assumes that the indexer is a valid(ated) indexer with
|
| 72 |
+
no out of bound indices.
|
| 73 |
+
|
| 74 |
+
Parameters
|
| 75 |
+
----------
|
| 76 |
+
arr : np.ndarray or ExtensionArray
|
| 77 |
+
Input array.
|
| 78 |
+
indexer : ndarray
|
| 79 |
+
1-D array of indices to take, subarrays corresponding to -1 value
|
| 80 |
+
indices are filed with fill_value
|
| 81 |
+
axis : int, default 0
|
| 82 |
+
Axis to take from
|
| 83 |
+
fill_value : any, default np.nan
|
| 84 |
+
Fill value to replace -1 values with
|
| 85 |
+
allow_fill : bool, default True
|
| 86 |
+
If False, indexer is assumed to contain no -1 values so no filling
|
| 87 |
+
will be done. This short-circuits computation of a mask. Result is
|
| 88 |
+
undefined if allow_fill == False and -1 is present in indexer.
|
| 89 |
+
|
| 90 |
+
Returns
|
| 91 |
+
-------
|
| 92 |
+
subarray : np.ndarray or ExtensionArray
|
| 93 |
+
May be the same type as the input, or cast to an ndarray.
|
| 94 |
+
"""
|
| 95 |
+
if fill_value is lib.no_default:
|
| 96 |
+
fill_value = na_value_for_dtype(arr.dtype, compat=False)
|
| 97 |
+
elif lib.is_np_dtype(arr.dtype, "mM"):
|
| 98 |
+
dtype, fill_value = maybe_promote(arr.dtype, fill_value)
|
| 99 |
+
if arr.dtype != dtype:
|
| 100 |
+
# EA.take is strict about returning a new object of the same type
|
| 101 |
+
# so for that case cast upfront
|
| 102 |
+
arr = arr.astype(dtype)
|
| 103 |
+
|
| 104 |
+
if not isinstance(arr, np.ndarray):
|
| 105 |
+
# i.e. ExtensionArray,
|
| 106 |
+
# includes for EA to catch DatetimeArray, TimedeltaArray
|
| 107 |
+
if not is_1d_only_ea_dtype(arr.dtype):
|
| 108 |
+
# i.e. DatetimeArray, TimedeltaArray
|
| 109 |
+
arr = cast("NDArrayBackedExtensionArray", arr)
|
| 110 |
+
return arr.take(
|
| 111 |
+
indexer, fill_value=fill_value, allow_fill=allow_fill, axis=axis
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill)
|
| 115 |
+
|
| 116 |
+
arr = np.asarray(arr)
|
| 117 |
+
return _take_nd_ndarray(arr, indexer, axis, fill_value, allow_fill)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def _take_nd_ndarray(
|
| 121 |
+
arr: np.ndarray,
|
| 122 |
+
indexer: npt.NDArray[np.intp] | None,
|
| 123 |
+
axis: AxisInt,
|
| 124 |
+
fill_value,
|
| 125 |
+
allow_fill: bool,
|
| 126 |
+
) -> np.ndarray:
|
| 127 |
+
if indexer is None:
|
| 128 |
+
indexer = np.arange(arr.shape[axis], dtype=np.intp)
|
| 129 |
+
dtype, fill_value = arr.dtype, arr.dtype.type()
|
| 130 |
+
else:
|
| 131 |
+
indexer = ensure_platform_int(indexer)
|
| 132 |
+
|
| 133 |
+
dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
|
| 134 |
+
arr, indexer, fill_value, allow_fill
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
flip_order = False
|
| 138 |
+
if arr.ndim == 2 and arr.flags.f_contiguous:
|
| 139 |
+
flip_order = True
|
| 140 |
+
|
| 141 |
+
if flip_order:
|
| 142 |
+
arr = arr.T
|
| 143 |
+
axis = arr.ndim - axis - 1
|
| 144 |
+
|
| 145 |
+
# at this point, it's guaranteed that dtype can hold both the arr values
|
| 146 |
+
# and the fill_value
|
| 147 |
+
out_shape_ = list(arr.shape)
|
| 148 |
+
out_shape_[axis] = len(indexer)
|
| 149 |
+
out_shape = tuple(out_shape_)
|
| 150 |
+
if arr.flags.f_contiguous and axis == arr.ndim - 1:
|
| 151 |
+
# minor tweak that can make an order-of-magnitude difference
|
| 152 |
+
# for dataframes initialized directly from 2-d ndarrays
|
| 153 |
+
# (s.t. df.values is c-contiguous and df._mgr.blocks[0] is its
|
| 154 |
+
# f-contiguous transpose)
|
| 155 |
+
out = np.empty(out_shape, dtype=dtype, order="F")
|
| 156 |
+
else:
|
| 157 |
+
out = np.empty(out_shape, dtype=dtype)
|
| 158 |
+
|
| 159 |
+
func = _get_take_nd_function(
|
| 160 |
+
arr.ndim, arr.dtype, out.dtype, axis=axis, mask_info=mask_info
|
| 161 |
+
)
|
| 162 |
+
func(arr, indexer, out, fill_value)
|
| 163 |
+
|
| 164 |
+
if flip_order:
|
| 165 |
+
out = out.T
|
| 166 |
+
return out
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
def take_1d(
|
| 170 |
+
arr: ArrayLike,
|
| 171 |
+
indexer: npt.NDArray[np.intp],
|
| 172 |
+
fill_value=None,
|
| 173 |
+
allow_fill: bool = True,
|
| 174 |
+
mask: npt.NDArray[np.bool_] | None = None,
|
| 175 |
+
) -> ArrayLike:
|
| 176 |
+
"""
|
| 177 |
+
Specialized version for 1D arrays. Differences compared to `take_nd`:
|
| 178 |
+
|
| 179 |
+
- Assumes input array has already been converted to numpy array / EA
|
| 180 |
+
- Assumes indexer is already guaranteed to be intp dtype ndarray
|
| 181 |
+
- Only works for 1D arrays
|
| 182 |
+
|
| 183 |
+
To ensure the lowest possible overhead.
|
| 184 |
+
|
| 185 |
+
Note: similarly to `take_nd`, this function assumes that the indexer is
|
| 186 |
+
a valid(ated) indexer with no out of bound indices.
|
| 187 |
+
|
| 188 |
+
Parameters
|
| 189 |
+
----------
|
| 190 |
+
arr : np.ndarray or ExtensionArray
|
| 191 |
+
Input array.
|
| 192 |
+
indexer : ndarray
|
| 193 |
+
1-D array of indices to take (validated indices, intp dtype).
|
| 194 |
+
fill_value : any, default np.nan
|
| 195 |
+
Fill value to replace -1 values with
|
| 196 |
+
allow_fill : bool, default True
|
| 197 |
+
If False, indexer is assumed to contain no -1 values so no filling
|
| 198 |
+
will be done. This short-circuits computation of a mask. Result is
|
| 199 |
+
undefined if allow_fill == False and -1 is present in indexer.
|
| 200 |
+
mask : np.ndarray, optional, default None
|
| 201 |
+
If `allow_fill` is True, and the mask (where indexer == -1) is already
|
| 202 |
+
known, it can be passed to avoid recomputation.
|
| 203 |
+
"""
|
| 204 |
+
if not isinstance(arr, np.ndarray):
|
| 205 |
+
# ExtensionArray -> dispatch to their method
|
| 206 |
+
return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill)
|
| 207 |
+
|
| 208 |
+
if not allow_fill:
|
| 209 |
+
return arr.take(indexer)
|
| 210 |
+
|
| 211 |
+
dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
|
| 212 |
+
arr, indexer, fill_value, True, mask
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
# at this point, it's guaranteed that dtype can hold both the arr values
|
| 216 |
+
# and the fill_value
|
| 217 |
+
out = np.empty(indexer.shape, dtype=dtype)
|
| 218 |
+
|
| 219 |
+
func = _get_take_nd_function(
|
| 220 |
+
arr.ndim, arr.dtype, out.dtype, axis=0, mask_info=mask_info
|
| 221 |
+
)
|
| 222 |
+
func(arr, indexer, out, fill_value)
|
| 223 |
+
|
| 224 |
+
return out
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
def take_2d_multi(
|
| 228 |
+
arr: np.ndarray,
|
| 229 |
+
indexer: tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]],
|
| 230 |
+
fill_value=np.nan,
|
| 231 |
+
) -> np.ndarray:
|
| 232 |
+
"""
|
| 233 |
+
Specialized Cython take which sets NaN values in one pass.
|
| 234 |
+
"""
|
| 235 |
+
# This is only called from one place in DataFrame._reindex_multi,
|
| 236 |
+
# so we know indexer is well-behaved.
|
| 237 |
+
assert indexer is not None
|
| 238 |
+
assert indexer[0] is not None
|
| 239 |
+
assert indexer[1] is not None
|
| 240 |
+
|
| 241 |
+
row_idx, col_idx = indexer
|
| 242 |
+
|
| 243 |
+
row_idx = ensure_platform_int(row_idx)
|
| 244 |
+
col_idx = ensure_platform_int(col_idx)
|
| 245 |
+
indexer = row_idx, col_idx
|
| 246 |
+
mask_info = None
|
| 247 |
+
|
| 248 |
+
# check for promotion based on types only (do this first because
|
| 249 |
+
# it's faster than computing a mask)
|
| 250 |
+
dtype, fill_value = maybe_promote(arr.dtype, fill_value)
|
| 251 |
+
if dtype != arr.dtype:
|
| 252 |
+
# check if promotion is actually required based on indexer
|
| 253 |
+
row_mask = row_idx == -1
|
| 254 |
+
col_mask = col_idx == -1
|
| 255 |
+
row_needs = row_mask.any()
|
| 256 |
+
col_needs = col_mask.any()
|
| 257 |
+
mask_info = (row_mask, col_mask), (row_needs, col_needs)
|
| 258 |
+
|
| 259 |
+
if not (row_needs or col_needs):
|
| 260 |
+
# if not, then depromote, set fill_value to dummy
|
| 261 |
+
# (it won't be used but we don't want the cython code
|
| 262 |
+
# to crash when trying to cast it to dtype)
|
| 263 |
+
dtype, fill_value = arr.dtype, arr.dtype.type()
|
| 264 |
+
|
| 265 |
+
# at this point, it's guaranteed that dtype can hold both the arr values
|
| 266 |
+
# and the fill_value
|
| 267 |
+
out_shape = len(row_idx), len(col_idx)
|
| 268 |
+
out = np.empty(out_shape, dtype=dtype)
|
| 269 |
+
|
| 270 |
+
func = _take_2d_multi_dict.get((arr.dtype.name, out.dtype.name), None)
|
| 271 |
+
if func is None and arr.dtype != out.dtype:
|
| 272 |
+
func = _take_2d_multi_dict.get((out.dtype.name, out.dtype.name), None)
|
| 273 |
+
if func is not None:
|
| 274 |
+
func = _convert_wrapper(func, out.dtype)
|
| 275 |
+
|
| 276 |
+
if func is not None:
|
| 277 |
+
func(arr, indexer, out=out, fill_value=fill_value)
|
| 278 |
+
else:
|
| 279 |
+
# test_reindex_multi
|
| 280 |
+
_take_2d_multi_object(
|
| 281 |
+
arr, indexer, out, fill_value=fill_value, mask_info=mask_info
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
return out
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
@functools.lru_cache
|
| 288 |
+
def _get_take_nd_function_cached(
|
| 289 |
+
ndim: int, arr_dtype: np.dtype, out_dtype: np.dtype, axis: AxisInt
|
| 290 |
+
):
|
| 291 |
+
"""
|
| 292 |
+
Part of _get_take_nd_function below that doesn't need `mask_info` and thus
|
| 293 |
+
can be cached (mask_info potentially contains a numpy ndarray which is not
|
| 294 |
+
hashable and thus cannot be used as argument for cached function).
|
| 295 |
+
"""
|
| 296 |
+
tup = (arr_dtype.name, out_dtype.name)
|
| 297 |
+
if ndim == 1:
|
| 298 |
+
func = _take_1d_dict.get(tup, None)
|
| 299 |
+
elif ndim == 2:
|
| 300 |
+
if axis == 0:
|
| 301 |
+
func = _take_2d_axis0_dict.get(tup, None)
|
| 302 |
+
else:
|
| 303 |
+
func = _take_2d_axis1_dict.get(tup, None)
|
| 304 |
+
if func is not None:
|
| 305 |
+
return func
|
| 306 |
+
|
| 307 |
+
# We get here with string, uint, float16, and complex dtypes that could
|
| 308 |
+
# potentially be handled in algos_take_helper.
|
| 309 |
+
# Also a couple with (M8[ns], object) and (m8[ns], object)
|
| 310 |
+
tup = (out_dtype.name, out_dtype.name)
|
| 311 |
+
if ndim == 1:
|
| 312 |
+
func = _take_1d_dict.get(tup, None)
|
| 313 |
+
elif ndim == 2:
|
| 314 |
+
if axis == 0:
|
| 315 |
+
func = _take_2d_axis0_dict.get(tup, None)
|
| 316 |
+
else:
|
| 317 |
+
func = _take_2d_axis1_dict.get(tup, None)
|
| 318 |
+
if func is not None:
|
| 319 |
+
func = _convert_wrapper(func, out_dtype)
|
| 320 |
+
return func
|
| 321 |
+
|
| 322 |
+
return None
|
| 323 |
+
|
| 324 |
+
|
| 325 |
+
def _get_take_nd_function(
|
| 326 |
+
ndim: int,
|
| 327 |
+
arr_dtype: np.dtype,
|
| 328 |
+
out_dtype: np.dtype,
|
| 329 |
+
axis: AxisInt = 0,
|
| 330 |
+
mask_info=None,
|
| 331 |
+
):
|
| 332 |
+
"""
|
| 333 |
+
Get the appropriate "take" implementation for the given dimension, axis
|
| 334 |
+
and dtypes.
|
| 335 |
+
"""
|
| 336 |
+
func = None
|
| 337 |
+
if ndim <= 2:
|
| 338 |
+
# for this part we don't need `mask_info` -> use the cached algo lookup
|
| 339 |
+
func = _get_take_nd_function_cached(ndim, arr_dtype, out_dtype, axis)
|
| 340 |
+
|
| 341 |
+
if func is None:
|
| 342 |
+
|
| 343 |
+
def func(arr, indexer, out, fill_value=np.nan) -> None:
|
| 344 |
+
indexer = ensure_platform_int(indexer)
|
| 345 |
+
_take_nd_object(
|
| 346 |
+
arr, indexer, out, axis=axis, fill_value=fill_value, mask_info=mask_info
|
| 347 |
+
)
|
| 348 |
+
|
| 349 |
+
return func
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
def _view_wrapper(f, arr_dtype=None, out_dtype=None, fill_wrap=None):
|
| 353 |
+
def wrapper(
|
| 354 |
+
arr: np.ndarray, indexer: np.ndarray, out: np.ndarray, fill_value=np.nan
|
| 355 |
+
) -> None:
|
| 356 |
+
if arr_dtype is not None:
|
| 357 |
+
arr = arr.view(arr_dtype)
|
| 358 |
+
if out_dtype is not None:
|
| 359 |
+
out = out.view(out_dtype)
|
| 360 |
+
if fill_wrap is not None:
|
| 361 |
+
# FIXME: if we get here with dt64/td64 we need to be sure we have
|
| 362 |
+
# matching resos
|
| 363 |
+
if fill_value.dtype.kind == "m":
|
| 364 |
+
fill_value = fill_value.astype("m8[ns]")
|
| 365 |
+
else:
|
| 366 |
+
fill_value = fill_value.astype("M8[ns]")
|
| 367 |
+
fill_value = fill_wrap(fill_value)
|
| 368 |
+
|
| 369 |
+
f(arr, indexer, out, fill_value=fill_value)
|
| 370 |
+
|
| 371 |
+
return wrapper
|
| 372 |
+
|
| 373 |
+
|
| 374 |
+
def _convert_wrapper(f, conv_dtype):
|
| 375 |
+
def wrapper(
|
| 376 |
+
arr: np.ndarray, indexer: np.ndarray, out: np.ndarray, fill_value=np.nan
|
| 377 |
+
) -> None:
|
| 378 |
+
if conv_dtype == object:
|
| 379 |
+
# GH#39755 avoid casting dt64/td64 to integers
|
| 380 |
+
arr = ensure_wrapped_if_datetimelike(arr)
|
| 381 |
+
arr = arr.astype(conv_dtype)
|
| 382 |
+
f(arr, indexer, out, fill_value=fill_value)
|
| 383 |
+
|
| 384 |
+
return wrapper
|
| 385 |
+
|
| 386 |
+
|
| 387 |
+
_take_1d_dict = {
|
| 388 |
+
("int8", "int8"): libalgos.take_1d_int8_int8,
|
| 389 |
+
("int8", "int32"): libalgos.take_1d_int8_int32,
|
| 390 |
+
("int8", "int64"): libalgos.take_1d_int8_int64,
|
| 391 |
+
("int8", "float64"): libalgos.take_1d_int8_float64,
|
| 392 |
+
("int16", "int16"): libalgos.take_1d_int16_int16,
|
| 393 |
+
("int16", "int32"): libalgos.take_1d_int16_int32,
|
| 394 |
+
("int16", "int64"): libalgos.take_1d_int16_int64,
|
| 395 |
+
("int16", "float64"): libalgos.take_1d_int16_float64,
|
| 396 |
+
("int32", "int32"): libalgos.take_1d_int32_int32,
|
| 397 |
+
("int32", "int64"): libalgos.take_1d_int32_int64,
|
| 398 |
+
("int32", "float64"): libalgos.take_1d_int32_float64,
|
| 399 |
+
("int64", "int64"): libalgos.take_1d_int64_int64,
|
| 400 |
+
("int64", "float64"): libalgos.take_1d_int64_float64,
|
| 401 |
+
("float32", "float32"): libalgos.take_1d_float32_float32,
|
| 402 |
+
("float32", "float64"): libalgos.take_1d_float32_float64,
|
| 403 |
+
("float64", "float64"): libalgos.take_1d_float64_float64,
|
| 404 |
+
("object", "object"): libalgos.take_1d_object_object,
|
| 405 |
+
("bool", "bool"): _view_wrapper(libalgos.take_1d_bool_bool, np.uint8, np.uint8),
|
| 406 |
+
("bool", "object"): _view_wrapper(libalgos.take_1d_bool_object, np.uint8, None),
|
| 407 |
+
("datetime64[ns]", "datetime64[ns]"): _view_wrapper(
|
| 408 |
+
libalgos.take_1d_int64_int64, np.int64, np.int64, np.int64
|
| 409 |
+
),
|
| 410 |
+
("timedelta64[ns]", "timedelta64[ns]"): _view_wrapper(
|
| 411 |
+
libalgos.take_1d_int64_int64, np.int64, np.int64, np.int64
|
| 412 |
+
),
|
| 413 |
+
}
|
| 414 |
+
|
| 415 |
+
_take_2d_axis0_dict = {
|
| 416 |
+
("int8", "int8"): libalgos.take_2d_axis0_int8_int8,
|
| 417 |
+
("int8", "int32"): libalgos.take_2d_axis0_int8_int32,
|
| 418 |
+
("int8", "int64"): libalgos.take_2d_axis0_int8_int64,
|
| 419 |
+
("int8", "float64"): libalgos.take_2d_axis0_int8_float64,
|
| 420 |
+
("int16", "int16"): libalgos.take_2d_axis0_int16_int16,
|
| 421 |
+
("int16", "int32"): libalgos.take_2d_axis0_int16_int32,
|
| 422 |
+
("int16", "int64"): libalgos.take_2d_axis0_int16_int64,
|
| 423 |
+
("int16", "float64"): libalgos.take_2d_axis0_int16_float64,
|
| 424 |
+
("int32", "int32"): libalgos.take_2d_axis0_int32_int32,
|
| 425 |
+
("int32", "int64"): libalgos.take_2d_axis0_int32_int64,
|
| 426 |
+
("int32", "float64"): libalgos.take_2d_axis0_int32_float64,
|
| 427 |
+
("int64", "int64"): libalgos.take_2d_axis0_int64_int64,
|
| 428 |
+
("int64", "float64"): libalgos.take_2d_axis0_int64_float64,
|
| 429 |
+
("float32", "float32"): libalgos.take_2d_axis0_float32_float32,
|
| 430 |
+
("float32", "float64"): libalgos.take_2d_axis0_float32_float64,
|
| 431 |
+
("float64", "float64"): libalgos.take_2d_axis0_float64_float64,
|
| 432 |
+
("object", "object"): libalgos.take_2d_axis0_object_object,
|
| 433 |
+
("bool", "bool"): _view_wrapper(
|
| 434 |
+
libalgos.take_2d_axis0_bool_bool, np.uint8, np.uint8
|
| 435 |
+
),
|
| 436 |
+
("bool", "object"): _view_wrapper(
|
| 437 |
+
libalgos.take_2d_axis0_bool_object, np.uint8, None
|
| 438 |
+
),
|
| 439 |
+
("datetime64[ns]", "datetime64[ns]"): _view_wrapper(
|
| 440 |
+
libalgos.take_2d_axis0_int64_int64, np.int64, np.int64, fill_wrap=np.int64
|
| 441 |
+
),
|
| 442 |
+
("timedelta64[ns]", "timedelta64[ns]"): _view_wrapper(
|
| 443 |
+
libalgos.take_2d_axis0_int64_int64, np.int64, np.int64, fill_wrap=np.int64
|
| 444 |
+
),
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
+
_take_2d_axis1_dict = {
|
| 448 |
+
("int8", "int8"): libalgos.take_2d_axis1_int8_int8,
|
| 449 |
+
("int8", "int32"): libalgos.take_2d_axis1_int8_int32,
|
| 450 |
+
("int8", "int64"): libalgos.take_2d_axis1_int8_int64,
|
| 451 |
+
("int8", "float64"): libalgos.take_2d_axis1_int8_float64,
|
| 452 |
+
("int16", "int16"): libalgos.take_2d_axis1_int16_int16,
|
| 453 |
+
("int16", "int32"): libalgos.take_2d_axis1_int16_int32,
|
| 454 |
+
("int16", "int64"): libalgos.take_2d_axis1_int16_int64,
|
| 455 |
+
("int16", "float64"): libalgos.take_2d_axis1_int16_float64,
|
| 456 |
+
("int32", "int32"): libalgos.take_2d_axis1_int32_int32,
|
| 457 |
+
("int32", "int64"): libalgos.take_2d_axis1_int32_int64,
|
| 458 |
+
("int32", "float64"): libalgos.take_2d_axis1_int32_float64,
|
| 459 |
+
("int64", "int64"): libalgos.take_2d_axis1_int64_int64,
|
| 460 |
+
("int64", "float64"): libalgos.take_2d_axis1_int64_float64,
|
| 461 |
+
("float32", "float32"): libalgos.take_2d_axis1_float32_float32,
|
| 462 |
+
("float32", "float64"): libalgos.take_2d_axis1_float32_float64,
|
| 463 |
+
("float64", "float64"): libalgos.take_2d_axis1_float64_float64,
|
| 464 |
+
("object", "object"): libalgos.take_2d_axis1_object_object,
|
| 465 |
+
("bool", "bool"): _view_wrapper(
|
| 466 |
+
libalgos.take_2d_axis1_bool_bool, np.uint8, np.uint8
|
| 467 |
+
),
|
| 468 |
+
("bool", "object"): _view_wrapper(
|
| 469 |
+
libalgos.take_2d_axis1_bool_object, np.uint8, None
|
| 470 |
+
),
|
| 471 |
+
("datetime64[ns]", "datetime64[ns]"): _view_wrapper(
|
| 472 |
+
libalgos.take_2d_axis1_int64_int64, np.int64, np.int64, fill_wrap=np.int64
|
| 473 |
+
),
|
| 474 |
+
("timedelta64[ns]", "timedelta64[ns]"): _view_wrapper(
|
| 475 |
+
libalgos.take_2d_axis1_int64_int64, np.int64, np.int64, fill_wrap=np.int64
|
| 476 |
+
),
|
| 477 |
+
}
|
| 478 |
+
|
| 479 |
+
_take_2d_multi_dict = {
|
| 480 |
+
("int8", "int8"): libalgos.take_2d_multi_int8_int8,
|
| 481 |
+
("int8", "int32"): libalgos.take_2d_multi_int8_int32,
|
| 482 |
+
("int8", "int64"): libalgos.take_2d_multi_int8_int64,
|
| 483 |
+
("int8", "float64"): libalgos.take_2d_multi_int8_float64,
|
| 484 |
+
("int16", "int16"): libalgos.take_2d_multi_int16_int16,
|
| 485 |
+
("int16", "int32"): libalgos.take_2d_multi_int16_int32,
|
| 486 |
+
("int16", "int64"): libalgos.take_2d_multi_int16_int64,
|
| 487 |
+
("int16", "float64"): libalgos.take_2d_multi_int16_float64,
|
| 488 |
+
("int32", "int32"): libalgos.take_2d_multi_int32_int32,
|
| 489 |
+
("int32", "int64"): libalgos.take_2d_multi_int32_int64,
|
| 490 |
+
("int32", "float64"): libalgos.take_2d_multi_int32_float64,
|
| 491 |
+
("int64", "int64"): libalgos.take_2d_multi_int64_int64,
|
| 492 |
+
("int64", "float64"): libalgos.take_2d_multi_int64_float64,
|
| 493 |
+
("float32", "float32"): libalgos.take_2d_multi_float32_float32,
|
| 494 |
+
("float32", "float64"): libalgos.take_2d_multi_float32_float64,
|
| 495 |
+
("float64", "float64"): libalgos.take_2d_multi_float64_float64,
|
| 496 |
+
("object", "object"): libalgos.take_2d_multi_object_object,
|
| 497 |
+
("bool", "bool"): _view_wrapper(
|
| 498 |
+
libalgos.take_2d_multi_bool_bool, np.uint8, np.uint8
|
| 499 |
+
),
|
| 500 |
+
("bool", "object"): _view_wrapper(
|
| 501 |
+
libalgos.take_2d_multi_bool_object, np.uint8, None
|
| 502 |
+
),
|
| 503 |
+
("datetime64[ns]", "datetime64[ns]"): _view_wrapper(
|
| 504 |
+
libalgos.take_2d_multi_int64_int64, np.int64, np.int64, fill_wrap=np.int64
|
| 505 |
+
),
|
| 506 |
+
("timedelta64[ns]", "timedelta64[ns]"): _view_wrapper(
|
| 507 |
+
libalgos.take_2d_multi_int64_int64, np.int64, np.int64, fill_wrap=np.int64
|
| 508 |
+
),
|
| 509 |
+
}
|
| 510 |
+
|
| 511 |
+
|
| 512 |
+
def _take_nd_object(
|
| 513 |
+
arr: np.ndarray,
|
| 514 |
+
indexer: npt.NDArray[np.intp],
|
| 515 |
+
out: np.ndarray,
|
| 516 |
+
axis: AxisInt,
|
| 517 |
+
fill_value,
|
| 518 |
+
mask_info,
|
| 519 |
+
) -> None:
|
| 520 |
+
if mask_info is not None:
|
| 521 |
+
mask, needs_masking = mask_info
|
| 522 |
+
else:
|
| 523 |
+
mask = indexer == -1
|
| 524 |
+
needs_masking = mask.any()
|
| 525 |
+
if arr.dtype != out.dtype:
|
| 526 |
+
arr = arr.astype(out.dtype)
|
| 527 |
+
if arr.shape[axis] > 0:
|
| 528 |
+
arr.take(indexer, axis=axis, out=out)
|
| 529 |
+
if needs_masking:
|
| 530 |
+
outindexer = [slice(None)] * arr.ndim
|
| 531 |
+
outindexer[axis] = mask
|
| 532 |
+
out[tuple(outindexer)] = fill_value
|
| 533 |
+
|
| 534 |
+
|
| 535 |
+
def _take_2d_multi_object(
|
| 536 |
+
arr: np.ndarray,
|
| 537 |
+
indexer: tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]],
|
| 538 |
+
out: np.ndarray,
|
| 539 |
+
fill_value,
|
| 540 |
+
mask_info,
|
| 541 |
+
) -> None:
|
| 542 |
+
# this is not ideal, performance-wise, but it's better than raising
|
| 543 |
+
# an exception (best to optimize in Cython to avoid getting here)
|
| 544 |
+
row_idx, col_idx = indexer # both np.intp
|
| 545 |
+
if mask_info is not None:
|
| 546 |
+
(row_mask, col_mask), (row_needs, col_needs) = mask_info
|
| 547 |
+
else:
|
| 548 |
+
row_mask = row_idx == -1
|
| 549 |
+
col_mask = col_idx == -1
|
| 550 |
+
row_needs = row_mask.any()
|
| 551 |
+
col_needs = col_mask.any()
|
| 552 |
+
if fill_value is not None:
|
| 553 |
+
if row_needs:
|
| 554 |
+
out[row_mask, :] = fill_value
|
| 555 |
+
if col_needs:
|
| 556 |
+
out[:, col_mask] = fill_value
|
| 557 |
+
for i, u_ in enumerate(row_idx):
|
| 558 |
+
if u_ != -1:
|
| 559 |
+
for j, v in enumerate(col_idx):
|
| 560 |
+
if v != -1:
|
| 561 |
+
out[i, j] = arr[u_, v]
|
| 562 |
+
|
| 563 |
+
|
| 564 |
+
def _take_preprocess_indexer_and_fill_value(
|
| 565 |
+
arr: np.ndarray,
|
| 566 |
+
indexer: npt.NDArray[np.intp],
|
| 567 |
+
fill_value,
|
| 568 |
+
allow_fill: bool,
|
| 569 |
+
mask: npt.NDArray[np.bool_] | None = None,
|
| 570 |
+
):
|
| 571 |
+
mask_info: tuple[np.ndarray | None, bool] | None = None
|
| 572 |
+
|
| 573 |
+
if not allow_fill:
|
| 574 |
+
dtype, fill_value = arr.dtype, arr.dtype.type()
|
| 575 |
+
mask_info = None, False
|
| 576 |
+
else:
|
| 577 |
+
# check for promotion based on types only (do this first because
|
| 578 |
+
# it's faster than computing a mask)
|
| 579 |
+
dtype, fill_value = maybe_promote(arr.dtype, fill_value)
|
| 580 |
+
if dtype != arr.dtype:
|
| 581 |
+
# check if promotion is actually required based on indexer
|
| 582 |
+
if mask is not None:
|
| 583 |
+
needs_masking = True
|
| 584 |
+
else:
|
| 585 |
+
mask = indexer == -1
|
| 586 |
+
needs_masking = bool(mask.any())
|
| 587 |
+
mask_info = mask, needs_masking
|
| 588 |
+
if not needs_masking:
|
| 589 |
+
# if not, then depromote, set fill_value to dummy
|
| 590 |
+
# (it won't be used but we don't want the cython code
|
| 591 |
+
# to crash when trying to cast it to dtype)
|
| 592 |
+
dtype, fill_value = arr.dtype, arr.dtype.type()
|
| 593 |
+
|
| 594 |
+
return dtype, fill_value, mask_info
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/array_algos/transforms.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
transforms.py is for shape-preserving functions.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from __future__ import annotations
|
| 6 |
+
|
| 7 |
+
from typing import TYPE_CHECKING
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
if TYPE_CHECKING:
|
| 12 |
+
from pandas._typing import (
|
| 13 |
+
AxisInt,
|
| 14 |
+
Scalar,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def shift(
|
| 19 |
+
values: np.ndarray, periods: int, axis: AxisInt, fill_value: Scalar
|
| 20 |
+
) -> np.ndarray:
|
| 21 |
+
new_values = values
|
| 22 |
+
|
| 23 |
+
if periods == 0 or values.size == 0:
|
| 24 |
+
return new_values.copy()
|
| 25 |
+
|
| 26 |
+
# make sure array sent to np.roll is c_contiguous
|
| 27 |
+
f_ordered = values.flags.f_contiguous
|
| 28 |
+
if f_ordered:
|
| 29 |
+
new_values = new_values.T
|
| 30 |
+
axis = new_values.ndim - axis - 1
|
| 31 |
+
|
| 32 |
+
if new_values.size:
|
| 33 |
+
new_values = np.roll(
|
| 34 |
+
new_values,
|
| 35 |
+
np.intp(periods),
|
| 36 |
+
axis=axis,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
axis_indexer = [slice(None)] * values.ndim
|
| 40 |
+
if periods > 0:
|
| 41 |
+
axis_indexer[axis] = slice(None, periods)
|
| 42 |
+
else:
|
| 43 |
+
axis_indexer[axis] = slice(periods, None)
|
| 44 |
+
new_values[tuple(axis_indexer)] = fill_value
|
| 45 |
+
|
| 46 |
+
# restore original order
|
| 47 |
+
if f_ordered:
|
| 48 |
+
new_values = new_values.T
|
| 49 |
+
|
| 50 |
+
return new_values
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__init__.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pandas.core.arrays.arrow import ArrowExtensionArray
|
| 2 |
+
from pandas.core.arrays.base import (
|
| 3 |
+
ExtensionArray,
|
| 4 |
+
ExtensionOpsMixin,
|
| 5 |
+
ExtensionScalarOpsMixin,
|
| 6 |
+
)
|
| 7 |
+
from pandas.core.arrays.boolean import BooleanArray
|
| 8 |
+
from pandas.core.arrays.categorical import Categorical
|
| 9 |
+
from pandas.core.arrays.datetimes import DatetimeArray
|
| 10 |
+
from pandas.core.arrays.floating import FloatingArray
|
| 11 |
+
from pandas.core.arrays.integer import IntegerArray
|
| 12 |
+
from pandas.core.arrays.interval import IntervalArray
|
| 13 |
+
from pandas.core.arrays.masked import BaseMaskedArray
|
| 14 |
+
from pandas.core.arrays.numpy_ import NumpyExtensionArray
|
| 15 |
+
from pandas.core.arrays.period import (
|
| 16 |
+
PeriodArray,
|
| 17 |
+
period_array,
|
| 18 |
+
)
|
| 19 |
+
from pandas.core.arrays.sparse import SparseArray
|
| 20 |
+
from pandas.core.arrays.string_ import StringArray
|
| 21 |
+
from pandas.core.arrays.string_arrow import ArrowStringArray
|
| 22 |
+
from pandas.core.arrays.timedeltas import TimedeltaArray
|
| 23 |
+
|
| 24 |
+
__all__ = [
|
| 25 |
+
"ArrowExtensionArray",
|
| 26 |
+
"ExtensionArray",
|
| 27 |
+
"ExtensionOpsMixin",
|
| 28 |
+
"ExtensionScalarOpsMixin",
|
| 29 |
+
"ArrowStringArray",
|
| 30 |
+
"BaseMaskedArray",
|
| 31 |
+
"BooleanArray",
|
| 32 |
+
"Categorical",
|
| 33 |
+
"DatetimeArray",
|
| 34 |
+
"FloatingArray",
|
| 35 |
+
"IntegerArray",
|
| 36 |
+
"IntervalArray",
|
| 37 |
+
"NumpyExtensionArray",
|
| 38 |
+
"PeriodArray",
|
| 39 |
+
"period_array",
|
| 40 |
+
"SparseArray",
|
| 41 |
+
"StringArray",
|
| 42 |
+
"TimedeltaArray",
|
| 43 |
+
]
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (1.4 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/_arrow_string_mixins.cpython-310.pyc
ADDED
|
Binary file (11.6 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/_mixins.cpython-310.pyc
ADDED
|
Binary file (14.3 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/_ranges.cpython-310.pyc
ADDED
|
Binary file (4.88 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/_utils.cpython-310.pyc
ADDED
|
Binary file (1.45 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/base.cpython-310.pyc
ADDED
|
Binary file (75 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/boolean.cpython-310.pyc
ADDED
|
Binary file (10.6 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/categorical.cpython-310.pyc
ADDED
|
Binary file (81.9 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/datetimelike.cpython-310.pyc
ADDED
|
Binary file (64.1 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/datetimes.cpython-310.pyc
ADDED
|
Binary file (70.9 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/floating.cpython-310.pyc
ADDED
|
Binary file (4.73 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/integer.cpython-310.pyc
ADDED
|
Binary file (7.02 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/interval.cpython-310.pyc
ADDED
|
Binary file (48.2 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/masked.cpython-310.pyc
ADDED
|
Binary file (42 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/pandas/core/arrays/__pycache__/numeric.cpython-310.pyc
ADDED
|
Binary file (7.44 kB). View file
|
|
|