Add loan-advisor-agent (part 2)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +9 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/array_algos/datetimelike_accumulations.py +73 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/array_algos/masked_accumulations.py +97 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/array_algos/masked_reductions.py +214 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/array_algos/putmask.py +150 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/array_algos/quantile.py +226 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/array_algos/replace.py +159 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/array_algos/take.py +546 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/array_algos/transforms.py +50 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arraylike.py +534 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__init__.py +43 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/__init__.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/_arrow_string_mixins.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/_mixins.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/_ranges.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/_utils.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/base.cpython-312.pyc +3 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/boolean.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/categorical.cpython-312.pyc +3 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/datetimelike.cpython-312.pyc +3 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/datetimes.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/floating.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/integer.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/interval.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/masked.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/numeric.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/numpy_.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/period.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/string_.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/string_arrow.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/timedeltas.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/_arrow_string_mixins.py +434 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/_mixins.py +539 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/_ranges.py +209 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/_utils.py +75 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/__init__.py +7 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/__pycache__/__init__.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/__pycache__/accessors.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/__pycache__/array.cpython-312.pyc +3 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/_arrow_utils.py +50 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/accessors.py +501 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/array.py +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/extension_types.py +174 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/base.py +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/boolean.py +438 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/categorical.py +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/datetimelike.py +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/datetimes.py +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/floating.py +192 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/integer.py +296 -0
.gitattributes
CHANGED
|
@@ -495,3 +495,12 @@ loan-advisor-agent/.venv/Lib/site-packages/pandas/core/__pycache__/generic.cpyth
|
|
| 495 |
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/__pycache__/indexing.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 496 |
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/__pycache__/resample.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 497 |
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/__pycache__/series.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/__pycache__/indexing.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 496 |
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/__pycache__/resample.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 497 |
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/__pycache__/series.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 498 |
+
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/base.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 499 |
+
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/categorical.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 500 |
+
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/datetimelike.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 501 |
+
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/__pycache__/array.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 502 |
+
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/groupby/__pycache__/generic.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 503 |
+
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/groupby/__pycache__/groupby.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 504 |
+
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/indexes/__pycache__/base.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 505 |
+
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/indexes/__pycache__/multi.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 506 |
+
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/reshape/__pycache__/merge.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/array_algos/datetimelike_accumulations.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
datetimelke_accumulations.py is for accumulations of datetimelike extension arrays
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from __future__ import annotations
|
| 6 |
+
|
| 7 |
+
from typing import TYPE_CHECKING
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
from pandas._libs import iNaT
|
| 12 |
+
|
| 13 |
+
from pandas.core.dtypes.missing import isna
|
| 14 |
+
|
| 15 |
+
if TYPE_CHECKING:
|
| 16 |
+
from collections.abc import Callable
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _cum_func(
|
| 20 |
+
func: Callable,
|
| 21 |
+
values: np.ndarray,
|
| 22 |
+
*,
|
| 23 |
+
skipna: bool = True,
|
| 24 |
+
) -> np.ndarray:
|
| 25 |
+
"""
|
| 26 |
+
Accumulations for 1D datetimelike arrays.
|
| 27 |
+
|
| 28 |
+
Parameters
|
| 29 |
+
----------
|
| 30 |
+
func : np.cumsum, np.maximum.accumulate, np.minimum.accumulate
|
| 31 |
+
values : np.ndarray
|
| 32 |
+
Numpy array with the values (can be of any dtype that support the
|
| 33 |
+
operation). Values is changed is modified inplace.
|
| 34 |
+
skipna : bool, default True
|
| 35 |
+
Whether to skip NA.
|
| 36 |
+
"""
|
| 37 |
+
try:
|
| 38 |
+
fill_value = {
|
| 39 |
+
np.maximum.accumulate: np.iinfo(np.int64).min,
|
| 40 |
+
np.cumsum: 0,
|
| 41 |
+
np.minimum.accumulate: np.iinfo(np.int64).max,
|
| 42 |
+
}[func]
|
| 43 |
+
except KeyError as err:
|
| 44 |
+
raise ValueError(
|
| 45 |
+
f"No accumulation for {func} implemented on BaseMaskedArray"
|
| 46 |
+
) from err
|
| 47 |
+
|
| 48 |
+
mask = isna(values)
|
| 49 |
+
y = values.view("i8")
|
| 50 |
+
y[mask] = fill_value
|
| 51 |
+
|
| 52 |
+
if not skipna:
|
| 53 |
+
mask = np.maximum.accumulate(mask)
|
| 54 |
+
|
| 55 |
+
# GH 57956
|
| 56 |
+
result = func(y, axis=0)
|
| 57 |
+
result[mask] = iNaT
|
| 58 |
+
|
| 59 |
+
if values.dtype.kind in "mM":
|
| 60 |
+
return result.view(values.dtype.base)
|
| 61 |
+
return result
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def cumsum(values: np.ndarray, *, skipna: bool = True) -> np.ndarray:
|
| 65 |
+
return _cum_func(np.cumsum, values, skipna=skipna)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def cummin(values: np.ndarray, *, skipna: bool = True) -> np.ndarray:
|
| 69 |
+
return _cum_func(np.minimum.accumulate, values, skipna=skipna)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def cummax(values: np.ndarray, *, skipna: bool = True) -> np.ndarray:
|
| 73 |
+
return _cum_func(np.maximum.accumulate, values, skipna=skipna)
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/array_algos/masked_accumulations.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 TYPE_CHECKING
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
if TYPE_CHECKING:
|
| 13 |
+
from collections.abc import Callable
|
| 14 |
+
|
| 15 |
+
from pandas._typing import npt
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def _cum_func(
|
| 19 |
+
func: Callable,
|
| 20 |
+
values: np.ndarray,
|
| 21 |
+
mask: npt.NDArray[np.bool_],
|
| 22 |
+
*,
|
| 23 |
+
skipna: bool = True,
|
| 24 |
+
) -> tuple[np.ndarray, npt.NDArray[np.bool_]]:
|
| 25 |
+
"""
|
| 26 |
+
Accumulations for 1D masked array.
|
| 27 |
+
|
| 28 |
+
We will modify values in place to replace NAs with the appropriate fill value.
|
| 29 |
+
|
| 30 |
+
Parameters
|
| 31 |
+
----------
|
| 32 |
+
func : np.cumsum, np.cumprod, np.maximum.accumulate, np.minimum.accumulate
|
| 33 |
+
values : np.ndarray
|
| 34 |
+
Numpy array with the values (can be of any dtype that support the
|
| 35 |
+
operation).
|
| 36 |
+
mask : np.ndarray
|
| 37 |
+
Boolean numpy array (True values indicate missing values).
|
| 38 |
+
skipna : bool, default True
|
| 39 |
+
Whether to skip NA.
|
| 40 |
+
"""
|
| 41 |
+
dtype_info: np.iinfo | np.finfo
|
| 42 |
+
if values.dtype.kind == "f":
|
| 43 |
+
dtype_info = np.finfo(values.dtype.type)
|
| 44 |
+
elif values.dtype.kind in "iu":
|
| 45 |
+
dtype_info = np.iinfo(values.dtype.type)
|
| 46 |
+
elif values.dtype.kind == "b":
|
| 47 |
+
# Max value of bool is 1, but since we are setting into a boolean
|
| 48 |
+
# array, 255 is fine as well. Min value has to be 0 when setting
|
| 49 |
+
# into the boolean array.
|
| 50 |
+
dtype_info = np.iinfo(np.uint8)
|
| 51 |
+
else:
|
| 52 |
+
raise NotImplementedError(
|
| 53 |
+
f"No masked accumulation defined for dtype {values.dtype.type}"
|
| 54 |
+
)
|
| 55 |
+
try:
|
| 56 |
+
fill_value = {
|
| 57 |
+
np.cumprod: 1,
|
| 58 |
+
np.maximum.accumulate: dtype_info.min,
|
| 59 |
+
np.cumsum: 0,
|
| 60 |
+
np.minimum.accumulate: dtype_info.max,
|
| 61 |
+
}[func]
|
| 62 |
+
except KeyError as err:
|
| 63 |
+
raise NotImplementedError(
|
| 64 |
+
f"No accumulation for {func} implemented on BaseMaskedArray"
|
| 65 |
+
) from err
|
| 66 |
+
|
| 67 |
+
values[mask] = fill_value
|
| 68 |
+
|
| 69 |
+
if not skipna:
|
| 70 |
+
mask = np.maximum.accumulate(mask)
|
| 71 |
+
|
| 72 |
+
values = func(values)
|
| 73 |
+
return values, mask
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def cumsum(
|
| 77 |
+
values: np.ndarray, mask: npt.NDArray[np.bool_], *, skipna: bool = True
|
| 78 |
+
) -> tuple[np.ndarray, npt.NDArray[np.bool_]]:
|
| 79 |
+
return _cum_func(np.cumsum, values, mask, skipna=skipna)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def cumprod(
|
| 83 |
+
values: np.ndarray, mask: npt.NDArray[np.bool_], *, skipna: bool = True
|
| 84 |
+
) -> tuple[np.ndarray, npt.NDArray[np.bool_]]:
|
| 85 |
+
return _cum_func(np.cumprod, values, mask, skipna=skipna)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def cummin(
|
| 89 |
+
values: np.ndarray, mask: npt.NDArray[np.bool_], *, skipna: bool = True
|
| 90 |
+
) -> tuple[np.ndarray, npt.NDArray[np.bool_]]:
|
| 91 |
+
return _cum_func(np.minimum.accumulate, values, mask, skipna=skipna)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def cummax(
|
| 95 |
+
values: np.ndarray, mask: npt.NDArray[np.bool_], *, skipna: bool = True
|
| 96 |
+
) -> tuple[np.ndarray, npt.NDArray[np.bool_]]:
|
| 97 |
+
return _cum_func(np.maximum.accumulate, values, mask, skipna=skipna)
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/array_algos/masked_reductions.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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annotations
|
| 7 |
+
|
| 8 |
+
from typing import TYPE_CHECKING
|
| 9 |
+
import warnings
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
from pandas._libs import (
|
| 14 |
+
lib,
|
| 15 |
+
missing as libmissing,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
from pandas.core.nanops import check_below_min_count
|
| 19 |
+
|
| 20 |
+
if TYPE_CHECKING:
|
| 21 |
+
from collections.abc import Callable
|
| 22 |
+
|
| 23 |
+
from pandas._typing import (
|
| 24 |
+
AxisInt,
|
| 25 |
+
npt,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def _reductions(
|
| 30 |
+
func: Callable,
|
| 31 |
+
values: np.ndarray,
|
| 32 |
+
mask: npt.NDArray[np.bool_],
|
| 33 |
+
*,
|
| 34 |
+
skipna: bool = True,
|
| 35 |
+
min_count: int = 0,
|
| 36 |
+
axis: AxisInt | None = None,
|
| 37 |
+
initial: object | lib.NoDefault = lib.no_default,
|
| 38 |
+
**kwargs,
|
| 39 |
+
):
|
| 40 |
+
"""
|
| 41 |
+
Sum, mean or product for 1D masked array.
|
| 42 |
+
|
| 43 |
+
Parameters
|
| 44 |
+
----------
|
| 45 |
+
func : np.sum or np.prod
|
| 46 |
+
values : np.ndarray
|
| 47 |
+
Numpy array with the values (can be of any dtype that support the
|
| 48 |
+
operation).
|
| 49 |
+
mask : np.ndarray[bool]
|
| 50 |
+
Boolean numpy array (True values indicate missing values).
|
| 51 |
+
skipna : bool, default True
|
| 52 |
+
Whether to skip NA.
|
| 53 |
+
min_count : int, default 0
|
| 54 |
+
The required number of valid values to perform the operation. If fewer than
|
| 55 |
+
``min_count`` non-NA values are present the result will be NA.
|
| 56 |
+
axis : int, optional, default None
|
| 57 |
+
initial : scalar, optional
|
| 58 |
+
Starting value for the reduction. NumPy has a default value for most
|
| 59 |
+
data types, but for object-dtype arrays we need to specify it explicitly
|
| 60 |
+
"""
|
| 61 |
+
if initial is not lib.no_default:
|
| 62 |
+
kwargs["initial"] = initial
|
| 63 |
+
|
| 64 |
+
if not skipna:
|
| 65 |
+
if mask.any() or check_below_min_count(values.shape, None, min_count):
|
| 66 |
+
return libmissing.NA
|
| 67 |
+
else:
|
| 68 |
+
return func(values, axis=axis, **kwargs)
|
| 69 |
+
else:
|
| 70 |
+
if check_below_min_count(values.shape, mask, min_count) and (
|
| 71 |
+
axis is None or values.ndim == 1
|
| 72 |
+
):
|
| 73 |
+
return libmissing.NA
|
| 74 |
+
|
| 75 |
+
return func(values, where=~mask, axis=axis, **kwargs)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def sum(
|
| 79 |
+
values: np.ndarray,
|
| 80 |
+
mask: npt.NDArray[np.bool_],
|
| 81 |
+
*,
|
| 82 |
+
skipna: bool = True,
|
| 83 |
+
min_count: int = 0,
|
| 84 |
+
axis: AxisInt | None = None,
|
| 85 |
+
initial: object | lib.NoDefault = lib.no_default,
|
| 86 |
+
):
|
| 87 |
+
return _reductions(
|
| 88 |
+
np.sum,
|
| 89 |
+
values=values,
|
| 90 |
+
mask=mask,
|
| 91 |
+
skipna=skipna,
|
| 92 |
+
min_count=min_count,
|
| 93 |
+
axis=axis,
|
| 94 |
+
initial=initial,
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def prod(
|
| 99 |
+
values: np.ndarray,
|
| 100 |
+
mask: npt.NDArray[np.bool_],
|
| 101 |
+
*,
|
| 102 |
+
skipna: bool = True,
|
| 103 |
+
min_count: int = 0,
|
| 104 |
+
axis: AxisInt | None = None,
|
| 105 |
+
):
|
| 106 |
+
return _reductions(
|
| 107 |
+
np.prod, values=values, mask=mask, skipna=skipna, min_count=min_count, axis=axis
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def _minmax(
|
| 112 |
+
func: Callable,
|
| 113 |
+
values: np.ndarray,
|
| 114 |
+
mask: npt.NDArray[np.bool_],
|
| 115 |
+
*,
|
| 116 |
+
skipna: bool = True,
|
| 117 |
+
axis: AxisInt | None = None,
|
| 118 |
+
):
|
| 119 |
+
"""
|
| 120 |
+
Reduction for 1D masked array.
|
| 121 |
+
|
| 122 |
+
Parameters
|
| 123 |
+
----------
|
| 124 |
+
func : np.min or np.max
|
| 125 |
+
values : np.ndarray
|
| 126 |
+
Numpy array with the values (can be of any dtype that support the
|
| 127 |
+
operation).
|
| 128 |
+
mask : np.ndarray[bool]
|
| 129 |
+
Boolean numpy array (True values indicate missing values).
|
| 130 |
+
skipna : bool, default True
|
| 131 |
+
Whether to skip NA.
|
| 132 |
+
axis : int, optional, default None
|
| 133 |
+
"""
|
| 134 |
+
if not skipna:
|
| 135 |
+
if mask.any() or not values.size:
|
| 136 |
+
# min/max with empty array raise in numpy, pandas returns NA
|
| 137 |
+
return libmissing.NA
|
| 138 |
+
else:
|
| 139 |
+
return func(values, axis=axis)
|
| 140 |
+
else:
|
| 141 |
+
subset = values[~mask]
|
| 142 |
+
if subset.size:
|
| 143 |
+
return func(subset, axis=axis)
|
| 144 |
+
else:
|
| 145 |
+
# min/max with empty array raise in numpy, pandas returns NA
|
| 146 |
+
return libmissing.NA
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def min(
|
| 150 |
+
values: np.ndarray,
|
| 151 |
+
mask: npt.NDArray[np.bool_],
|
| 152 |
+
*,
|
| 153 |
+
skipna: bool = True,
|
| 154 |
+
axis: AxisInt | None = None,
|
| 155 |
+
):
|
| 156 |
+
return _minmax(np.min, values=values, mask=mask, skipna=skipna, axis=axis)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def max(
|
| 160 |
+
values: np.ndarray,
|
| 161 |
+
mask: npt.NDArray[np.bool_],
|
| 162 |
+
*,
|
| 163 |
+
skipna: bool = True,
|
| 164 |
+
axis: AxisInt | None = None,
|
| 165 |
+
):
|
| 166 |
+
return _minmax(np.max, values=values, mask=mask, skipna=skipna, axis=axis)
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
def mean(
|
| 170 |
+
values: np.ndarray,
|
| 171 |
+
mask: npt.NDArray[np.bool_],
|
| 172 |
+
*,
|
| 173 |
+
skipna: bool = True,
|
| 174 |
+
axis: AxisInt | None = None,
|
| 175 |
+
):
|
| 176 |
+
if not values.size or mask.all():
|
| 177 |
+
return libmissing.NA
|
| 178 |
+
return _reductions(np.mean, values=values, mask=mask, skipna=skipna, axis=axis)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
def var(
|
| 182 |
+
values: np.ndarray,
|
| 183 |
+
mask: npt.NDArray[np.bool_],
|
| 184 |
+
*,
|
| 185 |
+
skipna: bool = True,
|
| 186 |
+
axis: AxisInt | None = None,
|
| 187 |
+
ddof: int = 1,
|
| 188 |
+
):
|
| 189 |
+
if not values.size or mask.all():
|
| 190 |
+
return libmissing.NA
|
| 191 |
+
|
| 192 |
+
with warnings.catch_warnings():
|
| 193 |
+
warnings.simplefilter("ignore", RuntimeWarning)
|
| 194 |
+
return _reductions(
|
| 195 |
+
np.var, values=values, mask=mask, skipna=skipna, axis=axis, ddof=ddof
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def std(
|
| 200 |
+
values: np.ndarray,
|
| 201 |
+
mask: npt.NDArray[np.bool_],
|
| 202 |
+
*,
|
| 203 |
+
skipna: bool = True,
|
| 204 |
+
axis: AxisInt | None = None,
|
| 205 |
+
ddof: int = 1,
|
| 206 |
+
):
|
| 207 |
+
if not values.size or mask.all():
|
| 208 |
+
return libmissing.NA
|
| 209 |
+
|
| 210 |
+
with warnings.catch_warnings():
|
| 211 |
+
warnings.simplefilter("ignore", RuntimeWarning)
|
| 212 |
+
return _reductions(
|
| 213 |
+
np.std, values=values, mask=mask, skipna=skipna, axis=axis, ddof=ddof
|
| 214 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/array_algos/putmask.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
| 11 |
+
|
| 12 |
+
import numpy as np
|
| 13 |
+
|
| 14 |
+
from pandas._libs import lib
|
| 15 |
+
|
| 16 |
+
from pandas.core.dtypes.cast import infer_dtype_from
|
| 17 |
+
from pandas.core.dtypes.common import is_list_like
|
| 18 |
+
|
| 19 |
+
from pandas.core.arrays import ExtensionArray
|
| 20 |
+
|
| 21 |
+
if TYPE_CHECKING:
|
| 22 |
+
from pandas._typing import (
|
| 23 |
+
ArrayLike,
|
| 24 |
+
npt,
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
from pandas import MultiIndex
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def putmask_inplace(values: ArrayLike, mask: npt.NDArray[np.bool_], value: Any) -> None:
|
| 31 |
+
"""
|
| 32 |
+
ExtensionArray-compatible implementation of np.putmask. The main
|
| 33 |
+
difference is we do not handle repeating or truncating like numpy.
|
| 34 |
+
|
| 35 |
+
Parameters
|
| 36 |
+
----------
|
| 37 |
+
values: np.ndarray or ExtensionArray
|
| 38 |
+
mask : np.ndarray[bool]
|
| 39 |
+
We assume extract_bool_array has already been called.
|
| 40 |
+
value : Any
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
if (
|
| 44 |
+
not isinstance(values, np.ndarray)
|
| 45 |
+
or (values.dtype == object and not lib.is_scalar(value))
|
| 46 |
+
# GH#43424: np.putmask raises TypeError if we cannot cast between types with
|
| 47 |
+
# rule = "safe", a stricter guarantee we may not have here
|
| 48 |
+
or (
|
| 49 |
+
isinstance(value, np.ndarray) and not np.can_cast(value.dtype, values.dtype)
|
| 50 |
+
)
|
| 51 |
+
):
|
| 52 |
+
# GH#19266 using np.putmask gives unexpected results with listlike value
|
| 53 |
+
# along with object dtype
|
| 54 |
+
if is_list_like(value) and len(value) == len(values):
|
| 55 |
+
values[mask] = value[mask]
|
| 56 |
+
else:
|
| 57 |
+
values[mask] = value
|
| 58 |
+
else:
|
| 59 |
+
# GH#37833 np.putmask is more performant than __setitem__
|
| 60 |
+
np.putmask(values, mask, value)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def putmask_without_repeat(
|
| 64 |
+
values: np.ndarray, mask: npt.NDArray[np.bool_], new: Any
|
| 65 |
+
) -> None:
|
| 66 |
+
"""
|
| 67 |
+
np.putmask will truncate or repeat if `new` is a listlike with
|
| 68 |
+
len(new) != len(values). We require an exact match.
|
| 69 |
+
|
| 70 |
+
Parameters
|
| 71 |
+
----------
|
| 72 |
+
values : np.ndarray
|
| 73 |
+
mask : np.ndarray[bool]
|
| 74 |
+
new : Any
|
| 75 |
+
"""
|
| 76 |
+
if getattr(new, "ndim", 0) >= 1:
|
| 77 |
+
new = new.astype(values.dtype, copy=False)
|
| 78 |
+
|
| 79 |
+
# TODO: this prob needs some better checking for 2D cases
|
| 80 |
+
nlocs = mask.sum()
|
| 81 |
+
if nlocs > 0 and is_list_like(new) and getattr(new, "ndim", 1) == 1:
|
| 82 |
+
shape = np.shape(new)
|
| 83 |
+
# np.shape compat for if setitem_datetimelike_compat
|
| 84 |
+
# changed arraylike to list e.g. test_where_dt64_2d
|
| 85 |
+
if nlocs == shape[-1]:
|
| 86 |
+
# GH#30567
|
| 87 |
+
# If length of ``new`` is less than the length of ``values``,
|
| 88 |
+
# `np.putmask` would first repeat the ``new`` array and then
|
| 89 |
+
# assign the masked values hence produces incorrect result.
|
| 90 |
+
# `np.place` on the other hand uses the ``new`` values at it is
|
| 91 |
+
# to place in the masked locations of ``values``
|
| 92 |
+
np.place(values, mask, new)
|
| 93 |
+
# i.e. values[mask] = new
|
| 94 |
+
elif mask.shape[-1] == shape[-1] or shape[-1] == 1:
|
| 95 |
+
np.putmask(values, mask, new)
|
| 96 |
+
else:
|
| 97 |
+
raise ValueError("cannot assign mismatch length to masked array")
|
| 98 |
+
else:
|
| 99 |
+
np.putmask(values, mask, new)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def validate_putmask(
|
| 103 |
+
values: ArrayLike | MultiIndex, mask: np.ndarray
|
| 104 |
+
) -> tuple[npt.NDArray[np.bool_], bool]:
|
| 105 |
+
"""
|
| 106 |
+
Validate mask and check if this putmask operation is a no-op.
|
| 107 |
+
"""
|
| 108 |
+
mask = extract_bool_array(mask)
|
| 109 |
+
if mask.shape != values.shape:
|
| 110 |
+
raise ValueError("putmask: mask and data must be the same size")
|
| 111 |
+
|
| 112 |
+
noop = not mask.any()
|
| 113 |
+
return mask, noop
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def extract_bool_array(mask: ArrayLike) -> npt.NDArray[np.bool_]:
|
| 117 |
+
"""
|
| 118 |
+
If we have a SparseArray or BooleanArray, convert it to ndarray[bool].
|
| 119 |
+
"""
|
| 120 |
+
if isinstance(mask, ExtensionArray):
|
| 121 |
+
# We could have BooleanArray, Sparse[bool], ...
|
| 122 |
+
# Except for BooleanArray, this is equivalent to just
|
| 123 |
+
# np.asarray(mask, dtype=bool)
|
| 124 |
+
mask = mask.to_numpy(dtype=bool, na_value=False)
|
| 125 |
+
|
| 126 |
+
mask = np.asarray(mask, dtype=bool)
|
| 127 |
+
return mask
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def setitem_datetimelike_compat(values: np.ndarray, num_set: int, other):
|
| 131 |
+
"""
|
| 132 |
+
Parameters
|
| 133 |
+
----------
|
| 134 |
+
values : np.ndarray
|
| 135 |
+
num_set : int
|
| 136 |
+
For putmask, this is mask.sum()
|
| 137 |
+
other : Any
|
| 138 |
+
"""
|
| 139 |
+
if values.dtype == object:
|
| 140 |
+
dtype, _ = infer_dtype_from(other)
|
| 141 |
+
|
| 142 |
+
if lib.is_np_dtype(dtype, "mM"):
|
| 143 |
+
# https://github.com/numpy/numpy/issues/12550
|
| 144 |
+
# timedelta64 will incorrectly cast to int
|
| 145 |
+
if not is_list_like(other):
|
| 146 |
+
other = [other] * num_set
|
| 147 |
+
else:
|
| 148 |
+
other = list(other)
|
| 149 |
+
|
| 150 |
+
return other
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/array_algos/quant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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.full(len(qs), fill_value)
|
| 95 |
+
result = np.repeat(flat, len(values)).reshape(len(values), len(qs))
|
| 96 |
+
else:
|
| 97 |
+
result = _nanquantile(
|
| 98 |
+
values,
|
| 99 |
+
qs,
|
| 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 _nanquantile_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.quantile 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.quantile(
|
| 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 _nanquantile(
|
| 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.quantile 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 = _nanquantile(
|
| 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 |
+
_nanquantile_1d(val, m, qs, na_value, interpolation=interpolation)
|
| 200 |
+
for (val, m) in zip(list(values), list(mask), strict=True)
|
| 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.quantile(
|
| 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 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/array_algos/replace.py
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Methods used by Block.replace and related methods.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from __future__ import annotations
|
| 6 |
+
|
| 7 |
+
import operator
|
| 8 |
+
import re
|
| 9 |
+
from re import Pattern
|
| 10 |
+
from typing import (
|
| 11 |
+
TYPE_CHECKING,
|
| 12 |
+
Any,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
import numpy as np
|
| 16 |
+
|
| 17 |
+
from pandas.core.dtypes.common import (
|
| 18 |
+
is_bool,
|
| 19 |
+
is_re,
|
| 20 |
+
is_re_compilable,
|
| 21 |
+
)
|
| 22 |
+
from pandas.core.dtypes.missing import isna
|
| 23 |
+
|
| 24 |
+
if TYPE_CHECKING:
|
| 25 |
+
from pandas._typing import (
|
| 26 |
+
ArrayLike,
|
| 27 |
+
Scalar,
|
| 28 |
+
npt,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def should_use_regex(regex: bool, to_replace: Any) -> bool:
|
| 33 |
+
"""
|
| 34 |
+
Decide whether to treat `to_replace` as a regular expression.
|
| 35 |
+
"""
|
| 36 |
+
if is_re(to_replace):
|
| 37 |
+
regex = True
|
| 38 |
+
|
| 39 |
+
regex = regex and is_re_compilable(to_replace)
|
| 40 |
+
|
| 41 |
+
# Don't use regex if the pattern is empty.
|
| 42 |
+
regex = regex and re.compile(to_replace).pattern != ""
|
| 43 |
+
return regex
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def compare_or_regex_search(
|
| 47 |
+
a: ArrayLike, b: Scalar | Pattern, regex: bool, mask: npt.NDArray[np.bool_]
|
| 48 |
+
) -> ArrayLike:
|
| 49 |
+
"""
|
| 50 |
+
Compare two array-like inputs of the same shape or two scalar values
|
| 51 |
+
|
| 52 |
+
Calls operator.eq or re.search, depending on regex argument. If regex is
|
| 53 |
+
True, perform an element-wise regex matching.
|
| 54 |
+
|
| 55 |
+
Parameters
|
| 56 |
+
----------
|
| 57 |
+
a : array-like
|
| 58 |
+
b : scalar or regex pattern
|
| 59 |
+
regex : bool
|
| 60 |
+
mask : np.ndarray[bool]
|
| 61 |
+
|
| 62 |
+
Returns
|
| 63 |
+
-------
|
| 64 |
+
mask : array-like of bool
|
| 65 |
+
"""
|
| 66 |
+
if isna(b):
|
| 67 |
+
return ~mask
|
| 68 |
+
|
| 69 |
+
def _check_comparison_types(
|
| 70 |
+
result: ArrayLike | bool, a: ArrayLike, b: Scalar | Pattern
|
| 71 |
+
) -> None:
|
| 72 |
+
"""
|
| 73 |
+
Raises an error if the two arrays (a,b) cannot be compared.
|
| 74 |
+
Otherwise, returns the comparison result as expected.
|
| 75 |
+
"""
|
| 76 |
+
if is_bool(result) and isinstance(a, np.ndarray):
|
| 77 |
+
type_names = [type(a).__name__, type(b).__name__]
|
| 78 |
+
|
| 79 |
+
type_names[0] = f"ndarray(dtype={a.dtype})"
|
| 80 |
+
|
| 81 |
+
raise TypeError(
|
| 82 |
+
f"Cannot compare types {type_names[0]!r} and {type_names[1]!r}"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
if not regex or not should_use_regex(regex, b):
|
| 86 |
+
# TODO: should use missing.mask_missing?
|
| 87 |
+
op = lambda x: operator.eq(x, b)
|
| 88 |
+
else:
|
| 89 |
+
op = np.vectorize(
|
| 90 |
+
lambda x: (
|
| 91 |
+
bool(re.search(b, x))
|
| 92 |
+
if isinstance(x, str) and isinstance(b, (str, Pattern))
|
| 93 |
+
else False
|
| 94 |
+
),
|
| 95 |
+
otypes=[bool],
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
# GH#32621 use mask to avoid comparing to NAs
|
| 99 |
+
if isinstance(a, np.ndarray) and mask is not None:
|
| 100 |
+
a = a[mask]
|
| 101 |
+
result = op(a)
|
| 102 |
+
|
| 103 |
+
if isinstance(result, np.ndarray):
|
| 104 |
+
# The shape of the mask can differ to that of the result
|
| 105 |
+
# since we may compare only a subset of a's or b's elements
|
| 106 |
+
tmp = np.zeros(mask.shape, dtype=np.bool_)
|
| 107 |
+
np.place(tmp, mask, result)
|
| 108 |
+
result = tmp
|
| 109 |
+
else:
|
| 110 |
+
result = op(a)
|
| 111 |
+
|
| 112 |
+
_check_comparison_types(result, a, b)
|
| 113 |
+
return result
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def replace_regex(
|
| 117 |
+
values: ArrayLike, rx: re.Pattern, value, mask: npt.NDArray[np.bool_] | None
|
| 118 |
+
) -> None:
|
| 119 |
+
"""
|
| 120 |
+
Parameters
|
| 121 |
+
----------
|
| 122 |
+
values : ArrayLike
|
| 123 |
+
Object dtype.
|
| 124 |
+
rx : re.Pattern
|
| 125 |
+
value : Any
|
| 126 |
+
mask : np.ndarray[bool], optional
|
| 127 |
+
|
| 128 |
+
Notes
|
| 129 |
+
-----
|
| 130 |
+
Alters values in-place.
|
| 131 |
+
"""
|
| 132 |
+
|
| 133 |
+
# deal with replacing values with objects (strings) that match but
|
| 134 |
+
# whose replacement is not a string (numeric, nan, object)
|
| 135 |
+
if isna(value) or not isinstance(value, str):
|
| 136 |
+
|
| 137 |
+
def re_replacer(s):
|
| 138 |
+
if is_re(rx) and isinstance(s, str):
|
| 139 |
+
return value if rx.search(s) is not None else s
|
| 140 |
+
else:
|
| 141 |
+
return s
|
| 142 |
+
|
| 143 |
+
else:
|
| 144 |
+
# value is guaranteed to be a string here, s can be either a string
|
| 145 |
+
# or null if it's null it gets returned
|
| 146 |
+
def re_replacer(s):
|
| 147 |
+
if is_re(rx) and isinstance(s, str):
|
| 148 |
+
return rx.sub(value, s)
|
| 149 |
+
else:
|
| 150 |
+
return s
|
| 151 |
+
|
| 152 |
+
f = np.vectorize(re_replacer, otypes=[np.object_])
|
| 153 |
+
|
| 154 |
+
if mask is None:
|
| 155 |
+
values[:] = f(values)
|
| 156 |
+
else:
|
| 157 |
+
if values.ndim != mask.ndim:
|
| 158 |
+
mask = np.broadcast_to(mask, values.shape)
|
| 159 |
+
values[mask] = f(values[mask])
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/array_algos/take.py
ADDED
|
@@ -0,0 +1,546 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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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 |
+
@overload
|
| 48 |
+
def take_nd(
|
| 49 |
+
arr: ExtensionArray,
|
| 50 |
+
indexer,
|
| 51 |
+
axis: AxisInt = ...,
|
| 52 |
+
fill_value=...,
|
| 53 |
+
allow_fill: bool = ...,
|
| 54 |
+
) -> ArrayLike: ...
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def take_nd(
|
| 58 |
+
arr: ArrayLike,
|
| 59 |
+
indexer,
|
| 60 |
+
axis: AxisInt = 0,
|
| 61 |
+
fill_value=lib.no_default,
|
| 62 |
+
allow_fill: bool = True,
|
| 63 |
+
) -> ArrayLike:
|
| 64 |
+
"""
|
| 65 |
+
Specialized Cython take which sets NaN values in one pass
|
| 66 |
+
|
| 67 |
+
This dispatches to ``take`` defined on ExtensionArrays.
|
| 68 |
+
|
| 69 |
+
Note: this function assumes that the indexer is a valid(ated) indexer with
|
| 70 |
+
no out of bound indices.
|
| 71 |
+
|
| 72 |
+
Parameters
|
| 73 |
+
----------
|
| 74 |
+
arr : np.ndarray or ExtensionArray
|
| 75 |
+
Input array.
|
| 76 |
+
indexer : ndarray
|
| 77 |
+
1-D array of indices to take, subarrays corresponding to -1 value
|
| 78 |
+
indices are filed with fill_value
|
| 79 |
+
axis : int, default 0
|
| 80 |
+
Axis to take from
|
| 81 |
+
fill_value : any, default np.nan
|
| 82 |
+
Fill value to replace -1 values with
|
| 83 |
+
allow_fill : bool, default True
|
| 84 |
+
If False, indexer is assumed to contain no -1 values so no filling
|
| 85 |
+
will be done. This short-circuits computation of a mask. Result is
|
| 86 |
+
undefined if allow_fill == False and -1 is present in indexer.
|
| 87 |
+
|
| 88 |
+
Returns
|
| 89 |
+
-------
|
| 90 |
+
subarray : np.ndarray or ExtensionArray
|
| 91 |
+
May be the same type as the input, or cast to an ndarray.
|
| 92 |
+
"""
|
| 93 |
+
if fill_value is lib.no_default:
|
| 94 |
+
fill_value = na_value_for_dtype(arr.dtype, compat=False)
|
| 95 |
+
elif lib.is_np_dtype(arr.dtype, "mM"):
|
| 96 |
+
dtype, fill_value = maybe_promote(arr.dtype, fill_value)
|
| 97 |
+
if arr.dtype != dtype:
|
| 98 |
+
# EA.take is strict about returning a new object of the same type
|
| 99 |
+
# so for that case cast upfront
|
| 100 |
+
arr = arr.astype(dtype)
|
| 101 |
+
|
| 102 |
+
if not isinstance(arr, np.ndarray):
|
| 103 |
+
# i.e. ExtensionArray,
|
| 104 |
+
# includes for EA to catch DatetimeArray, TimedeltaArray
|
| 105 |
+
if not is_1d_only_ea_dtype(arr.dtype):
|
| 106 |
+
# i.e. DatetimeArray, TimedeltaArray
|
| 107 |
+
arr = cast("NDArrayBackedExtensionArray", arr)
|
| 108 |
+
return arr.take(
|
| 109 |
+
indexer, fill_value=fill_value, allow_fill=allow_fill, axis=axis
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill)
|
| 113 |
+
|
| 114 |
+
arr = np.asarray(arr)
|
| 115 |
+
return _take_nd_ndarray(arr, indexer, axis, fill_value, allow_fill)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def _take_nd_ndarray(
|
| 119 |
+
arr: np.ndarray,
|
| 120 |
+
indexer: npt.NDArray[np.intp] | None,
|
| 121 |
+
axis: AxisInt,
|
| 122 |
+
fill_value,
|
| 123 |
+
allow_fill: bool,
|
| 124 |
+
) -> np.ndarray:
|
| 125 |
+
if indexer is None:
|
| 126 |
+
indexer = np.arange(arr.shape[axis], dtype=np.intp)
|
| 127 |
+
dtype, fill_value = arr.dtype, arr.dtype.type()
|
| 128 |
+
else:
|
| 129 |
+
indexer = ensure_platform_int(indexer)
|
| 130 |
+
|
| 131 |
+
dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
|
| 132 |
+
arr, indexer, fill_value, allow_fill
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
flip_order = False
|
| 136 |
+
if arr.ndim == 2 and arr.flags.f_contiguous:
|
| 137 |
+
flip_order = True
|
| 138 |
+
|
| 139 |
+
if flip_order:
|
| 140 |
+
arr = arr.T
|
| 141 |
+
axis = arr.ndim - axis - 1
|
| 142 |
+
|
| 143 |
+
# at this point, it's guaranteed that dtype can hold both the arr values
|
| 144 |
+
# and the fill_value
|
| 145 |
+
out_shape_ = list(arr.shape)
|
| 146 |
+
out_shape_[axis] = len(indexer)
|
| 147 |
+
out_shape = tuple(out_shape_)
|
| 148 |
+
if arr.flags.f_contiguous and axis == arr.ndim - 1:
|
| 149 |
+
# minor tweak that can make an order-of-magnitude difference
|
| 150 |
+
# for dataframes initialized directly from 2-d ndarrays
|
| 151 |
+
# (s.t. df.values is c-contiguous and df._mgr.blocks[0] is its
|
| 152 |
+
# f-contiguous transpose)
|
| 153 |
+
out = np.empty(out_shape, dtype=dtype, order="F")
|
| 154 |
+
else:
|
| 155 |
+
out = np.empty(out_shape, dtype=dtype)
|
| 156 |
+
|
| 157 |
+
func = _get_take_nd_function(
|
| 158 |
+
arr.ndim, arr.dtype, out.dtype, axis=axis, mask_info=mask_info
|
| 159 |
+
)
|
| 160 |
+
func(arr, indexer, out, fill_value)
|
| 161 |
+
|
| 162 |
+
if flip_order:
|
| 163 |
+
out = out.T
|
| 164 |
+
return out
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
def take_2d_multi(
|
| 168 |
+
arr: np.ndarray,
|
| 169 |
+
indexer: tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]],
|
| 170 |
+
fill_value=np.nan,
|
| 171 |
+
) -> np.ndarray:
|
| 172 |
+
"""
|
| 173 |
+
Specialized Cython take which sets NaN values in one pass.
|
| 174 |
+
"""
|
| 175 |
+
# This is only called from one place in DataFrame._reindex_multi,
|
| 176 |
+
# so we know indexer is well-behaved.
|
| 177 |
+
assert indexer is not None
|
| 178 |
+
assert indexer[0] is not None
|
| 179 |
+
assert indexer[1] is not None
|
| 180 |
+
|
| 181 |
+
row_idx, col_idx = indexer
|
| 182 |
+
|
| 183 |
+
row_idx = ensure_platform_int(row_idx)
|
| 184 |
+
col_idx = ensure_platform_int(col_idx)
|
| 185 |
+
indexer = row_idx, col_idx
|
| 186 |
+
mask_info = None
|
| 187 |
+
|
| 188 |
+
# check for promotion based on types only (do this first because
|
| 189 |
+
# it's faster than computing a mask)
|
| 190 |
+
dtype, fill_value = maybe_promote(arr.dtype, fill_value)
|
| 191 |
+
if dtype != arr.dtype:
|
| 192 |
+
# check if promotion is actually required based on indexer
|
| 193 |
+
row_mask = row_idx == -1
|
| 194 |
+
col_mask = col_idx == -1
|
| 195 |
+
row_needs = row_mask.any()
|
| 196 |
+
col_needs = col_mask.any()
|
| 197 |
+
mask_info = (row_mask, col_mask), (row_needs, col_needs)
|
| 198 |
+
|
| 199 |
+
if not (row_needs or col_needs):
|
| 200 |
+
# if not, then depromote, set fill_value to dummy
|
| 201 |
+
# (it won't be used but we don't want the cython code
|
| 202 |
+
# to crash when trying to cast it to dtype)
|
| 203 |
+
dtype, fill_value = arr.dtype, arr.dtype.type()
|
| 204 |
+
|
| 205 |
+
# at this point, it's guaranteed that dtype can hold both the arr values
|
| 206 |
+
# and the fill_value
|
| 207 |
+
out_shape = len(row_idx), len(col_idx)
|
| 208 |
+
out = np.empty(out_shape, dtype=dtype)
|
| 209 |
+
|
| 210 |
+
func = _take_2d_multi_dict.get((arr.dtype.name, out.dtype.name), None)
|
| 211 |
+
if func is None and arr.dtype != out.dtype:
|
| 212 |
+
func = _take_2d_multi_dict.get((out.dtype.name, out.dtype.name), None)
|
| 213 |
+
if func is not None:
|
| 214 |
+
func = _convert_wrapper(func, out.dtype)
|
| 215 |
+
|
| 216 |
+
if func is not None:
|
| 217 |
+
func(arr, indexer, out=out, fill_value=fill_value)
|
| 218 |
+
else:
|
| 219 |
+
# test_reindex_multi
|
| 220 |
+
_take_2d_multi_object(
|
| 221 |
+
arr, indexer, out, fill_value=fill_value, mask_info=mask_info
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
return out
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
@functools.lru_cache
|
| 228 |
+
def _get_take_nd_function_cached(
|
| 229 |
+
ndim: int, arr_dtype: np.dtype, out_dtype: np.dtype, axis: AxisInt
|
| 230 |
+
):
|
| 231 |
+
"""
|
| 232 |
+
Part of _get_take_nd_function below that doesn't need `mask_info` and thus
|
| 233 |
+
can be cached (mask_info potentially contains a numpy ndarray which is not
|
| 234 |
+
hashable and thus cannot be used as argument for cached function).
|
| 235 |
+
"""
|
| 236 |
+
tup = (arr_dtype.name, out_dtype.name)
|
| 237 |
+
if ndim == 1:
|
| 238 |
+
func = _take_1d_dict.get(tup, None)
|
| 239 |
+
elif ndim == 2:
|
| 240 |
+
if axis == 0:
|
| 241 |
+
func = _take_2d_axis0_dict.get(tup, None)
|
| 242 |
+
else:
|
| 243 |
+
func = _take_2d_axis1_dict.get(tup, None)
|
| 244 |
+
if func is not None:
|
| 245 |
+
return func
|
| 246 |
+
|
| 247 |
+
# We get here with string, uint, float16, and complex dtypes that could
|
| 248 |
+
# potentially be handled in algos_take_helper.
|
| 249 |
+
# Also a couple with (M8[ns], object) and (m8[ns], object)
|
| 250 |
+
tup = (out_dtype.name, out_dtype.name)
|
| 251 |
+
if ndim == 1:
|
| 252 |
+
func = _take_1d_dict.get(tup, None)
|
| 253 |
+
elif ndim == 2:
|
| 254 |
+
if axis == 0:
|
| 255 |
+
func = _take_2d_axis0_dict.get(tup, None)
|
| 256 |
+
else:
|
| 257 |
+
func = _take_2d_axis1_dict.get(tup, None)
|
| 258 |
+
if func is not None:
|
| 259 |
+
func = _convert_wrapper(func, out_dtype)
|
| 260 |
+
return func
|
| 261 |
+
|
| 262 |
+
return None
|
| 263 |
+
|
| 264 |
+
|
| 265 |
+
def _get_take_nd_function(
|
| 266 |
+
ndim: int,
|
| 267 |
+
arr_dtype: np.dtype,
|
| 268 |
+
out_dtype: np.dtype,
|
| 269 |
+
axis: AxisInt = 0,
|
| 270 |
+
mask_info=None,
|
| 271 |
+
):
|
| 272 |
+
"""
|
| 273 |
+
Get the appropriate "take" implementation for the given dimension, axis
|
| 274 |
+
and dtypes.
|
| 275 |
+
"""
|
| 276 |
+
func = None
|
| 277 |
+
if ndim <= 2:
|
| 278 |
+
# for this part we don't need `mask_info` -> use the cached algo lookup
|
| 279 |
+
func = _get_take_nd_function_cached(ndim, arr_dtype, out_dtype, axis)
|
| 280 |
+
|
| 281 |
+
if func is None:
|
| 282 |
+
|
| 283 |
+
def func(arr, indexer, out, fill_value=np.nan) -> None:
|
| 284 |
+
indexer = ensure_platform_int(indexer)
|
| 285 |
+
_take_nd_object(
|
| 286 |
+
arr, indexer, out, axis=axis, fill_value=fill_value, mask_info=mask_info
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
return func
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
def _view_wrapper(f, arr_dtype=None, out_dtype=None, fill_wrap=None):
|
| 293 |
+
def wrapper(
|
| 294 |
+
arr: np.ndarray, indexer: np.ndarray, out: np.ndarray, fill_value=np.nan
|
| 295 |
+
) -> None:
|
| 296 |
+
if arr_dtype is not None:
|
| 297 |
+
arr = arr.view(arr_dtype)
|
| 298 |
+
if out_dtype is not None:
|
| 299 |
+
out = out.view(out_dtype)
|
| 300 |
+
if fill_wrap is not None:
|
| 301 |
+
# FIXME: if we get here with dt64/td64 we need to be sure we have
|
| 302 |
+
# matching resos
|
| 303 |
+
if fill_value.dtype.kind == "m":
|
| 304 |
+
fill_value = fill_value.astype("m8[ns]")
|
| 305 |
+
else:
|
| 306 |
+
fill_value = fill_value.astype("M8[ns]")
|
| 307 |
+
fill_value = fill_wrap(fill_value)
|
| 308 |
+
|
| 309 |
+
f(arr, indexer, out, fill_value=fill_value)
|
| 310 |
+
|
| 311 |
+
return wrapper
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
def _convert_wrapper(f, conv_dtype):
|
| 315 |
+
def wrapper(
|
| 316 |
+
arr: np.ndarray, indexer: np.ndarray, out: np.ndarray, fill_value=np.nan
|
| 317 |
+
) -> None:
|
| 318 |
+
if conv_dtype == object:
|
| 319 |
+
# GH#39755 avoid casting dt64/td64 to integers
|
| 320 |
+
arr = ensure_wrapped_if_datetimelike(arr)
|
| 321 |
+
arr = arr.astype(conv_dtype)
|
| 322 |
+
f(arr, indexer, out, fill_value=fill_value)
|
| 323 |
+
|
| 324 |
+
return wrapper
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
_take_1d_dict = {
|
| 328 |
+
("int8", "int8"): libalgos.take_1d_int8_int8,
|
| 329 |
+
("int8", "int32"): libalgos.take_1d_int8_int32,
|
| 330 |
+
("int8", "int64"): libalgos.take_1d_int8_int64,
|
| 331 |
+
("int8", "float64"): libalgos.take_1d_int8_float64,
|
| 332 |
+
("int16", "int16"): libalgos.take_1d_int16_int16,
|
| 333 |
+
("int16", "int32"): libalgos.take_1d_int16_int32,
|
| 334 |
+
("int16", "int64"): libalgos.take_1d_int16_int64,
|
| 335 |
+
("int16", "float64"): libalgos.take_1d_int16_float64,
|
| 336 |
+
("int32", "int32"): libalgos.take_1d_int32_int32,
|
| 337 |
+
("int32", "int64"): libalgos.take_1d_int32_int64,
|
| 338 |
+
("int32", "float64"): libalgos.take_1d_int32_float64,
|
| 339 |
+
("int64", "int64"): libalgos.take_1d_int64_int64,
|
| 340 |
+
("uint8", "uint8"): libalgos.take_1d_bool_bool,
|
| 341 |
+
("uint16", "int64"): libalgos.take_1d_uint16_uint16,
|
| 342 |
+
("uint32", "int64"): libalgos.take_1d_uint32_uint32,
|
| 343 |
+
("uint64", "int64"): libalgos.take_1d_uint64_uint64,
|
| 344 |
+
("int64", "float64"): libalgos.take_1d_int64_float64,
|
| 345 |
+
("float32", "float32"): libalgos.take_1d_float32_float32,
|
| 346 |
+
("float32", "float64"): libalgos.take_1d_float32_float64,
|
| 347 |
+
("float64", "float64"): libalgos.take_1d_float64_float64,
|
| 348 |
+
("object", "object"): libalgos.take_1d_object_object,
|
| 349 |
+
("bool", "bool"): _view_wrapper(libalgos.take_1d_bool_bool, np.uint8, np.uint8),
|
| 350 |
+
("bool", "object"): _view_wrapper(libalgos.take_1d_bool_object, np.uint8, None),
|
| 351 |
+
("datetime64[ns]", "datetime64[ns]"): _view_wrapper(
|
| 352 |
+
libalgos.take_1d_int64_int64, np.int64, np.int64, np.int64
|
| 353 |
+
),
|
| 354 |
+
("timedelta64[ns]", "timedelta64[ns]"): _view_wrapper(
|
| 355 |
+
libalgos.take_1d_int64_int64, np.int64, np.int64, np.int64
|
| 356 |
+
),
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
_take_2d_axis0_dict = {
|
| 360 |
+
("int8", "int8"): libalgos.take_2d_axis0_int8_int8,
|
| 361 |
+
("int8", "int32"): libalgos.take_2d_axis0_int8_int32,
|
| 362 |
+
("int8", "int64"): libalgos.take_2d_axis0_int8_int64,
|
| 363 |
+
("int8", "float64"): libalgos.take_2d_axis0_int8_float64,
|
| 364 |
+
("int16", "int16"): libalgos.take_2d_axis0_int16_int16,
|
| 365 |
+
("int16", "int32"): libalgos.take_2d_axis0_int16_int32,
|
| 366 |
+
("int16", "int64"): libalgos.take_2d_axis0_int16_int64,
|
| 367 |
+
("int16", "float64"): libalgos.take_2d_axis0_int16_float64,
|
| 368 |
+
("int32", "int32"): libalgos.take_2d_axis0_int32_int32,
|
| 369 |
+
("int32", "int64"): libalgos.take_2d_axis0_int32_int64,
|
| 370 |
+
("int32", "float64"): libalgos.take_2d_axis0_int32_float64,
|
| 371 |
+
("int64", "int64"): libalgos.take_2d_axis0_int64_int64,
|
| 372 |
+
("int64", "float64"): libalgos.take_2d_axis0_int64_float64,
|
| 373 |
+
("uint8", "uint8"): libalgos.take_2d_axis0_bool_bool,
|
| 374 |
+
("uint16", "uint16"): libalgos.take_2d_axis0_uint16_uint16,
|
| 375 |
+
("uint32", "uint32"): libalgos.take_2d_axis0_uint32_uint32,
|
| 376 |
+
("uint64", "uint64"): libalgos.take_2d_axis0_uint64_uint64,
|
| 377 |
+
("float32", "float32"): libalgos.take_2d_axis0_float32_float32,
|
| 378 |
+
("float32", "float64"): libalgos.take_2d_axis0_float32_float64,
|
| 379 |
+
("float64", "float64"): libalgos.take_2d_axis0_float64_float64,
|
| 380 |
+
("object", "object"): libalgos.take_2d_axis0_object_object,
|
| 381 |
+
("bool", "bool"): _view_wrapper(
|
| 382 |
+
libalgos.take_2d_axis0_bool_bool, np.uint8, np.uint8
|
| 383 |
+
),
|
| 384 |
+
("bool", "object"): _view_wrapper(
|
| 385 |
+
libalgos.take_2d_axis0_bool_object, np.uint8, None
|
| 386 |
+
),
|
| 387 |
+
("datetime64[ns]", "datetime64[ns]"): _view_wrapper(
|
| 388 |
+
libalgos.take_2d_axis0_int64_int64, np.int64, np.int64, fill_wrap=np.int64
|
| 389 |
+
),
|
| 390 |
+
("timedelta64[ns]", "timedelta64[ns]"): _view_wrapper(
|
| 391 |
+
libalgos.take_2d_axis0_int64_int64, np.int64, np.int64, fill_wrap=np.int64
|
| 392 |
+
),
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
_take_2d_axis1_dict = {
|
| 396 |
+
("int8", "int8"): libalgos.take_2d_axis1_int8_int8,
|
| 397 |
+
("int8", "int32"): libalgos.take_2d_axis1_int8_int32,
|
| 398 |
+
("int8", "int64"): libalgos.take_2d_axis1_int8_int64,
|
| 399 |
+
("int8", "float64"): libalgos.take_2d_axis1_int8_float64,
|
| 400 |
+
("int16", "int16"): libalgos.take_2d_axis1_int16_int16,
|
| 401 |
+
("int16", "int32"): libalgos.take_2d_axis1_int16_int32,
|
| 402 |
+
("int16", "int64"): libalgos.take_2d_axis1_int16_int64,
|
| 403 |
+
("int16", "float64"): libalgos.take_2d_axis1_int16_float64,
|
| 404 |
+
("int32", "int32"): libalgos.take_2d_axis1_int32_int32,
|
| 405 |
+
("int32", "int64"): libalgos.take_2d_axis1_int32_int64,
|
| 406 |
+
("int32", "float64"): libalgos.take_2d_axis1_int32_float64,
|
| 407 |
+
("int64", "int64"): libalgos.take_2d_axis1_int64_int64,
|
| 408 |
+
("int64", "float64"): libalgos.take_2d_axis1_int64_float64,
|
| 409 |
+
("uint8", "uint8"): libalgos.take_2d_axis1_bool_bool,
|
| 410 |
+
("uint16", "uint16"): libalgos.take_2d_axis1_uint16_uint16,
|
| 411 |
+
("uint32", "uint32"): libalgos.take_2d_axis1_uint32_uint32,
|
| 412 |
+
("uint64", "uint64"): libalgos.take_2d_axis1_uint64_uint64,
|
| 413 |
+
("float32", "float32"): libalgos.take_2d_axis1_float32_float32,
|
| 414 |
+
("float32", "float64"): libalgos.take_2d_axis1_float32_float64,
|
| 415 |
+
("float64", "float64"): libalgos.take_2d_axis1_float64_float64,
|
| 416 |
+
("object", "object"): libalgos.take_2d_axis1_object_object,
|
| 417 |
+
("bool", "bool"): _view_wrapper(
|
| 418 |
+
libalgos.take_2d_axis1_bool_bool, np.uint8, np.uint8
|
| 419 |
+
),
|
| 420 |
+
("bool", "object"): _view_wrapper(
|
| 421 |
+
libalgos.take_2d_axis1_bool_object, np.uint8, None
|
| 422 |
+
),
|
| 423 |
+
("datetime64[ns]", "datetime64[ns]"): _view_wrapper(
|
| 424 |
+
libalgos.take_2d_axis1_int64_int64, np.int64, np.int64, fill_wrap=np.int64
|
| 425 |
+
),
|
| 426 |
+
("timedelta64[ns]", "timedelta64[ns]"): _view_wrapper(
|
| 427 |
+
libalgos.take_2d_axis1_int64_int64, np.int64, np.int64, fill_wrap=np.int64
|
| 428 |
+
),
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
_take_2d_multi_dict = {
|
| 432 |
+
("int8", "int8"): libalgos.take_2d_multi_int8_int8,
|
| 433 |
+
("int8", "int32"): libalgos.take_2d_multi_int8_int32,
|
| 434 |
+
("int8", "int64"): libalgos.take_2d_multi_int8_int64,
|
| 435 |
+
("int8", "float64"): libalgos.take_2d_multi_int8_float64,
|
| 436 |
+
("int16", "int16"): libalgos.take_2d_multi_int16_int16,
|
| 437 |
+
("int16", "int32"): libalgos.take_2d_multi_int16_int32,
|
| 438 |
+
("int16", "int64"): libalgos.take_2d_multi_int16_int64,
|
| 439 |
+
("int16", "float64"): libalgos.take_2d_multi_int16_float64,
|
| 440 |
+
("int32", "int32"): libalgos.take_2d_multi_int32_int32,
|
| 441 |
+
("int32", "int64"): libalgos.take_2d_multi_int32_int64,
|
| 442 |
+
("int32", "float64"): libalgos.take_2d_multi_int32_float64,
|
| 443 |
+
("int64", "int64"): libalgos.take_2d_multi_int64_int64,
|
| 444 |
+
("int64", "float64"): libalgos.take_2d_multi_int64_float64,
|
| 445 |
+
("float32", "float32"): libalgos.take_2d_multi_float32_float32,
|
| 446 |
+
("float32", "float64"): libalgos.take_2d_multi_float32_float64,
|
| 447 |
+
("float64", "float64"): libalgos.take_2d_multi_float64_float64,
|
| 448 |
+
("object", "object"): libalgos.take_2d_multi_object_object,
|
| 449 |
+
("bool", "bool"): _view_wrapper(
|
| 450 |
+
libalgos.take_2d_multi_bool_bool, np.uint8, np.uint8
|
| 451 |
+
),
|
| 452 |
+
("bool", "object"): _view_wrapper(
|
| 453 |
+
libalgos.take_2d_multi_bool_object, np.uint8, None
|
| 454 |
+
),
|
| 455 |
+
("datetime64[ns]", "datetime64[ns]"): _view_wrapper(
|
| 456 |
+
libalgos.take_2d_multi_int64_int64, np.int64, np.int64, fill_wrap=np.int64
|
| 457 |
+
),
|
| 458 |
+
("timedelta64[ns]", "timedelta64[ns]"): _view_wrapper(
|
| 459 |
+
libalgos.take_2d_multi_int64_int64, np.int64, np.int64, fill_wrap=np.int64
|
| 460 |
+
),
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
|
| 464 |
+
def _take_nd_object(
|
| 465 |
+
arr: np.ndarray,
|
| 466 |
+
indexer: npt.NDArray[np.intp],
|
| 467 |
+
out: np.ndarray,
|
| 468 |
+
axis: AxisInt,
|
| 469 |
+
fill_value,
|
| 470 |
+
mask_info,
|
| 471 |
+
) -> None:
|
| 472 |
+
if mask_info is not None:
|
| 473 |
+
mask, needs_masking = mask_info
|
| 474 |
+
else:
|
| 475 |
+
mask = indexer == -1
|
| 476 |
+
needs_masking = mask.any()
|
| 477 |
+
if arr.dtype != out.dtype:
|
| 478 |
+
arr = arr.astype(out.dtype)
|
| 479 |
+
if arr.shape[axis] > 0:
|
| 480 |
+
arr.take(indexer, axis=axis, out=out)
|
| 481 |
+
if needs_masking:
|
| 482 |
+
outindexer = [slice(None)] * arr.ndim
|
| 483 |
+
outindexer[axis] = mask
|
| 484 |
+
out[tuple(outindexer)] = fill_value
|
| 485 |
+
|
| 486 |
+
|
| 487 |
+
def _take_2d_multi_object(
|
| 488 |
+
arr: np.ndarray,
|
| 489 |
+
indexer: tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]],
|
| 490 |
+
out: np.ndarray,
|
| 491 |
+
fill_value,
|
| 492 |
+
mask_info,
|
| 493 |
+
) -> None:
|
| 494 |
+
# this is not ideal, performance-wise, but it's better than raising
|
| 495 |
+
# an exception (best to optimize in Cython to avoid getting here)
|
| 496 |
+
row_idx, col_idx = indexer # both np.intp
|
| 497 |
+
if mask_info is not None:
|
| 498 |
+
(row_mask, col_mask), (row_needs, col_needs) = mask_info
|
| 499 |
+
else:
|
| 500 |
+
row_mask = row_idx == -1
|
| 501 |
+
col_mask = col_idx == -1
|
| 502 |
+
row_needs = row_mask.any()
|
| 503 |
+
col_needs = col_mask.any()
|
| 504 |
+
if fill_value is not None:
|
| 505 |
+
if row_needs:
|
| 506 |
+
out[row_mask, :] = fill_value
|
| 507 |
+
if col_needs:
|
| 508 |
+
out[:, col_mask] = fill_value
|
| 509 |
+
for i, u_ in enumerate(row_idx):
|
| 510 |
+
if u_ != -1:
|
| 511 |
+
for j, v in enumerate(col_idx):
|
| 512 |
+
if v != -1:
|
| 513 |
+
out[i, j] = arr[u_, v]
|
| 514 |
+
|
| 515 |
+
|
| 516 |
+
def _take_preprocess_indexer_and_fill_value(
|
| 517 |
+
arr: np.ndarray,
|
| 518 |
+
indexer: npt.NDArray[np.intp],
|
| 519 |
+
fill_value,
|
| 520 |
+
allow_fill: bool,
|
| 521 |
+
mask: npt.NDArray[np.bool_] | None = None,
|
| 522 |
+
):
|
| 523 |
+
mask_info: tuple[np.ndarray | None, bool] | None = None
|
| 524 |
+
|
| 525 |
+
if not allow_fill:
|
| 526 |
+
dtype, fill_value = arr.dtype, arr.dtype.type()
|
| 527 |
+
mask_info = None, False
|
| 528 |
+
else:
|
| 529 |
+
# check for promotion based on types only (do this first because
|
| 530 |
+
# it's faster than computing a mask)
|
| 531 |
+
dtype, fill_value = maybe_promote(arr.dtype, fill_value)
|
| 532 |
+
if dtype != arr.dtype:
|
| 533 |
+
# check if promotion is actually required based on indexer
|
| 534 |
+
if mask is not None:
|
| 535 |
+
needs_masking = True
|
| 536 |
+
else:
|
| 537 |
+
mask = indexer == -1
|
| 538 |
+
needs_masking = bool(mask.any())
|
| 539 |
+
mask_info = mask, needs_masking
|
| 540 |
+
if not needs_masking:
|
| 541 |
+
# if not, then depromote, set fill_value to dummy
|
| 542 |
+
# (it won't be used but we don't want the cython code
|
| 543 |
+
# to crash when trying to cast it to dtype)
|
| 544 |
+
dtype, fill_value = arr.dtype, arr.dtype.type()
|
| 545 |
+
|
| 546 |
+
return dtype, fill_value, mask_info
|
loan-advisor-agent/.venv/Lib/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
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arraylike.py
ADDED
|
@@ -0,0 +1,534 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
+
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import operator
|
| 11 |
+
from typing import Any
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
from pandas._libs import lib
|
| 16 |
+
from pandas._libs.ops_dispatch import maybe_dispatch_ufunc_to_dunder_op
|
| 17 |
+
|
| 18 |
+
from pandas.core.dtypes.cast import maybe_unbox_numpy_scalar
|
| 19 |
+
from pandas.core.dtypes.generic import ABCNDFrame
|
| 20 |
+
|
| 21 |
+
from pandas.core import roperator
|
| 22 |
+
from pandas.core.construction import extract_array
|
| 23 |
+
from pandas.core.ops.common import unpack_zerodim_and_defer
|
| 24 |
+
|
| 25 |
+
REDUCTION_ALIASES = {
|
| 26 |
+
"maximum": "max",
|
| 27 |
+
"minimum": "min",
|
| 28 |
+
"add": "sum",
|
| 29 |
+
"multiply": "prod",
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class OpsMixin:
|
| 34 |
+
# -------------------------------------------------------------
|
| 35 |
+
# Comparisons
|
| 36 |
+
|
| 37 |
+
def _cmp_method(self, other, op):
|
| 38 |
+
return NotImplemented
|
| 39 |
+
|
| 40 |
+
@unpack_zerodim_and_defer("__eq__")
|
| 41 |
+
def __eq__(self, other):
|
| 42 |
+
return self._cmp_method(other, operator.eq)
|
| 43 |
+
|
| 44 |
+
@unpack_zerodim_and_defer("__ne__")
|
| 45 |
+
def __ne__(self, other):
|
| 46 |
+
return self._cmp_method(other, operator.ne)
|
| 47 |
+
|
| 48 |
+
@unpack_zerodim_and_defer("__lt__")
|
| 49 |
+
def __lt__(self, other):
|
| 50 |
+
return self._cmp_method(other, operator.lt)
|
| 51 |
+
|
| 52 |
+
@unpack_zerodim_and_defer("__le__")
|
| 53 |
+
def __le__(self, other):
|
| 54 |
+
return self._cmp_method(other, operator.le)
|
| 55 |
+
|
| 56 |
+
@unpack_zerodim_and_defer("__gt__")
|
| 57 |
+
def __gt__(self, other):
|
| 58 |
+
return self._cmp_method(other, operator.gt)
|
| 59 |
+
|
| 60 |
+
@unpack_zerodim_and_defer("__ge__")
|
| 61 |
+
def __ge__(self, other):
|
| 62 |
+
return self._cmp_method(other, operator.ge)
|
| 63 |
+
|
| 64 |
+
# -------------------------------------------------------------
|
| 65 |
+
# Logical Methods
|
| 66 |
+
|
| 67 |
+
def _logical_method(self, other, op):
|
| 68 |
+
return NotImplemented
|
| 69 |
+
|
| 70 |
+
@unpack_zerodim_and_defer("__and__")
|
| 71 |
+
def __and__(self, other):
|
| 72 |
+
return self._logical_method(other, operator.and_)
|
| 73 |
+
|
| 74 |
+
@unpack_zerodim_and_defer("__rand__")
|
| 75 |
+
def __rand__(self, other):
|
| 76 |
+
return self._logical_method(other, roperator.rand_)
|
| 77 |
+
|
| 78 |
+
@unpack_zerodim_and_defer("__or__")
|
| 79 |
+
def __or__(self, other):
|
| 80 |
+
return self._logical_method(other, operator.or_)
|
| 81 |
+
|
| 82 |
+
@unpack_zerodim_and_defer("__ror__")
|
| 83 |
+
def __ror__(self, other):
|
| 84 |
+
return self._logical_method(other, roperator.ror_)
|
| 85 |
+
|
| 86 |
+
@unpack_zerodim_and_defer("__xor__")
|
| 87 |
+
def __xor__(self, other):
|
| 88 |
+
return self._logical_method(other, operator.xor)
|
| 89 |
+
|
| 90 |
+
@unpack_zerodim_and_defer("__rxor__")
|
| 91 |
+
def __rxor__(self, other):
|
| 92 |
+
return self._logical_method(other, roperator.rxor)
|
| 93 |
+
|
| 94 |
+
# -------------------------------------------------------------
|
| 95 |
+
# Arithmetic Methods
|
| 96 |
+
|
| 97 |
+
def _arith_method(self, other, op):
|
| 98 |
+
return NotImplemented
|
| 99 |
+
|
| 100 |
+
@unpack_zerodim_and_defer("__add__")
|
| 101 |
+
def __add__(self, other):
|
| 102 |
+
"""
|
| 103 |
+
Get Addition of DataFrame and other, column-wise.
|
| 104 |
+
|
| 105 |
+
Equivalent to ``DataFrame.add(other)``.
|
| 106 |
+
|
| 107 |
+
Parameters
|
| 108 |
+
----------
|
| 109 |
+
other : scalar, sequence, Series, dict or DataFrame
|
| 110 |
+
Object to be added to the DataFrame.
|
| 111 |
+
|
| 112 |
+
Returns
|
| 113 |
+
-------
|
| 114 |
+
DataFrame
|
| 115 |
+
The result of adding ``other`` to DataFrame.
|
| 116 |
+
|
| 117 |
+
See Also
|
| 118 |
+
--------
|
| 119 |
+
DataFrame.add : Add a DataFrame and another object, with option for index-
|
| 120 |
+
or column-oriented addition.
|
| 121 |
+
|
| 122 |
+
Examples
|
| 123 |
+
--------
|
| 124 |
+
>>> df = pd.DataFrame(
|
| 125 |
+
... {"height": [1.5, 2.6], "weight": [500, 800]}, index=["elk", "moose"]
|
| 126 |
+
... )
|
| 127 |
+
>>> df
|
| 128 |
+
height weight
|
| 129 |
+
elk 1.5 500
|
| 130 |
+
moose 2.6 800
|
| 131 |
+
|
| 132 |
+
Adding a scalar affects all rows and columns.
|
| 133 |
+
|
| 134 |
+
>>> df[["height", "weight"]] + 1.5
|
| 135 |
+
height weight
|
| 136 |
+
elk 3.0 501.5
|
| 137 |
+
moose 4.1 801.5
|
| 138 |
+
|
| 139 |
+
Each element of a list is added to a column of the DataFrame, in order.
|
| 140 |
+
|
| 141 |
+
>>> df[["height", "weight"]] + [0.5, 1.5]
|
| 142 |
+
height weight
|
| 143 |
+
elk 2.0 501.5
|
| 144 |
+
moose 3.1 801.5
|
| 145 |
+
|
| 146 |
+
Keys of a dictionary are aligned to the DataFrame, based on column names;
|
| 147 |
+
each value in the dictionary is added to the corresponding column.
|
| 148 |
+
|
| 149 |
+
>>> df[["height", "weight"]] + {"height": 0.5, "weight": 1.5}
|
| 150 |
+
height weight
|
| 151 |
+
elk 2.0 501.5
|
| 152 |
+
moose 3.1 801.5
|
| 153 |
+
|
| 154 |
+
When `other` is a :class:`Series`, the index of `other` is aligned with the
|
| 155 |
+
columns of the DataFrame.
|
| 156 |
+
|
| 157 |
+
>>> s1 = pd.Series([0.5, 1.5], index=["weight", "height"])
|
| 158 |
+
>>> df[["height", "weight"]] + s1
|
| 159 |
+
height weight
|
| 160 |
+
elk 3.0 500.5
|
| 161 |
+
moose 4.1 800.5
|
| 162 |
+
|
| 163 |
+
Even when the index of `other` is the same as the index of the DataFrame,
|
| 164 |
+
the :class:`Series` will not be reoriented. If index-wise alignment is desired,
|
| 165 |
+
:meth:`DataFrame.add` should be used with `axis='index'`.
|
| 166 |
+
|
| 167 |
+
>>> s2 = pd.Series([0.5, 1.5], index=["elk", "moose"])
|
| 168 |
+
>>> df[["height", "weight"]] + s2
|
| 169 |
+
elk height moose weight
|
| 170 |
+
elk NaN NaN NaN NaN
|
| 171 |
+
moose NaN NaN NaN NaN
|
| 172 |
+
|
| 173 |
+
>>> df[["height", "weight"]].add(s2, axis="index")
|
| 174 |
+
height weight
|
| 175 |
+
elk 2.0 500.5
|
| 176 |
+
moose 4.1 801.5
|
| 177 |
+
|
| 178 |
+
When `other` is a :class:`DataFrame`, both columns names and the
|
| 179 |
+
index are aligned.
|
| 180 |
+
|
| 181 |
+
>>> other = pd.DataFrame(
|
| 182 |
+
... {"height": [0.2, 0.4, 0.6]}, index=["elk", "moose", "deer"]
|
| 183 |
+
... )
|
| 184 |
+
>>> df[["height", "weight"]] + other
|
| 185 |
+
height weight
|
| 186 |
+
deer NaN NaN
|
| 187 |
+
elk 1.7 NaN
|
| 188 |
+
moose 3.0 NaN
|
| 189 |
+
"""
|
| 190 |
+
return self._arith_method(other, operator.add)
|
| 191 |
+
|
| 192 |
+
@unpack_zerodim_and_defer("__radd__")
|
| 193 |
+
def __radd__(self, other):
|
| 194 |
+
return self._arith_method(other, roperator.radd)
|
| 195 |
+
|
| 196 |
+
@unpack_zerodim_and_defer("__sub__")
|
| 197 |
+
def __sub__(self, other):
|
| 198 |
+
return self._arith_method(other, operator.sub)
|
| 199 |
+
|
| 200 |
+
@unpack_zerodim_and_defer("__rsub__")
|
| 201 |
+
def __rsub__(self, other):
|
| 202 |
+
return self._arith_method(other, roperator.rsub)
|
| 203 |
+
|
| 204 |
+
@unpack_zerodim_and_defer("__mul__")
|
| 205 |
+
def __mul__(self, other):
|
| 206 |
+
return self._arith_method(other, operator.mul)
|
| 207 |
+
|
| 208 |
+
@unpack_zerodim_and_defer("__rmul__")
|
| 209 |
+
def __rmul__(self, other):
|
| 210 |
+
return self._arith_method(other, roperator.rmul)
|
| 211 |
+
|
| 212 |
+
@unpack_zerodim_and_defer("__truediv__")
|
| 213 |
+
def __truediv__(self, other):
|
| 214 |
+
return self._arith_method(other, operator.truediv)
|
| 215 |
+
|
| 216 |
+
@unpack_zerodim_and_defer("__rtruediv__")
|
| 217 |
+
def __rtruediv__(self, other):
|
| 218 |
+
return self._arith_method(other, roperator.rtruediv)
|
| 219 |
+
|
| 220 |
+
@unpack_zerodim_and_defer("__floordiv__")
|
| 221 |
+
def __floordiv__(self, other):
|
| 222 |
+
return self._arith_method(other, operator.floordiv)
|
| 223 |
+
|
| 224 |
+
@unpack_zerodim_and_defer("__rfloordiv")
|
| 225 |
+
def __rfloordiv__(self, other):
|
| 226 |
+
return self._arith_method(other, roperator.rfloordiv)
|
| 227 |
+
|
| 228 |
+
@unpack_zerodim_and_defer("__mod__")
|
| 229 |
+
def __mod__(self, other):
|
| 230 |
+
return self._arith_method(other, operator.mod)
|
| 231 |
+
|
| 232 |
+
@unpack_zerodim_and_defer("__rmod__")
|
| 233 |
+
def __rmod__(self, other):
|
| 234 |
+
return self._arith_method(other, roperator.rmod)
|
| 235 |
+
|
| 236 |
+
@unpack_zerodim_and_defer("__divmod__")
|
| 237 |
+
def __divmod__(self, other):
|
| 238 |
+
return self._arith_method(other, divmod)
|
| 239 |
+
|
| 240 |
+
@unpack_zerodim_and_defer("__rdivmod__")
|
| 241 |
+
def __rdivmod__(self, other):
|
| 242 |
+
return self._arith_method(other, roperator.rdivmod)
|
| 243 |
+
|
| 244 |
+
@unpack_zerodim_and_defer("__pow__")
|
| 245 |
+
def __pow__(self, other):
|
| 246 |
+
return self._arith_method(other, operator.pow)
|
| 247 |
+
|
| 248 |
+
@unpack_zerodim_and_defer("__rpow__")
|
| 249 |
+
def __rpow__(self, other):
|
| 250 |
+
return self._arith_method(other, roperator.rpow)
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
# -----------------------------------------------------------------------------
|
| 254 |
+
# Helpers to implement __array_ufunc__
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
def array_ufunc(self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any):
|
| 258 |
+
"""
|
| 259 |
+
Compatibility with numpy ufuncs.
|
| 260 |
+
|
| 261 |
+
See also
|
| 262 |
+
--------
|
| 263 |
+
numpy.org/doc/stable/reference/arrays.classes.html#numpy.class.__array_ufunc__
|
| 264 |
+
"""
|
| 265 |
+
from pandas.core.frame import (
|
| 266 |
+
DataFrame,
|
| 267 |
+
Series,
|
| 268 |
+
)
|
| 269 |
+
from pandas.core.generic import NDFrame
|
| 270 |
+
from pandas.core.internals import BlockManager
|
| 271 |
+
|
| 272 |
+
cls = type(self)
|
| 273 |
+
|
| 274 |
+
kwargs = _standardize_out_kwarg(**kwargs)
|
| 275 |
+
|
| 276 |
+
# for binary ops, use our custom dunder methods
|
| 277 |
+
result = maybe_dispatch_ufunc_to_dunder_op(self, ufunc, method, *inputs, **kwargs)
|
| 278 |
+
if result is not NotImplemented:
|
| 279 |
+
return result
|
| 280 |
+
|
| 281 |
+
# Determine if we should defer.
|
| 282 |
+
no_defer = (
|
| 283 |
+
np.ndarray.__array_ufunc__,
|
| 284 |
+
cls.__array_ufunc__,
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
for item in inputs:
|
| 288 |
+
higher_priority = (
|
| 289 |
+
hasattr(item, "__array_priority__")
|
| 290 |
+
and item.__array_priority__ > self.__array_priority__
|
| 291 |
+
)
|
| 292 |
+
has_array_ufunc = (
|
| 293 |
+
hasattr(item, "__array_ufunc__")
|
| 294 |
+
and type(item).__array_ufunc__ not in no_defer
|
| 295 |
+
and not isinstance(item, self._HANDLED_TYPES)
|
| 296 |
+
)
|
| 297 |
+
if higher_priority or has_array_ufunc:
|
| 298 |
+
return NotImplemented
|
| 299 |
+
|
| 300 |
+
# align all the inputs.
|
| 301 |
+
types = tuple(type(x) for x in inputs)
|
| 302 |
+
alignable = [
|
| 303 |
+
x for x, t in zip(inputs, types, strict=True) if issubclass(t, NDFrame)
|
| 304 |
+
]
|
| 305 |
+
|
| 306 |
+
if len(alignable) > 1:
|
| 307 |
+
# This triggers alignment.
|
| 308 |
+
# At the moment, there aren't any ufuncs with more than two inputs
|
| 309 |
+
# so this ends up just being x1.index | x2.index, but we write
|
| 310 |
+
# it to handle *args.
|
| 311 |
+
set_types = set(types)
|
| 312 |
+
if len(set_types) > 1 and {DataFrame, Series}.issubset(set_types):
|
| 313 |
+
# We currently don't handle ufunc(DataFrame, Series)
|
| 314 |
+
# well. Previously this raised an internal ValueError. We might
|
| 315 |
+
# support it someday, so raise a NotImplementedError.
|
| 316 |
+
raise NotImplementedError(
|
| 317 |
+
f"Cannot apply ufunc {ufunc} to mixed DataFrame and Series inputs."
|
| 318 |
+
)
|
| 319 |
+
axes = self.axes
|
| 320 |
+
for obj in alignable[1:]:
|
| 321 |
+
# this relies on the fact that we aren't handling mixed
|
| 322 |
+
# series / frame ufuncs.
|
| 323 |
+
for i, (ax1, ax2) in enumerate(zip(axes, obj.axes, strict=True)):
|
| 324 |
+
axes[i] = ax1.union(ax2)
|
| 325 |
+
|
| 326 |
+
reconstruct_axes = dict(zip(self._AXIS_ORDERS, axes, strict=True))
|
| 327 |
+
inputs = tuple(
|
| 328 |
+
x.reindex(**reconstruct_axes) if issubclass(t, NDFrame) else x
|
| 329 |
+
for x, t in zip(inputs, types, strict=True)
|
| 330 |
+
)
|
| 331 |
+
else:
|
| 332 |
+
reconstruct_axes = dict(zip(self._AXIS_ORDERS, self.axes, strict=True))
|
| 333 |
+
|
| 334 |
+
if self.ndim == 1:
|
| 335 |
+
names = {x.name for x in inputs if hasattr(x, "name")}
|
| 336 |
+
name = names.pop() if len(names) == 1 else None
|
| 337 |
+
reconstruct_kwargs = {"name": name}
|
| 338 |
+
else:
|
| 339 |
+
reconstruct_kwargs = {}
|
| 340 |
+
|
| 341 |
+
def reconstruct(result):
|
| 342 |
+
if ufunc.nout > 1:
|
| 343 |
+
# np.modf, np.frexp, np.divmod
|
| 344 |
+
return tuple(_reconstruct(x) for x in result)
|
| 345 |
+
|
| 346 |
+
return _reconstruct(result)
|
| 347 |
+
|
| 348 |
+
def _reconstruct(result):
|
| 349 |
+
if lib.is_scalar(result):
|
| 350 |
+
return result
|
| 351 |
+
|
| 352 |
+
if result.ndim != self.ndim:
|
| 353 |
+
if method == "outer":
|
| 354 |
+
raise NotImplementedError
|
| 355 |
+
return result
|
| 356 |
+
if isinstance(result, BlockManager):
|
| 357 |
+
# we went through BlockManager.apply e.g. np.sqrt
|
| 358 |
+
result = self._constructor_from_mgr(result, axes=result.axes)
|
| 359 |
+
else:
|
| 360 |
+
# we converted an array, lost our axes
|
| 361 |
+
result = self._constructor(
|
| 362 |
+
result, **reconstruct_axes, **reconstruct_kwargs, copy=False
|
| 363 |
+
)
|
| 364 |
+
# TODO: When we support multiple values in __finalize__, this
|
| 365 |
+
# should pass alignable to `__finalize__` instead of self.
|
| 366 |
+
# Then `np.add(a, b)` would consider attrs from both a and b
|
| 367 |
+
# when a and b are NDFrames.
|
| 368 |
+
if len(alignable) == 1:
|
| 369 |
+
result = result.__finalize__(self)
|
| 370 |
+
return result
|
| 371 |
+
|
| 372 |
+
if "out" in kwargs:
|
| 373 |
+
# e.g. test_multiindex_get_loc
|
| 374 |
+
result = dispatch_ufunc_with_out(self, ufunc, method, *inputs, **kwargs)
|
| 375 |
+
return reconstruct(result)
|
| 376 |
+
|
| 377 |
+
if method == "reduce":
|
| 378 |
+
# e.g. test.series.test_ufunc.test_reduce
|
| 379 |
+
result = dispatch_reduction_ufunc(self, ufunc, method, *inputs, **kwargs)
|
| 380 |
+
if result is not NotImplemented:
|
| 381 |
+
return result
|
| 382 |
+
|
| 383 |
+
# We still get here with kwargs `axis` for e.g. np.maximum.accumulate
|
| 384 |
+
# and `dtype` and `keepdims` for np.ptp
|
| 385 |
+
|
| 386 |
+
if self.ndim > 1 and (len(inputs) > 1 or ufunc.nout > 1):
|
| 387 |
+
# Just give up on preserving types in the complex case.
|
| 388 |
+
# In theory we could preserve them for them.
|
| 389 |
+
# * nout>1 is doable if BlockManager.apply took nout and
|
| 390 |
+
# returned a Tuple[BlockManager].
|
| 391 |
+
# * len(inputs) > 1 is doable when we know that we have
|
| 392 |
+
# aligned blocks / dtypes.
|
| 393 |
+
|
| 394 |
+
# e.g. my_ufunc, modf, logaddexp, heaviside, subtract, add
|
| 395 |
+
inputs = tuple(np.asarray(x) for x in inputs)
|
| 396 |
+
# Note: we can't use default_array_ufunc here bc reindexing means
|
| 397 |
+
# that `self` may not be among `inputs`
|
| 398 |
+
result = getattr(ufunc, method)(*inputs, **kwargs)
|
| 399 |
+
elif self.ndim == 1:
|
| 400 |
+
# ufunc(series, ...)
|
| 401 |
+
inputs = tuple(extract_array(x, extract_numpy=True) for x in inputs)
|
| 402 |
+
result = getattr(ufunc, method)(*inputs, **kwargs)
|
| 403 |
+
# ufunc(dataframe)
|
| 404 |
+
elif method == "__call__" and not kwargs:
|
| 405 |
+
# for np.<ufunc>(..) calls
|
| 406 |
+
# kwargs cannot necessarily be handled block-by-block, so only
|
| 407 |
+
# take this path if there are no kwargs
|
| 408 |
+
mgr = inputs[0]._mgr # pyright: ignore[reportGeneralTypeIssues]
|
| 409 |
+
result = mgr.apply(getattr(ufunc, method))
|
| 410 |
+
else:
|
| 411 |
+
# otherwise specific ufunc methods (eg np.<ufunc>.accumulate(..))
|
| 412 |
+
# Those can have an axis keyword and thus can't be called block-by-block
|
| 413 |
+
result = default_array_ufunc(inputs[0], ufunc, method, *inputs, **kwargs) # pyright: ignore[reportGeneralTypeIssues]
|
| 414 |
+
# e.g. np.negative (only one reached), with "where" and "out" in kwargs
|
| 415 |
+
|
| 416 |
+
result = reconstruct(result)
|
| 417 |
+
return result
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
def _standardize_out_kwarg(**kwargs) -> dict:
|
| 421 |
+
"""
|
| 422 |
+
If kwargs contain "out1" and "out2", replace that with a tuple "out"
|
| 423 |
+
|
| 424 |
+
np.divmod, np.modf, np.frexp can have either `out=(out1, out2)` or
|
| 425 |
+
`out1=out1, out2=out2)`
|
| 426 |
+
"""
|
| 427 |
+
if "out" not in kwargs and "out1" in kwargs and "out2" in kwargs:
|
| 428 |
+
out1 = kwargs.pop("out1")
|
| 429 |
+
out2 = kwargs.pop("out2")
|
| 430 |
+
out = (out1, out2)
|
| 431 |
+
kwargs["out"] = out
|
| 432 |
+
return kwargs
|
| 433 |
+
|
| 434 |
+
|
| 435 |
+
def dispatch_ufunc_with_out(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
|
| 436 |
+
"""
|
| 437 |
+
If we have an `out` keyword, then call the ufunc without `out` and then
|
| 438 |
+
set the result into the given `out`.
|
| 439 |
+
"""
|
| 440 |
+
|
| 441 |
+
# Note: we assume _standardize_out_kwarg has already been called.
|
| 442 |
+
out = kwargs.pop("out")
|
| 443 |
+
where = kwargs.pop("where", None)
|
| 444 |
+
|
| 445 |
+
result = getattr(ufunc, method)(*inputs, **kwargs)
|
| 446 |
+
|
| 447 |
+
if result is NotImplemented:
|
| 448 |
+
return NotImplemented
|
| 449 |
+
|
| 450 |
+
if isinstance(result, tuple):
|
| 451 |
+
# i.e. np.divmod, np.modf, np.frexp
|
| 452 |
+
if not isinstance(out, tuple) or len(out) != len(result):
|
| 453 |
+
raise NotImplementedError
|
| 454 |
+
|
| 455 |
+
for arr, res in zip(out, result, strict=True):
|
| 456 |
+
_assign_where(arr, res, where)
|
| 457 |
+
|
| 458 |
+
return out
|
| 459 |
+
|
| 460 |
+
if isinstance(out, tuple):
|
| 461 |
+
if len(out) == 1:
|
| 462 |
+
out = out[0]
|
| 463 |
+
else:
|
| 464 |
+
raise NotImplementedError
|
| 465 |
+
|
| 466 |
+
_assign_where(out, result, where)
|
| 467 |
+
return out
|
| 468 |
+
|
| 469 |
+
|
| 470 |
+
def _assign_where(out, result, where) -> None:
|
| 471 |
+
"""
|
| 472 |
+
Set a ufunc result into 'out', masking with a 'where' argument if necessary.
|
| 473 |
+
"""
|
| 474 |
+
if where is None:
|
| 475 |
+
# no 'where' arg passed to ufunc
|
| 476 |
+
out[:] = result
|
| 477 |
+
else:
|
| 478 |
+
np.putmask(out, where, result)
|
| 479 |
+
|
| 480 |
+
|
| 481 |
+
def default_array_ufunc(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
|
| 482 |
+
"""
|
| 483 |
+
Fallback to the behavior we would get if we did not define __array_ufunc__.
|
| 484 |
+
|
| 485 |
+
Notes
|
| 486 |
+
-----
|
| 487 |
+
We are assuming that `self` is among `inputs`.
|
| 488 |
+
"""
|
| 489 |
+
if not any(x is self for x in inputs):
|
| 490 |
+
raise NotImplementedError
|
| 491 |
+
|
| 492 |
+
new_inputs = [x if x is not self else np.asarray(x) for x in inputs]
|
| 493 |
+
|
| 494 |
+
return getattr(ufunc, method)(*new_inputs, **kwargs)
|
| 495 |
+
|
| 496 |
+
|
| 497 |
+
def dispatch_reduction_ufunc(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
|
| 498 |
+
"""
|
| 499 |
+
Dispatch ufunc reductions to self's reduction methods.
|
| 500 |
+
"""
|
| 501 |
+
assert method == "reduce"
|
| 502 |
+
|
| 503 |
+
if len(inputs) != 1 or inputs[0] is not self:
|
| 504 |
+
return NotImplemented
|
| 505 |
+
|
| 506 |
+
if ufunc.__name__ not in REDUCTION_ALIASES:
|
| 507 |
+
return NotImplemented
|
| 508 |
+
|
| 509 |
+
method_name = REDUCTION_ALIASES[ufunc.__name__]
|
| 510 |
+
|
| 511 |
+
# NB: we are assuming that min/max represent minimum/maximum methods,
|
| 512 |
+
# which would not be accurate for e.g. Timestamp.min
|
| 513 |
+
if not hasattr(self, method_name):
|
| 514 |
+
return NotImplemented
|
| 515 |
+
|
| 516 |
+
if self.ndim > 1:
|
| 517 |
+
if isinstance(self, ABCNDFrame):
|
| 518 |
+
# TODO: test cases where this doesn't hold, i.e. 2D DTA/TDA
|
| 519 |
+
kwargs["numeric_only"] = False
|
| 520 |
+
|
| 521 |
+
if "axis" not in kwargs:
|
| 522 |
+
# For DataFrame reductions we don't want the default axis=0
|
| 523 |
+
# Note: np.min is not a ufunc, but uses array_function_dispatch,
|
| 524 |
+
# so calls DataFrame.min (without ever getting here) with the np.min
|
| 525 |
+
# default of axis=None, which DataFrame.min catches and changes to axis=0.
|
| 526 |
+
# np.minimum.reduce(df) gets here bc axis is not in kwargs,
|
| 527 |
+
# so we set axis=0 to match the behavior of np.minimum.reduce(df.values)
|
| 528 |
+
kwargs["axis"] = 0
|
| 529 |
+
|
| 530 |
+
# By default, numpy's reductions do not skip NaNs, so we have to
|
| 531 |
+
# pass skipna=False
|
| 532 |
+
result = getattr(self, method_name)(skipna=False, **kwargs)
|
| 533 |
+
result = maybe_unbox_numpy_scalar(result)
|
| 534 |
+
return result
|
loan-advisor-agent/.venv/Lib/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 |
+
"ArrowStringArray",
|
| 27 |
+
"BaseMaskedArray",
|
| 28 |
+
"BooleanArray",
|
| 29 |
+
"Categorical",
|
| 30 |
+
"DatetimeArray",
|
| 31 |
+
"ExtensionArray",
|
| 32 |
+
"ExtensionOpsMixin",
|
| 33 |
+
"ExtensionScalarOpsMixin",
|
| 34 |
+
"FloatingArray",
|
| 35 |
+
"IntegerArray",
|
| 36 |
+
"IntervalArray",
|
| 37 |
+
"NumpyExtensionArray",
|
| 38 |
+
"PeriodArray",
|
| 39 |
+
"SparseArray",
|
| 40 |
+
"StringArray",
|
| 41 |
+
"TimedeltaArray",
|
| 42 |
+
"period_array",
|
| 43 |
+
]
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (1.4 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/_arrow_string_mixins.cpython-312.pyc
ADDED
|
Binary file (22.5 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/_mixins.cpython-312.pyc
ADDED
|
Binary file (20.9 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/_ranges.cpython-312.pyc
ADDED
|
Binary file (6.96 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/_utils.cpython-312.pyc
ADDED
|
Binary file (2.79 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/base.cpython-312.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2ba070a245d36b09a5be7a0c99e486aa2b3a9be6bcdd7b457e6478d5c4db9045
|
| 3 |
+
size 105942
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/boolean.cpython-312.pyc
ADDED
|
Binary file (17.5 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/categorical.cpython-312.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:17704147e68fa5d76bfe10c4c7bdc307eba1c58522963d3e084551c16395c8c3
|
| 3 |
+
size 110796
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/datetimelike.cpython-312.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a912340326214a78af9e25f72e03c5a89a510d85dec7b3aea8149b43faf204d0
|
| 3 |
+
size 106012
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/datetimes.cpython-312.pyc
ADDED
|
Binary file (98 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/floating.cpython-312.pyc
ADDED
|
Binary file (6.16 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/integer.cpython-312.pyc
ADDED
|
Binary file (10 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/interval.cpython-312.pyc
ADDED
|
Binary file (81.7 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/masked.cpython-312.pyc
ADDED
|
Binary file (79.1 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/numeric.cpython-312.pyc
ADDED
|
Binary file (12.7 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/numpy_.cpython-312.pyc
ADDED
|
Binary file (23 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/period.cpython-312.pyc
ADDED
|
Binary file (53.5 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/string_.cpython-312.pyc
ADDED
|
Binary file (46.3 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/string_arrow.cpython-312.pyc
ADDED
|
Binary file (27.5 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/__pycache__/timedeltas.cpython-312.pyc
ADDED
|
Binary file (48 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/_arrow_string_mixins.py
ADDED
|
@@ -0,0 +1,434 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from functools import partial
|
| 4 |
+
import re
|
| 5 |
+
from typing import (
|
| 6 |
+
TYPE_CHECKING,
|
| 7 |
+
Any,
|
| 8 |
+
Literal,
|
| 9 |
+
Self,
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
import numpy as np
|
| 13 |
+
|
| 14 |
+
from pandas._libs import lib
|
| 15 |
+
from pandas.compat import (
|
| 16 |
+
HAS_PYARROW,
|
| 17 |
+
pa_version_under17p0,
|
| 18 |
+
pa_version_under21p0,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
if HAS_PYARROW:
|
| 22 |
+
import pyarrow as pa
|
| 23 |
+
import pyarrow.compute as pc
|
| 24 |
+
|
| 25 |
+
if TYPE_CHECKING:
|
| 26 |
+
from collections.abc import Callable
|
| 27 |
+
|
| 28 |
+
from pandas._typing import Scalar
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class ArrowStringArrayMixin:
|
| 32 |
+
_pa_array: pa.ChunkedArray
|
| 33 |
+
|
| 34 |
+
def __init__(self, *args, **kwargs) -> None:
|
| 35 |
+
raise NotImplementedError
|
| 36 |
+
|
| 37 |
+
def _from_pyarrow_array(self, pa_array) -> Self:
|
| 38 |
+
raise NotImplementedError
|
| 39 |
+
|
| 40 |
+
def _convert_bool_result(self, result, na=lib.no_default, method_name=None):
|
| 41 |
+
# Convert a bool-dtype result to the appropriate result type
|
| 42 |
+
raise NotImplementedError
|
| 43 |
+
|
| 44 |
+
def _convert_int_result(self, result):
|
| 45 |
+
# Convert an integer-dtype result to the appropriate result type
|
| 46 |
+
raise NotImplementedError
|
| 47 |
+
|
| 48 |
+
def _apply_elementwise(self, func: Callable) -> list[list[Any]]:
|
| 49 |
+
raise NotImplementedError
|
| 50 |
+
|
| 51 |
+
@staticmethod
|
| 52 |
+
def _has_unsupported_regex(pat: str | re.Pattern) -> bool:
|
| 53 |
+
"""
|
| 54 |
+
Determine if regex pattern contains features not supported by RE2 / pyarrow.
|
| 55 |
+
|
| 56 |
+
This includes lookaround (lookahead or lookbehind) assertions and
|
| 57 |
+
backreferences.
|
| 58 |
+
|
| 59 |
+
Parameters
|
| 60 |
+
----------
|
| 61 |
+
pat: str | re.Pattern
|
| 62 |
+
Regex pattern.
|
| 63 |
+
|
| 64 |
+
Returns
|
| 65 |
+
-------
|
| 66 |
+
bool
|
| 67 |
+
Whether `pat` contains a lookahead or lookbehind.
|
| 68 |
+
"""
|
| 69 |
+
try:
|
| 70 |
+
# error: Module "re" has no attribute "_parser"
|
| 71 |
+
from re import _parser # type: ignore[attr-defined]
|
| 72 |
+
|
| 73 |
+
regex_parser = _parser.parse
|
| 74 |
+
except Exception as err:
|
| 75 |
+
raise type(err)(
|
| 76 |
+
"Incompatible version for regex; you will need to upgrade pandas "
|
| 77 |
+
"or downgrade Python"
|
| 78 |
+
) from err
|
| 79 |
+
|
| 80 |
+
def has_unsupported_code(tokens):
|
| 81 |
+
# For certain op codes we need to recurse.
|
| 82 |
+
for op_code, argument in tokens:
|
| 83 |
+
if (
|
| 84 |
+
(
|
| 85 |
+
op_code == _parser.SUBPATTERN
|
| 86 |
+
and has_unsupported_code(argument[3])
|
| 87 |
+
)
|
| 88 |
+
or (
|
| 89 |
+
op_code == _parser.BRANCH
|
| 90 |
+
and any(has_unsupported_code(tokens) for tokens in argument[1])
|
| 91 |
+
)
|
| 92 |
+
or (
|
| 93 |
+
op_code
|
| 94 |
+
in [_parser.ASSERT_NOT, _parser.ASSERT, _parser.GROUPREF]
|
| 95 |
+
)
|
| 96 |
+
):
|
| 97 |
+
return True
|
| 98 |
+
return False
|
| 99 |
+
|
| 100 |
+
str_pat = pat.pattern if isinstance(pat, re.Pattern) else pat
|
| 101 |
+
try:
|
| 102 |
+
tokens = regex_parser(str_pat)
|
| 103 |
+
except re.error:
|
| 104 |
+
# Pattern not valid for Python's re (e.g. RE2 syntax like \x{...} or \p)
|
| 105 |
+
# Let the pyarrow backend handle it.
|
| 106 |
+
return False
|
| 107 |
+
return has_unsupported_code(tokens)
|
| 108 |
+
|
| 109 |
+
def _str_len(self):
|
| 110 |
+
result = pc.utf8_length(self._pa_array)
|
| 111 |
+
return self._convert_int_result(result)
|
| 112 |
+
|
| 113 |
+
def _str_lower(self) -> Self:
|
| 114 |
+
return self._from_pyarrow_array(pc.utf8_lower(self._pa_array))
|
| 115 |
+
|
| 116 |
+
def _str_upper(self) -> Self:
|
| 117 |
+
return self._from_pyarrow_array(pc.utf8_upper(self._pa_array))
|
| 118 |
+
|
| 119 |
+
def _str_strip(self, to_strip=None) -> Self:
|
| 120 |
+
if to_strip is None:
|
| 121 |
+
result = pc.utf8_trim_whitespace(self._pa_array)
|
| 122 |
+
else:
|
| 123 |
+
result = pc.utf8_trim(self._pa_array, characters=to_strip)
|
| 124 |
+
return self._from_pyarrow_array(result)
|
| 125 |
+
|
| 126 |
+
def _str_lstrip(self, to_strip=None) -> Self:
|
| 127 |
+
if to_strip is None:
|
| 128 |
+
result = pc.utf8_ltrim_whitespace(self._pa_array)
|
| 129 |
+
else:
|
| 130 |
+
result = pc.utf8_ltrim(self._pa_array, characters=to_strip)
|
| 131 |
+
return self._from_pyarrow_array(result)
|
| 132 |
+
|
| 133 |
+
def _str_rstrip(self, to_strip=None) -> Self:
|
| 134 |
+
if to_strip is None:
|
| 135 |
+
result = pc.utf8_rtrim_whitespace(self._pa_array)
|
| 136 |
+
else:
|
| 137 |
+
result = pc.utf8_rtrim(self._pa_array, characters=to_strip)
|
| 138 |
+
return self._from_pyarrow_array(result)
|
| 139 |
+
|
| 140 |
+
def _str_pad(
|
| 141 |
+
self,
|
| 142 |
+
width: int,
|
| 143 |
+
side: Literal["left", "right", "both"] = "left",
|
| 144 |
+
fillchar: str = " ",
|
| 145 |
+
) -> Self:
|
| 146 |
+
if side == "left":
|
| 147 |
+
pa_pad = pc.utf8_lpad
|
| 148 |
+
elif side == "right":
|
| 149 |
+
pa_pad = pc.utf8_rpad
|
| 150 |
+
elif side == "both":
|
| 151 |
+
if pa_version_under17p0:
|
| 152 |
+
# GH#59624 fall back to object dtype
|
| 153 |
+
from pandas import array
|
| 154 |
+
|
| 155 |
+
obj_arr = self.astype(object, copy=False) # type: ignore[attr-defined]
|
| 156 |
+
obj = array(obj_arr, dtype=object)
|
| 157 |
+
result = obj._str_pad(width, side, fillchar) # type: ignore[attr-defined]
|
| 158 |
+
return type(self)._from_sequence(result, dtype=self.dtype) # type: ignore[attr-defined]
|
| 159 |
+
else:
|
| 160 |
+
# GH#54792
|
| 161 |
+
# https://github.com/apache/arrow/issues/15053#issuecomment-2317032347
|
| 162 |
+
lean_left = (width % 2) == 0
|
| 163 |
+
pa_pad = partial(pc.utf8_center, lean_left_on_odd_padding=lean_left)
|
| 164 |
+
else:
|
| 165 |
+
raise ValueError(
|
| 166 |
+
f"Invalid side: {side}. Side must be one of 'left', 'right', 'both'"
|
| 167 |
+
)
|
| 168 |
+
return self._from_pyarrow_array(
|
| 169 |
+
pa_pad(self._pa_array, width=width, padding=fillchar)
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
def _str_get(self, i: int) -> Self:
|
| 173 |
+
lengths = pc.utf8_length(self._pa_array)
|
| 174 |
+
if i >= 0:
|
| 175 |
+
out_of_bounds = pc.greater_equal(i, lengths)
|
| 176 |
+
start = i
|
| 177 |
+
stop = i + 1
|
| 178 |
+
step = 1
|
| 179 |
+
else:
|
| 180 |
+
out_of_bounds = pc.greater(-i, lengths)
|
| 181 |
+
start = i
|
| 182 |
+
stop = i - 1
|
| 183 |
+
step = -1
|
| 184 |
+
not_out_of_bounds = pc.invert(out_of_bounds.fill_null(True))
|
| 185 |
+
selected = pc.utf8_slice_codeunits(
|
| 186 |
+
self._pa_array, start=start, stop=stop, step=step
|
| 187 |
+
)
|
| 188 |
+
null_value = pa.scalar(None, type=self._pa_array.type)
|
| 189 |
+
result = pc.if_else(not_out_of_bounds, selected, null_value)
|
| 190 |
+
return self._from_pyarrow_array(result)
|
| 191 |
+
|
| 192 |
+
def _str_slice(
|
| 193 |
+
self, start: int | None = None, stop: int | None = None, step: int | None = None
|
| 194 |
+
) -> Self:
|
| 195 |
+
if start is None:
|
| 196 |
+
if step is not None and step < 0:
|
| 197 |
+
# GH#59710
|
| 198 |
+
start = -1
|
| 199 |
+
else:
|
| 200 |
+
start = 0
|
| 201 |
+
if step is None:
|
| 202 |
+
step = 1
|
| 203 |
+
return self._from_pyarrow_array(
|
| 204 |
+
pc.utf8_slice_codeunits(self._pa_array, start=start, stop=stop, step=step)
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
def _str_slice_replace(
|
| 208 |
+
self, start: int | None = None, stop: int | None = None, repl: str | None = None
|
| 209 |
+
) -> Self:
|
| 210 |
+
if repl is None:
|
| 211 |
+
repl = ""
|
| 212 |
+
if start is None:
|
| 213 |
+
start = 0
|
| 214 |
+
if stop is None:
|
| 215 |
+
stop = np.iinfo(np.int64).max
|
| 216 |
+
return self._from_pyarrow_array(
|
| 217 |
+
pc.utf8_replace_slice(self._pa_array, start, stop, repl)
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
def _str_replace(
|
| 221 |
+
self,
|
| 222 |
+
pat: str | re.Pattern,
|
| 223 |
+
repl: str | Callable,
|
| 224 |
+
n: int = -1,
|
| 225 |
+
case: bool = True,
|
| 226 |
+
flags: int = 0,
|
| 227 |
+
regex: bool = True,
|
| 228 |
+
) -> Self:
|
| 229 |
+
if (
|
| 230 |
+
isinstance(pat, re.Pattern)
|
| 231 |
+
or callable(repl)
|
| 232 |
+
or not case
|
| 233 |
+
or flags
|
| 234 |
+
or (isinstance(repl, str) and r"\g<" in repl)
|
| 235 |
+
):
|
| 236 |
+
raise NotImplementedError(
|
| 237 |
+
"replace is not supported with a re.Pattern, callable repl, "
|
| 238 |
+
"case=False, flags!=0, or when the replacement string contains "
|
| 239 |
+
"named group references (\\g<...>)"
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
func = pc.replace_substring_regex if regex else pc.replace_substring
|
| 243 |
+
# https://github.com/apache/arrow/issues/39149
|
| 244 |
+
# GH 56404, unexpected behavior with negative max_replacements with pyarrow.
|
| 245 |
+
pa_max_replacements = None if n < 0 else n
|
| 246 |
+
result = func(
|
| 247 |
+
self._pa_array,
|
| 248 |
+
pattern=pat,
|
| 249 |
+
replacement=repl,
|
| 250 |
+
max_replacements=pa_max_replacements,
|
| 251 |
+
)
|
| 252 |
+
return self._from_pyarrow_array(result)
|
| 253 |
+
|
| 254 |
+
def _str_capitalize(self) -> Self:
|
| 255 |
+
return self._from_pyarrow_array(pc.utf8_capitalize(self._pa_array))
|
| 256 |
+
|
| 257 |
+
def _str_title(self) -> Self:
|
| 258 |
+
return self._from_pyarrow_array(pc.utf8_title(self._pa_array))
|
| 259 |
+
|
| 260 |
+
def _str_swapcase(self) -> Self:
|
| 261 |
+
return self._from_pyarrow_array(pc.utf8_swapcase(self._pa_array))
|
| 262 |
+
|
| 263 |
+
def _str_removeprefix(self, prefix: str):
|
| 264 |
+
if prefix == "":
|
| 265 |
+
return self._from_pyarrow_array(self._pa_array)
|
| 266 |
+
starts_with = pc.starts_with(self._pa_array, pattern=prefix)
|
| 267 |
+
removed = pc.utf8_slice_codeunits(self._pa_array, len(prefix))
|
| 268 |
+
result = pc.if_else(starts_with, removed, self._pa_array)
|
| 269 |
+
return self._from_pyarrow_array(result)
|
| 270 |
+
|
| 271 |
+
def _str_removesuffix(self, suffix: str):
|
| 272 |
+
if suffix == "":
|
| 273 |
+
return self._from_pyarrow_array(self._pa_array)
|
| 274 |
+
ends_with = pc.ends_with(self._pa_array, pattern=suffix)
|
| 275 |
+
removed = pc.utf8_slice_codeunits(self._pa_array, 0, stop=-len(suffix))
|
| 276 |
+
result = pc.if_else(ends_with, removed, self._pa_array)
|
| 277 |
+
return self._from_pyarrow_array(result)
|
| 278 |
+
|
| 279 |
+
def _str_startswith(
|
| 280 |
+
self, pat: str | tuple[str, ...], na: Scalar | lib.NoDefault = lib.no_default
|
| 281 |
+
):
|
| 282 |
+
if isinstance(pat, str):
|
| 283 |
+
result = pc.starts_with(self._pa_array, pattern=pat)
|
| 284 |
+
elif len(pat) == 0:
|
| 285 |
+
# For empty tuple we return null for missing values and False
|
| 286 |
+
# for valid values.
|
| 287 |
+
result = pc.if_else(pc.is_null(self._pa_array), None, False)
|
| 288 |
+
else:
|
| 289 |
+
result = pc.starts_with(self._pa_array, pattern=pat[0])
|
| 290 |
+
|
| 291 |
+
for p in pat[1:]:
|
| 292 |
+
result = pc.or_(result, pc.starts_with(self._pa_array, pattern=p))
|
| 293 |
+
return self._convert_bool_result(result, na=na, method_name="startswith")
|
| 294 |
+
|
| 295 |
+
def _str_endswith(
|
| 296 |
+
self, pat: str | tuple[str, ...], na: Scalar | lib.NoDefault = lib.no_default
|
| 297 |
+
):
|
| 298 |
+
if isinstance(pat, str):
|
| 299 |
+
result = pc.ends_with(self._pa_array, pattern=pat)
|
| 300 |
+
elif len(pat) == 0:
|
| 301 |
+
# For empty tuple we return null for missing values and False
|
| 302 |
+
# for valid values.
|
| 303 |
+
result = pc.if_else(pc.is_null(self._pa_array), None, False)
|
| 304 |
+
else:
|
| 305 |
+
result = pc.ends_with(self._pa_array, pattern=pat[0])
|
| 306 |
+
|
| 307 |
+
for p in pat[1:]:
|
| 308 |
+
result = pc.or_(result, pc.ends_with(self._pa_array, pattern=p))
|
| 309 |
+
return self._convert_bool_result(result, na=na, method_name="endswith")
|
| 310 |
+
|
| 311 |
+
def _str_isalnum(self):
|
| 312 |
+
result = pc.utf8_is_alnum(self._pa_array)
|
| 313 |
+
return self._convert_bool_result(result)
|
| 314 |
+
|
| 315 |
+
def _str_isalpha(self):
|
| 316 |
+
result = pc.utf8_is_alpha(self._pa_array)
|
| 317 |
+
return self._convert_bool_result(result)
|
| 318 |
+
|
| 319 |
+
def _str_isascii(self):
|
| 320 |
+
result = pc.string_is_ascii(self._pa_array)
|
| 321 |
+
return self._convert_bool_result(result)
|
| 322 |
+
|
| 323 |
+
def _str_isdecimal(self):
|
| 324 |
+
result = pc.utf8_is_decimal(self._pa_array)
|
| 325 |
+
return self._convert_bool_result(result)
|
| 326 |
+
|
| 327 |
+
def _str_isdigit(self):
|
| 328 |
+
if pa_version_under21p0:
|
| 329 |
+
# https://github.com/pandas-dev/pandas/issues/61466
|
| 330 |
+
res_list = self._apply_elementwise(str.isdigit)
|
| 331 |
+
return self._convert_bool_result(
|
| 332 |
+
pa.chunked_array(res_list, type=pa.bool_())
|
| 333 |
+
)
|
| 334 |
+
result = pc.utf8_is_digit(self._pa_array)
|
| 335 |
+
return self._convert_bool_result(result)
|
| 336 |
+
|
| 337 |
+
def _str_islower(self):
|
| 338 |
+
result = pc.utf8_is_lower(self._pa_array)
|
| 339 |
+
return self._convert_bool_result(result)
|
| 340 |
+
|
| 341 |
+
def _str_isnumeric(self):
|
| 342 |
+
result = pc.utf8_is_numeric(self._pa_array)
|
| 343 |
+
return self._convert_bool_result(result)
|
| 344 |
+
|
| 345 |
+
def _str_isspace(self):
|
| 346 |
+
result = pc.utf8_is_space(self._pa_array)
|
| 347 |
+
return self._convert_bool_result(result)
|
| 348 |
+
|
| 349 |
+
def _str_istitle(self):
|
| 350 |
+
result = pc.utf8_is_title(self._pa_array)
|
| 351 |
+
return self._convert_bool_result(result)
|
| 352 |
+
|
| 353 |
+
def _str_isupper(self):
|
| 354 |
+
result = pc.utf8_is_upper(self._pa_array)
|
| 355 |
+
return self._convert_bool_result(result)
|
| 356 |
+
|
| 357 |
+
def _str_contains(
|
| 358 |
+
self,
|
| 359 |
+
pat,
|
| 360 |
+
case: bool = True,
|
| 361 |
+
flags: int = 0,
|
| 362 |
+
na: Scalar | lib.NoDefault = lib.no_default,
|
| 363 |
+
regex: bool = True,
|
| 364 |
+
):
|
| 365 |
+
if flags:
|
| 366 |
+
raise NotImplementedError(f"contains not implemented with {flags=}")
|
| 367 |
+
|
| 368 |
+
if regex:
|
| 369 |
+
pa_contains = pc.match_substring_regex
|
| 370 |
+
else:
|
| 371 |
+
pa_contains = pc.match_substring
|
| 372 |
+
result = pa_contains(self._pa_array, pat, ignore_case=not case)
|
| 373 |
+
return self._convert_bool_result(result, na=na, method_name="contains")
|
| 374 |
+
|
| 375 |
+
def _str_match(
|
| 376 |
+
self,
|
| 377 |
+
pat: str,
|
| 378 |
+
case: bool = True,
|
| 379 |
+
flags: int = 0,
|
| 380 |
+
na: Scalar | lib.NoDefault = lib.no_default,
|
| 381 |
+
):
|
| 382 |
+
if not pat.startswith("^"):
|
| 383 |
+
pat = f"^({pat})"
|
| 384 |
+
return ArrowStringArrayMixin._str_contains(
|
| 385 |
+
self, pat, case, flags, na, regex=True
|
| 386 |
+
)
|
| 387 |
+
|
| 388 |
+
def _str_fullmatch(
|
| 389 |
+
self,
|
| 390 |
+
pat: str,
|
| 391 |
+
case: bool = True,
|
| 392 |
+
flags: int = 0,
|
| 393 |
+
na: Scalar | lib.NoDefault = lib.no_default,
|
| 394 |
+
):
|
| 395 |
+
if (not pat.endswith("$") or pat.endswith("\\$")) and not pat.startswith("^"):
|
| 396 |
+
pat = f"^({pat})$"
|
| 397 |
+
elif not pat.endswith("$") or pat.endswith("\\$"):
|
| 398 |
+
pat = f"^({pat[1:]})$"
|
| 399 |
+
elif not pat.startswith("^"):
|
| 400 |
+
pat = f"^({pat[0:-1]})$"
|
| 401 |
+
return ArrowStringArrayMixin._str_match(self, pat, case, flags, na)
|
| 402 |
+
|
| 403 |
+
def _str_find(self, sub: str, start: int = 0, end: int | None = None):
|
| 404 |
+
if not pc.all(pc.string_is_ascii(self._pa_array)).as_py():
|
| 405 |
+
# GH#64123 - pc.find_substring returns byte offsets instead of
|
| 406 |
+
# character offsets for multi-byte UTF-8 characters, so we fall back
|
| 407 |
+
# to Python str.find which correctly returns character offsets.
|
| 408 |
+
res_list = self._apply_elementwise(lambda val: val.find(sub, start, end))
|
| 409 |
+
return self._convert_int_result(pa.chunked_array(res_list))
|
| 410 |
+
|
| 411 |
+
if (start == 0 or start is None) and end is None:
|
| 412 |
+
result = pc.find_substring(self._pa_array, sub)
|
| 413 |
+
else:
|
| 414 |
+
if sub == "":
|
| 415 |
+
# GH#56792
|
| 416 |
+
res_list = self._apply_elementwise(
|
| 417 |
+
lambda val: val.find(sub, start, end)
|
| 418 |
+
)
|
| 419 |
+
return self._convert_int_result(pa.chunked_array(res_list))
|
| 420 |
+
if start is None:
|
| 421 |
+
start_offset = 0
|
| 422 |
+
start = 0
|
| 423 |
+
elif start < 0:
|
| 424 |
+
start_offset = pc.add(start, pc.utf8_length(self._pa_array))
|
| 425 |
+
start_offset = pc.if_else(pc.less(start_offset, 0), 0, start_offset)
|
| 426 |
+
else:
|
| 427 |
+
start_offset = start
|
| 428 |
+
slices = pc.utf8_slice_codeunits(self._pa_array, start, stop=end)
|
| 429 |
+
result = pc.find_substring(slices, sub)
|
| 430 |
+
found = pc.not_equal(result, pa.scalar(-1, type=result.type))
|
| 431 |
+
offset_result = pc.add(result, start_offset)
|
| 432 |
+
result = pc.if_else(found, offset_result, -1)
|
| 433 |
+
result = result.cast(pa.int64())
|
| 434 |
+
return self._convert_int_result(result)
|
loan-advisor-agent/.venv/Lib/site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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from functools import wraps
|
| 4 |
+
from typing import (
|
| 5 |
+
TYPE_CHECKING,
|
| 6 |
+
Any,
|
| 7 |
+
Literal,
|
| 8 |
+
Self,
|
| 9 |
+
cast,
|
| 10 |
+
overload,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
from pandas._libs import lib
|
| 16 |
+
from pandas._libs.arrays import NDArrayBacked
|
| 17 |
+
from pandas._libs.tslibs import is_supported_dtype
|
| 18 |
+
from pandas._typing import (
|
| 19 |
+
ArrayLike,
|
| 20 |
+
AxisInt,
|
| 21 |
+
Dtype,
|
| 22 |
+
F,
|
| 23 |
+
FillnaOptions,
|
| 24 |
+
PositionalIndexer2D,
|
| 25 |
+
PositionalIndexerTuple,
|
| 26 |
+
ScalarIndexer,
|
| 27 |
+
SequenceIndexer,
|
| 28 |
+
Shape,
|
| 29 |
+
TakeIndexer,
|
| 30 |
+
npt,
|
| 31 |
+
)
|
| 32 |
+
from pandas.errors import AbstractMethodError
|
| 33 |
+
from pandas.util._decorators import doc
|
| 34 |
+
from pandas.util._validators import (
|
| 35 |
+
validate_bool_kwarg,
|
| 36 |
+
validate_insert_loc,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
from pandas.core.dtypes.common import pandas_dtype
|
| 40 |
+
from pandas.core.dtypes.dtypes import (
|
| 41 |
+
DatetimeTZDtype,
|
| 42 |
+
ExtensionDtype,
|
| 43 |
+
PeriodDtype,
|
| 44 |
+
)
|
| 45 |
+
from pandas.core.dtypes.missing import array_equivalent
|
| 46 |
+
|
| 47 |
+
from pandas.core import missing
|
| 48 |
+
from pandas.core.algorithms import (
|
| 49 |
+
take,
|
| 50 |
+
unique,
|
| 51 |
+
value_counts_internal as value_counts,
|
| 52 |
+
)
|
| 53 |
+
from pandas.core.array_algos.quantile import quantile_with_mask
|
| 54 |
+
from pandas.core.array_algos.transforms import shift
|
| 55 |
+
from pandas.core.arrays.base import ExtensionArray
|
| 56 |
+
from pandas.core.construction import extract_array
|
| 57 |
+
from pandas.core.indexers import (
|
| 58 |
+
check_array_indexer,
|
| 59 |
+
getitem_returns_view,
|
| 60 |
+
)
|
| 61 |
+
from pandas.core.sorting import nargminmax
|
| 62 |
+
|
| 63 |
+
if TYPE_CHECKING:
|
| 64 |
+
from collections.abc import Sequence
|
| 65 |
+
|
| 66 |
+
from pandas._typing import (
|
| 67 |
+
NumpySorter,
|
| 68 |
+
NumpyValueArrayLike,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
from pandas import Series
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def ravel_compat(meth: F) -> F:
|
| 75 |
+
"""
|
| 76 |
+
Decorator to ravel a 2D array before passing it to a cython operation,
|
| 77 |
+
then reshape the result to our own shape.
|
| 78 |
+
"""
|
| 79 |
+
|
| 80 |
+
@wraps(meth)
|
| 81 |
+
def method(self, *args, **kwargs):
|
| 82 |
+
if self.ndim == 1:
|
| 83 |
+
return meth(self, *args, **kwargs)
|
| 84 |
+
|
| 85 |
+
flags = self._ndarray.flags
|
| 86 |
+
flat = self.ravel("K")
|
| 87 |
+
result = meth(flat, *args, **kwargs)
|
| 88 |
+
order = "F" if flags.f_contiguous else "C"
|
| 89 |
+
return result.reshape(self.shape, order=order)
|
| 90 |
+
|
| 91 |
+
return cast(F, method)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
class NDArrayBackedExtensionArray(NDArrayBacked, ExtensionArray):
|
| 95 |
+
"""
|
| 96 |
+
ExtensionArray that is backed by a single NumPy ndarray.
|
| 97 |
+
"""
|
| 98 |
+
|
| 99 |
+
_ndarray: np.ndarray
|
| 100 |
+
|
| 101 |
+
# scalar used to denote NA value inside our self._ndarray, e.g. -1
|
| 102 |
+
# for Categorical, iNaT for Period. Outside of object dtype,
|
| 103 |
+
# self.isna() should be exactly locations in self._ndarray with
|
| 104 |
+
# _internal_fill_value.
|
| 105 |
+
_internal_fill_value: Any
|
| 106 |
+
|
| 107 |
+
def _box_func(self, x):
|
| 108 |
+
"""
|
| 109 |
+
Wrap numpy type in our dtype.type if necessary.
|
| 110 |
+
"""
|
| 111 |
+
return x
|
| 112 |
+
|
| 113 |
+
def _validate_scalar(self, value):
|
| 114 |
+
# used by NDArrayBackedExtensionIndex.insert
|
| 115 |
+
raise AbstractMethodError(self)
|
| 116 |
+
|
| 117 |
+
# ------------------------------------------------------------------------
|
| 118 |
+
|
| 119 |
+
@overload
|
| 120 |
+
def view(self) -> Self: ...
|
| 121 |
+
|
| 122 |
+
@overload
|
| 123 |
+
def view(self, dtype: Dtype | None = ...) -> ArrayLike: ...
|
| 124 |
+
|
| 125 |
+
def view(self, dtype: Dtype | None = None) -> ArrayLike:
|
| 126 |
+
# We handle datetime64, datetime64tz, timedelta64, and period
|
| 127 |
+
# dtypes here. Everything else we pass through to the underlying
|
| 128 |
+
# ndarray.
|
| 129 |
+
if dtype is None or dtype is self.dtype:
|
| 130 |
+
return self._from_backing_data(self._ndarray)
|
| 131 |
+
|
| 132 |
+
if isinstance(dtype, type):
|
| 133 |
+
# we sometimes pass non-dtype objects, e.g np.ndarray;
|
| 134 |
+
# pass those through to the underlying ndarray
|
| 135 |
+
return self._ndarray.view(dtype)
|
| 136 |
+
|
| 137 |
+
dtype = pandas_dtype(dtype)
|
| 138 |
+
arr = self._ndarray
|
| 139 |
+
|
| 140 |
+
if isinstance(dtype, PeriodDtype):
|
| 141 |
+
cls = dtype.construct_array_type()
|
| 142 |
+
return cls(arr.view("i8"), dtype=dtype)
|
| 143 |
+
elif isinstance(dtype, DatetimeTZDtype):
|
| 144 |
+
dt_cls = dtype.construct_array_type()
|
| 145 |
+
dt64_values = arr.view(f"M8[{dtype.unit}]")
|
| 146 |
+
return dt_cls._simple_new(dt64_values, dtype=dtype)
|
| 147 |
+
elif lib.is_np_dtype(dtype, "M") and is_supported_dtype(dtype):
|
| 148 |
+
from pandas.core.arrays import DatetimeArray
|
| 149 |
+
|
| 150 |
+
dt64_values = arr.view(dtype)
|
| 151 |
+
return DatetimeArray._simple_new(dt64_values, dtype=dtype)
|
| 152 |
+
elif lib.is_np_dtype(dtype, "m") and is_supported_dtype(dtype):
|
| 153 |
+
from pandas.core.arrays import TimedeltaArray
|
| 154 |
+
|
| 155 |
+
td64_values = arr.view(dtype)
|
| 156 |
+
return TimedeltaArray._simple_new(td64_values, dtype=dtype)
|
| 157 |
+
# error: Argument "dtype" to "view" of "ndarray" has incompatible type
|
| 158 |
+
# "ExtensionDtype | dtype[Any]"; expected "dtype[Any] | _HasDType[dtype[Any]]"
|
| 159 |
+
return arr.view(dtype=dtype) # type: ignore[arg-type]
|
| 160 |
+
|
| 161 |
+
def take(
|
| 162 |
+
self,
|
| 163 |
+
indices: TakeIndexer,
|
| 164 |
+
*,
|
| 165 |
+
allow_fill: bool = False,
|
| 166 |
+
fill_value: Any = None,
|
| 167 |
+
axis: AxisInt = 0,
|
| 168 |
+
) -> Self:
|
| 169 |
+
if allow_fill:
|
| 170 |
+
fill_value = self._validate_scalar(fill_value)
|
| 171 |
+
|
| 172 |
+
new_data = take(
|
| 173 |
+
self._ndarray,
|
| 174 |
+
indices,
|
| 175 |
+
allow_fill=allow_fill,
|
| 176 |
+
fill_value=fill_value,
|
| 177 |
+
axis=axis,
|
| 178 |
+
)
|
| 179 |
+
return self._from_backing_data(new_data)
|
| 180 |
+
|
| 181 |
+
# ------------------------------------------------------------------------
|
| 182 |
+
|
| 183 |
+
def equals(self, other) -> bool:
|
| 184 |
+
if type(self) is not type(other):
|
| 185 |
+
return False
|
| 186 |
+
if self.dtype != other.dtype:
|
| 187 |
+
return False
|
| 188 |
+
return bool(array_equivalent(self._ndarray, other._ndarray, dtype_equal=True))
|
| 189 |
+
|
| 190 |
+
@classmethod
|
| 191 |
+
def _from_factorized(cls, values, original):
|
| 192 |
+
assert values.dtype == original._ndarray.dtype
|
| 193 |
+
return original._from_backing_data(values)
|
| 194 |
+
|
| 195 |
+
def _values_for_argsort(self) -> np.ndarray:
|
| 196 |
+
return self._ndarray
|
| 197 |
+
|
| 198 |
+
def _values_for_factorize(self):
|
| 199 |
+
return self._ndarray, self._internal_fill_value
|
| 200 |
+
|
| 201 |
+
def _hash_pandas_object(
|
| 202 |
+
self, *, encoding: str, hash_key: str, categorize: bool
|
| 203 |
+
) -> npt.NDArray[np.uint64]:
|
| 204 |
+
from pandas.core.util.hashing import hash_array
|
| 205 |
+
|
| 206 |
+
values = self._ndarray
|
| 207 |
+
return hash_array(
|
| 208 |
+
values, encoding=encoding, hash_key=hash_key, categorize=categorize
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
def _cast_pointwise_result(self, values: ArrayLike) -> ArrayLike:
|
| 212 |
+
values = np.asarray(values, dtype=object)
|
| 213 |
+
return lib.maybe_convert_objects(values, convert_non_numeric=True)
|
| 214 |
+
|
| 215 |
+
# Signature of "argmin" incompatible with supertype "ExtensionArray"
|
| 216 |
+
def argmin(self, axis: AxisInt = 0, skipna: bool = True): # type: ignore[override]
|
| 217 |
+
# override base class by adding axis keyword
|
| 218 |
+
validate_bool_kwarg(skipna, "skipna")
|
| 219 |
+
if not skipna and self._hasna:
|
| 220 |
+
raise ValueError("Encountered an NA value with skipna=False")
|
| 221 |
+
return nargminmax(self, "argmin", axis=axis)
|
| 222 |
+
|
| 223 |
+
# Signature of "argmax" incompatible with supertype "ExtensionArray"
|
| 224 |
+
def argmax(self, axis: AxisInt = 0, skipna: bool = True): # type: ignore[override]
|
| 225 |
+
# override base class by adding axis keyword
|
| 226 |
+
validate_bool_kwarg(skipna, "skipna")
|
| 227 |
+
if not skipna and self._hasna:
|
| 228 |
+
raise ValueError("Encountered an NA value with skipna=False")
|
| 229 |
+
return nargminmax(self, "argmax", axis=axis)
|
| 230 |
+
|
| 231 |
+
def unique(self) -> Self:
|
| 232 |
+
new_data = unique(self._ndarray)
|
| 233 |
+
return self._from_backing_data(new_data)
|
| 234 |
+
|
| 235 |
+
@classmethod
|
| 236 |
+
@doc(ExtensionArray._concat_same_type)
|
| 237 |
+
def _concat_same_type(
|
| 238 |
+
cls,
|
| 239 |
+
to_concat: Sequence[Self],
|
| 240 |
+
axis: AxisInt = 0,
|
| 241 |
+
) -> Self:
|
| 242 |
+
if not lib.dtypes_all_equal([x.dtype for x in to_concat]):
|
| 243 |
+
dtypes = {str(x.dtype) for x in to_concat}
|
| 244 |
+
raise ValueError("to_concat must have the same dtype", dtypes)
|
| 245 |
+
|
| 246 |
+
return super()._concat_same_type(to_concat, axis=axis)
|
| 247 |
+
|
| 248 |
+
@doc(ExtensionArray.searchsorted)
|
| 249 |
+
def searchsorted(
|
| 250 |
+
self,
|
| 251 |
+
value: NumpyValueArrayLike | ExtensionArray,
|
| 252 |
+
side: Literal["left", "right"] = "left",
|
| 253 |
+
sorter: NumpySorter | None = None,
|
| 254 |
+
) -> npt.NDArray[np.intp] | np.intp:
|
| 255 |
+
npvalue = self._validate_setitem_value(value)
|
| 256 |
+
return self._ndarray.searchsorted(npvalue, side=side, sorter=sorter)
|
| 257 |
+
|
| 258 |
+
@doc(ExtensionArray.shift)
|
| 259 |
+
def shift(self, periods: int = 1, fill_value=None) -> Self:
|
| 260 |
+
# NB: shift is always along axis=0
|
| 261 |
+
axis = 0
|
| 262 |
+
fill_value = self._validate_scalar(fill_value)
|
| 263 |
+
new_values = shift(self._ndarray, periods, axis, fill_value)
|
| 264 |
+
|
| 265 |
+
return self._from_backing_data(new_values)
|
| 266 |
+
|
| 267 |
+
def __setitem__(self, key, value) -> None:
|
| 268 |
+
if self._readonly:
|
| 269 |
+
raise ValueError("Cannot modify read-only array")
|
| 270 |
+
|
| 271 |
+
key = check_array_indexer(self, key)
|
| 272 |
+
value = self._validate_setitem_value(value)
|
| 273 |
+
self._ndarray[key] = value
|
| 274 |
+
|
| 275 |
+
def _validate_setitem_value(self, value):
|
| 276 |
+
return value
|
| 277 |
+
|
| 278 |
+
@overload
|
| 279 |
+
def __getitem__(self, key: ScalarIndexer) -> Any: ...
|
| 280 |
+
|
| 281 |
+
@overload
|
| 282 |
+
def __getitem__(
|
| 283 |
+
self,
|
| 284 |
+
key: SequenceIndexer | PositionalIndexerTuple,
|
| 285 |
+
) -> Self: ...
|
| 286 |
+
|
| 287 |
+
def __getitem__(
|
| 288 |
+
self,
|
| 289 |
+
key: PositionalIndexer2D,
|
| 290 |
+
) -> Self | Any:
|
| 291 |
+
if lib.is_integer(key):
|
| 292 |
+
# fast-path
|
| 293 |
+
result = self._ndarray[key]
|
| 294 |
+
if self.ndim == 1:
|
| 295 |
+
return self._box_func(result)
|
| 296 |
+
result = self._from_backing_data(result)
|
| 297 |
+
if getitem_returns_view(self, key):
|
| 298 |
+
result._readonly = self._readonly
|
| 299 |
+
return result
|
| 300 |
+
|
| 301 |
+
# error: Incompatible types in assignment (expression has type "ExtensionArray",
|
| 302 |
+
# variable has type "Union[int, slice, ndarray]")
|
| 303 |
+
key = extract_array(key, extract_numpy=True) # type: ignore[assignment]
|
| 304 |
+
key = check_array_indexer(self, key)
|
| 305 |
+
result = self._ndarray[key]
|
| 306 |
+
if lib.is_scalar(result):
|
| 307 |
+
return self._box_func(result)
|
| 308 |
+
|
| 309 |
+
result = self._from_backing_data(result)
|
| 310 |
+
if getitem_returns_view(self, key):
|
| 311 |
+
result._readonly = self._readonly
|
| 312 |
+
return result
|
| 313 |
+
|
| 314 |
+
def _pad_or_backfill(
|
| 315 |
+
self,
|
| 316 |
+
*,
|
| 317 |
+
method: FillnaOptions,
|
| 318 |
+
limit: int | None = None,
|
| 319 |
+
limit_area: Literal["inside", "outside"] | None = None,
|
| 320 |
+
copy: bool = True,
|
| 321 |
+
) -> Self:
|
| 322 |
+
mask = self.isna()
|
| 323 |
+
if mask.any():
|
| 324 |
+
# (for now) when self.ndim == 2, we assume axis=0
|
| 325 |
+
func = missing.get_fill_func(method, ndim=self.ndim)
|
| 326 |
+
|
| 327 |
+
npvalues = self._ndarray.T
|
| 328 |
+
if copy:
|
| 329 |
+
npvalues = npvalues.copy()
|
| 330 |
+
func(npvalues, limit=limit, limit_area=limit_area, mask=mask.T)
|
| 331 |
+
npvalues = npvalues.T
|
| 332 |
+
|
| 333 |
+
if copy:
|
| 334 |
+
new_values = self._from_backing_data(npvalues)
|
| 335 |
+
else:
|
| 336 |
+
new_values = self
|
| 337 |
+
|
| 338 |
+
elif copy:
|
| 339 |
+
new_values = self.copy()
|
| 340 |
+
else:
|
| 341 |
+
new_values = self
|
| 342 |
+
return new_values
|
| 343 |
+
|
| 344 |
+
@doc(ExtensionArray.fillna)
|
| 345 |
+
def fillna(self, value, limit: int | None = None, copy: bool = True) -> Self:
|
| 346 |
+
mask = self.isna()
|
| 347 |
+
if limit is not None and limit < len(self):
|
| 348 |
+
# mypy doesn't like that mask can be an EA which need not have `cumsum`
|
| 349 |
+
modify = mask.cumsum() > limit # type: ignore[union-attr]
|
| 350 |
+
if modify.any():
|
| 351 |
+
# Only copy mask if necessary
|
| 352 |
+
mask = mask.copy()
|
| 353 |
+
mask[modify] = False
|
| 354 |
+
# error: Argument 2 to "check_value_size" has incompatible type
|
| 355 |
+
# "ExtensionArray"; expected "ndarray"
|
| 356 |
+
value = missing.check_value_size(
|
| 357 |
+
value,
|
| 358 |
+
mask, # type: ignore[arg-type]
|
| 359 |
+
len(self),
|
| 360 |
+
)
|
| 361 |
+
|
| 362 |
+
if mask.any():
|
| 363 |
+
# fill with value
|
| 364 |
+
if copy:
|
| 365 |
+
new_values = self.copy()
|
| 366 |
+
else:
|
| 367 |
+
new_values = self[:]
|
| 368 |
+
new_values[mask] = value
|
| 369 |
+
else:
|
| 370 |
+
# We validate the fill_value even if there is nothing to fill
|
| 371 |
+
self._validate_setitem_value(value)
|
| 372 |
+
|
| 373 |
+
if not copy:
|
| 374 |
+
new_values = self[:]
|
| 375 |
+
else:
|
| 376 |
+
new_values = self.copy()
|
| 377 |
+
return new_values
|
| 378 |
+
|
| 379 |
+
# ------------------------------------------------------------------------
|
| 380 |
+
# Reductions
|
| 381 |
+
|
| 382 |
+
def _wrap_reduction_result(self, axis: AxisInt | None, result) -> Any:
|
| 383 |
+
if axis is None or self.ndim == 1:
|
| 384 |
+
return self._box_func(result)
|
| 385 |
+
return self._from_backing_data(result)
|
| 386 |
+
|
| 387 |
+
# ------------------------------------------------------------------------
|
| 388 |
+
# __array_function__ methods
|
| 389 |
+
|
| 390 |
+
def _putmask(self, mask: npt.NDArray[np.bool_], value) -> None:
|
| 391 |
+
"""
|
| 392 |
+
Analogue to np.putmask(self, mask, value)
|
| 393 |
+
|
| 394 |
+
Parameters
|
| 395 |
+
----------
|
| 396 |
+
mask : np.ndarray[bool]
|
| 397 |
+
value : scalar or listlike
|
| 398 |
+
|
| 399 |
+
Raises
|
| 400 |
+
------
|
| 401 |
+
TypeError
|
| 402 |
+
If value cannot be cast to self.dtype.
|
| 403 |
+
"""
|
| 404 |
+
value = self._validate_setitem_value(value)
|
| 405 |
+
|
| 406 |
+
np.putmask(self._ndarray, mask, value)
|
| 407 |
+
|
| 408 |
+
def _where(self: Self, mask: npt.NDArray[np.bool_], value) -> Self:
|
| 409 |
+
"""
|
| 410 |
+
Analogue to np.where(mask, self, value)
|
| 411 |
+
|
| 412 |
+
Parameters
|
| 413 |
+
----------
|
| 414 |
+
mask : np.ndarray[bool]
|
| 415 |
+
value : scalar or listlike
|
| 416 |
+
|
| 417 |
+
Raises
|
| 418 |
+
------
|
| 419 |
+
TypeError
|
| 420 |
+
If value cannot be cast to self.dtype.
|
| 421 |
+
"""
|
| 422 |
+
value = self._validate_setitem_value(value)
|
| 423 |
+
|
| 424 |
+
res_values = np.where(mask, self._ndarray, value)
|
| 425 |
+
if res_values.dtype != self._ndarray.dtype:
|
| 426 |
+
raise AssertionError(
|
| 427 |
+
# GH#56410
|
| 428 |
+
"Something has gone wrong, please report a bug at "
|
| 429 |
+
"github.com/pandas-dev/pandas/"
|
| 430 |
+
)
|
| 431 |
+
return self._from_backing_data(res_values)
|
| 432 |
+
|
| 433 |
+
# ------------------------------------------------------------------------
|
| 434 |
+
# Index compat methods
|
| 435 |
+
|
| 436 |
+
def insert(self, loc: int, item) -> Self:
|
| 437 |
+
"""
|
| 438 |
+
Make new ExtensionArray inserting new item at location. Follows
|
| 439 |
+
Python list.append semantics for negative values.
|
| 440 |
+
|
| 441 |
+
Parameters
|
| 442 |
+
----------
|
| 443 |
+
loc : int
|
| 444 |
+
item : object
|
| 445 |
+
|
| 446 |
+
Returns
|
| 447 |
+
-------
|
| 448 |
+
type(self)
|
| 449 |
+
"""
|
| 450 |
+
loc = validate_insert_loc(loc, len(self))
|
| 451 |
+
|
| 452 |
+
code = self._validate_scalar(item)
|
| 453 |
+
|
| 454 |
+
new_vals = np.concatenate(
|
| 455 |
+
(
|
| 456 |
+
self._ndarray[:loc],
|
| 457 |
+
np.asarray([code], dtype=self._ndarray.dtype),
|
| 458 |
+
self._ndarray[loc:],
|
| 459 |
+
)
|
| 460 |
+
)
|
| 461 |
+
return self._from_backing_data(new_vals)
|
| 462 |
+
|
| 463 |
+
# ------------------------------------------------------------------------
|
| 464 |
+
# Additional array methods
|
| 465 |
+
# These are not part of the EA API, but we implement them because
|
| 466 |
+
# pandas assumes they're there.
|
| 467 |
+
|
| 468 |
+
def value_counts(self, dropna: bool = True) -> Series:
|
| 469 |
+
"""
|
| 470 |
+
Return a Series containing counts of unique values.
|
| 471 |
+
|
| 472 |
+
Parameters
|
| 473 |
+
----------
|
| 474 |
+
dropna : bool, default True
|
| 475 |
+
Don't include counts of NA values.
|
| 476 |
+
|
| 477 |
+
Returns
|
| 478 |
+
-------
|
| 479 |
+
Series
|
| 480 |
+
"""
|
| 481 |
+
if self.ndim != 1:
|
| 482 |
+
raise NotImplementedError
|
| 483 |
+
|
| 484 |
+
from pandas import (
|
| 485 |
+
Index,
|
| 486 |
+
Series,
|
| 487 |
+
)
|
| 488 |
+
|
| 489 |
+
if dropna:
|
| 490 |
+
# error: Unsupported operand type for ~ ("ExtensionArray")
|
| 491 |
+
values = self[~self.isna()]._ndarray # type: ignore[operator]
|
| 492 |
+
else:
|
| 493 |
+
values = self._ndarray
|
| 494 |
+
|
| 495 |
+
result = value_counts(values, sort=False, dropna=dropna)
|
| 496 |
+
|
| 497 |
+
index_arr = self._from_backing_data(np.asarray(result.index._data))
|
| 498 |
+
index = Index(index_arr, name=result.index.name, copy=False)
|
| 499 |
+
return Series(result._values, index=index, name=result.name, copy=False)
|
| 500 |
+
|
| 501 |
+
def _quantile(
|
| 502 |
+
self,
|
| 503 |
+
qs: npt.NDArray[np.float64],
|
| 504 |
+
interpolation: str,
|
| 505 |
+
) -> Self:
|
| 506 |
+
# TODO: disable for Categorical if not ordered?
|
| 507 |
+
|
| 508 |
+
mask = np.asarray(self.isna())
|
| 509 |
+
arr = self._ndarray
|
| 510 |
+
fill_value = self._internal_fill_value
|
| 511 |
+
|
| 512 |
+
res_values = quantile_with_mask(arr, mask, fill_value, qs, interpolation)
|
| 513 |
+
if res_values.dtype == self._ndarray.dtype:
|
| 514 |
+
return self._from_backing_data(res_values)
|
| 515 |
+
else:
|
| 516 |
+
# e.g. test_quantile_empty we are empty integer dtype and res_values
|
| 517 |
+
# has floating dtype
|
| 518 |
+
# TODO: technically __init__ isn't defined here.
|
| 519 |
+
# Should we raise NotImplementedError and handle this on NumpyEA?
|
| 520 |
+
return type(self)(res_values) # type: ignore[call-arg]
|
| 521 |
+
|
| 522 |
+
# ------------------------------------------------------------------------
|
| 523 |
+
# numpy-like methods
|
| 524 |
+
|
| 525 |
+
@classmethod
|
| 526 |
+
def _empty(cls, shape: Shape, dtype: ExtensionDtype) -> Self:
|
| 527 |
+
"""
|
| 528 |
+
Analogous to np.empty(shape, dtype=dtype)
|
| 529 |
+
|
| 530 |
+
Parameters
|
| 531 |
+
----------
|
| 532 |
+
shape : tuple[int]
|
| 533 |
+
dtype : ExtensionDtype
|
| 534 |
+
"""
|
| 535 |
+
# The base implementation uses a naive approach to find the dtype
|
| 536 |
+
# for the backing ndarray
|
| 537 |
+
arr = cls._from_sequence([], dtype=dtype)
|
| 538 |
+
backing = np.empty(shape, dtype=arr._ndarray.dtype)
|
| 539 |
+
return arr._from_backing_data(backing)
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/_ranges.py
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Helper functions to generate range-like data for DatetimeArray
|
| 3 |
+
(and possibly TimedeltaArray/PeriodArray)
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from __future__ import annotations
|
| 7 |
+
|
| 8 |
+
from typing import TYPE_CHECKING
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
from pandas._libs.lib import i8max
|
| 13 |
+
from pandas._libs.tslibs import (
|
| 14 |
+
BaseOffset,
|
| 15 |
+
Day,
|
| 16 |
+
OutOfBoundsDatetime,
|
| 17 |
+
Timedelta,
|
| 18 |
+
Timestamp,
|
| 19 |
+
iNaT,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
from pandas.core.construction import range_to_ndarray
|
| 23 |
+
|
| 24 |
+
if TYPE_CHECKING:
|
| 25 |
+
from pandas._typing import (
|
| 26 |
+
TimeUnit,
|
| 27 |
+
npt,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def generate_regular_range(
|
| 32 |
+
start: Timestamp | Timedelta | None,
|
| 33 |
+
end: Timestamp | Timedelta | None,
|
| 34 |
+
periods: int | None,
|
| 35 |
+
freq: BaseOffset,
|
| 36 |
+
unit: TimeUnit = "ns",
|
| 37 |
+
) -> npt.NDArray[np.intp]:
|
| 38 |
+
"""
|
| 39 |
+
Generate a range of dates or timestamps with the spans between dates
|
| 40 |
+
described by the given `freq` DateOffset.
|
| 41 |
+
|
| 42 |
+
Parameters
|
| 43 |
+
----------
|
| 44 |
+
start : Timedelta, Timestamp or None
|
| 45 |
+
First point of produced date range.
|
| 46 |
+
end : Timedelta, Timestamp or None
|
| 47 |
+
Last point of produced date range.
|
| 48 |
+
periods : int or None
|
| 49 |
+
Number of periods in produced date range.
|
| 50 |
+
freq : Tick
|
| 51 |
+
Describes space between dates in produced date range.
|
| 52 |
+
unit : {'s', 'ms', 'us', 'ns'}, default "ns"
|
| 53 |
+
The resolution the output is meant to represent.
|
| 54 |
+
|
| 55 |
+
Returns
|
| 56 |
+
-------
|
| 57 |
+
ndarray[np.int64]
|
| 58 |
+
Representing the given resolution.
|
| 59 |
+
"""
|
| 60 |
+
istart = start._value if start is not None else None
|
| 61 |
+
iend = end._value if end is not None else None
|
| 62 |
+
if isinstance(freq, Day):
|
| 63 |
+
# In contexts without a timezone, a Day offset is unambiguously
|
| 64 |
+
# interpretable as Timedelta-like.
|
| 65 |
+
td = Timedelta(days=freq.n)
|
| 66 |
+
else:
|
| 67 |
+
freq.nanos # raises if non-fixed frequency
|
| 68 |
+
td = Timedelta(freq)
|
| 69 |
+
b: int
|
| 70 |
+
e: int
|
| 71 |
+
try:
|
| 72 |
+
td = td.as_unit(unit, round_ok=False)
|
| 73 |
+
except ValueError as err:
|
| 74 |
+
raise ValueError(
|
| 75 |
+
f"freq={freq} is incompatible with unit={unit}. "
|
| 76 |
+
"Use a lower freq or a higher unit instead."
|
| 77 |
+
) from err
|
| 78 |
+
stride = int(td._value)
|
| 79 |
+
|
| 80 |
+
if periods is None and istart is not None and iend is not None:
|
| 81 |
+
b = istart
|
| 82 |
+
# cannot just use e = Timestamp(end) + 1 because arange breaks when
|
| 83 |
+
# stride is too large, see GH10887
|
| 84 |
+
e = b + (iend - b) // stride * stride + stride // 2 + 1
|
| 85 |
+
elif istart is not None and periods is not None:
|
| 86 |
+
b = istart
|
| 87 |
+
e = _generate_range_overflow_safe(b, periods, stride, side="start")
|
| 88 |
+
elif iend is not None and periods is not None:
|
| 89 |
+
e = iend + stride
|
| 90 |
+
b = _generate_range_overflow_safe(e, periods, stride, side="end")
|
| 91 |
+
else:
|
| 92 |
+
raise ValueError(
|
| 93 |
+
"at least 'start' or 'end' should be specified if a 'period' is given."
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
return range_to_ndarray(range(b, e, stride))
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def _generate_range_overflow_safe(
|
| 100 |
+
endpoint: int, periods: int, stride: int, side: str = "start"
|
| 101 |
+
) -> int:
|
| 102 |
+
"""
|
| 103 |
+
Calculate the second endpoint for passing to np.arange, checking
|
| 104 |
+
to avoid an integer overflow. Catch OverflowError and re-raise
|
| 105 |
+
as OutOfBoundsDatetime.
|
| 106 |
+
|
| 107 |
+
Parameters
|
| 108 |
+
----------
|
| 109 |
+
endpoint : int
|
| 110 |
+
nanosecond timestamp of the known endpoint of the desired range
|
| 111 |
+
periods : int
|
| 112 |
+
number of periods in the desired range
|
| 113 |
+
stride : int
|
| 114 |
+
nanoseconds between periods in the desired range
|
| 115 |
+
side : {'start', 'end'}
|
| 116 |
+
which end of the range `endpoint` refers to
|
| 117 |
+
|
| 118 |
+
Returns
|
| 119 |
+
-------
|
| 120 |
+
other_end : int
|
| 121 |
+
|
| 122 |
+
Raises
|
| 123 |
+
------
|
| 124 |
+
OutOfBoundsDatetime
|
| 125 |
+
"""
|
| 126 |
+
# GH#14187 raise instead of incorrectly wrapping around
|
| 127 |
+
assert side in ["start", "end"]
|
| 128 |
+
|
| 129 |
+
i64max = np.uint64(i8max)
|
| 130 |
+
msg = f"Cannot generate range with {side}={endpoint} and periods={periods}"
|
| 131 |
+
|
| 132 |
+
with np.errstate(over="raise"):
|
| 133 |
+
# if periods * strides cannot be multiplied within the *uint64* bounds,
|
| 134 |
+
# we cannot salvage the operation by recursing, so raise
|
| 135 |
+
try:
|
| 136 |
+
addend = np.uint64(periods) * np.uint64(np.abs(stride))
|
| 137 |
+
except FloatingPointError as err:
|
| 138 |
+
raise OutOfBoundsDatetime(msg) from err
|
| 139 |
+
|
| 140 |
+
if np.abs(addend) <= i64max:
|
| 141 |
+
# relatively easy case without casting concerns
|
| 142 |
+
return _generate_range_overflow_safe_signed(endpoint, periods, stride, side)
|
| 143 |
+
|
| 144 |
+
elif (endpoint > 0 and side == "start" and stride > 0) or (
|
| 145 |
+
endpoint < 0 < stride and side == "end"
|
| 146 |
+
):
|
| 147 |
+
# no chance of not-overflowing
|
| 148 |
+
raise OutOfBoundsDatetime(msg)
|
| 149 |
+
|
| 150 |
+
elif side == "end" and endpoint - stride <= i64max < endpoint:
|
| 151 |
+
# in _generate_regular_range we added `stride` thereby overflowing
|
| 152 |
+
# the bounds. Adjust to fix this.
|
| 153 |
+
return _generate_range_overflow_safe(
|
| 154 |
+
endpoint - stride, periods - 1, stride, side
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
# split into smaller pieces
|
| 158 |
+
mid_periods = periods // 2
|
| 159 |
+
remaining = periods - mid_periods
|
| 160 |
+
assert 0 < remaining < periods, (remaining, periods, endpoint, stride)
|
| 161 |
+
|
| 162 |
+
midpoint = int(_generate_range_overflow_safe(endpoint, mid_periods, stride, side))
|
| 163 |
+
return _generate_range_overflow_safe(midpoint, remaining, stride, side)
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def _generate_range_overflow_safe_signed(
|
| 167 |
+
endpoint: int, periods: int, stride: int, side: str
|
| 168 |
+
) -> int:
|
| 169 |
+
"""
|
| 170 |
+
A special case for _generate_range_overflow_safe where `periods * stride`
|
| 171 |
+
can be calculated without overflowing int64 bounds.
|
| 172 |
+
"""
|
| 173 |
+
assert side in ["start", "end"]
|
| 174 |
+
if side == "end":
|
| 175 |
+
stride *= -1
|
| 176 |
+
|
| 177 |
+
with np.errstate(over="raise"):
|
| 178 |
+
addend = np.int64(periods) * np.int64(stride)
|
| 179 |
+
try:
|
| 180 |
+
# easy case with no overflows
|
| 181 |
+
result = np.int64(endpoint) + addend
|
| 182 |
+
if result == iNaT:
|
| 183 |
+
# Putting this into a DatetimeArray/TimedeltaArray
|
| 184 |
+
# would incorrectly be interpreted as NaT
|
| 185 |
+
raise OverflowError
|
| 186 |
+
return int(result)
|
| 187 |
+
except (FloatingPointError, OverflowError):
|
| 188 |
+
# with endpoint negative and addend positive we risk
|
| 189 |
+
# FloatingPointError; with reversed signed we risk OverflowError
|
| 190 |
+
pass
|
| 191 |
+
|
| 192 |
+
# if stride and endpoint had opposite signs, then endpoint + addend
|
| 193 |
+
# should never overflow. so they must have the same signs
|
| 194 |
+
assert (stride > 0 and endpoint >= 0) or (stride < 0 and endpoint <= 0)
|
| 195 |
+
|
| 196 |
+
if stride > 0:
|
| 197 |
+
# watch out for very special case in which we just slightly
|
| 198 |
+
# exceed implementation bounds, but when passing the result to
|
| 199 |
+
# np.arange will get a result slightly within the bounds
|
| 200 |
+
|
| 201 |
+
uresult = np.uint64(endpoint) + np.uint64(addend)
|
| 202 |
+
i64max = np.uint64(i8max)
|
| 203 |
+
assert uresult > i64max
|
| 204 |
+
if uresult <= i64max + np.uint64(stride):
|
| 205 |
+
return int(uresult)
|
| 206 |
+
|
| 207 |
+
raise OutOfBoundsDatetime(
|
| 208 |
+
f"Cannot generate range with {side}={endpoint} and periods={periods}"
|
| 209 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/_utils.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import (
|
| 4 |
+
TYPE_CHECKING,
|
| 5 |
+
Any,
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
from pandas._config import is_nan_na
|
| 11 |
+
|
| 12 |
+
from pandas._libs import lib
|
| 13 |
+
from pandas._libs.missing import NA
|
| 14 |
+
from pandas.errors import LossySetitemError
|
| 15 |
+
|
| 16 |
+
from pandas.core.dtypes.cast import np_can_hold_element
|
| 17 |
+
from pandas.core.dtypes.common import is_numeric_dtype
|
| 18 |
+
|
| 19 |
+
if TYPE_CHECKING:
|
| 20 |
+
from pandas._typing import (
|
| 21 |
+
npt,
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
from pandas.core.arrays.base import ExtensionArray
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def to_numpy_dtype_inference(
|
| 28 |
+
arr: ExtensionArray,
|
| 29 |
+
dtype: npt.DTypeLike | None,
|
| 30 |
+
na_value,
|
| 31 |
+
hasna: bool,
|
| 32 |
+
) -> tuple[np.dtype | None, Any]:
|
| 33 |
+
result_dtype: np.dtype | None
|
| 34 |
+
inferred_numeric_dtype = False
|
| 35 |
+
if dtype is None and is_numeric_dtype(arr.dtype):
|
| 36 |
+
inferred_numeric_dtype = True
|
| 37 |
+
if hasna:
|
| 38 |
+
if arr.dtype.kind == "b":
|
| 39 |
+
result_dtype = np.dtype(np.object_)
|
| 40 |
+
else:
|
| 41 |
+
if arr.dtype.kind in "iu":
|
| 42 |
+
result_dtype = np.dtype(np.float64)
|
| 43 |
+
else:
|
| 44 |
+
result_dtype = arr.dtype.numpy_dtype # type: ignore[attr-defined]
|
| 45 |
+
if na_value is lib.no_default:
|
| 46 |
+
if not is_nan_na():
|
| 47 |
+
na_value = NA
|
| 48 |
+
dtype = np.dtype(object)
|
| 49 |
+
else:
|
| 50 |
+
na_value = np.nan
|
| 51 |
+
else:
|
| 52 |
+
result_dtype = arr.dtype.numpy_dtype # type: ignore[attr-defined]
|
| 53 |
+
elif dtype is not None:
|
| 54 |
+
result_dtype = np.dtype(dtype)
|
| 55 |
+
else:
|
| 56 |
+
result_dtype = None
|
| 57 |
+
|
| 58 |
+
if na_value is lib.no_default:
|
| 59 |
+
if result_dtype is None or not hasna:
|
| 60 |
+
na_value = arr.dtype.na_value
|
| 61 |
+
elif result_dtype.kind == "f":
|
| 62 |
+
na_value = np.nan
|
| 63 |
+
elif result_dtype.kind == "M":
|
| 64 |
+
na_value = np.datetime64("nat")
|
| 65 |
+
elif result_dtype.kind == "m":
|
| 66 |
+
na_value = np.timedelta64("nat")
|
| 67 |
+
else:
|
| 68 |
+
na_value = arr.dtype.na_value
|
| 69 |
+
|
| 70 |
+
if inferred_numeric_dtype and hasna:
|
| 71 |
+
try:
|
| 72 |
+
np_can_hold_element(result_dtype, na_value) # type: ignore[arg-type]
|
| 73 |
+
except LossySetitemError:
|
| 74 |
+
result_dtype = np.dtype(np.object_)
|
| 75 |
+
return result_dtype, na_value
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/__init__.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pandas.core.arrays.arrow.accessors import (
|
| 2 |
+
ListAccessor,
|
| 3 |
+
StructAccessor,
|
| 4 |
+
)
|
| 5 |
+
from pandas.core.arrays.arrow.array import ArrowExtensionArray
|
| 6 |
+
|
| 7 |
+
__all__ = ["ArrowExtensionArray", "ListAccessor", "StructAccessor"]
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (418 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/__pycache__/accessors.cpython-312.pyc
ADDED
|
Binary file (17.4 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/__pycache__/array.cpython-312.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0ebc5b31b420a69b57dce1c0c71fdcdc6a615cb673a8be8aa1cde1c90c21fe17
|
| 3 |
+
size 155819
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/_arrow_utils.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pyarrow
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def pyarrow_array_to_numpy_and_mask(
|
| 8 |
+
arr, dtype: np.dtype
|
| 9 |
+
) -> tuple[np.ndarray, np.ndarray]:
|
| 10 |
+
"""
|
| 11 |
+
Convert a primitive pyarrow.Array to a numpy array and boolean mask based
|
| 12 |
+
on the buffers of the Array.
|
| 13 |
+
|
| 14 |
+
At the moment pyarrow.BooleanArray is not supported.
|
| 15 |
+
|
| 16 |
+
Parameters
|
| 17 |
+
----------
|
| 18 |
+
arr : pyarrow.Array
|
| 19 |
+
dtype : numpy.dtype
|
| 20 |
+
|
| 21 |
+
Returns
|
| 22 |
+
-------
|
| 23 |
+
(data, mask)
|
| 24 |
+
Tuple of two numpy arrays with the raw data (with specified dtype) and
|
| 25 |
+
a boolean mask (validity mask, so False means missing)
|
| 26 |
+
"""
|
| 27 |
+
dtype = np.dtype(dtype)
|
| 28 |
+
|
| 29 |
+
if pyarrow.types.is_null(arr.type):
|
| 30 |
+
# No initialization of data is needed since everything is null
|
| 31 |
+
data = np.empty(len(arr), dtype=dtype)
|
| 32 |
+
mask = np.zeros(len(arr), dtype=bool)
|
| 33 |
+
return data, mask
|
| 34 |
+
buflist = arr.buffers()
|
| 35 |
+
# Since Arrow buffers might contain padding and the data might be offset,
|
| 36 |
+
# the buffer gets sliced here before handing it to numpy.
|
| 37 |
+
# See also https://github.com/pandas-dev/pandas/issues/40896
|
| 38 |
+
offset = arr.offset * dtype.itemsize
|
| 39 |
+
length = len(arr) * dtype.itemsize
|
| 40 |
+
data_buf = buflist[1][offset : offset + length]
|
| 41 |
+
data = np.frombuffer(data_buf, dtype=dtype)
|
| 42 |
+
bitmask = buflist[0]
|
| 43 |
+
if bitmask is not None:
|
| 44 |
+
mask = pyarrow.BooleanArray.from_buffers(
|
| 45 |
+
pyarrow.bool_(), len(arr), [None, bitmask], offset=arr.offset
|
| 46 |
+
)
|
| 47 |
+
mask = np.asarray(mask)
|
| 48 |
+
else:
|
| 49 |
+
mask = np.ones(len(arr), dtype=bool)
|
| 50 |
+
return data, mask
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/accessors.py
ADDED
|
@@ -0,0 +1,501 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Accessors for arrow-backed data."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from abc import (
|
| 6 |
+
ABCMeta,
|
| 7 |
+
abstractmethod,
|
| 8 |
+
)
|
| 9 |
+
from typing import (
|
| 10 |
+
TYPE_CHECKING,
|
| 11 |
+
cast,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
from pandas.compat import HAS_PYARROW
|
| 15 |
+
|
| 16 |
+
from pandas.core.dtypes.common import is_list_like
|
| 17 |
+
|
| 18 |
+
if HAS_PYARROW:
|
| 19 |
+
import pyarrow as pa
|
| 20 |
+
import pyarrow.compute as pc
|
| 21 |
+
|
| 22 |
+
from pandas.core.dtypes.dtypes import ArrowDtype
|
| 23 |
+
|
| 24 |
+
if TYPE_CHECKING:
|
| 25 |
+
from collections.abc import Iterator
|
| 26 |
+
|
| 27 |
+
from pandas import (
|
| 28 |
+
DataFrame,
|
| 29 |
+
Series,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class ArrowAccessor(metaclass=ABCMeta):
|
| 34 |
+
@abstractmethod
|
| 35 |
+
def __init__(self, data, validation_msg: str) -> None:
|
| 36 |
+
self._data = data
|
| 37 |
+
self._validation_msg = validation_msg
|
| 38 |
+
self._validate(data)
|
| 39 |
+
|
| 40 |
+
@abstractmethod
|
| 41 |
+
def _is_valid_pyarrow_dtype(self, pyarrow_dtype) -> bool:
|
| 42 |
+
pass
|
| 43 |
+
|
| 44 |
+
def _validate(self, data) -> None:
|
| 45 |
+
dtype = data.dtype
|
| 46 |
+
if not HAS_PYARROW or not isinstance(dtype, ArrowDtype):
|
| 47 |
+
# Raise AttributeError so that inspect can handle non-struct Series.
|
| 48 |
+
raise AttributeError(self._validation_msg.format(dtype=dtype))
|
| 49 |
+
|
| 50 |
+
if not self._is_valid_pyarrow_dtype(dtype.pyarrow_dtype):
|
| 51 |
+
# Raise AttributeError so that inspect can handle invalid Series.
|
| 52 |
+
raise AttributeError(self._validation_msg.format(dtype=dtype))
|
| 53 |
+
|
| 54 |
+
@property
|
| 55 |
+
def _pa_array(self):
|
| 56 |
+
return self._data.array._pa_array
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class ListAccessor(ArrowAccessor):
|
| 60 |
+
"""
|
| 61 |
+
Accessor object for list data properties of the Series values.
|
| 62 |
+
|
| 63 |
+
Parameters
|
| 64 |
+
----------
|
| 65 |
+
data : Series
|
| 66 |
+
Series containing Arrow list data.
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
def __init__(self, data=None) -> None:
|
| 70 |
+
super().__init__(
|
| 71 |
+
data,
|
| 72 |
+
validation_msg="Can only use the '.list' accessor with "
|
| 73 |
+
"'list[pyarrow]' dtype, not {dtype}.",
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
def _is_valid_pyarrow_dtype(self, pyarrow_dtype) -> bool:
|
| 77 |
+
return (
|
| 78 |
+
pa.types.is_list(pyarrow_dtype)
|
| 79 |
+
or pa.types.is_fixed_size_list(pyarrow_dtype)
|
| 80 |
+
or pa.types.is_large_list(pyarrow_dtype)
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
def len(self) -> Series:
|
| 84 |
+
"""
|
| 85 |
+
Return the length of each list in the Series.
|
| 86 |
+
|
| 87 |
+
Returns
|
| 88 |
+
-------
|
| 89 |
+
pandas.Series
|
| 90 |
+
The length of each list.
|
| 91 |
+
|
| 92 |
+
See Also
|
| 93 |
+
--------
|
| 94 |
+
str.len : Python built-in function returning the length of an object.
|
| 95 |
+
Series.size : Returns the length of the Series.
|
| 96 |
+
StringMethods.len : Compute the length of each element in the Series/Index.
|
| 97 |
+
|
| 98 |
+
Examples
|
| 99 |
+
--------
|
| 100 |
+
>>> import pyarrow as pa
|
| 101 |
+
>>> s = pd.Series(
|
| 102 |
+
... [
|
| 103 |
+
... [1, 2, 3],
|
| 104 |
+
... [3],
|
| 105 |
+
... ],
|
| 106 |
+
... dtype=pd.ArrowDtype(pa.list_(pa.int64())),
|
| 107 |
+
... )
|
| 108 |
+
>>> s.list.len()
|
| 109 |
+
0 3
|
| 110 |
+
1 1
|
| 111 |
+
dtype: int32[pyarrow]
|
| 112 |
+
"""
|
| 113 |
+
from pandas import Series
|
| 114 |
+
|
| 115 |
+
value_lengths = pc.list_value_length(self._pa_array)
|
| 116 |
+
return Series(
|
| 117 |
+
value_lengths,
|
| 118 |
+
dtype=ArrowDtype(value_lengths.type),
|
| 119 |
+
index=self._data.index,
|
| 120 |
+
name=self._data.name,
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
def __getitem__(self, key: int | slice) -> Series:
|
| 124 |
+
"""
|
| 125 |
+
Index or slice lists in the Series.
|
| 126 |
+
|
| 127 |
+
Parameters
|
| 128 |
+
----------
|
| 129 |
+
key : int | slice
|
| 130 |
+
Index or slice of indices to access from each list.
|
| 131 |
+
|
| 132 |
+
Returns
|
| 133 |
+
-------
|
| 134 |
+
pandas.Series
|
| 135 |
+
The list at requested index.
|
| 136 |
+
|
| 137 |
+
See Also
|
| 138 |
+
--------
|
| 139 |
+
ListAccessor.flatten : Flatten list values.
|
| 140 |
+
|
| 141 |
+
Examples
|
| 142 |
+
--------
|
| 143 |
+
>>> import pyarrow as pa
|
| 144 |
+
>>> s = pd.Series(
|
| 145 |
+
... [
|
| 146 |
+
... [1, 2, 3],
|
| 147 |
+
... [3],
|
| 148 |
+
... ],
|
| 149 |
+
... dtype=pd.ArrowDtype(pa.list_(pa.int64())),
|
| 150 |
+
... )
|
| 151 |
+
>>> s.list[0]
|
| 152 |
+
0 1
|
| 153 |
+
1 3
|
| 154 |
+
dtype: int64[pyarrow]
|
| 155 |
+
"""
|
| 156 |
+
from pandas import Series
|
| 157 |
+
|
| 158 |
+
if isinstance(key, int):
|
| 159 |
+
# TODO: Support negative key but pyarrow does not allow
|
| 160 |
+
# element index to be an array.
|
| 161 |
+
# if key < 0:
|
| 162 |
+
# key = pc.add(key, pc.list_value_length(self._pa_array))
|
| 163 |
+
element = pc.list_element(self._pa_array, key)
|
| 164 |
+
return Series(
|
| 165 |
+
element,
|
| 166 |
+
dtype=ArrowDtype(element.type),
|
| 167 |
+
index=self._data.index,
|
| 168 |
+
name=self._data.name,
|
| 169 |
+
)
|
| 170 |
+
elif isinstance(key, slice):
|
| 171 |
+
# TODO: Support negative start/stop/step, ideally this would be added
|
| 172 |
+
# upstream in pyarrow.
|
| 173 |
+
start, stop, step = key.start, key.stop, key.step
|
| 174 |
+
if start is None:
|
| 175 |
+
# TODO: When adding negative step support
|
| 176 |
+
# this should be setto last element of array
|
| 177 |
+
# when step is negative.
|
| 178 |
+
start = 0
|
| 179 |
+
if step is None:
|
| 180 |
+
step = 1
|
| 181 |
+
sliced = pc.list_slice(self._pa_array, start, stop, step)
|
| 182 |
+
return Series(
|
| 183 |
+
sliced,
|
| 184 |
+
dtype=ArrowDtype(sliced.type),
|
| 185 |
+
index=self._data.index,
|
| 186 |
+
name=self._data.name,
|
| 187 |
+
)
|
| 188 |
+
else:
|
| 189 |
+
raise ValueError(f"key must be an int or slice, got {type(key).__name__}")
|
| 190 |
+
|
| 191 |
+
def __iter__(self) -> Iterator:
|
| 192 |
+
raise TypeError(f"'{type(self).__name__}' object is not iterable")
|
| 193 |
+
|
| 194 |
+
def flatten(self) -> Series:
|
| 195 |
+
"""
|
| 196 |
+
Flatten list values.
|
| 197 |
+
|
| 198 |
+
Returns
|
| 199 |
+
-------
|
| 200 |
+
pandas.Series
|
| 201 |
+
The data from all lists in the series flattened.
|
| 202 |
+
|
| 203 |
+
See Also
|
| 204 |
+
--------
|
| 205 |
+
ListAccessor.__getitem__ : Index or slice values in the Series.
|
| 206 |
+
|
| 207 |
+
Examples
|
| 208 |
+
--------
|
| 209 |
+
>>> import pyarrow as pa
|
| 210 |
+
>>> s = pd.Series(
|
| 211 |
+
... [
|
| 212 |
+
... [1, 2, 3],
|
| 213 |
+
... [3],
|
| 214 |
+
... ],
|
| 215 |
+
... dtype=pd.ArrowDtype(pa.list_(pa.int64())),
|
| 216 |
+
... )
|
| 217 |
+
>>> s.list.flatten()
|
| 218 |
+
0 1
|
| 219 |
+
0 2
|
| 220 |
+
0 3
|
| 221 |
+
1 3
|
| 222 |
+
dtype: int64[pyarrow]
|
| 223 |
+
"""
|
| 224 |
+
from pandas import Series
|
| 225 |
+
|
| 226 |
+
counts = pa.compute.list_value_length(self._pa_array)
|
| 227 |
+
flattened = pa.compute.list_flatten(self._pa_array)
|
| 228 |
+
index = self._data.index.repeat(counts.fill_null(pa.scalar(0, counts.type)))
|
| 229 |
+
return Series(
|
| 230 |
+
flattened,
|
| 231 |
+
dtype=ArrowDtype(flattened.type),
|
| 232 |
+
index=index,
|
| 233 |
+
name=self._data.name,
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
class StructAccessor(ArrowAccessor):
|
| 238 |
+
"""
|
| 239 |
+
Accessor object for structured data properties of the Series values.
|
| 240 |
+
|
| 241 |
+
Parameters
|
| 242 |
+
----------
|
| 243 |
+
data : Series
|
| 244 |
+
Series containing Arrow struct data.
|
| 245 |
+
"""
|
| 246 |
+
|
| 247 |
+
def __init__(self, data=None) -> None:
|
| 248 |
+
super().__init__(
|
| 249 |
+
data,
|
| 250 |
+
validation_msg=(
|
| 251 |
+
"Can only use the '.struct' accessor with 'struct[pyarrow]' "
|
| 252 |
+
"dtype, not {dtype}."
|
| 253 |
+
),
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
def _is_valid_pyarrow_dtype(self, pyarrow_dtype) -> bool:
|
| 257 |
+
return pa.types.is_struct(pyarrow_dtype)
|
| 258 |
+
|
| 259 |
+
@property
|
| 260 |
+
def dtypes(self) -> Series:
|
| 261 |
+
"""
|
| 262 |
+
Return the dtype object of each child field of the struct.
|
| 263 |
+
|
| 264 |
+
Returns
|
| 265 |
+
-------
|
| 266 |
+
pandas.Series
|
| 267 |
+
The data type of each child field.
|
| 268 |
+
|
| 269 |
+
See Also
|
| 270 |
+
--------
|
| 271 |
+
Series.dtype: Return the dtype object of the underlying data.
|
| 272 |
+
|
| 273 |
+
Examples
|
| 274 |
+
--------
|
| 275 |
+
>>> import pyarrow as pa
|
| 276 |
+
>>> s = pd.Series(
|
| 277 |
+
... [
|
| 278 |
+
... {"version": 1, "project": "pandas"},
|
| 279 |
+
... {"version": 2, "project": "pandas"},
|
| 280 |
+
... {"version": 1, "project": "numpy"},
|
| 281 |
+
... ],
|
| 282 |
+
... dtype=pd.ArrowDtype(
|
| 283 |
+
... pa.struct([("version", pa.int64()), ("project", pa.string())])
|
| 284 |
+
... ),
|
| 285 |
+
... )
|
| 286 |
+
>>> s.struct.dtypes
|
| 287 |
+
version int64[pyarrow]
|
| 288 |
+
project string[pyarrow]
|
| 289 |
+
dtype: object
|
| 290 |
+
"""
|
| 291 |
+
from pandas import (
|
| 292 |
+
Index,
|
| 293 |
+
Series,
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
pa_type = self._data.dtype.pyarrow_dtype
|
| 297 |
+
types = [ArrowDtype(struct.type) for struct in pa_type]
|
| 298 |
+
names = [struct.name for struct in pa_type]
|
| 299 |
+
return Series(types, index=Index(names))
|
| 300 |
+
|
| 301 |
+
def field(
|
| 302 |
+
self,
|
| 303 |
+
name_or_index: list[str]
|
| 304 |
+
| list[bytes]
|
| 305 |
+
| list[int]
|
| 306 |
+
| pc.Expression
|
| 307 |
+
| bytes
|
| 308 |
+
| str
|
| 309 |
+
| int,
|
| 310 |
+
) -> Series:
|
| 311 |
+
"""
|
| 312 |
+
Extract a child field of a struct as a Series.
|
| 313 |
+
|
| 314 |
+
Parameters
|
| 315 |
+
----------
|
| 316 |
+
name_or_index : str | bytes | int | expression | list
|
| 317 |
+
Name or index of the child field to extract.
|
| 318 |
+
|
| 319 |
+
For list-like inputs, this will index into a nested
|
| 320 |
+
struct.
|
| 321 |
+
|
| 322 |
+
Returns
|
| 323 |
+
-------
|
| 324 |
+
pandas.Series
|
| 325 |
+
The data corresponding to the selected child field.
|
| 326 |
+
|
| 327 |
+
See Also
|
| 328 |
+
--------
|
| 329 |
+
Series.struct.explode : Return all child fields as a DataFrame.
|
| 330 |
+
|
| 331 |
+
Notes
|
| 332 |
+
-----
|
| 333 |
+
The name of the resulting Series will be set using the following
|
| 334 |
+
rules:
|
| 335 |
+
|
| 336 |
+
- For string, bytes, or integer `name_or_index` (or a list of these, for
|
| 337 |
+
a nested selection), the Series name is set to the selected
|
| 338 |
+
field's name.
|
| 339 |
+
- For a :class:`pyarrow.compute.Expression`, this is set to
|
| 340 |
+
the string form of the expression.
|
| 341 |
+
- For list-like `name_or_index`, the name will be set to the
|
| 342 |
+
name of the final field selected.
|
| 343 |
+
|
| 344 |
+
Examples
|
| 345 |
+
--------
|
| 346 |
+
>>> import pyarrow as pa
|
| 347 |
+
>>> s = pd.Series(
|
| 348 |
+
... [
|
| 349 |
+
... {"version": 1, "project": "pandas"},
|
| 350 |
+
... {"version": 2, "project": "pandas"},
|
| 351 |
+
... {"version": 1, "project": "numpy"},
|
| 352 |
+
... ],
|
| 353 |
+
... dtype=pd.ArrowDtype(
|
| 354 |
+
... pa.struct([("version", pa.int64()), ("project", pa.string())])
|
| 355 |
+
... ),
|
| 356 |
+
... )
|
| 357 |
+
|
| 358 |
+
Extract by field name.
|
| 359 |
+
|
| 360 |
+
>>> s.struct.field("project")
|
| 361 |
+
0 pandas
|
| 362 |
+
1 pandas
|
| 363 |
+
2 numpy
|
| 364 |
+
Name: project, dtype: string[pyarrow]
|
| 365 |
+
|
| 366 |
+
Extract by field index.
|
| 367 |
+
|
| 368 |
+
>>> s.struct.field(0)
|
| 369 |
+
0 1
|
| 370 |
+
1 2
|
| 371 |
+
2 1
|
| 372 |
+
Name: version, dtype: int64[pyarrow]
|
| 373 |
+
|
| 374 |
+
Or an expression
|
| 375 |
+
|
| 376 |
+
>>> import pyarrow.compute as pc
|
| 377 |
+
>>> s.struct.field(pc.field("project"))
|
| 378 |
+
0 pandas
|
| 379 |
+
1 pandas
|
| 380 |
+
2 numpy
|
| 381 |
+
Name: project, dtype: string[pyarrow]
|
| 382 |
+
|
| 383 |
+
For nested struct types, you can pass a list of values to index
|
| 384 |
+
multiple levels:
|
| 385 |
+
|
| 386 |
+
>>> version_type = pa.struct(
|
| 387 |
+
... [
|
| 388 |
+
... ("major", pa.int64()),
|
| 389 |
+
... ("minor", pa.int64()),
|
| 390 |
+
... ]
|
| 391 |
+
... )
|
| 392 |
+
>>> s = pd.Series(
|
| 393 |
+
... [
|
| 394 |
+
... {"version": {"major": 1, "minor": 5}, "project": "pandas"},
|
| 395 |
+
... {"version": {"major": 2, "minor": 1}, "project": "pandas"},
|
| 396 |
+
... {"version": {"major": 1, "minor": 26}, "project": "numpy"},
|
| 397 |
+
... ],
|
| 398 |
+
... dtype=pd.ArrowDtype(
|
| 399 |
+
... pa.struct([("version", version_type), ("project", pa.string())])
|
| 400 |
+
... ),
|
| 401 |
+
... )
|
| 402 |
+
>>> s.struct.field(["version", "minor"])
|
| 403 |
+
0 5
|
| 404 |
+
1 1
|
| 405 |
+
2 26
|
| 406 |
+
Name: minor, dtype: int64[pyarrow]
|
| 407 |
+
>>> s.struct.field([0, 0])
|
| 408 |
+
0 1
|
| 409 |
+
1 2
|
| 410 |
+
2 1
|
| 411 |
+
Name: major, dtype: int64[pyarrow]
|
| 412 |
+
"""
|
| 413 |
+
from pandas import Series
|
| 414 |
+
|
| 415 |
+
def get_name(
|
| 416 |
+
level_name_or_index: list[str]
|
| 417 |
+
| list[bytes]
|
| 418 |
+
| list[int]
|
| 419 |
+
| pc.Expression
|
| 420 |
+
| bytes
|
| 421 |
+
| str
|
| 422 |
+
| int,
|
| 423 |
+
data: pa.ChunkedArray,
|
| 424 |
+
):
|
| 425 |
+
if isinstance(level_name_or_index, int):
|
| 426 |
+
name = data.type.field(level_name_or_index).name
|
| 427 |
+
elif isinstance(level_name_or_index, (str, bytes)):
|
| 428 |
+
name = level_name_or_index
|
| 429 |
+
elif isinstance(level_name_or_index, pc.Expression):
|
| 430 |
+
name = str(level_name_or_index)
|
| 431 |
+
elif is_list_like(level_name_or_index):
|
| 432 |
+
# For nested input like [2, 1, 2]
|
| 433 |
+
# iteratively get the struct and field name. The last
|
| 434 |
+
# one is used for the name of the index.
|
| 435 |
+
level_name_or_index = list(reversed(level_name_or_index))
|
| 436 |
+
selected = data
|
| 437 |
+
while level_name_or_index:
|
| 438 |
+
# we need the cast, otherwise mypy complains about
|
| 439 |
+
# getting ints, bytes, or str here, which isn't possible.
|
| 440 |
+
level_name_or_index = cast(list, level_name_or_index)
|
| 441 |
+
name_or_index = level_name_or_index.pop()
|
| 442 |
+
name = get_name(name_or_index, selected)
|
| 443 |
+
selected = selected.type.field(selected.type.get_field_index(name))
|
| 444 |
+
name = selected.name
|
| 445 |
+
else:
|
| 446 |
+
raise ValueError(
|
| 447 |
+
"name_or_index must be an int, str, bytes, "
|
| 448 |
+
"pyarrow.compute.Expression, or list of those"
|
| 449 |
+
)
|
| 450 |
+
return name
|
| 451 |
+
|
| 452 |
+
pa_arr = self._data.array._pa_array
|
| 453 |
+
name = get_name(name_or_index, pa_arr)
|
| 454 |
+
field_arr = pc.struct_field(pa_arr, name_or_index)
|
| 455 |
+
|
| 456 |
+
return Series(
|
| 457 |
+
field_arr,
|
| 458 |
+
dtype=ArrowDtype(field_arr.type),
|
| 459 |
+
index=self._data.index,
|
| 460 |
+
name=name,
|
| 461 |
+
)
|
| 462 |
+
|
| 463 |
+
def explode(self) -> DataFrame:
|
| 464 |
+
"""
|
| 465 |
+
Extract all child fields of a struct as a DataFrame.
|
| 466 |
+
|
| 467 |
+
Returns
|
| 468 |
+
-------
|
| 469 |
+
pandas.DataFrame
|
| 470 |
+
The data corresponding to all child fields.
|
| 471 |
+
|
| 472 |
+
See Also
|
| 473 |
+
--------
|
| 474 |
+
Series.struct.field : Return a single child field as a Series.
|
| 475 |
+
|
| 476 |
+
Examples
|
| 477 |
+
--------
|
| 478 |
+
>>> import pyarrow as pa
|
| 479 |
+
>>> s = pd.Series(
|
| 480 |
+
... [
|
| 481 |
+
... {"version": 1, "project": "pandas"},
|
| 482 |
+
... {"version": 2, "project": "pandas"},
|
| 483 |
+
... {"version": 1, "project": "numpy"},
|
| 484 |
+
... ],
|
| 485 |
+
... dtype=pd.ArrowDtype(
|
| 486 |
+
... pa.struct([("version", pa.int64()), ("project", pa.string())])
|
| 487 |
+
... ),
|
| 488 |
+
... )
|
| 489 |
+
|
| 490 |
+
>>> s.struct.explode()
|
| 491 |
+
version project
|
| 492 |
+
0 1 pandas
|
| 493 |
+
1 2 pandas
|
| 494 |
+
2 1 numpy
|
| 495 |
+
"""
|
| 496 |
+
from pandas import concat
|
| 497 |
+
|
| 498 |
+
pa_type = self._pa_array.type
|
| 499 |
+
return concat(
|
| 500 |
+
[self.field(i) for i in range(pa_type.num_fields)], axis="columns"
|
| 501 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/array.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/arrow/extension_types.py
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
from typing import TYPE_CHECKING
|
| 5 |
+
|
| 6 |
+
import pyarrow
|
| 7 |
+
|
| 8 |
+
from pandas.compat import pa_version_under14p1
|
| 9 |
+
|
| 10 |
+
from pandas.core.dtypes.dtypes import (
|
| 11 |
+
IntervalDtype,
|
| 12 |
+
PeriodDtype,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
from pandas.core.arrays.interval import VALID_CLOSED
|
| 16 |
+
|
| 17 |
+
if TYPE_CHECKING:
|
| 18 |
+
from pandas._typing import IntervalClosedType
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class ArrowPeriodType(pyarrow.ExtensionType):
|
| 22 |
+
def __init__(self, freq) -> None:
|
| 23 |
+
# attributes need to be set first before calling
|
| 24 |
+
# super init (as that calls serialize)
|
| 25 |
+
self._freq = freq
|
| 26 |
+
pyarrow.ExtensionType.__init__(self, pyarrow.int64(), "pandas.period")
|
| 27 |
+
|
| 28 |
+
@property
|
| 29 |
+
def freq(self):
|
| 30 |
+
return self._freq
|
| 31 |
+
|
| 32 |
+
def __arrow_ext_serialize__(self) -> bytes:
|
| 33 |
+
metadata = {"freq": self.freq}
|
| 34 |
+
return json.dumps(metadata).encode()
|
| 35 |
+
|
| 36 |
+
@classmethod
|
| 37 |
+
def __arrow_ext_deserialize__(cls, storage_type, serialized) -> ArrowPeriodType:
|
| 38 |
+
metadata = json.loads(serialized.decode())
|
| 39 |
+
return ArrowPeriodType(metadata["freq"])
|
| 40 |
+
|
| 41 |
+
def __eq__(self, other):
|
| 42 |
+
if isinstance(other, pyarrow.BaseExtensionType):
|
| 43 |
+
return type(self) == type(other) and self.freq == other.freq
|
| 44 |
+
else:
|
| 45 |
+
return NotImplemented
|
| 46 |
+
|
| 47 |
+
def __ne__(self, other) -> bool:
|
| 48 |
+
return not self == other
|
| 49 |
+
|
| 50 |
+
def __hash__(self) -> int:
|
| 51 |
+
return hash((str(self), self.freq))
|
| 52 |
+
|
| 53 |
+
def to_pandas_dtype(self) -> PeriodDtype:
|
| 54 |
+
return PeriodDtype(freq=self.freq)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# register the type with a dummy instance
|
| 58 |
+
_period_type = ArrowPeriodType("D")
|
| 59 |
+
pyarrow.register_extension_type(_period_type)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
class ArrowIntervalType(pyarrow.ExtensionType):
|
| 63 |
+
def __init__(self, subtype, closed: IntervalClosedType) -> None:
|
| 64 |
+
# attributes need to be set first before calling
|
| 65 |
+
# super init (as that calls serialize)
|
| 66 |
+
assert closed in VALID_CLOSED
|
| 67 |
+
self._closed: IntervalClosedType = closed
|
| 68 |
+
if not isinstance(subtype, pyarrow.DataType):
|
| 69 |
+
subtype = pyarrow.type_for_alias(str(subtype))
|
| 70 |
+
self._subtype = subtype
|
| 71 |
+
|
| 72 |
+
storage_type = pyarrow.struct([("left", subtype), ("right", subtype)])
|
| 73 |
+
pyarrow.ExtensionType.__init__(self, storage_type, "pandas.interval")
|
| 74 |
+
|
| 75 |
+
@property
|
| 76 |
+
def subtype(self):
|
| 77 |
+
return self._subtype
|
| 78 |
+
|
| 79 |
+
@property
|
| 80 |
+
def closed(self) -> IntervalClosedType:
|
| 81 |
+
return self._closed
|
| 82 |
+
|
| 83 |
+
def __arrow_ext_serialize__(self) -> bytes:
|
| 84 |
+
metadata = {"subtype": str(self.subtype), "closed": self.closed}
|
| 85 |
+
return json.dumps(metadata).encode()
|
| 86 |
+
|
| 87 |
+
@classmethod
|
| 88 |
+
def __arrow_ext_deserialize__(cls, storage_type, serialized) -> ArrowIntervalType:
|
| 89 |
+
metadata = json.loads(serialized.decode())
|
| 90 |
+
subtype = pyarrow.type_for_alias(metadata["subtype"])
|
| 91 |
+
closed = metadata["closed"]
|
| 92 |
+
return ArrowIntervalType(subtype, closed)
|
| 93 |
+
|
| 94 |
+
def __eq__(self, other):
|
| 95 |
+
if isinstance(other, pyarrow.BaseExtensionType):
|
| 96 |
+
return (
|
| 97 |
+
type(self) == type(other)
|
| 98 |
+
and self.subtype == other.subtype
|
| 99 |
+
and self.closed == other.closed
|
| 100 |
+
)
|
| 101 |
+
else:
|
| 102 |
+
return NotImplemented
|
| 103 |
+
|
| 104 |
+
def __ne__(self, other) -> bool:
|
| 105 |
+
return not self == other
|
| 106 |
+
|
| 107 |
+
def __hash__(self) -> int:
|
| 108 |
+
return hash((str(self), str(self.subtype), self.closed))
|
| 109 |
+
|
| 110 |
+
def to_pandas_dtype(self) -> IntervalDtype:
|
| 111 |
+
return IntervalDtype(self.subtype.to_pandas_dtype(), self.closed)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
# register the type with a dummy instance
|
| 115 |
+
_interval_type = ArrowIntervalType(pyarrow.int64(), "left")
|
| 116 |
+
pyarrow.register_extension_type(_interval_type)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
_ERROR_MSG = """\
|
| 120 |
+
Disallowed deserialization of 'arrow.py_extension_type':
|
| 121 |
+
storage_type = {storage_type}
|
| 122 |
+
serialized = {serialized}
|
| 123 |
+
pickle disassembly:\n{pickle_disassembly}
|
| 124 |
+
|
| 125 |
+
Reading of untrusted Parquet or Feather files with a PyExtensionType column
|
| 126 |
+
allows arbitrary code execution.
|
| 127 |
+
If you trust this file, you can enable reading the extension type by one of:
|
| 128 |
+
|
| 129 |
+
- upgrading to pyarrow >= 14.0.1, and call `pa.PyExtensionType.set_auto_load(True)`
|
| 130 |
+
- install pyarrow-hotfix (`pip install pyarrow-hotfix`) and disable it by running
|
| 131 |
+
`import pyarrow_hotfix; pyarrow_hotfix.uninstall()`
|
| 132 |
+
|
| 133 |
+
We strongly recommend updating your Parquet/Feather files to use extension types
|
| 134 |
+
derived from `pyarrow.ExtensionType` instead, and register this type explicitly.
|
| 135 |
+
"""
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def patch_pyarrow() -> None:
|
| 139 |
+
# starting from pyarrow 14.0.1, it has its own mechanism
|
| 140 |
+
if not pa_version_under14p1:
|
| 141 |
+
return
|
| 142 |
+
|
| 143 |
+
# if https://github.com/pitrou/pyarrow-hotfix was installed and enabled
|
| 144 |
+
if getattr(pyarrow, "_hotfix_installed", False):
|
| 145 |
+
return
|
| 146 |
+
|
| 147 |
+
class ForbiddenExtensionType(pyarrow.ExtensionType):
|
| 148 |
+
def __arrow_ext_serialize__(self) -> bytes:
|
| 149 |
+
return b""
|
| 150 |
+
|
| 151 |
+
@classmethod
|
| 152 |
+
def __arrow_ext_deserialize__(cls, storage_type, serialized):
|
| 153 |
+
import io
|
| 154 |
+
import pickletools
|
| 155 |
+
|
| 156 |
+
out = io.StringIO()
|
| 157 |
+
pickletools.dis(serialized, out)
|
| 158 |
+
raise RuntimeError(
|
| 159 |
+
_ERROR_MSG.format(
|
| 160 |
+
storage_type=storage_type,
|
| 161 |
+
serialized=serialized,
|
| 162 |
+
pickle_disassembly=out.getvalue(),
|
| 163 |
+
)
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
pyarrow.unregister_extension_type("arrow.py_extension_type")
|
| 167 |
+
pyarrow.register_extension_type(
|
| 168 |
+
ForbiddenExtensionType(pyarrow.null(), "arrow.py_extension_type")
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
pyarrow._hotfix_installed = True
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
patch_pyarrow()
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/base.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/boolean.py
ADDED
|
@@ -0,0 +1,438 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import numbers
|
| 4 |
+
from typing import (
|
| 5 |
+
TYPE_CHECKING,
|
| 6 |
+
ClassVar,
|
| 7 |
+
Self,
|
| 8 |
+
cast,
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
from pandas._libs import (
|
| 14 |
+
lib,
|
| 15 |
+
missing as libmissing,
|
| 16 |
+
)
|
| 17 |
+
from pandas.util._decorators import set_module
|
| 18 |
+
|
| 19 |
+
from pandas.core.dtypes.common import is_list_like
|
| 20 |
+
from pandas.core.dtypes.dtypes import register_extension_dtype
|
| 21 |
+
from pandas.core.dtypes.missing import isna
|
| 22 |
+
|
| 23 |
+
from pandas.core import ops
|
| 24 |
+
from pandas.core.array_algos import masked_accumulations
|
| 25 |
+
from pandas.core.arrays.masked import (
|
| 26 |
+
BaseMaskedArray,
|
| 27 |
+
BaseMaskedDtype,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
if TYPE_CHECKING:
|
| 31 |
+
import pyarrow
|
| 32 |
+
|
| 33 |
+
from pandas._typing import (
|
| 34 |
+
DtypeObj,
|
| 35 |
+
npt,
|
| 36 |
+
type_t,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
from pandas.core.dtypes.dtypes import ExtensionDtype
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
@register_extension_dtype
|
| 43 |
+
@set_module("pandas")
|
| 44 |
+
class BooleanDtype(BaseMaskedDtype):
|
| 45 |
+
"""
|
| 46 |
+
Extension dtype for boolean data.
|
| 47 |
+
|
| 48 |
+
This is a pandas Extension dtype for boolean data with support for
|
| 49 |
+
missing values. BooleanDtype is the dtype companion to :class:`.BooleanArray`,
|
| 50 |
+
which implements Kleene logic (sometimes called three-value logic) for
|
| 51 |
+
logical operations. See :ref:`boolean.kleene` for more.
|
| 52 |
+
|
| 53 |
+
.. warning::
|
| 54 |
+
|
| 55 |
+
BooleanDtype is considered experimental. The implementation and
|
| 56 |
+
parts of the API may change without warning.
|
| 57 |
+
|
| 58 |
+
Attributes
|
| 59 |
+
----------
|
| 60 |
+
None
|
| 61 |
+
|
| 62 |
+
Methods
|
| 63 |
+
-------
|
| 64 |
+
None
|
| 65 |
+
|
| 66 |
+
See Also
|
| 67 |
+
--------
|
| 68 |
+
arrays.BooleanArray : Array of boolean (True/False) data with missing values.
|
| 69 |
+
Int64Dtype : Extension dtype for int64 integer data.
|
| 70 |
+
StringDtype : Extension dtype for string data.
|
| 71 |
+
|
| 72 |
+
Examples
|
| 73 |
+
--------
|
| 74 |
+
>>> pd.BooleanDtype()
|
| 75 |
+
BooleanDtype
|
| 76 |
+
|
| 77 |
+
>>> pd.array([True, False, None], dtype=pd.BooleanDtype())
|
| 78 |
+
<BooleanArray>
|
| 79 |
+
[True, False, <NA>]
|
| 80 |
+
Length: 3, dtype: boolean
|
| 81 |
+
|
| 82 |
+
>>> pd.array([True, False, None], dtype="boolean")
|
| 83 |
+
<BooleanArray>
|
| 84 |
+
[True, False, <NA>]
|
| 85 |
+
Length: 3, dtype: boolean
|
| 86 |
+
"""
|
| 87 |
+
|
| 88 |
+
name: ClassVar[str] = "boolean"
|
| 89 |
+
|
| 90 |
+
# The value used to fill '_data' to avoid upcasting
|
| 91 |
+
_internal_fill_value = False
|
| 92 |
+
|
| 93 |
+
# https://github.com/python/mypy/issues/4125
|
| 94 |
+
# error: Signature of "type" incompatible with supertype "BaseMaskedDtype"
|
| 95 |
+
@property
|
| 96 |
+
def type(self) -> type: # type: ignore[override]
|
| 97 |
+
return np.bool_
|
| 98 |
+
|
| 99 |
+
@property
|
| 100 |
+
def kind(self) -> str:
|
| 101 |
+
return "b"
|
| 102 |
+
|
| 103 |
+
@property
|
| 104 |
+
def numpy_dtype(self) -> np.dtype:
|
| 105 |
+
return np.dtype("bool")
|
| 106 |
+
|
| 107 |
+
def construct_array_type(self) -> type_t[BooleanArray]:
|
| 108 |
+
"""
|
| 109 |
+
Return the array type associated with this dtype.
|
| 110 |
+
|
| 111 |
+
Returns
|
| 112 |
+
-------
|
| 113 |
+
type
|
| 114 |
+
"""
|
| 115 |
+
return BooleanArray
|
| 116 |
+
|
| 117 |
+
def __repr__(self) -> str:
|
| 118 |
+
return "BooleanDtype"
|
| 119 |
+
|
| 120 |
+
@property
|
| 121 |
+
def _is_boolean(self) -> bool:
|
| 122 |
+
return True
|
| 123 |
+
|
| 124 |
+
@property
|
| 125 |
+
def _is_numeric(self) -> bool:
|
| 126 |
+
return True
|
| 127 |
+
|
| 128 |
+
def __from_arrow__(
|
| 129 |
+
self, array: pyarrow.Array | pyarrow.ChunkedArray
|
| 130 |
+
) -> BooleanArray:
|
| 131 |
+
"""
|
| 132 |
+
Construct BooleanArray from pyarrow Array/ChunkedArray.
|
| 133 |
+
"""
|
| 134 |
+
import pyarrow
|
| 135 |
+
|
| 136 |
+
if array.type != pyarrow.bool_() and not pyarrow.types.is_null(array.type):
|
| 137 |
+
raise TypeError(f"Expected array of boolean type, got {array.type} instead")
|
| 138 |
+
|
| 139 |
+
if isinstance(array, pyarrow.Array):
|
| 140 |
+
chunks = [array]
|
| 141 |
+
length = len(array)
|
| 142 |
+
else:
|
| 143 |
+
# pyarrow.ChunkedArray
|
| 144 |
+
chunks = array.chunks
|
| 145 |
+
length = array.length()
|
| 146 |
+
|
| 147 |
+
if pyarrow.types.is_null(array.type):
|
| 148 |
+
mask = np.ones(length, dtype=bool)
|
| 149 |
+
# No need to init data, since all null
|
| 150 |
+
data = np.empty(length, dtype=bool)
|
| 151 |
+
return BooleanArray(data, mask)
|
| 152 |
+
|
| 153 |
+
results = []
|
| 154 |
+
for arr in chunks:
|
| 155 |
+
buflist = arr.buffers()
|
| 156 |
+
data = pyarrow.BooleanArray.from_buffers(
|
| 157 |
+
arr.type, len(arr), [None, buflist[1]], offset=arr.offset
|
| 158 |
+
).to_numpy(zero_copy_only=False)
|
| 159 |
+
if arr.null_count != 0:
|
| 160 |
+
mask = pyarrow.BooleanArray.from_buffers(
|
| 161 |
+
arr.type, len(arr), [None, buflist[0]], offset=arr.offset
|
| 162 |
+
).to_numpy(zero_copy_only=False)
|
| 163 |
+
mask = ~mask
|
| 164 |
+
else:
|
| 165 |
+
mask = np.zeros(len(arr), dtype=bool)
|
| 166 |
+
|
| 167 |
+
bool_arr = BooleanArray(data, mask)
|
| 168 |
+
results.append(bool_arr)
|
| 169 |
+
|
| 170 |
+
if not results:
|
| 171 |
+
return BooleanArray(
|
| 172 |
+
np.array([], dtype=np.bool_), np.array([], dtype=np.bool_)
|
| 173 |
+
)
|
| 174 |
+
else:
|
| 175 |
+
return BooleanArray._concat_same_type(results)
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def coerce_to_array(
|
| 179 |
+
values, mask=None, copy: bool = False
|
| 180 |
+
) -> tuple[np.ndarray, np.ndarray]:
|
| 181 |
+
"""
|
| 182 |
+
Coerce the input values array to numpy arrays with a mask.
|
| 183 |
+
|
| 184 |
+
Parameters
|
| 185 |
+
----------
|
| 186 |
+
values : 1D list-like
|
| 187 |
+
mask : bool 1D array, optional
|
| 188 |
+
copy : bool, default False
|
| 189 |
+
if True, copy the input
|
| 190 |
+
|
| 191 |
+
Returns
|
| 192 |
+
-------
|
| 193 |
+
tuple of (values, mask)
|
| 194 |
+
"""
|
| 195 |
+
if isinstance(values, BooleanArray):
|
| 196 |
+
if mask is not None:
|
| 197 |
+
raise ValueError("cannot pass mask for BooleanArray input")
|
| 198 |
+
values, mask = values._data, values._mask
|
| 199 |
+
if copy:
|
| 200 |
+
values = values.copy()
|
| 201 |
+
mask = mask.copy()
|
| 202 |
+
return values, mask
|
| 203 |
+
|
| 204 |
+
mask_values = None
|
| 205 |
+
if isinstance(values, np.ndarray) and values.dtype == np.bool_:
|
| 206 |
+
if copy:
|
| 207 |
+
values = values.copy()
|
| 208 |
+
elif isinstance(values, np.ndarray) and values.dtype.kind in "iufcb":
|
| 209 |
+
mask_values = isna(values)
|
| 210 |
+
|
| 211 |
+
values_bool = np.zeros(len(values), dtype=bool)
|
| 212 |
+
values_bool[~mask_values] = values[~mask_values].astype(bool)
|
| 213 |
+
|
| 214 |
+
if not np.all(
|
| 215 |
+
values_bool[~mask_values].astype(values.dtype) == values[~mask_values]
|
| 216 |
+
):
|
| 217 |
+
raise TypeError("Need to pass bool-like values")
|
| 218 |
+
|
| 219 |
+
values = values_bool
|
| 220 |
+
else:
|
| 221 |
+
values_object = np.asarray(values, dtype=object)
|
| 222 |
+
|
| 223 |
+
inferred_dtype = lib.infer_dtype(values_object, skipna=True)
|
| 224 |
+
integer_like = ("floating", "integer", "mixed-integer-float")
|
| 225 |
+
if inferred_dtype not in ("boolean", "empty", *integer_like):
|
| 226 |
+
raise TypeError("Need to pass bool-like values")
|
| 227 |
+
|
| 228 |
+
# mypy does not narrow the type of mask_values to npt.NDArray[np.bool_]
|
| 229 |
+
# within this branch, it assumes it can also be None
|
| 230 |
+
mask_values = cast("npt.NDArray[np.bool_]", isna(values_object))
|
| 231 |
+
values = np.zeros(len(values), dtype=bool)
|
| 232 |
+
values[~mask_values] = values_object[~mask_values].astype(bool)
|
| 233 |
+
|
| 234 |
+
# if the values were integer-like, validate it were actually 0/1's
|
| 235 |
+
if (inferred_dtype in integer_like) and not (
|
| 236 |
+
np.all(
|
| 237 |
+
values[~mask_values].astype(float)
|
| 238 |
+
== values_object[~mask_values].astype(float)
|
| 239 |
+
)
|
| 240 |
+
):
|
| 241 |
+
raise TypeError("Need to pass bool-like values")
|
| 242 |
+
|
| 243 |
+
if mask is None and mask_values is None:
|
| 244 |
+
mask = np.zeros(values.shape, dtype=bool)
|
| 245 |
+
elif mask is None:
|
| 246 |
+
mask = mask_values
|
| 247 |
+
elif isinstance(mask, np.ndarray) and mask.dtype == np.bool_:
|
| 248 |
+
if mask_values is not None:
|
| 249 |
+
mask = mask | mask_values
|
| 250 |
+
elif copy:
|
| 251 |
+
mask = mask.copy()
|
| 252 |
+
else:
|
| 253 |
+
mask = np.array(mask, dtype=bool)
|
| 254 |
+
if mask_values is not None:
|
| 255 |
+
mask = mask | mask_values
|
| 256 |
+
|
| 257 |
+
if values.shape != mask.shape:
|
| 258 |
+
raise ValueError("values.shape and mask.shape must match")
|
| 259 |
+
|
| 260 |
+
return values, mask
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
@set_module("pandas.arrays")
|
| 264 |
+
class BooleanArray(BaseMaskedArray):
|
| 265 |
+
"""
|
| 266 |
+
Array of boolean (True/False) data with missing values.
|
| 267 |
+
|
| 268 |
+
This is a pandas Extension array for boolean data, under the hood
|
| 269 |
+
represented by 2 numpy arrays: a boolean array with the data and
|
| 270 |
+
a boolean array with the mask (True indicating missing).
|
| 271 |
+
|
| 272 |
+
BooleanArray implements Kleene logic (sometimes called three-value
|
| 273 |
+
logic) for logical operations. See :ref:`boolean.kleene` for more.
|
| 274 |
+
|
| 275 |
+
To construct a BooleanArray from generic array-like input, use
|
| 276 |
+
:func:`pandas.array` specifying ``dtype="boolean"`` (see examples
|
| 277 |
+
below).
|
| 278 |
+
|
| 279 |
+
.. warning::
|
| 280 |
+
|
| 281 |
+
BooleanArray is considered experimental. The implementation and
|
| 282 |
+
parts of the API may change without warning.
|
| 283 |
+
|
| 284 |
+
Parameters
|
| 285 |
+
----------
|
| 286 |
+
values : numpy.ndarray
|
| 287 |
+
A 1-d boolean-dtype array with the data.
|
| 288 |
+
mask : numpy.ndarray
|
| 289 |
+
A 1-d boolean-dtype array indicating missing values (True
|
| 290 |
+
indicates missing).
|
| 291 |
+
copy : bool, default False
|
| 292 |
+
Whether to copy the `values` and `mask` arrays.
|
| 293 |
+
|
| 294 |
+
Attributes
|
| 295 |
+
----------
|
| 296 |
+
None
|
| 297 |
+
|
| 298 |
+
Methods
|
| 299 |
+
-------
|
| 300 |
+
None
|
| 301 |
+
|
| 302 |
+
Returns
|
| 303 |
+
-------
|
| 304 |
+
BooleanArray
|
| 305 |
+
|
| 306 |
+
See Also
|
| 307 |
+
--------
|
| 308 |
+
array : Create an array from data with the appropriate dtype.
|
| 309 |
+
BooleanDtype : Extension dtype for boolean data.
|
| 310 |
+
Series : One-dimensional ndarray with axis labels (including time series).
|
| 311 |
+
DataFrame : Two-dimensional, size-mutable, potentially heterogeneous tabular data.
|
| 312 |
+
|
| 313 |
+
Examples
|
| 314 |
+
--------
|
| 315 |
+
Create a BooleanArray with :func:`pandas.array`:
|
| 316 |
+
|
| 317 |
+
>>> pd.array([True, False, None], dtype="boolean")
|
| 318 |
+
<BooleanArray>
|
| 319 |
+
[True, False, <NA>]
|
| 320 |
+
Length: 3, dtype: boolean
|
| 321 |
+
"""
|
| 322 |
+
|
| 323 |
+
_TRUE_VALUES = {"True", "TRUE", "true", "1", "1.0"}
|
| 324 |
+
_FALSE_VALUES = {"False", "FALSE", "false", "0", "0.0"}
|
| 325 |
+
|
| 326 |
+
@classmethod
|
| 327 |
+
def _simple_new(cls, values: np.ndarray, mask: npt.NDArray[np.bool_]) -> Self:
|
| 328 |
+
result = super()._simple_new(values, mask)
|
| 329 |
+
result._dtype = BooleanDtype()
|
| 330 |
+
return result
|
| 331 |
+
|
| 332 |
+
def __init__(
|
| 333 |
+
self, values: np.ndarray, mask: np.ndarray, copy: bool = False
|
| 334 |
+
) -> None:
|
| 335 |
+
if not (isinstance(values, np.ndarray) and values.dtype == np.bool_):
|
| 336 |
+
raise TypeError(
|
| 337 |
+
"values should be boolean numpy array. Use "
|
| 338 |
+
"the 'pd.array' function instead"
|
| 339 |
+
)
|
| 340 |
+
self._dtype = BooleanDtype()
|
| 341 |
+
super().__init__(values, mask, copy=copy)
|
| 342 |
+
|
| 343 |
+
@property
|
| 344 |
+
def dtype(self) -> BooleanDtype:
|
| 345 |
+
return self._dtype
|
| 346 |
+
|
| 347 |
+
@classmethod
|
| 348 |
+
def _from_sequence_of_strings(
|
| 349 |
+
cls,
|
| 350 |
+
strings: list[str],
|
| 351 |
+
*,
|
| 352 |
+
dtype: ExtensionDtype,
|
| 353 |
+
copy: bool = False,
|
| 354 |
+
true_values: list[str] | None = None,
|
| 355 |
+
false_values: list[str] | None = None,
|
| 356 |
+
none_values: list[str] | None = None,
|
| 357 |
+
) -> BooleanArray:
|
| 358 |
+
true_values_union = cls._TRUE_VALUES.union(true_values or [])
|
| 359 |
+
false_values_union = cls._FALSE_VALUES.union(false_values or [])
|
| 360 |
+
|
| 361 |
+
if none_values is None:
|
| 362 |
+
none_values = []
|
| 363 |
+
|
| 364 |
+
def map_string(s) -> bool | None:
|
| 365 |
+
if s in true_values_union:
|
| 366 |
+
return True
|
| 367 |
+
elif s in false_values_union:
|
| 368 |
+
return False
|
| 369 |
+
elif s in none_values:
|
| 370 |
+
return None
|
| 371 |
+
else:
|
| 372 |
+
raise ValueError(f"{s} cannot be cast to bool")
|
| 373 |
+
|
| 374 |
+
scalars = np.array(strings, dtype=object)
|
| 375 |
+
mask = isna(scalars)
|
| 376 |
+
scalars[~mask] = list(map(map_string, scalars[~mask]))
|
| 377 |
+
return cls._from_sequence(scalars, dtype=dtype, copy=copy)
|
| 378 |
+
|
| 379 |
+
_HANDLED_TYPES = (np.ndarray, numbers.Number, bool, np.bool_)
|
| 380 |
+
|
| 381 |
+
@classmethod
|
| 382 |
+
def _coerce_to_array(
|
| 383 |
+
cls, value, *, dtype: DtypeObj, copy: bool = False
|
| 384 |
+
) -> tuple[np.ndarray, np.ndarray]:
|
| 385 |
+
if dtype:
|
| 386 |
+
assert dtype == "boolean"
|
| 387 |
+
return coerce_to_array(value, copy=copy)
|
| 388 |
+
|
| 389 |
+
def _logical_method(self, other, op):
|
| 390 |
+
assert op.__name__ in {"or_", "ror_", "and_", "rand_", "xor", "rxor"}
|
| 391 |
+
other_is_scalar = lib.is_scalar(other)
|
| 392 |
+
mask = None
|
| 393 |
+
|
| 394 |
+
if isinstance(other, BooleanArray):
|
| 395 |
+
other, mask = other._data, other._mask
|
| 396 |
+
elif is_list_like(other):
|
| 397 |
+
other = np.asarray(other, dtype="bool")
|
| 398 |
+
if other.ndim > 1:
|
| 399 |
+
return NotImplemented
|
| 400 |
+
other, mask = coerce_to_array(other, copy=False)
|
| 401 |
+
elif isinstance(other, np.bool_):
|
| 402 |
+
other = other.item()
|
| 403 |
+
|
| 404 |
+
if other_is_scalar and other is not libmissing.NA and not lib.is_bool(other):
|
| 405 |
+
raise TypeError(
|
| 406 |
+
"'other' should be pandas.NA or a bool. "
|
| 407 |
+
f"Got {type(other).__name__} instead."
|
| 408 |
+
)
|
| 409 |
+
|
| 410 |
+
if not other_is_scalar and len(self) != len(other):
|
| 411 |
+
raise ValueError("Lengths must match")
|
| 412 |
+
|
| 413 |
+
if op.__name__ in {"or_", "ror_"}:
|
| 414 |
+
result, mask = ops.kleene_or(self._data, other, self._mask, mask)
|
| 415 |
+
elif op.__name__ in {"and_", "rand_"}:
|
| 416 |
+
result, mask = ops.kleene_and(self._data, other, self._mask, mask)
|
| 417 |
+
else:
|
| 418 |
+
# i.e. xor, rxor
|
| 419 |
+
result, mask = ops.kleene_xor(self._data, other, self._mask, mask)
|
| 420 |
+
|
| 421 |
+
# i.e. BooleanArray
|
| 422 |
+
return self._maybe_mask_result(result, mask)
|
| 423 |
+
|
| 424 |
+
def _accumulate(
|
| 425 |
+
self, name: str, *, skipna: bool = True, **kwargs
|
| 426 |
+
) -> BaseMaskedArray:
|
| 427 |
+
data = self._data
|
| 428 |
+
mask = self._mask
|
| 429 |
+
if name in ("cummin", "cummax"):
|
| 430 |
+
op = getattr(masked_accumulations, name)
|
| 431 |
+
data, mask = op(data, mask, skipna=skipna, **kwargs)
|
| 432 |
+
return self._simple_new(data, mask)
|
| 433 |
+
else:
|
| 434 |
+
from pandas.core.arrays import IntegerArray
|
| 435 |
+
|
| 436 |
+
return IntegerArray(data.astype(int), mask)._accumulate(
|
| 437 |
+
name, skipna=skipna, **kwargs
|
| 438 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/categorical.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/datetimelike.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/datetimes.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/floating.py
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import (
|
| 4 |
+
TYPE_CHECKING,
|
| 5 |
+
Any,
|
| 6 |
+
ClassVar,
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
from pandas.util._decorators import set_module
|
| 12 |
+
|
| 13 |
+
from pandas.core.dtypes.base import register_extension_dtype
|
| 14 |
+
from pandas.core.dtypes.common import is_float_dtype
|
| 15 |
+
|
| 16 |
+
from pandas.core.arrays.numeric import (
|
| 17 |
+
NumericArray,
|
| 18 |
+
NumericDtype,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
if TYPE_CHECKING:
|
| 22 |
+
from collections.abc import Callable
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class FloatingDtype(NumericDtype):
|
| 26 |
+
"""
|
| 27 |
+
An ExtensionDtype to hold a single size of floating dtype.
|
| 28 |
+
|
| 29 |
+
These specific implementations are subclasses of the non-public
|
| 30 |
+
FloatingDtype. For example we have Float32Dtype to represent float32.
|
| 31 |
+
|
| 32 |
+
The attributes name & type are set when these subclasses are created.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
# The value used to fill '_data' to avoid upcasting
|
| 36 |
+
_internal_fill_value = np.nan
|
| 37 |
+
_default_np_dtype = np.dtype(np.float64)
|
| 38 |
+
_checker: Callable[[Any], bool] = is_float_dtype
|
| 39 |
+
|
| 40 |
+
def construct_array_type(self) -> type[FloatingArray]:
|
| 41 |
+
"""
|
| 42 |
+
Return the array type associated with this dtype.
|
| 43 |
+
|
| 44 |
+
Returns
|
| 45 |
+
-------
|
| 46 |
+
type
|
| 47 |
+
"""
|
| 48 |
+
return FloatingArray
|
| 49 |
+
|
| 50 |
+
@classmethod
|
| 51 |
+
def _get_dtype_mapping(cls) -> dict[np.dtype, FloatingDtype]:
|
| 52 |
+
return NUMPY_FLOAT_TO_DTYPE
|
| 53 |
+
|
| 54 |
+
@classmethod
|
| 55 |
+
def _safe_cast(cls, values: np.ndarray, dtype: np.dtype, copy: bool) -> np.ndarray:
|
| 56 |
+
"""
|
| 57 |
+
Safely cast the values to the given dtype.
|
| 58 |
+
|
| 59 |
+
"safe" in this context means the casting is lossless.
|
| 60 |
+
"""
|
| 61 |
+
# This is really only here for compatibility with IntegerDtype
|
| 62 |
+
# Here for compat with IntegerDtype
|
| 63 |
+
return values.astype(dtype, copy=copy)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
@set_module("pandas.arrays")
|
| 67 |
+
class FloatingArray(NumericArray):
|
| 68 |
+
"""
|
| 69 |
+
Array of floating (optional missing) values.
|
| 70 |
+
|
| 71 |
+
.. warning::
|
| 72 |
+
|
| 73 |
+
FloatingArray is currently experimental, and its API or internal
|
| 74 |
+
implementation may change without warning. Especially the behaviour
|
| 75 |
+
regarding NaN (distinct from NA missing values) is subject to change.
|
| 76 |
+
|
| 77 |
+
We represent a FloatingArray with 2 numpy arrays:
|
| 78 |
+
|
| 79 |
+
- data: contains a numpy float array of the appropriate dtype
|
| 80 |
+
- mask: a boolean array holding a mask on the data, True is missing
|
| 81 |
+
|
| 82 |
+
To construct a FloatingArray from generic array-like input, use
|
| 83 |
+
:func:`pandas.array` with one of the float dtypes (see examples).
|
| 84 |
+
|
| 85 |
+
See :ref:`integer_na` for more.
|
| 86 |
+
|
| 87 |
+
Parameters
|
| 88 |
+
----------
|
| 89 |
+
values : numpy.ndarray
|
| 90 |
+
A 1-d float-dtype array.
|
| 91 |
+
mask : numpy.ndarray
|
| 92 |
+
A 1-d boolean-dtype array indicating missing values.
|
| 93 |
+
copy : bool, default False
|
| 94 |
+
Whether to copy the `values` and `mask`.
|
| 95 |
+
|
| 96 |
+
Attributes
|
| 97 |
+
----------
|
| 98 |
+
None
|
| 99 |
+
|
| 100 |
+
Methods
|
| 101 |
+
-------
|
| 102 |
+
None
|
| 103 |
+
|
| 104 |
+
Returns
|
| 105 |
+
-------
|
| 106 |
+
FloatingArray
|
| 107 |
+
|
| 108 |
+
See Also
|
| 109 |
+
--------
|
| 110 |
+
array : Create an array.
|
| 111 |
+
Float32Dtype : Float32 dtype for FloatingArray.
|
| 112 |
+
Float64Dtype : Float64 dtype for FloatingArray.
|
| 113 |
+
Series : One-dimensional labeled array capable of holding data.
|
| 114 |
+
DataFrame : Two-dimensional, size-mutable, potentially heterogeneous tabular data.
|
| 115 |
+
|
| 116 |
+
Examples
|
| 117 |
+
--------
|
| 118 |
+
Create a FloatingArray with :func:`pandas.array`:
|
| 119 |
+
|
| 120 |
+
>>> pd.array([0.1, None, 0.3], dtype=pd.Float32Dtype())
|
| 121 |
+
<FloatingArray>
|
| 122 |
+
[0.1, <NA>, 0.3]
|
| 123 |
+
Length: 3, dtype: Float32
|
| 124 |
+
|
| 125 |
+
String aliases for the dtypes are also available. They are capitalized.
|
| 126 |
+
|
| 127 |
+
>>> pd.array([0.1, None, 0.3], dtype="Float32")
|
| 128 |
+
<FloatingArray>
|
| 129 |
+
[0.1, <NA>, 0.3]
|
| 130 |
+
Length: 3, dtype: Float32
|
| 131 |
+
"""
|
| 132 |
+
|
| 133 |
+
_dtype_cls = FloatingDtype
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
_dtype_docstring = """
|
| 137 |
+
An ExtensionDtype for {dtype} data.
|
| 138 |
+
|
| 139 |
+
This dtype uses ``pd.NA`` as missing value indicator.
|
| 140 |
+
|
| 141 |
+
Attributes
|
| 142 |
+
----------
|
| 143 |
+
None
|
| 144 |
+
|
| 145 |
+
Methods
|
| 146 |
+
-------
|
| 147 |
+
None
|
| 148 |
+
|
| 149 |
+
See Also
|
| 150 |
+
--------
|
| 151 |
+
CategoricalDtype : Type for categorical data with the categories and orderedness.
|
| 152 |
+
IntegerDtype : An ExtensionDtype to hold a single size & kind of integer dtype.
|
| 153 |
+
StringDtype : An ExtensionDtype for string data.
|
| 154 |
+
|
| 155 |
+
Examples
|
| 156 |
+
--------
|
| 157 |
+
For Float32Dtype:
|
| 158 |
+
|
| 159 |
+
>>> ser = pd.Series([2.25, pd.NA], dtype=pd.Float32Dtype())
|
| 160 |
+
>>> ser.dtype
|
| 161 |
+
Float32Dtype()
|
| 162 |
+
|
| 163 |
+
For Float64Dtype:
|
| 164 |
+
|
| 165 |
+
>>> ser = pd.Series([2.25, pd.NA], dtype=pd.Float64Dtype())
|
| 166 |
+
>>> ser.dtype
|
| 167 |
+
Float64Dtype()
|
| 168 |
+
"""
|
| 169 |
+
|
| 170 |
+
# create the Dtype
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
@register_extension_dtype
|
| 174 |
+
@set_module("pandas")
|
| 175 |
+
class Float32Dtype(FloatingDtype):
|
| 176 |
+
type = np.float32
|
| 177 |
+
name: ClassVar[str] = "Float32"
|
| 178 |
+
__doc__ = _dtype_docstring.format(dtype="float32")
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
@register_extension_dtype
|
| 182 |
+
@set_module("pandas")
|
| 183 |
+
class Float64Dtype(FloatingDtype):
|
| 184 |
+
type = np.float64
|
| 185 |
+
name: ClassVar[str] = "Float64"
|
| 186 |
+
__doc__ = _dtype_docstring.format(dtype="float64")
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
NUMPY_FLOAT_TO_DTYPE: dict[np.dtype, FloatingDtype] = {
|
| 190 |
+
np.dtype(np.float32): Float32Dtype(),
|
| 191 |
+
np.dtype(np.float64): Float64Dtype(),
|
| 192 |
+
}
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/arrays/integer.py
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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| 4 |
+
TYPE_CHECKING,
|
| 5 |
+
Any,
|
| 6 |
+
ClassVar,
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
from pandas.util._decorators import set_module
|
| 12 |
+
|
| 13 |
+
from pandas.core.dtypes.base import register_extension_dtype
|
| 14 |
+
from pandas.core.dtypes.common import is_integer_dtype
|
| 15 |
+
|
| 16 |
+
from pandas.core.arrays.numeric import (
|
| 17 |
+
NumericArray,
|
| 18 |
+
NumericDtype,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
if TYPE_CHECKING:
|
| 22 |
+
from collections.abc import Callable
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class IntegerDtype(NumericDtype):
|
| 26 |
+
"""
|
| 27 |
+
An ExtensionDtype to hold a single size & kind of integer dtype.
|
| 28 |
+
|
| 29 |
+
These specific implementations are subclasses of the non-public
|
| 30 |
+
IntegerDtype. For example, we have Int8Dtype to represent signed int 8s.
|
| 31 |
+
|
| 32 |
+
The attributes name & type are set when these subclasses are created.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
# The value used to fill '_data' to avoid upcasting
|
| 36 |
+
_internal_fill_value = 1
|
| 37 |
+
_default_np_dtype = np.dtype(np.int64)
|
| 38 |
+
_checker: Callable[[Any], bool] = is_integer_dtype
|
| 39 |
+
|
| 40 |
+
def construct_array_type(self) -> type[IntegerArray]:
|
| 41 |
+
"""
|
| 42 |
+
Return the array type associated with this dtype.
|
| 43 |
+
|
| 44 |
+
Returns
|
| 45 |
+
-------
|
| 46 |
+
type
|
| 47 |
+
"""
|
| 48 |
+
return IntegerArray
|
| 49 |
+
|
| 50 |
+
@classmethod
|
| 51 |
+
def _get_dtype_mapping(cls) -> dict[np.dtype, IntegerDtype]:
|
| 52 |
+
return NUMPY_INT_TO_DTYPE
|
| 53 |
+
|
| 54 |
+
@classmethod
|
| 55 |
+
def _safe_cast(cls, values: np.ndarray, dtype: np.dtype, copy: bool) -> np.ndarray:
|
| 56 |
+
"""
|
| 57 |
+
Safely cast the values to the given dtype.
|
| 58 |
+
|
| 59 |
+
"safe" in this context means the casting is lossless. e.g. if 'values'
|
| 60 |
+
has a floating dtype, each value must be an integer.
|
| 61 |
+
"""
|
| 62 |
+
try:
|
| 63 |
+
return values.astype(dtype, casting="safe", copy=copy)
|
| 64 |
+
except TypeError as err:
|
| 65 |
+
casted = values.astype(dtype, copy=copy)
|
| 66 |
+
if (casted == values).all():
|
| 67 |
+
return casted
|
| 68 |
+
|
| 69 |
+
raise TypeError(
|
| 70 |
+
f"cannot safely cast non-equivalent {values.dtype} to {np.dtype(dtype)}"
|
| 71 |
+
) from err
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
@set_module("pandas.arrays")
|
| 75 |
+
class IntegerArray(NumericArray):
|
| 76 |
+
"""
|
| 77 |
+
Array of integer (optional missing) values.
|
| 78 |
+
|
| 79 |
+
Uses :attr:`pandas.NA` as the missing value.
|
| 80 |
+
|
| 81 |
+
.. warning::
|
| 82 |
+
|
| 83 |
+
IntegerArray is currently experimental, and its API or internal
|
| 84 |
+
implementation may change without warning.
|
| 85 |
+
|
| 86 |
+
We represent an IntegerArray with 2 numpy arrays:
|
| 87 |
+
|
| 88 |
+
- data: contains a numpy integer array of the appropriate dtype
|
| 89 |
+
- mask: a boolean array holding a mask on the data, True is missing
|
| 90 |
+
|
| 91 |
+
To construct an IntegerArray from generic array-like input, use
|
| 92 |
+
:func:`pandas.array` with one of the integer dtypes (see examples).
|
| 93 |
+
|
| 94 |
+
See :ref:`integer_na` for more.
|
| 95 |
+
|
| 96 |
+
Parameters
|
| 97 |
+
----------
|
| 98 |
+
values : numpy.ndarray
|
| 99 |
+
A 1-d integer-dtype array.
|
| 100 |
+
mask : numpy.ndarray
|
| 101 |
+
A 1-d boolean-dtype array indicating missing values.
|
| 102 |
+
copy : bool, default False
|
| 103 |
+
Whether to copy the `values` and `mask`.
|
| 104 |
+
|
| 105 |
+
Attributes
|
| 106 |
+
----------
|
| 107 |
+
None
|
| 108 |
+
|
| 109 |
+
Methods
|
| 110 |
+
-------
|
| 111 |
+
None
|
| 112 |
+
|
| 113 |
+
Returns
|
| 114 |
+
-------
|
| 115 |
+
IntegerArray
|
| 116 |
+
|
| 117 |
+
See Also
|
| 118 |
+
--------
|
| 119 |
+
array : Create an array using the appropriate dtype, including ``IntegerArray``.
|
| 120 |
+
Int32Dtype : An ExtensionDtype for int32 integer data.
|
| 121 |
+
UInt16Dtype : An ExtensionDtype for uint16 integer data.
|
| 122 |
+
|
| 123 |
+
Examples
|
| 124 |
+
--------
|
| 125 |
+
Create an IntegerArray with :func:`pandas.array`.
|
| 126 |
+
|
| 127 |
+
>>> int_array = pd.array([1, None, 3], dtype=pd.Int32Dtype())
|
| 128 |
+
>>> int_array
|
| 129 |
+
<IntegerArray>
|
| 130 |
+
[1, <NA>, 3]
|
| 131 |
+
Length: 3, dtype: Int32
|
| 132 |
+
|
| 133 |
+
String aliases for the dtypes are also available. They are capitalized.
|
| 134 |
+
|
| 135 |
+
>>> pd.array([1, None, 3], dtype="Int32")
|
| 136 |
+
<IntegerArray>
|
| 137 |
+
[1, <NA>, 3]
|
| 138 |
+
Length: 3, dtype: Int32
|
| 139 |
+
|
| 140 |
+
>>> pd.array([1, None, 3], dtype="UInt16")
|
| 141 |
+
<IntegerArray>
|
| 142 |
+
[1, <NA>, 3]
|
| 143 |
+
Length: 3, dtype: UInt16
|
| 144 |
+
"""
|
| 145 |
+
|
| 146 |
+
_dtype_cls = IntegerDtype
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
_dtype_docstring = """
|
| 150 |
+
An ExtensionDtype for {dtype} integer data.
|
| 151 |
+
|
| 152 |
+
Uses :attr:`pandas.NA` as its missing value, rather than :attr:`numpy.nan`.
|
| 153 |
+
|
| 154 |
+
Attributes
|
| 155 |
+
----------
|
| 156 |
+
None
|
| 157 |
+
|
| 158 |
+
Methods
|
| 159 |
+
-------
|
| 160 |
+
None
|
| 161 |
+
|
| 162 |
+
See Also
|
| 163 |
+
--------
|
| 164 |
+
Int8Dtype : 8-bit nullable integer type.
|
| 165 |
+
Int16Dtype : 16-bit nullable integer type.
|
| 166 |
+
Int32Dtype : 32-bit nullable integer type.
|
| 167 |
+
Int64Dtype : 64-bit nullable integer type.
|
| 168 |
+
|
| 169 |
+
Examples
|
| 170 |
+
--------
|
| 171 |
+
For Int8Dtype:
|
| 172 |
+
|
| 173 |
+
>>> ser = pd.Series([2, pd.NA], dtype=pd.Int8Dtype())
|
| 174 |
+
>>> ser.dtype
|
| 175 |
+
Int8Dtype()
|
| 176 |
+
|
| 177 |
+
For Int16Dtype:
|
| 178 |
+
|
| 179 |
+
>>> ser = pd.Series([2, pd.NA], dtype=pd.Int16Dtype())
|
| 180 |
+
>>> ser.dtype
|
| 181 |
+
Int16Dtype()
|
| 182 |
+
|
| 183 |
+
For Int32Dtype:
|
| 184 |
+
|
| 185 |
+
>>> ser = pd.Series([2, pd.NA], dtype=pd.Int32Dtype())
|
| 186 |
+
>>> ser.dtype
|
| 187 |
+
Int32Dtype()
|
| 188 |
+
|
| 189 |
+
For Int64Dtype:
|
| 190 |
+
|
| 191 |
+
>>> ser = pd.Series([2, pd.NA], dtype=pd.Int64Dtype())
|
| 192 |
+
>>> ser.dtype
|
| 193 |
+
Int64Dtype()
|
| 194 |
+
|
| 195 |
+
For UInt8Dtype:
|
| 196 |
+
|
| 197 |
+
>>> ser = pd.Series([2, pd.NA], dtype=pd.UInt8Dtype())
|
| 198 |
+
>>> ser.dtype
|
| 199 |
+
UInt8Dtype()
|
| 200 |
+
|
| 201 |
+
For UInt16Dtype:
|
| 202 |
+
|
| 203 |
+
>>> ser = pd.Series([2, pd.NA], dtype=pd.UInt16Dtype())
|
| 204 |
+
>>> ser.dtype
|
| 205 |
+
UInt16Dtype()
|
| 206 |
+
|
| 207 |
+
For UInt32Dtype:
|
| 208 |
+
|
| 209 |
+
>>> ser = pd.Series([2, pd.NA], dtype=pd.UInt32Dtype())
|
| 210 |
+
>>> ser.dtype
|
| 211 |
+
UInt32Dtype()
|
| 212 |
+
|
| 213 |
+
For UInt64Dtype:
|
| 214 |
+
|
| 215 |
+
>>> ser = pd.Series([2, pd.NA], dtype=pd.UInt64Dtype())
|
| 216 |
+
>>> ser.dtype
|
| 217 |
+
UInt64Dtype()
|
| 218 |
+
"""
|
| 219 |
+
|
| 220 |
+
# create the Dtype
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
@register_extension_dtype
|
| 224 |
+
@set_module("pandas")
|
| 225 |
+
class Int8Dtype(IntegerDtype):
|
| 226 |
+
type = np.int8
|
| 227 |
+
name: ClassVar[str] = "Int8"
|
| 228 |
+
__doc__ = _dtype_docstring.format(dtype="int8")
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
@register_extension_dtype
|
| 232 |
+
@set_module("pandas")
|
| 233 |
+
class Int16Dtype(IntegerDtype):
|
| 234 |
+
type = np.int16
|
| 235 |
+
name: ClassVar[str] = "Int16"
|
| 236 |
+
__doc__ = _dtype_docstring.format(dtype="int16")
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
@register_extension_dtype
|
| 240 |
+
@set_module("pandas")
|
| 241 |
+
class Int32Dtype(IntegerDtype):
|
| 242 |
+
type = np.int32
|
| 243 |
+
name: ClassVar[str] = "Int32"
|
| 244 |
+
__doc__ = _dtype_docstring.format(dtype="int32")
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
@register_extension_dtype
|
| 248 |
+
@set_module("pandas")
|
| 249 |
+
class Int64Dtype(IntegerDtype):
|
| 250 |
+
type = np.int64
|
| 251 |
+
name: ClassVar[str] = "Int64"
|
| 252 |
+
__doc__ = _dtype_docstring.format(dtype="int64")
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
@register_extension_dtype
|
| 256 |
+
@set_module("pandas")
|
| 257 |
+
class UInt8Dtype(IntegerDtype):
|
| 258 |
+
type = np.uint8
|
| 259 |
+
name: ClassVar[str] = "UInt8"
|
| 260 |
+
__doc__ = _dtype_docstring.format(dtype="uint8")
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
@register_extension_dtype
|
| 264 |
+
@set_module("pandas")
|
| 265 |
+
class UInt16Dtype(IntegerDtype):
|
| 266 |
+
type = np.uint16
|
| 267 |
+
name: ClassVar[str] = "UInt16"
|
| 268 |
+
__doc__ = _dtype_docstring.format(dtype="uint16")
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
@register_extension_dtype
|
| 272 |
+
@set_module("pandas")
|
| 273 |
+
class UInt32Dtype(IntegerDtype):
|
| 274 |
+
type = np.uint32
|
| 275 |
+
name: ClassVar[str] = "UInt32"
|
| 276 |
+
__doc__ = _dtype_docstring.format(dtype="uint32")
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
@register_extension_dtype
|
| 280 |
+
@set_module("pandas")
|
| 281 |
+
class UInt64Dtype(IntegerDtype):
|
| 282 |
+
type = np.uint64
|
| 283 |
+
name: ClassVar[str] = "UInt64"
|
| 284 |
+
__doc__ = _dtype_docstring.format(dtype="uint64")
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
NUMPY_INT_TO_DTYPE: dict[np.dtype, IntegerDtype] = {
|
| 288 |
+
np.dtype(np.int8): Int8Dtype(),
|
| 289 |
+
np.dtype(np.int16): Int16Dtype(),
|
| 290 |
+
np.dtype(np.int32): Int32Dtype(),
|
| 291 |
+
np.dtype(np.int64): Int64Dtype(),
|
| 292 |
+
np.dtype(np.uint8): UInt8Dtype(),
|
| 293 |
+
np.dtype(np.uint16): UInt16Dtype(),
|
| 294 |
+
np.dtype(np.uint32): UInt32Dtype(),
|
| 295 |
+
np.dtype(np.uint64): UInt64Dtype(),
|
| 296 |
+
}
|