Add loan-advisor-agent (part 3)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +5 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/strings/__pycache__/accessor.cpython-312.pyc +3 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/strings/__pycache__/object_array.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/strings/accessor.py +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/strings/object_array.py +532 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/__init__.py +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/__pycache__/__init__.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/__pycache__/datetimes.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/__pycache__/numeric.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/__pycache__/timedeltas.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/__pycache__/times.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/datetimes.py +1213 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/numeric.py +327 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/timedeltas.py +246 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/times.py +153 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/util/__init__.py +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/util/__pycache__/__init__.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/util/__pycache__/hashing.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/util/__pycache__/numba_.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/util/hashing.py +361 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/util/numba_.py +150 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__init__.py +23 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__pycache__/__init__.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__pycache__/common.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__pycache__/ewm.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__pycache__/expanding.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__pycache__/numba_.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__pycache__/online.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__pycache__/rolling.cpython-312.pyc +3 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/common.py +171 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/doc.py +174 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/ewm.py +1175 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/expanding.py +1412 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/numba_.py +357 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/online.py +117 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/rolling.py +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/errors/__init__.py +1087 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/errors/__pycache__/__init__.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/errors/__pycache__/cow.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/errors/cow.py +43 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__init__.py +13 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/__init__.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/_util.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/api.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/clipboards.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/common.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/feather_format.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/html.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/iceberg.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/orc.cpython-312.pyc +0 -0
.gitattributes
CHANGED
|
@@ -504,3 +504,8 @@ loan-advisor-agent/.venv/Lib/site-packages/pandas/core/groupby/__pycache__/group
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 507 |
+
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/strings/__pycache__/accessor.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 508 |
+
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__pycache__/rolling.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 509 |
+
loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/pytables.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 510 |
+
loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/sql.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 511 |
+
loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/stata.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/strings/__pycache__/accessor.cpython-312.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1f65e83f1dc3eae0b81bcd4bbe566754ecf1f21eaec0cda38c94654e5c207f3f
|
| 3 |
+
size 174127
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/strings/__pycache__/object_array.cpython-312.pyc
ADDED
|
Binary file (32.8 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/strings/accessor.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/strings/object_array.py
ADDED
|
@@ -0,0 +1,532 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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2 |
+
|
| 3 |
+
import functools
|
| 4 |
+
import re
|
| 5 |
+
import textwrap
|
| 6 |
+
from typing import (
|
| 7 |
+
TYPE_CHECKING,
|
| 8 |
+
Literal,
|
| 9 |
+
cast,
|
| 10 |
+
)
|
| 11 |
+
import unicodedata
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
from pandas._libs import lib
|
| 16 |
+
import pandas._libs.missing as libmissing
|
| 17 |
+
import pandas._libs.ops as libops
|
| 18 |
+
from pandas.util._validators import validate_na_arg
|
| 19 |
+
|
| 20 |
+
from pandas.core.dtypes.common import pandas_dtype
|
| 21 |
+
from pandas.core.dtypes.missing import isna
|
| 22 |
+
|
| 23 |
+
if TYPE_CHECKING:
|
| 24 |
+
from collections.abc import (
|
| 25 |
+
Callable,
|
| 26 |
+
Sequence,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
from pandas._typing import (
|
| 30 |
+
NpDtype,
|
| 31 |
+
Scalar,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class ObjectStringArrayMixin:
|
| 36 |
+
"""
|
| 37 |
+
String Methods operating on object-dtype ndarrays.
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
def __len__(self) -> int:
|
| 41 |
+
# For typing, _str_map relies on the object being sized.
|
| 42 |
+
raise NotImplementedError
|
| 43 |
+
|
| 44 |
+
def _str_getitem(self, key):
|
| 45 |
+
if isinstance(key, slice):
|
| 46 |
+
return self._str_slice(start=key.start, stop=key.stop, step=key.step)
|
| 47 |
+
else:
|
| 48 |
+
return self._str_get(key)
|
| 49 |
+
|
| 50 |
+
def _str_map(
|
| 51 |
+
self,
|
| 52 |
+
f,
|
| 53 |
+
na_value=lib.no_default,
|
| 54 |
+
dtype: NpDtype | None = None,
|
| 55 |
+
convert: bool = True,
|
| 56 |
+
):
|
| 57 |
+
"""
|
| 58 |
+
Map a callable over valid elements of the array.
|
| 59 |
+
|
| 60 |
+
Parameters
|
| 61 |
+
----------
|
| 62 |
+
f : Callable
|
| 63 |
+
A function to call on each non-NA element.
|
| 64 |
+
na_value : Scalar, optional
|
| 65 |
+
The value to set for NA values. Might also be used for the
|
| 66 |
+
fill value if the callable `f` raises an exception.
|
| 67 |
+
This defaults to ``self.dtype.na_value`` which is ``np.nan``
|
| 68 |
+
for object-dtype and Categorical and ``pd.NA`` for StringArray.
|
| 69 |
+
dtype : Dtype, optional
|
| 70 |
+
The dtype of the result array.
|
| 71 |
+
convert : bool, default True
|
| 72 |
+
Whether to call `maybe_convert_objects` on the resulting ndarray
|
| 73 |
+
"""
|
| 74 |
+
if dtype is None:
|
| 75 |
+
dtype = np.dtype("object")
|
| 76 |
+
if na_value is lib.no_default:
|
| 77 |
+
na_value = self.dtype.na_value # type: ignore[attr-defined]
|
| 78 |
+
|
| 79 |
+
if not len(self):
|
| 80 |
+
return np.array([], dtype=dtype)
|
| 81 |
+
|
| 82 |
+
arr = np.asarray(self, dtype=object)
|
| 83 |
+
mask = isna(arr)
|
| 84 |
+
map_convert = convert and not np.all(mask)
|
| 85 |
+
try:
|
| 86 |
+
result = lib.map_infer_mask(
|
| 87 |
+
arr, f, mask.view(np.uint8), convert=map_convert
|
| 88 |
+
)
|
| 89 |
+
except (TypeError, AttributeError) as err:
|
| 90 |
+
# Reraise the exception if callable `f` got wrong number of args.
|
| 91 |
+
# The user may want to be warned by this, instead of getting NaN
|
| 92 |
+
p_err = (
|
| 93 |
+
r"((takes)|(missing)) (?(2)from \d+ to )?\d+ "
|
| 94 |
+
r"(?(3)required )positional arguments?"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
if len(err.args) >= 1 and re.search(p_err, err.args[0]):
|
| 98 |
+
# FIXME: this should be totally avoidable
|
| 99 |
+
raise err
|
| 100 |
+
|
| 101 |
+
def g(x):
|
| 102 |
+
# This type of fallback behavior can be removed once
|
| 103 |
+
# we remove object-dtype .str accessor.
|
| 104 |
+
try:
|
| 105 |
+
return f(x)
|
| 106 |
+
except (TypeError, AttributeError):
|
| 107 |
+
return na_value
|
| 108 |
+
|
| 109 |
+
return self._str_map(g, na_value=na_value, dtype=dtype)
|
| 110 |
+
if not isinstance(result, np.ndarray):
|
| 111 |
+
return result
|
| 112 |
+
if na_value is not np.nan:
|
| 113 |
+
np.putmask(result, mask, na_value)
|
| 114 |
+
if convert and result.dtype == object:
|
| 115 |
+
result = lib.maybe_convert_objects(result)
|
| 116 |
+
return result
|
| 117 |
+
|
| 118 |
+
def _str_count(self, pat, flags: int = 0):
|
| 119 |
+
regex = re.compile(pat, flags=flags)
|
| 120 |
+
f = lambda x: len(regex.findall(x))
|
| 121 |
+
return self._str_map(f, dtype="int64")
|
| 122 |
+
|
| 123 |
+
def _str_pad(
|
| 124 |
+
self,
|
| 125 |
+
width: int,
|
| 126 |
+
side: Literal["left", "right", "both"] = "left",
|
| 127 |
+
fillchar: str = " ",
|
| 128 |
+
):
|
| 129 |
+
if side == "left":
|
| 130 |
+
f = lambda x: x.rjust(width, fillchar)
|
| 131 |
+
elif side == "right":
|
| 132 |
+
f = lambda x: x.ljust(width, fillchar)
|
| 133 |
+
elif side == "both":
|
| 134 |
+
f = lambda x: x.center(width, fillchar)
|
| 135 |
+
else: # pragma: no cover
|
| 136 |
+
raise ValueError("Invalid side")
|
| 137 |
+
return self._str_map(f)
|
| 138 |
+
|
| 139 |
+
def _str_contains(
|
| 140 |
+
self,
|
| 141 |
+
pat,
|
| 142 |
+
case: bool = True,
|
| 143 |
+
flags: int = 0,
|
| 144 |
+
na=lib.no_default,
|
| 145 |
+
regex: bool = True,
|
| 146 |
+
):
|
| 147 |
+
validate_na_arg(na, name="na")
|
| 148 |
+
if regex:
|
| 149 |
+
if not case:
|
| 150 |
+
flags |= re.IGNORECASE
|
| 151 |
+
|
| 152 |
+
pat = re.compile(pat, flags=flags)
|
| 153 |
+
|
| 154 |
+
f = lambda x: pat.search(x) is not None
|
| 155 |
+
elif case:
|
| 156 |
+
f = lambda x: pat in x
|
| 157 |
+
else:
|
| 158 |
+
upper_pat = pat.upper()
|
| 159 |
+
f = lambda x: upper_pat in x.upper()
|
| 160 |
+
return self._str_map(f, na, dtype=np.dtype("bool"))
|
| 161 |
+
|
| 162 |
+
def _str_startswith(self, pat, na=lib.no_default):
|
| 163 |
+
validate_na_arg(na, name="na")
|
| 164 |
+
f = lambda x: x.startswith(pat)
|
| 165 |
+
return self._str_map(f, na_value=na, dtype=np.dtype(bool))
|
| 166 |
+
|
| 167 |
+
def _str_endswith(self, pat, na=lib.no_default):
|
| 168 |
+
validate_na_arg(na, name="na")
|
| 169 |
+
f = lambda x: x.endswith(pat)
|
| 170 |
+
return self._str_map(f, na_value=na, dtype=np.dtype(bool))
|
| 171 |
+
|
| 172 |
+
def _str_replace(
|
| 173 |
+
self,
|
| 174 |
+
pat: str | re.Pattern,
|
| 175 |
+
repl: str | Callable,
|
| 176 |
+
n: int = -1,
|
| 177 |
+
case: bool = True,
|
| 178 |
+
flags: int = 0,
|
| 179 |
+
regex: bool = True,
|
| 180 |
+
):
|
| 181 |
+
if case is False:
|
| 182 |
+
# add case flag, if provided
|
| 183 |
+
flags |= re.IGNORECASE
|
| 184 |
+
|
| 185 |
+
if regex or flags or callable(repl):
|
| 186 |
+
if not isinstance(pat, re.Pattern):
|
| 187 |
+
if regex is False:
|
| 188 |
+
pat = re.escape(pat)
|
| 189 |
+
pat = re.compile(pat, flags=flags)
|
| 190 |
+
|
| 191 |
+
n = n if n >= 0 else 0
|
| 192 |
+
f = lambda x: pat.sub(repl=repl, string=x, count=n)
|
| 193 |
+
else:
|
| 194 |
+
f = lambda x: x.replace(pat, repl, n)
|
| 195 |
+
|
| 196 |
+
return self._str_map(f, dtype=str)
|
| 197 |
+
|
| 198 |
+
def _str_repeat(self, repeats: int | Sequence[int]):
|
| 199 |
+
if lib.is_integer(repeats):
|
| 200 |
+
rint = cast(int, repeats)
|
| 201 |
+
|
| 202 |
+
def scalar_rep(x):
|
| 203 |
+
try:
|
| 204 |
+
return bytes.__mul__(x, rint)
|
| 205 |
+
except TypeError:
|
| 206 |
+
return str.__mul__(x, rint)
|
| 207 |
+
|
| 208 |
+
return self._str_map(scalar_rep, dtype=str)
|
| 209 |
+
else:
|
| 210 |
+
from pandas.core.arrays.string_ import BaseStringArray
|
| 211 |
+
|
| 212 |
+
def rep(x, r):
|
| 213 |
+
if x is libmissing.NA:
|
| 214 |
+
return x
|
| 215 |
+
try:
|
| 216 |
+
return bytes.__mul__(x, r)
|
| 217 |
+
except TypeError:
|
| 218 |
+
return str.__mul__(x, r)
|
| 219 |
+
|
| 220 |
+
result = libops.vec_binop(
|
| 221 |
+
np.asarray(self),
|
| 222 |
+
np.asarray(repeats, dtype=object),
|
| 223 |
+
rep,
|
| 224 |
+
)
|
| 225 |
+
if not isinstance(self, BaseStringArray):
|
| 226 |
+
return result
|
| 227 |
+
# Not going through map, so we have to do this here.
|
| 228 |
+
return type(self)._from_sequence(result, dtype=self.dtype)
|
| 229 |
+
|
| 230 |
+
def _str_match(
|
| 231 |
+
self,
|
| 232 |
+
pat: str | re.Pattern,
|
| 233 |
+
case: bool = True,
|
| 234 |
+
flags: int = 0,
|
| 235 |
+
na: Scalar | lib.NoDefault = lib.no_default,
|
| 236 |
+
):
|
| 237 |
+
if not case:
|
| 238 |
+
flags |= re.IGNORECASE
|
| 239 |
+
|
| 240 |
+
if isinstance(pat, re.Pattern):
|
| 241 |
+
# We need to check that flags matches pat.flags.
|
| 242 |
+
# pat.flags will have re.U regardless, so we need to add it here
|
| 243 |
+
# before checking for a match
|
| 244 |
+
flags = flags | re.U
|
| 245 |
+
|
| 246 |
+
if flags != pat.flags:
|
| 247 |
+
raise ValueError("Cannot pass flags that do not match pat.flags")
|
| 248 |
+
regex = pat
|
| 249 |
+
else:
|
| 250 |
+
regex = re.compile(pat, flags=flags)
|
| 251 |
+
|
| 252 |
+
f = lambda x: regex.match(x) is not None
|
| 253 |
+
return self._str_map(f, na_value=na, dtype=np.dtype(bool))
|
| 254 |
+
|
| 255 |
+
def _str_fullmatch(
|
| 256 |
+
self,
|
| 257 |
+
pat: str | re.Pattern,
|
| 258 |
+
case: bool = True,
|
| 259 |
+
flags: int = 0,
|
| 260 |
+
na: Scalar | lib.NoDefault = lib.no_default,
|
| 261 |
+
):
|
| 262 |
+
if not case:
|
| 263 |
+
flags |= re.IGNORECASE
|
| 264 |
+
|
| 265 |
+
regex = re.compile(pat, flags=flags)
|
| 266 |
+
|
| 267 |
+
f = lambda x: regex.fullmatch(x) is not None
|
| 268 |
+
return self._str_map(f, na_value=na, dtype=np.dtype(bool))
|
| 269 |
+
|
| 270 |
+
def _str_encode(self, encoding, errors: str = "strict"):
|
| 271 |
+
f = lambda x: x.encode(encoding, errors=errors)
|
| 272 |
+
return self._str_map(f, dtype=object)
|
| 273 |
+
|
| 274 |
+
def _str_find(self, sub, start: int = 0, end=None):
|
| 275 |
+
return self._str_find_(sub, start, end, side="left")
|
| 276 |
+
|
| 277 |
+
def _str_rfind(self, sub, start: int = 0, end=None):
|
| 278 |
+
return self._str_find_(sub, start, end, side="right")
|
| 279 |
+
|
| 280 |
+
def _str_find_(self, sub, start, end, side):
|
| 281 |
+
if side == "left":
|
| 282 |
+
method = "find"
|
| 283 |
+
elif side == "right":
|
| 284 |
+
method = "rfind"
|
| 285 |
+
else: # pragma: no cover
|
| 286 |
+
raise ValueError("Invalid side")
|
| 287 |
+
|
| 288 |
+
if end is None:
|
| 289 |
+
f = lambda x: getattr(x, method)(sub, start)
|
| 290 |
+
else:
|
| 291 |
+
f = lambda x: getattr(x, method)(sub, start, end)
|
| 292 |
+
return self._str_map(f, dtype="int64")
|
| 293 |
+
|
| 294 |
+
def _str_findall(self, pat, flags: int = 0):
|
| 295 |
+
regex = re.compile(pat, flags=flags)
|
| 296 |
+
return self._str_map(regex.findall, dtype="object")
|
| 297 |
+
|
| 298 |
+
def _str_get(self, i):
|
| 299 |
+
def f(x):
|
| 300 |
+
if isinstance(x, dict):
|
| 301 |
+
return x.get(i)
|
| 302 |
+
elif len(x) > i >= -len(x):
|
| 303 |
+
return x[i]
|
| 304 |
+
return self.dtype.na_value # type: ignore[attr-defined]
|
| 305 |
+
|
| 306 |
+
return self._str_map(f)
|
| 307 |
+
|
| 308 |
+
def _str_index(self, sub, start: int = 0, end=None):
|
| 309 |
+
if end:
|
| 310 |
+
f = lambda x: x.index(sub, start, end)
|
| 311 |
+
else:
|
| 312 |
+
f = lambda x: x.index(sub, start, end)
|
| 313 |
+
return self._str_map(f, dtype="int64")
|
| 314 |
+
|
| 315 |
+
def _str_rindex(self, sub, start: int = 0, end=None):
|
| 316 |
+
if end:
|
| 317 |
+
f = lambda x: x.rindex(sub, start, end)
|
| 318 |
+
else:
|
| 319 |
+
f = lambda x: x.rindex(sub, start, end)
|
| 320 |
+
return self._str_map(f, dtype="int64")
|
| 321 |
+
|
| 322 |
+
def _str_join(self, sep: str):
|
| 323 |
+
return self._str_map(sep.join)
|
| 324 |
+
|
| 325 |
+
def _str_partition(self, sep: str, expand):
|
| 326 |
+
result = self._str_map(lambda x: x.partition(sep), dtype="object")
|
| 327 |
+
return result
|
| 328 |
+
|
| 329 |
+
def _str_rpartition(self, sep: str, expand):
|
| 330 |
+
return self._str_map(lambda x: x.rpartition(sep), dtype="object")
|
| 331 |
+
|
| 332 |
+
def _str_len(self):
|
| 333 |
+
return self._str_map(len, dtype="int64")
|
| 334 |
+
|
| 335 |
+
def _str_slice(self, start=None, stop=None, step=None):
|
| 336 |
+
obj = slice(start, stop, step)
|
| 337 |
+
return self._str_map(lambda x: x[obj])
|
| 338 |
+
|
| 339 |
+
def _str_slice_replace(self, start=None, stop=None, repl=None):
|
| 340 |
+
if repl is None:
|
| 341 |
+
repl = ""
|
| 342 |
+
|
| 343 |
+
def f(x):
|
| 344 |
+
if x[start:stop] == "":
|
| 345 |
+
local_stop = start
|
| 346 |
+
else:
|
| 347 |
+
local_stop = stop
|
| 348 |
+
y = ""
|
| 349 |
+
if start is not None:
|
| 350 |
+
y += x[:start]
|
| 351 |
+
y += repl
|
| 352 |
+
if stop is not None:
|
| 353 |
+
y += x[local_stop:]
|
| 354 |
+
return y
|
| 355 |
+
|
| 356 |
+
return self._str_map(f)
|
| 357 |
+
|
| 358 |
+
def _str_split(
|
| 359 |
+
self,
|
| 360 |
+
pat: str | re.Pattern | None = None,
|
| 361 |
+
n=-1,
|
| 362 |
+
expand: bool = False,
|
| 363 |
+
regex: bool | None = None,
|
| 364 |
+
):
|
| 365 |
+
if pat is None:
|
| 366 |
+
if n is None or n == 0:
|
| 367 |
+
n = -1
|
| 368 |
+
f = lambda x: x.split(pat, n)
|
| 369 |
+
else:
|
| 370 |
+
new_pat: str | re.Pattern
|
| 371 |
+
if regex is True or isinstance(pat, re.Pattern):
|
| 372 |
+
new_pat = re.compile(pat)
|
| 373 |
+
elif regex is False:
|
| 374 |
+
new_pat = pat
|
| 375 |
+
# regex is None so link to old behavior #43563
|
| 376 |
+
elif len(pat) == 1:
|
| 377 |
+
new_pat = pat
|
| 378 |
+
else:
|
| 379 |
+
new_pat = re.compile(pat)
|
| 380 |
+
|
| 381 |
+
if isinstance(new_pat, re.Pattern):
|
| 382 |
+
if n is None or n == -1:
|
| 383 |
+
n = 0
|
| 384 |
+
f = lambda x: new_pat.split(x, maxsplit=n)
|
| 385 |
+
else:
|
| 386 |
+
if n is None or n == 0:
|
| 387 |
+
n = -1
|
| 388 |
+
f = lambda x: x.split(pat, n)
|
| 389 |
+
return self._str_map(f, dtype=object)
|
| 390 |
+
|
| 391 |
+
def _str_rsplit(self, pat=None, n=-1):
|
| 392 |
+
if n is None or n == 0:
|
| 393 |
+
n = -1
|
| 394 |
+
f = lambda x: x.rsplit(pat, n)
|
| 395 |
+
return self._str_map(f, dtype="object")
|
| 396 |
+
|
| 397 |
+
def _str_translate(self, table):
|
| 398 |
+
return self._str_map(lambda x: x.translate(table))
|
| 399 |
+
|
| 400 |
+
def _str_wrap(self, width: int, **kwargs):
|
| 401 |
+
kwargs["width"] = width
|
| 402 |
+
tw = textwrap.TextWrapper(**kwargs)
|
| 403 |
+
return self._str_map(lambda s: "\n".join(tw.wrap(s)))
|
| 404 |
+
|
| 405 |
+
def _str_get_dummies(self, sep: str = "|", dtype: NpDtype | None = None):
|
| 406 |
+
from pandas import Series
|
| 407 |
+
|
| 408 |
+
if dtype is None:
|
| 409 |
+
dtype = np.int64
|
| 410 |
+
arr = Series(self).fillna("")
|
| 411 |
+
try:
|
| 412 |
+
arr = sep + arr + sep
|
| 413 |
+
except (TypeError, NotImplementedError):
|
| 414 |
+
arr = sep + arr.astype(str) + sep
|
| 415 |
+
|
| 416 |
+
tags: set[str] = set()
|
| 417 |
+
for ts in Series(arr, copy=False).str.split(sep):
|
| 418 |
+
tags.update(ts)
|
| 419 |
+
tags2 = sorted(tags - {""})
|
| 420 |
+
|
| 421 |
+
_dtype = pandas_dtype(dtype)
|
| 422 |
+
dummies_dtype: NpDtype
|
| 423 |
+
if isinstance(_dtype, np.dtype):
|
| 424 |
+
dummies_dtype = _dtype
|
| 425 |
+
else:
|
| 426 |
+
dummies_dtype = np.bool_
|
| 427 |
+
dummies = np.empty((len(arr), len(tags2)), dtype=dummies_dtype, order="F")
|
| 428 |
+
|
| 429 |
+
def _isin(test_elements: str, element: str) -> bool:
|
| 430 |
+
return element in test_elements
|
| 431 |
+
|
| 432 |
+
for i, t in enumerate(tags2):
|
| 433 |
+
pat = sep + t + sep
|
| 434 |
+
dummies[:, i] = lib.map_infer(
|
| 435 |
+
arr.to_numpy(), functools.partial(_isin, element=pat)
|
| 436 |
+
)
|
| 437 |
+
return dummies, tags2
|
| 438 |
+
|
| 439 |
+
def _str_upper(self):
|
| 440 |
+
return self._str_map(lambda x: x.upper())
|
| 441 |
+
|
| 442 |
+
def _str_isalnum(self):
|
| 443 |
+
return self._str_map(str.isalnum, dtype="bool")
|
| 444 |
+
|
| 445 |
+
def _str_isalpha(self):
|
| 446 |
+
return self._str_map(str.isalpha, dtype="bool")
|
| 447 |
+
|
| 448 |
+
def _str_isascii(self):
|
| 449 |
+
return self._str_map(str.isascii, dtype="bool")
|
| 450 |
+
|
| 451 |
+
def _str_isdecimal(self):
|
| 452 |
+
return self._str_map(str.isdecimal, dtype="bool")
|
| 453 |
+
|
| 454 |
+
def _str_isdigit(self):
|
| 455 |
+
return self._str_map(str.isdigit, dtype="bool")
|
| 456 |
+
|
| 457 |
+
def _str_islower(self):
|
| 458 |
+
return self._str_map(str.islower, dtype="bool")
|
| 459 |
+
|
| 460 |
+
def _str_isnumeric(self):
|
| 461 |
+
return self._str_map(str.isnumeric, dtype="bool")
|
| 462 |
+
|
| 463 |
+
def _str_isspace(self):
|
| 464 |
+
return self._str_map(str.isspace, dtype="bool")
|
| 465 |
+
|
| 466 |
+
def _str_istitle(self):
|
| 467 |
+
return self._str_map(str.istitle, dtype="bool")
|
| 468 |
+
|
| 469 |
+
def _str_isupper(self):
|
| 470 |
+
return self._str_map(str.isupper, dtype="bool")
|
| 471 |
+
|
| 472 |
+
def _str_capitalize(self):
|
| 473 |
+
return self._str_map(str.capitalize)
|
| 474 |
+
|
| 475 |
+
def _str_casefold(self):
|
| 476 |
+
return self._str_map(str.casefold)
|
| 477 |
+
|
| 478 |
+
def _str_title(self):
|
| 479 |
+
return self._str_map(str.title)
|
| 480 |
+
|
| 481 |
+
def _str_swapcase(self):
|
| 482 |
+
return self._str_map(str.swapcase)
|
| 483 |
+
|
| 484 |
+
def _str_lower(self):
|
| 485 |
+
return self._str_map(str.lower)
|
| 486 |
+
|
| 487 |
+
def _str_normalize(self, form):
|
| 488 |
+
f = lambda x: unicodedata.normalize(form, x)
|
| 489 |
+
return self._str_map(f)
|
| 490 |
+
|
| 491 |
+
def _str_strip(self, to_strip=None):
|
| 492 |
+
return self._str_map(lambda x: x.strip(to_strip))
|
| 493 |
+
|
| 494 |
+
def _str_lstrip(self, to_strip=None):
|
| 495 |
+
return self._str_map(lambda x: x.lstrip(to_strip))
|
| 496 |
+
|
| 497 |
+
def _str_rstrip(self, to_strip=None):
|
| 498 |
+
return self._str_map(lambda x: x.rstrip(to_strip))
|
| 499 |
+
|
| 500 |
+
def _str_removeprefix(self, prefix: str):
|
| 501 |
+
return self._str_map(lambda x: x.removeprefix(prefix))
|
| 502 |
+
|
| 503 |
+
def _str_removesuffix(self, suffix: str):
|
| 504 |
+
return self._str_map(lambda x: x.removesuffix(suffix))
|
| 505 |
+
|
| 506 |
+
def _str_extract(self, pat: str, flags: int = 0, expand: bool = True):
|
| 507 |
+
regex = re.compile(pat, flags=flags)
|
| 508 |
+
na_value = self.dtype.na_value # type: ignore[attr-defined]
|
| 509 |
+
|
| 510 |
+
if not expand:
|
| 511 |
+
|
| 512 |
+
def g(x):
|
| 513 |
+
m = regex.search(x)
|
| 514 |
+
return m.groups()[0] if m else na_value
|
| 515 |
+
|
| 516 |
+
return self._str_map(g, convert=False)
|
| 517 |
+
|
| 518 |
+
empty_row = [na_value] * regex.groups
|
| 519 |
+
|
| 520 |
+
def f(x):
|
| 521 |
+
if not isinstance(x, str):
|
| 522 |
+
return empty_row
|
| 523 |
+
m = regex.search(x)
|
| 524 |
+
if m:
|
| 525 |
+
return [na_value if item is None else item for item in m.groups()]
|
| 526 |
+
else:
|
| 527 |
+
return empty_row
|
| 528 |
+
|
| 529 |
+
return [f(val) for val in np.asarray(self)]
|
| 530 |
+
|
| 531 |
+
def _str_zfill(self, width: int):
|
| 532 |
+
return self._str_map(lambda x: x.zfill(width))
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/__init__.py
ADDED
|
File without changes
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (194 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/__pycache__/datetimes.cpython-312.pyc
ADDED
|
Binary file (44.3 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/__pycache__/numeric.cpython-312.pyc
ADDED
|
Binary file (11 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/__pycache__/timedeltas.cpython-312.pyc
ADDED
|
Binary file (8.21 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/__pycache__/times.cpython-312.pyc
ADDED
|
Binary file (5.29 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/datetimes.py
ADDED
|
@@ -0,0 +1,1213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from collections import abc
|
| 4 |
+
from datetime import date
|
| 5 |
+
from functools import partial
|
| 6 |
+
from itertools import islice
|
| 7 |
+
from typing import (
|
| 8 |
+
TYPE_CHECKING,
|
| 9 |
+
TypeAlias,
|
| 10 |
+
TypedDict,
|
| 11 |
+
Union,
|
| 12 |
+
cast,
|
| 13 |
+
overload,
|
| 14 |
+
)
|
| 15 |
+
import warnings
|
| 16 |
+
|
| 17 |
+
import numpy as np
|
| 18 |
+
|
| 19 |
+
from pandas._libs import (
|
| 20 |
+
lib,
|
| 21 |
+
tslib,
|
| 22 |
+
)
|
| 23 |
+
from pandas._libs.tslibs import (
|
| 24 |
+
NaT,
|
| 25 |
+
OutOfBoundsDatetime,
|
| 26 |
+
Timedelta,
|
| 27 |
+
Timestamp,
|
| 28 |
+
astype_overflowsafe,
|
| 29 |
+
get_supported_dtype,
|
| 30 |
+
is_supported_dtype,
|
| 31 |
+
timezones as libtimezones,
|
| 32 |
+
)
|
| 33 |
+
from pandas._libs.tslibs.conversion import cast_from_unit_vectorized
|
| 34 |
+
from pandas._libs.tslibs.parsing import (
|
| 35 |
+
DateParseError,
|
| 36 |
+
guess_datetime_format,
|
| 37 |
+
)
|
| 38 |
+
from pandas._libs.tslibs.strptime import array_strptime
|
| 39 |
+
from pandas._typing import (
|
| 40 |
+
AnyArrayLike,
|
| 41 |
+
ArrayLike,
|
| 42 |
+
DateTimeErrorChoices,
|
| 43 |
+
)
|
| 44 |
+
from pandas.util._decorators import set_module
|
| 45 |
+
from pandas.util._exceptions import find_stack_level
|
| 46 |
+
|
| 47 |
+
from pandas.core.dtypes.common import (
|
| 48 |
+
ensure_object,
|
| 49 |
+
is_float,
|
| 50 |
+
is_float_dtype,
|
| 51 |
+
is_integer,
|
| 52 |
+
is_integer_dtype,
|
| 53 |
+
is_list_like,
|
| 54 |
+
is_numeric_dtype,
|
| 55 |
+
)
|
| 56 |
+
from pandas.core.dtypes.dtypes import (
|
| 57 |
+
ArrowDtype,
|
| 58 |
+
DatetimeTZDtype,
|
| 59 |
+
)
|
| 60 |
+
from pandas.core.dtypes.generic import (
|
| 61 |
+
ABCDataFrame,
|
| 62 |
+
ABCSeries,
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
from pandas.arrays import (
|
| 66 |
+
DatetimeArray,
|
| 67 |
+
IntegerArray,
|
| 68 |
+
NumpyExtensionArray,
|
| 69 |
+
)
|
| 70 |
+
from pandas.core.algorithms import unique
|
| 71 |
+
from pandas.core.arrays import ArrowExtensionArray
|
| 72 |
+
from pandas.core.arrays.base import ExtensionArray
|
| 73 |
+
from pandas.core.arrays.datetimes import (
|
| 74 |
+
maybe_convert_dtype,
|
| 75 |
+
objects_to_datetime64,
|
| 76 |
+
tz_to_dtype,
|
| 77 |
+
)
|
| 78 |
+
from pandas.core.construction import extract_array
|
| 79 |
+
from pandas.core.indexes.base import Index
|
| 80 |
+
from pandas.core.indexes.datetimes import DatetimeIndex
|
| 81 |
+
|
| 82 |
+
if TYPE_CHECKING:
|
| 83 |
+
from collections.abc import (
|
| 84 |
+
Callable,
|
| 85 |
+
Hashable,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
from pandas._libs.tslibs.nattype import NaTType
|
| 89 |
+
from pandas._libs.tslibs.timedeltas import UnitChoices
|
| 90 |
+
from pandas._typing import TimeUnit
|
| 91 |
+
|
| 92 |
+
from pandas import (
|
| 93 |
+
DataFrame,
|
| 94 |
+
Series,
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# ---------------------------------------------------------------------
|
| 98 |
+
# types used in annotations
|
| 99 |
+
|
| 100 |
+
ArrayConvertible: TypeAlias = list | tuple | AnyArrayLike
|
| 101 |
+
Scalar: TypeAlias = float | str
|
| 102 |
+
DatetimeScalar: TypeAlias = Scalar | date | np.datetime64
|
| 103 |
+
|
| 104 |
+
DatetimeScalarOrArrayConvertible: TypeAlias = DatetimeScalar | ArrayConvertible
|
| 105 |
+
DatetimeDictArg: TypeAlias = list[Scalar] | tuple[Scalar, ...] | AnyArrayLike
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
class YearMonthDayDict(TypedDict, total=True):
|
| 109 |
+
year: DatetimeDictArg
|
| 110 |
+
month: DatetimeDictArg
|
| 111 |
+
day: DatetimeDictArg
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
class FulldatetimeDict(YearMonthDayDict, total=False):
|
| 115 |
+
hour: DatetimeDictArg
|
| 116 |
+
hours: DatetimeDictArg
|
| 117 |
+
minute: DatetimeDictArg
|
| 118 |
+
minutes: DatetimeDictArg
|
| 119 |
+
second: DatetimeDictArg
|
| 120 |
+
seconds: DatetimeDictArg
|
| 121 |
+
ms: DatetimeDictArg
|
| 122 |
+
us: DatetimeDictArg
|
| 123 |
+
ns: DatetimeDictArg
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
DictConvertible = Union[FulldatetimeDict, "DataFrame"]
|
| 127 |
+
start_caching_at = 50
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
# ---------------------------------------------------------------------
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def _guess_datetime_format_for_array(arr, dayfirst: bool | None = False) -> str | None:
|
| 134 |
+
# Try to guess the format based on the first non-NaN element, return None if can't
|
| 135 |
+
if (first_non_null := tslib.first_non_null(arr)) != -1:
|
| 136 |
+
if type(first_non_nan_element := arr[first_non_null]) is str:
|
| 137 |
+
# GH#32264 np.str_ object
|
| 138 |
+
guessed_format = guess_datetime_format(
|
| 139 |
+
first_non_nan_element, dayfirst=dayfirst
|
| 140 |
+
)
|
| 141 |
+
if guessed_format is not None:
|
| 142 |
+
return guessed_format
|
| 143 |
+
# If there are multiple non-null elements, warn about
|
| 144 |
+
# how parsing might not be consistent
|
| 145 |
+
if tslib.first_non_null(arr[first_non_null + 1 :]) != -1:
|
| 146 |
+
warnings.warn(
|
| 147 |
+
"Could not infer format, so each element will be parsed "
|
| 148 |
+
"individually, falling back to `dateutil`. To ensure parsing is "
|
| 149 |
+
"consistent and as-expected, please specify a format.",
|
| 150 |
+
UserWarning,
|
| 151 |
+
stacklevel=find_stack_level(),
|
| 152 |
+
)
|
| 153 |
+
return None
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def should_cache(
|
| 157 |
+
arg: ArrayConvertible, unique_share: float = 0.7, check_count: int | None = None
|
| 158 |
+
) -> bool:
|
| 159 |
+
"""
|
| 160 |
+
Decides whether to do caching.
|
| 161 |
+
|
| 162 |
+
If the percent of unique elements among `check_count` elements less
|
| 163 |
+
than `unique_share * 100` then we can do caching.
|
| 164 |
+
|
| 165 |
+
Parameters
|
| 166 |
+
----------
|
| 167 |
+
arg: listlike, tuple, 1-d array, Series
|
| 168 |
+
unique_share: float, default=0.7, optional
|
| 169 |
+
0 < unique_share < 1
|
| 170 |
+
check_count: int, optional
|
| 171 |
+
0 <= check_count <= len(arg)
|
| 172 |
+
|
| 173 |
+
Returns
|
| 174 |
+
-------
|
| 175 |
+
do_caching: bool
|
| 176 |
+
|
| 177 |
+
Notes
|
| 178 |
+
-----
|
| 179 |
+
By default for a sequence of less than 50 items in size, we don't do
|
| 180 |
+
caching; for the number of elements less than 5000, we take ten percent of
|
| 181 |
+
all elements to check for a uniqueness share; if the sequence size is more
|
| 182 |
+
than 5000, then we check only the first 500 elements.
|
| 183 |
+
All constants were chosen empirically by.
|
| 184 |
+
"""
|
| 185 |
+
do_caching = True
|
| 186 |
+
|
| 187 |
+
# default realization
|
| 188 |
+
if check_count is None:
|
| 189 |
+
# in this case, the gain from caching is negligible
|
| 190 |
+
if len(arg) <= start_caching_at:
|
| 191 |
+
return False
|
| 192 |
+
|
| 193 |
+
if len(arg) <= 5000:
|
| 194 |
+
check_count = len(arg) // 10
|
| 195 |
+
else:
|
| 196 |
+
check_count = 500
|
| 197 |
+
else:
|
| 198 |
+
assert 0 <= check_count <= len(arg), (
|
| 199 |
+
"check_count must be in next bounds: [0; len(arg)]"
|
| 200 |
+
)
|
| 201 |
+
if check_count == 0:
|
| 202 |
+
return False
|
| 203 |
+
|
| 204 |
+
assert 0 < unique_share < 1, "unique_share must be in next bounds: (0; 1)"
|
| 205 |
+
|
| 206 |
+
try:
|
| 207 |
+
# We can't cache if the items are not hashable.
|
| 208 |
+
unique_elements = set(islice(arg, check_count))
|
| 209 |
+
except TypeError:
|
| 210 |
+
return False
|
| 211 |
+
if len(unique_elements) > check_count * unique_share:
|
| 212 |
+
do_caching = False
|
| 213 |
+
return do_caching
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
def _maybe_cache(
|
| 217 |
+
arg: ArrayConvertible,
|
| 218 |
+
format: str | None,
|
| 219 |
+
cache: bool,
|
| 220 |
+
convert_listlike: Callable,
|
| 221 |
+
) -> Series:
|
| 222 |
+
"""
|
| 223 |
+
Create a cache of unique dates from an array of dates
|
| 224 |
+
|
| 225 |
+
Parameters
|
| 226 |
+
----------
|
| 227 |
+
arg : listlike, tuple, 1-d array, Series
|
| 228 |
+
format : string
|
| 229 |
+
Strftime format to parse time
|
| 230 |
+
cache : bool
|
| 231 |
+
True attempts to create a cache of converted values
|
| 232 |
+
convert_listlike : function
|
| 233 |
+
Conversion function to apply on dates
|
| 234 |
+
|
| 235 |
+
Returns
|
| 236 |
+
-------
|
| 237 |
+
cache_array : Series
|
| 238 |
+
Cache of converted, unique dates. Can be empty
|
| 239 |
+
"""
|
| 240 |
+
from pandas import Series
|
| 241 |
+
|
| 242 |
+
cache_array = Series(dtype=object)
|
| 243 |
+
|
| 244 |
+
if cache:
|
| 245 |
+
# Perform a quicker unique check
|
| 246 |
+
if not should_cache(arg):
|
| 247 |
+
return cache_array
|
| 248 |
+
|
| 249 |
+
if not isinstance(arg, (np.ndarray, ExtensionArray, Index, ABCSeries)):
|
| 250 |
+
arg = np.array(arg)
|
| 251 |
+
|
| 252 |
+
unique_dates = unique(arg)
|
| 253 |
+
if len(unique_dates) < len(arg):
|
| 254 |
+
cache_dates = convert_listlike(unique_dates, format)
|
| 255 |
+
# GH#45319
|
| 256 |
+
try:
|
| 257 |
+
cache_array = Series(cache_dates, index=unique_dates, copy=False)
|
| 258 |
+
except OutOfBoundsDatetime:
|
| 259 |
+
return cache_array
|
| 260 |
+
# GH#39882 and GH#35888 in case of None and NaT we get duplicates
|
| 261 |
+
if not cache_array.index.is_unique:
|
| 262 |
+
cache_array = cache_array[~cache_array.index.duplicated()]
|
| 263 |
+
return cache_array
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
def _box_as_indexlike(
|
| 267 |
+
dt_array: ArrayLike, utc: bool = False, name: Hashable | None = None
|
| 268 |
+
) -> Index:
|
| 269 |
+
"""
|
| 270 |
+
Properly boxes the ndarray of datetimes to DatetimeIndex
|
| 271 |
+
if it is possible or to generic Index instead
|
| 272 |
+
|
| 273 |
+
Parameters
|
| 274 |
+
----------
|
| 275 |
+
dt_array: 1-d array
|
| 276 |
+
Array of datetimes to be wrapped in an Index.
|
| 277 |
+
utc : bool
|
| 278 |
+
Whether to convert/localize timestamps to UTC.
|
| 279 |
+
name : string, default None
|
| 280 |
+
Name for a resulting index
|
| 281 |
+
|
| 282 |
+
Returns
|
| 283 |
+
-------
|
| 284 |
+
result : datetime of converted dates
|
| 285 |
+
- DatetimeIndex if convertible to sole datetime64 type
|
| 286 |
+
- general Index otherwise
|
| 287 |
+
"""
|
| 288 |
+
|
| 289 |
+
if lib.is_np_dtype(dt_array.dtype, "M"):
|
| 290 |
+
tz = "utc" if utc else None
|
| 291 |
+
return DatetimeIndex(dt_array, tz=tz, name=name)
|
| 292 |
+
return Index(dt_array, name=name, dtype=dt_array.dtype)
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
def _convert_and_box_cache(
|
| 296 |
+
arg: DatetimeScalarOrArrayConvertible,
|
| 297 |
+
cache_array: Series,
|
| 298 |
+
name: Hashable | None = None,
|
| 299 |
+
) -> Index:
|
| 300 |
+
"""
|
| 301 |
+
Convert array of dates with a cache and wrap the result in an Index.
|
| 302 |
+
|
| 303 |
+
Parameters
|
| 304 |
+
----------
|
| 305 |
+
arg : integer, float, string, datetime, list, tuple, 1-d array, Series
|
| 306 |
+
cache_array : Series
|
| 307 |
+
Cache of converted, unique dates
|
| 308 |
+
name : string, default None
|
| 309 |
+
Name for a DatetimeIndex
|
| 310 |
+
|
| 311 |
+
Returns
|
| 312 |
+
-------
|
| 313 |
+
result : Index-like of converted dates
|
| 314 |
+
"""
|
| 315 |
+
from pandas import Series
|
| 316 |
+
|
| 317 |
+
result = Series(arg, dtype=cache_array.index.dtype).map(cache_array)
|
| 318 |
+
return _box_as_indexlike(result._values, utc=False, name=name)
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
def _convert_listlike_datetimes(
|
| 322 |
+
arg,
|
| 323 |
+
format: str | None,
|
| 324 |
+
name: Hashable | None = None,
|
| 325 |
+
utc: bool = False,
|
| 326 |
+
unit: str | None = None,
|
| 327 |
+
errors: DateTimeErrorChoices = "raise",
|
| 328 |
+
dayfirst: bool | None = None,
|
| 329 |
+
yearfirst: bool | None = None,
|
| 330 |
+
exact: bool = True,
|
| 331 |
+
):
|
| 332 |
+
"""
|
| 333 |
+
Helper function for to_datetime. Performs the conversions of 1D listlike
|
| 334 |
+
of dates
|
| 335 |
+
|
| 336 |
+
Parameters
|
| 337 |
+
----------
|
| 338 |
+
arg : list, tuple, ndarray, Series, Index
|
| 339 |
+
date to be parsed
|
| 340 |
+
name : object
|
| 341 |
+
None or string for the Index name
|
| 342 |
+
utc : bool
|
| 343 |
+
Whether to convert/localize timestamps to UTC.
|
| 344 |
+
unit : str
|
| 345 |
+
None or string of the frequency of the passed data
|
| 346 |
+
errors : str
|
| 347 |
+
error handing behaviors from to_datetime, 'raise', 'coerce'
|
| 348 |
+
dayfirst : bool
|
| 349 |
+
dayfirst parsing behavior from to_datetime
|
| 350 |
+
yearfirst : bool
|
| 351 |
+
yearfirst parsing behavior from to_datetime
|
| 352 |
+
exact : bool, default True
|
| 353 |
+
exact format matching behavior from to_datetime
|
| 354 |
+
|
| 355 |
+
Returns
|
| 356 |
+
-------
|
| 357 |
+
Index-like of parsed dates
|
| 358 |
+
"""
|
| 359 |
+
if isinstance(arg, (list, tuple)):
|
| 360 |
+
arg = np.array(arg, dtype="O")
|
| 361 |
+
elif isinstance(arg, NumpyExtensionArray):
|
| 362 |
+
arg = np.array(arg)
|
| 363 |
+
|
| 364 |
+
arg_dtype = getattr(arg, "dtype", None)
|
| 365 |
+
# these are shortcutable
|
| 366 |
+
tz = "utc" if utc else None
|
| 367 |
+
if isinstance(arg_dtype, DatetimeTZDtype):
|
| 368 |
+
if not isinstance(arg, (DatetimeArray, DatetimeIndex)):
|
| 369 |
+
return DatetimeIndex(arg, tz=tz, name=name)
|
| 370 |
+
if utc:
|
| 371 |
+
arg = arg.tz_convert(None).tz_localize("utc")
|
| 372 |
+
return arg
|
| 373 |
+
|
| 374 |
+
elif isinstance(arg_dtype, ArrowDtype) and arg_dtype.type is Timestamp:
|
| 375 |
+
# TODO: Combine with above if DTI/DTA supports Arrow timestamps
|
| 376 |
+
if utc:
|
| 377 |
+
# pyarrow uses UTC, not lowercase utc
|
| 378 |
+
if isinstance(arg, Index):
|
| 379 |
+
arg_array = cast(ArrowExtensionArray, arg.array)
|
| 380 |
+
if arg_dtype.pyarrow_dtype.tz is not None:
|
| 381 |
+
arg_array = arg_array._dt_tz_convert("UTC")
|
| 382 |
+
else:
|
| 383 |
+
arg_array = arg_array._dt_tz_localize("UTC")
|
| 384 |
+
arg = Index(arg_array, copy=False)
|
| 385 |
+
# ArrowExtensionArray
|
| 386 |
+
elif arg_dtype.pyarrow_dtype.tz is not None:
|
| 387 |
+
arg = arg._dt_tz_convert("UTC")
|
| 388 |
+
else:
|
| 389 |
+
arg = arg._dt_tz_localize("UTC")
|
| 390 |
+
return arg
|
| 391 |
+
|
| 392 |
+
elif lib.is_np_dtype(arg_dtype, "M"):
|
| 393 |
+
if not is_supported_dtype(arg_dtype):
|
| 394 |
+
# We go to closest supported reso, i.e. "s"
|
| 395 |
+
arg = astype_overflowsafe(
|
| 396 |
+
np.asarray(arg),
|
| 397 |
+
np.dtype("M8[s]"),
|
| 398 |
+
is_coerce=errors == "coerce",
|
| 399 |
+
)
|
| 400 |
+
|
| 401 |
+
if not isinstance(arg, (DatetimeArray, DatetimeIndex)):
|
| 402 |
+
return DatetimeIndex(arg, tz=tz, name=name)
|
| 403 |
+
elif utc:
|
| 404 |
+
# DatetimeArray, DatetimeIndex
|
| 405 |
+
return arg.tz_localize("utc")
|
| 406 |
+
|
| 407 |
+
return arg
|
| 408 |
+
|
| 409 |
+
elif unit is not None:
|
| 410 |
+
if format is not None:
|
| 411 |
+
raise ValueError("cannot specify both format and unit")
|
| 412 |
+
return _to_datetime_with_unit(arg, unit, name, utc, errors)
|
| 413 |
+
elif getattr(arg, "ndim", 1) > 1:
|
| 414 |
+
raise TypeError(
|
| 415 |
+
"arg must be a string, datetime, list, tuple, 1-d array, or Series"
|
| 416 |
+
)
|
| 417 |
+
|
| 418 |
+
# warn if passing timedelta64, raise for PeriodDtype
|
| 419 |
+
# NB: this must come after unit transformation
|
| 420 |
+
try:
|
| 421 |
+
arg, _ = maybe_convert_dtype(arg, copy=False, tz=libtimezones.maybe_get_tz(tz))
|
| 422 |
+
except TypeError:
|
| 423 |
+
if errors == "coerce":
|
| 424 |
+
npvalues = np.full(len(arg), np.datetime64("NaT", "ns"))
|
| 425 |
+
return DatetimeIndex(npvalues, name=name)
|
| 426 |
+
raise
|
| 427 |
+
|
| 428 |
+
arg = ensure_object(arg)
|
| 429 |
+
|
| 430 |
+
if format is None:
|
| 431 |
+
format = _guess_datetime_format_for_array(arg, dayfirst=dayfirst)
|
| 432 |
+
|
| 433 |
+
# `format` could be inferred, or user didn't ask for mixed-format parsing.
|
| 434 |
+
if format is not None and format != "mixed":
|
| 435 |
+
return _array_strptime_with_fallback(arg, name, utc, format, exact, errors)
|
| 436 |
+
|
| 437 |
+
result, tz_parsed = objects_to_datetime64(
|
| 438 |
+
arg,
|
| 439 |
+
dayfirst=dayfirst,
|
| 440 |
+
yearfirst=yearfirst,
|
| 441 |
+
utc=utc,
|
| 442 |
+
errors=errors,
|
| 443 |
+
allow_object=True,
|
| 444 |
+
)
|
| 445 |
+
|
| 446 |
+
if tz_parsed is not None:
|
| 447 |
+
# We can take a shortcut since the datetime64 numpy array
|
| 448 |
+
# is in UTC
|
| 449 |
+
out_unit = np.datetime_data(result.dtype)[0]
|
| 450 |
+
out_unit = cast("TimeUnit", out_unit)
|
| 451 |
+
dtype = tz_to_dtype(tz_parsed, out_unit)
|
| 452 |
+
dt64_values = result.view(f"M8[{dtype.unit}]")
|
| 453 |
+
dta = DatetimeArray._simple_new(dt64_values, dtype=dtype)
|
| 454 |
+
return DatetimeIndex._simple_new(dta, name=name)
|
| 455 |
+
|
| 456 |
+
return _box_as_indexlike(result, utc=utc, name=name)
|
| 457 |
+
|
| 458 |
+
|
| 459 |
+
def _array_strptime_with_fallback(
|
| 460 |
+
arg,
|
| 461 |
+
name,
|
| 462 |
+
utc: bool,
|
| 463 |
+
fmt: str,
|
| 464 |
+
exact: bool,
|
| 465 |
+
errors: str,
|
| 466 |
+
) -> Index:
|
| 467 |
+
"""
|
| 468 |
+
Call array_strptime, with fallback behavior depending on 'errors'.
|
| 469 |
+
"""
|
| 470 |
+
result, tz_out = array_strptime(arg, fmt, exact=exact, errors=errors, utc=utc)
|
| 471 |
+
if tz_out is not None:
|
| 472 |
+
unit = np.datetime_data(result.dtype)[0]
|
| 473 |
+
unit = cast("TimeUnit", unit)
|
| 474 |
+
dtype = DatetimeTZDtype(tz=tz_out, unit=unit)
|
| 475 |
+
dta = DatetimeArray._simple_new(result, dtype=dtype)
|
| 476 |
+
if utc:
|
| 477 |
+
dta = dta.tz_convert("UTC")
|
| 478 |
+
return Index(dta, name=name, copy=False)
|
| 479 |
+
elif result.dtype != object and utc:
|
| 480 |
+
unit = np.datetime_data(result.dtype)[0]
|
| 481 |
+
unit = cast("TimeUnit", unit)
|
| 482 |
+
res = Index(result, dtype=f"M8[{unit}, UTC]", name=name, copy=False)
|
| 483 |
+
return res
|
| 484 |
+
return Index(result, dtype=result.dtype, name=name, copy=False)
|
| 485 |
+
|
| 486 |
+
|
| 487 |
+
def _to_datetime_with_unit(arg, unit, name, utc: bool, errors: str) -> Index:
|
| 488 |
+
"""
|
| 489 |
+
to_datetime specalized to the case where a 'unit' is passed.
|
| 490 |
+
"""
|
| 491 |
+
arg = extract_array(arg, extract_numpy=True)
|
| 492 |
+
|
| 493 |
+
# GH#30050 pass an ndarray to tslib.array_to_datetime
|
| 494 |
+
# because it expects an ndarray argument
|
| 495 |
+
if isinstance(arg, IntegerArray):
|
| 496 |
+
arr = arg.astype(f"datetime64[{unit}]")
|
| 497 |
+
tz_parsed = None
|
| 498 |
+
else:
|
| 499 |
+
arg = np.asarray(arg)
|
| 500 |
+
|
| 501 |
+
if arg.dtype.kind in "iu":
|
| 502 |
+
# Note we can't do "f" here because that could induce unwanted
|
| 503 |
+
# rounding GH#14156, GH#20445
|
| 504 |
+
arr = arg.astype(f"datetime64[{unit}]", copy=False)
|
| 505 |
+
dtype = get_supported_dtype(arr.dtype)
|
| 506 |
+
try:
|
| 507 |
+
arr = astype_overflowsafe(arr, dtype, copy=False)
|
| 508 |
+
except OutOfBoundsDatetime:
|
| 509 |
+
if errors == "raise":
|
| 510 |
+
raise
|
| 511 |
+
arg = arg.astype(object)
|
| 512 |
+
return _to_datetime_with_unit(arg, unit, name, utc, errors)
|
| 513 |
+
tz_parsed = None
|
| 514 |
+
|
| 515 |
+
elif arg.dtype.kind == "f":
|
| 516 |
+
with np.errstate(invalid="ignore"):
|
| 517 |
+
int_values = arg.astype(np.int64)
|
| 518 |
+
mask = np.isnan(arg)
|
| 519 |
+
if (mask | (arg == int_values)).all():
|
| 520 |
+
# With all-round-or-NaN entries, we give the requested unit
|
| 521 |
+
# back like with integers
|
| 522 |
+
result = _to_datetime_with_unit(
|
| 523 |
+
int_values, unit=unit, name=name, utc=utc, errors=errors
|
| 524 |
+
)
|
| 525 |
+
result._data[mask] = NaT
|
| 526 |
+
return result
|
| 527 |
+
with np.errstate(over="raise"):
|
| 528 |
+
try:
|
| 529 |
+
arr = cast_from_unit_vectorized(arg, unit=unit)
|
| 530 |
+
except OutOfBoundsDatetime as err:
|
| 531 |
+
if errors != "raise":
|
| 532 |
+
return _to_datetime_with_unit(
|
| 533 |
+
arg.astype(object), unit, name, utc, errors
|
| 534 |
+
)
|
| 535 |
+
raise OutOfBoundsDatetime(
|
| 536 |
+
f"cannot convert input with unit '{unit}'"
|
| 537 |
+
) from err
|
| 538 |
+
|
| 539 |
+
arr = arr.view("M8[ns]")
|
| 540 |
+
tz_parsed = None
|
| 541 |
+
else:
|
| 542 |
+
arg = arg.astype(object, copy=False)
|
| 543 |
+
arr, tz_parsed = tslib.array_to_datetime(
|
| 544 |
+
arg,
|
| 545 |
+
utc=utc,
|
| 546 |
+
errors=errors,
|
| 547 |
+
unit_for_numerics=unit,
|
| 548 |
+
)
|
| 549 |
+
|
| 550 |
+
result = DatetimeIndex(arr, name=name)
|
| 551 |
+
if not isinstance(result, DatetimeIndex):
|
| 552 |
+
return result
|
| 553 |
+
|
| 554 |
+
# GH#23758: We may still need to localize the result with tz
|
| 555 |
+
# GH#25546: Apply tz_parsed first (from arg), then tz (from caller)
|
| 556 |
+
# result will be naive but in UTC
|
| 557 |
+
result = result.tz_localize("UTC").tz_convert(tz_parsed)
|
| 558 |
+
|
| 559 |
+
if utc:
|
| 560 |
+
if result.tz is None:
|
| 561 |
+
result = result.tz_localize("utc")
|
| 562 |
+
else:
|
| 563 |
+
result = result.tz_convert("utc")
|
| 564 |
+
return result
|
| 565 |
+
|
| 566 |
+
|
| 567 |
+
def _adjust_to_origin(arg, origin, unit):
|
| 568 |
+
"""
|
| 569 |
+
Helper function for to_datetime.
|
| 570 |
+
Adjust input argument to the specified origin
|
| 571 |
+
|
| 572 |
+
Parameters
|
| 573 |
+
----------
|
| 574 |
+
arg : list, tuple, ndarray, Series, Index
|
| 575 |
+
date to be adjusted
|
| 576 |
+
origin : 'julian' or Timestamp
|
| 577 |
+
origin offset for the arg
|
| 578 |
+
unit : str
|
| 579 |
+
passed unit from to_datetime, must be 'D'
|
| 580 |
+
|
| 581 |
+
Returns
|
| 582 |
+
-------
|
| 583 |
+
ndarray or scalar of adjusted date(s)
|
| 584 |
+
"""
|
| 585 |
+
if origin == "julian":
|
| 586 |
+
original = arg
|
| 587 |
+
j0 = Timestamp(0).to_julian_date()
|
| 588 |
+
if unit != "D":
|
| 589 |
+
raise ValueError("unit must be 'D' for origin='julian'")
|
| 590 |
+
try:
|
| 591 |
+
arg = arg - j0
|
| 592 |
+
except TypeError as err:
|
| 593 |
+
raise ValueError(
|
| 594 |
+
"incompatible 'arg' type for given 'origin'='julian'"
|
| 595 |
+
) from err
|
| 596 |
+
|
| 597 |
+
# preemptively check this for a nice range
|
| 598 |
+
j_max = Timestamp.max.to_julian_date() - j0
|
| 599 |
+
j_min = Timestamp.min.to_julian_date() - j0
|
| 600 |
+
if np.any(arg > j_max) or np.any(arg < j_min):
|
| 601 |
+
raise OutOfBoundsDatetime(
|
| 602 |
+
f"{original} is Out of Bounds for origin='julian'"
|
| 603 |
+
)
|
| 604 |
+
else:
|
| 605 |
+
# arg must be numeric
|
| 606 |
+
if not (
|
| 607 |
+
(is_integer(arg) or is_float(arg)) or is_numeric_dtype(np.asarray(arg))
|
| 608 |
+
):
|
| 609 |
+
raise ValueError(
|
| 610 |
+
f"'{arg}' is not compatible with origin='{origin}'; "
|
| 611 |
+
"it must be numeric with a unit specified"
|
| 612 |
+
)
|
| 613 |
+
|
| 614 |
+
# we are going to offset back to unix / epoch time
|
| 615 |
+
try:
|
| 616 |
+
if lib.is_integer(origin) or lib.is_float(origin):
|
| 617 |
+
offset = Timestamp(origin, unit=unit)
|
| 618 |
+
else:
|
| 619 |
+
offset = Timestamp(origin)
|
| 620 |
+
except OutOfBoundsDatetime as err:
|
| 621 |
+
raise OutOfBoundsDatetime(f"origin {origin} is Out of Bounds") from err
|
| 622 |
+
except ValueError as err:
|
| 623 |
+
raise ValueError(
|
| 624 |
+
f"origin {origin} cannot be converted to a Timestamp"
|
| 625 |
+
) from err
|
| 626 |
+
|
| 627 |
+
if offset.tz is not None:
|
| 628 |
+
raise ValueError(f"origin offset {offset} must be tz-naive")
|
| 629 |
+
td_offset = offset - Timestamp(0)
|
| 630 |
+
|
| 631 |
+
# convert the offset to the unit of the arg
|
| 632 |
+
# this should be lossless in terms of precision
|
| 633 |
+
ioffset = td_offset // Timedelta(1, unit=unit)
|
| 634 |
+
|
| 635 |
+
# scalars & ndarray-like can handle the addition
|
| 636 |
+
if is_list_like(arg) and not isinstance(arg, (ABCSeries, Index, np.ndarray)):
|
| 637 |
+
arg = np.asarray(arg)
|
| 638 |
+
arg = arg + ioffset
|
| 639 |
+
return arg
|
| 640 |
+
|
| 641 |
+
|
| 642 |
+
@overload
|
| 643 |
+
def to_datetime(
|
| 644 |
+
arg: DatetimeScalar,
|
| 645 |
+
errors: DateTimeErrorChoices = ...,
|
| 646 |
+
dayfirst: bool = ...,
|
| 647 |
+
yearfirst: bool = ...,
|
| 648 |
+
utc: bool = ...,
|
| 649 |
+
format: str | None = ...,
|
| 650 |
+
exact: bool = ...,
|
| 651 |
+
unit: str | None = ...,
|
| 652 |
+
origin=...,
|
| 653 |
+
cache: bool = ...,
|
| 654 |
+
) -> Timestamp: ...
|
| 655 |
+
|
| 656 |
+
|
| 657 |
+
@overload
|
| 658 |
+
def to_datetime(
|
| 659 |
+
arg: Series | DictConvertible,
|
| 660 |
+
errors: DateTimeErrorChoices = ...,
|
| 661 |
+
dayfirst: bool = ...,
|
| 662 |
+
yearfirst: bool = ...,
|
| 663 |
+
utc: bool = ...,
|
| 664 |
+
format: str | None = ...,
|
| 665 |
+
exact: bool = ...,
|
| 666 |
+
unit: str | None = ...,
|
| 667 |
+
origin=...,
|
| 668 |
+
cache: bool = ...,
|
| 669 |
+
) -> Series: ...
|
| 670 |
+
|
| 671 |
+
|
| 672 |
+
@overload
|
| 673 |
+
def to_datetime(
|
| 674 |
+
arg: list | tuple | Index | ArrayLike,
|
| 675 |
+
errors: DateTimeErrorChoices = ...,
|
| 676 |
+
dayfirst: bool = ...,
|
| 677 |
+
yearfirst: bool = ...,
|
| 678 |
+
utc: bool = ...,
|
| 679 |
+
format: str | None = ...,
|
| 680 |
+
exact: bool = ...,
|
| 681 |
+
unit: str | None = ...,
|
| 682 |
+
origin=...,
|
| 683 |
+
cache: bool = ...,
|
| 684 |
+
) -> DatetimeIndex: ...
|
| 685 |
+
|
| 686 |
+
|
| 687 |
+
@set_module("pandas")
|
| 688 |
+
def to_datetime(
|
| 689 |
+
arg: DatetimeScalarOrArrayConvertible | DictConvertible,
|
| 690 |
+
errors: DateTimeErrorChoices = "raise",
|
| 691 |
+
dayfirst: bool = False,
|
| 692 |
+
yearfirst: bool = False,
|
| 693 |
+
utc: bool = False,
|
| 694 |
+
format: str | None = None,
|
| 695 |
+
exact: bool | lib.NoDefault = lib.no_default,
|
| 696 |
+
unit: str | None = None,
|
| 697 |
+
origin: str = "unix",
|
| 698 |
+
cache: bool = True,
|
| 699 |
+
) -> DatetimeIndex | Series | DatetimeScalar | NaTType:
|
| 700 |
+
"""
|
| 701 |
+
Convert argument to datetime.
|
| 702 |
+
|
| 703 |
+
This function converts a scalar, array-like, :class:`Series` or
|
| 704 |
+
:class:`DataFrame`/dict-like to a pandas datetime object.
|
| 705 |
+
|
| 706 |
+
Parameters
|
| 707 |
+
----------
|
| 708 |
+
arg : int, float, str, datetime, list, tuple, 1-d array, Series, DataFrame/dict-like
|
| 709 |
+
The object to convert to a datetime. If a :class:`DataFrame` is provided, the
|
| 710 |
+
method expects minimally the following columns: :const:`"year"`,
|
| 711 |
+
:const:`"month"`, :const:`"day"`. The column "year"
|
| 712 |
+
must be specified in 4-digit format.
|
| 713 |
+
errors : {'raise', 'coerce'}, default 'raise'
|
| 714 |
+
- If :const:`'raise'`, then invalid parsing will raise an exception.
|
| 715 |
+
- If :const:`'coerce'`, then invalid parsing will be set as :const:`NaT`.
|
| 716 |
+
dayfirst : bool, default False
|
| 717 |
+
Specify a date parse order if `arg` is str or is list-like.
|
| 718 |
+
If :const:`True`, parses dates with the day first, e.g. :const:`"10/11/12"`
|
| 719 |
+
is parsed as :const:`2012-11-10`.
|
| 720 |
+
|
| 721 |
+
.. warning::
|
| 722 |
+
|
| 723 |
+
``dayfirst=True`` is not strict, but will prefer to parse
|
| 724 |
+
with day first.
|
| 725 |
+
|
| 726 |
+
yearfirst : bool, default False
|
| 727 |
+
Specify a date parse order if `arg` is str or is list-like.
|
| 728 |
+
|
| 729 |
+
- If :const:`True` parses dates with the year first, e.g.
|
| 730 |
+
:const:`"10/11/12"` is parsed as :const:`2010-11-12`.
|
| 731 |
+
- If both `dayfirst` and `yearfirst` are :const:`True`, `yearfirst` is
|
| 732 |
+
preceded (same as :mod:`dateutil`).
|
| 733 |
+
|
| 734 |
+
.. warning::
|
| 735 |
+
|
| 736 |
+
``yearfirst=True`` is not strict, but will prefer to parse
|
| 737 |
+
with year first.
|
| 738 |
+
|
| 739 |
+
utc : bool, default False
|
| 740 |
+
Control timezone-related parsing, localization and conversion.
|
| 741 |
+
|
| 742 |
+
- If :const:`True`, the function *always* returns a timezone-aware
|
| 743 |
+
UTC-localized :class:`Timestamp`, :class:`Series` or
|
| 744 |
+
:class:`DatetimeIndex`. To do this, timezone-naive inputs are
|
| 745 |
+
*localized* as UTC, while timezone-aware inputs are *converted* to UTC.
|
| 746 |
+
|
| 747 |
+
- If :const:`False` (default), inputs will not be coerced to UTC.
|
| 748 |
+
Timezone-naive inputs will remain naive, while timezone-aware ones
|
| 749 |
+
will keep their time offsets. Limitations exist for mixed
|
| 750 |
+
offsets (typically, daylight savings), see :ref:`Examples
|
| 751 |
+
<to_datetime_tz_examples>` section for details.
|
| 752 |
+
|
| 753 |
+
See also: pandas general documentation about `timezone conversion and
|
| 754 |
+
localization
|
| 755 |
+
<https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html
|
| 756 |
+
#time-zone-handling>`_.
|
| 757 |
+
|
| 758 |
+
format : str, default None
|
| 759 |
+
The strftime to parse time, e.g. :const:`"%d/%m/%Y"`. See
|
| 760 |
+
`strftime documentation
|
| 761 |
+
<https://docs.python.org/3/library/datetime.html
|
| 762 |
+
#strftime-and-strptime-behavior>`_ for more information on choices, though
|
| 763 |
+
note that :const:`"%f"` will parse all the way up to nanoseconds.
|
| 764 |
+
You can also pass:
|
| 765 |
+
|
| 766 |
+
- "ISO8601", to parse any `ISO8601 <https://en.wikipedia.org/wiki/ISO_8601>`_
|
| 767 |
+
time string (not necessarily in exactly the same format);
|
| 768 |
+
- "mixed", to infer the format for each element individually. This is risky,
|
| 769 |
+
and you should probably use it along with `dayfirst`.
|
| 770 |
+
|
| 771 |
+
.. note::
|
| 772 |
+
|
| 773 |
+
If a :class:`DataFrame` is passed, then `format` has no effect.
|
| 774 |
+
|
| 775 |
+
exact : bool, default True
|
| 776 |
+
Control how `format` is used:
|
| 777 |
+
|
| 778 |
+
- If :const:`True`, require an exact `format` match.
|
| 779 |
+
- If :const:`False`, allow the `format` to match anywhere in the target
|
| 780 |
+
string.
|
| 781 |
+
|
| 782 |
+
Cannot be used alongside ``format='ISO8601'`` or ``format='mixed'``.
|
| 783 |
+
unit : str, default 'ns'
|
| 784 |
+
The unit of the arg (D,s,ms,us,ns) denote the unit, which is an
|
| 785 |
+
integer or float number. This will be based off the origin.
|
| 786 |
+
Example, with ``unit='ms'`` and ``origin='unix'``, this would calculate
|
| 787 |
+
the number of milliseconds to the unix epoch start.
|
| 788 |
+
origin : scalar, default 'unix'
|
| 789 |
+
Define the reference date. The numeric values would be parsed as number
|
| 790 |
+
of units (defined by `unit`) since this reference date.
|
| 791 |
+
|
| 792 |
+
- If :const:`'unix'` (or POSIX) time; origin is set to 1970-01-01.
|
| 793 |
+
- If :const:`'julian'`, unit must be :const:`'D'`, and origin is set to
|
| 794 |
+
beginning of Julian Calendar. Julian day number :const:`0` is assigned
|
| 795 |
+
to the day starting at noon on January 1, 4713 BC.
|
| 796 |
+
- If Timestamp convertible (Timestamp, dt.datetime, np.datetimt64 or date
|
| 797 |
+
string), origin is set to Timestamp identified by origin.
|
| 798 |
+
- If a float or integer, origin is the difference
|
| 799 |
+
(in units determined by the ``unit`` argument) relative to 1970-01-01.
|
| 800 |
+
cache : bool, default True
|
| 801 |
+
If :const:`True`, use a cache of unique, converted dates to apply the
|
| 802 |
+
datetime conversion. May produce significant speed-up when parsing
|
| 803 |
+
duplicate date strings, especially ones with timezone offsets. The cache
|
| 804 |
+
is only used when there are at least 50 values. The presence of
|
| 805 |
+
out-of-bounds values will render the cache unusable and may slow down
|
| 806 |
+
parsing.
|
| 807 |
+
|
| 808 |
+
Returns
|
| 809 |
+
-------
|
| 810 |
+
datetime
|
| 811 |
+
If parsing succeeded.
|
| 812 |
+
Return type depends on input (types in parenthesis correspond to
|
| 813 |
+
fallback in case of unsuccessful timezone or out-of-range timestamp
|
| 814 |
+
parsing):
|
| 815 |
+
|
| 816 |
+
- scalar: :class:`Timestamp` (or :class:`datetime.datetime`)
|
| 817 |
+
- array-like: :class:`DatetimeIndex` (or :class:`Series` with
|
| 818 |
+
:class:`object` dtype containing :class:`datetime.datetime`)
|
| 819 |
+
- Series: :class:`Series` of :class:`datetime64` dtype (or
|
| 820 |
+
:class:`Series` of :class:`object` dtype containing
|
| 821 |
+
:class:`datetime.datetime`)
|
| 822 |
+
- DataFrame: :class:`Series` of :class:`datetime64` dtype (or
|
| 823 |
+
:class:`Series` of :class:`object` dtype containing
|
| 824 |
+
:class:`datetime.datetime`)
|
| 825 |
+
|
| 826 |
+
Raises
|
| 827 |
+
------
|
| 828 |
+
ParserError
|
| 829 |
+
When parsing a date from string fails.
|
| 830 |
+
ValueError
|
| 831 |
+
When another datetime conversion error happens. For example when one
|
| 832 |
+
of 'year', 'month', day' columns is missing in a :class:`DataFrame`, or
|
| 833 |
+
when a Timezone-aware :class:`datetime.datetime` is found in an array-like
|
| 834 |
+
of mixed time offsets, and ``utc=False``, or when parsing datetimes
|
| 835 |
+
with mixed time zones unless ``utc=True``. If parsing datetimes with mixed
|
| 836 |
+
time zones, please specify ``utc=True``.
|
| 837 |
+
|
| 838 |
+
See Also
|
| 839 |
+
--------
|
| 840 |
+
DataFrame.astype : Cast argument to a specified dtype.
|
| 841 |
+
to_timedelta : Convert argument to timedelta.
|
| 842 |
+
convert_dtypes : Convert dtypes.
|
| 843 |
+
|
| 844 |
+
Notes
|
| 845 |
+
-----
|
| 846 |
+
|
| 847 |
+
Many input types are supported, and lead to different output types:
|
| 848 |
+
|
| 849 |
+
- **scalars** can be int, float, str, datetime object (from stdlib :mod:`datetime`
|
| 850 |
+
module or :mod:`numpy`). They are converted to :class:`Timestamp` when
|
| 851 |
+
possible, otherwise they are converted to :class:`datetime.datetime`.
|
| 852 |
+
None/NaN/null scalars are converted to :const:`NaT`.
|
| 853 |
+
|
| 854 |
+
- **array-like** can contain int, float, str, datetime objects. They are
|
| 855 |
+
converted to :class:`DatetimeIndex` when possible, otherwise they are
|
| 856 |
+
converted to :class:`Index` with :class:`object` dtype, containing
|
| 857 |
+
:class:`datetime.datetime`. None/NaN/null entries are converted to
|
| 858 |
+
:const:`NaT` in both cases.
|
| 859 |
+
|
| 860 |
+
- **Series** are converted to :class:`Series` with :class:`datetime64`
|
| 861 |
+
dtype when possible, otherwise they are converted to :class:`Series` with
|
| 862 |
+
:class:`object` dtype, containing :class:`datetime.datetime`. None/NaN/null
|
| 863 |
+
entries are converted to :const:`NaT` in both cases.
|
| 864 |
+
|
| 865 |
+
- **DataFrame/dict-like** are converted to :class:`Series` with
|
| 866 |
+
:class:`datetime64` dtype. For each row a datetime is created from assembling
|
| 867 |
+
the various dataframe columns. Column keys can be common abbreviations
|
| 868 |
+
like ['year', 'month', 'day', 'minute', 'second', 'ms', 'us', 'ns']) or
|
| 869 |
+
plurals of the same.
|
| 870 |
+
|
| 871 |
+
The following causes are responsible for :class:`datetime.datetime` objects
|
| 872 |
+
being returned (possibly inside an :class:`Index` or a :class:`Series` with
|
| 873 |
+
:class:`object` dtype) instead of a proper pandas designated type
|
| 874 |
+
(:class:`Timestamp`, :class:`DatetimeIndex` or :class:`Series`
|
| 875 |
+
with :class:`datetime64` dtype):
|
| 876 |
+
|
| 877 |
+
- when any input element is before :const:`Timestamp.min` or after
|
| 878 |
+
:const:`Timestamp.max`, see `timestamp limitations
|
| 879 |
+
<https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html
|
| 880 |
+
#timeseries-timestamp-limits>`_.
|
| 881 |
+
|
| 882 |
+
- when ``utc=False`` (default) and the input is an array-like or
|
| 883 |
+
:class:`Series` containing mixed naive/aware datetime, or aware with mixed
|
| 884 |
+
time offsets. Note that this happens in the (quite frequent) situation when
|
| 885 |
+
the timezone has a daylight savings policy. In that case you may wish to
|
| 886 |
+
use ``utc=True``.
|
| 887 |
+
|
| 888 |
+
Examples
|
| 889 |
+
--------
|
| 890 |
+
|
| 891 |
+
**Handling various input formats**
|
| 892 |
+
|
| 893 |
+
Assembling a datetime from multiple columns of a :class:`DataFrame`. The keys
|
| 894 |
+
can be common abbreviations like ['year', 'month', 'day', 'minute', 'second',
|
| 895 |
+
'ms', 'us', 'ns']) or plurals of the same
|
| 896 |
+
|
| 897 |
+
>>> df = pd.DataFrame({"year": [2015, 2016], "month": [2, 3], "day": [4, 5]})
|
| 898 |
+
>>> pd.to_datetime(df)
|
| 899 |
+
0 2015-02-04
|
| 900 |
+
1 2016-03-05
|
| 901 |
+
dtype: datetime64[us]
|
| 902 |
+
|
| 903 |
+
Using a unix epoch time
|
| 904 |
+
|
| 905 |
+
>>> pd.to_datetime(1490195805, unit="s")
|
| 906 |
+
Timestamp('2017-03-22 15:16:45')
|
| 907 |
+
>>> pd.to_datetime(1490195805433502912, unit="ns")
|
| 908 |
+
Timestamp('2017-03-22 15:16:45.433502912')
|
| 909 |
+
|
| 910 |
+
.. warning:: For float arg, precision rounding might happen. To prevent
|
| 911 |
+
unexpected behavior use a fixed-width exact type.
|
| 912 |
+
|
| 913 |
+
Using a non-unix epoch origin
|
| 914 |
+
|
| 915 |
+
>>> pd.to_datetime([1, 2, 3], unit="D", origin=pd.Timestamp("1960-01-01"))
|
| 916 |
+
DatetimeIndex(['1960-01-02', '1960-01-03', '1960-01-04'],
|
| 917 |
+
dtype='datetime64[s]', freq=None)
|
| 918 |
+
|
| 919 |
+
**Differences with strptime behavior**
|
| 920 |
+
|
| 921 |
+
:const:`"%f"` will parse all the way up to nanoseconds.
|
| 922 |
+
|
| 923 |
+
>>> pd.to_datetime("2018-10-26 12:00:00.0000000011", format="%Y-%m-%d %H:%M:%S.%f")
|
| 924 |
+
Timestamp('2018-10-26 12:00:00.000000001')
|
| 925 |
+
|
| 926 |
+
**Non-convertible date/times**
|
| 927 |
+
|
| 928 |
+
Passing ``errors='coerce'`` will force an out-of-bounds date to :const:`NaT`,
|
| 929 |
+
in addition to forcing non-dates (or non-parseable dates) to :const:`NaT`.
|
| 930 |
+
|
| 931 |
+
>>> pd.to_datetime("invalid for Ymd", format="%Y%m%d", errors="coerce")
|
| 932 |
+
NaT
|
| 933 |
+
|
| 934 |
+
.. _to_datetime_tz_examples:
|
| 935 |
+
|
| 936 |
+
**Timezones and time offsets**
|
| 937 |
+
|
| 938 |
+
The default behaviour (``utc=False``) is as follows:
|
| 939 |
+
|
| 940 |
+
- Timezone-naive inputs are converted to timezone-naive :class:`DatetimeIndex`:
|
| 941 |
+
|
| 942 |
+
>>> pd.to_datetime(["2018-10-26 12:00:00", "2018-10-26 13:00:15"])
|
| 943 |
+
DatetimeIndex(['2018-10-26 12:00:00', '2018-10-26 13:00:15'],
|
| 944 |
+
dtype='datetime64[us]', freq=None)
|
| 945 |
+
|
| 946 |
+
- Timezone-aware inputs *with constant time offset* are converted to
|
| 947 |
+
timezone-aware :class:`DatetimeIndex`:
|
| 948 |
+
|
| 949 |
+
>>> pd.to_datetime(["2018-10-26 12:00 -0500", "2018-10-26 13:00 -0500"])
|
| 950 |
+
DatetimeIndex(['2018-10-26 12:00:00-05:00', '2018-10-26 13:00:00-05:00'],
|
| 951 |
+
dtype='datetime64[us, UTC-05:00]', freq=None)
|
| 952 |
+
|
| 953 |
+
- However, timezone-aware inputs *with mixed time offsets* (for example
|
| 954 |
+
issued from a timezone with daylight savings, such as Europe/Paris)
|
| 955 |
+
are **not successfully converted** to a :class:`DatetimeIndex`.
|
| 956 |
+
Parsing datetimes with mixed time zones will raise a ValueError unless
|
| 957 |
+
``utc=True``:
|
| 958 |
+
|
| 959 |
+
>>> pd.to_datetime(
|
| 960 |
+
... ["2020-10-25 02:00 +0200", "2020-10-25 04:00 +0100"]
|
| 961 |
+
... ) # doctest: +SKIP
|
| 962 |
+
ValueError: Mixed timezones detected. Pass utc=True in to_datetime
|
| 963 |
+
or tz='UTC' in DatetimeIndex to convert to a common timezone.
|
| 964 |
+
|
| 965 |
+
- To create a :class:`Series` with mixed offsets and ``object`` dtype, please use
|
| 966 |
+
:meth:`Series.apply` and :func:`datetime.datetime.strptime`:
|
| 967 |
+
|
| 968 |
+
>>> import datetime as dt
|
| 969 |
+
>>> ser = pd.Series(["2020-10-25 02:00 +0200", "2020-10-25 04:00 +0100"])
|
| 970 |
+
>>> ser.apply(lambda x: dt.datetime.strptime(x, "%Y-%m-%d %H:%M %z"))
|
| 971 |
+
0 2020-10-25 02:00:00+02:00
|
| 972 |
+
1 2020-10-25 04:00:00+01:00
|
| 973 |
+
dtype: object
|
| 974 |
+
|
| 975 |
+
- A mix of timezone-aware and timezone-naive inputs will also raise a ValueError
|
| 976 |
+
unless ``utc=True``:
|
| 977 |
+
|
| 978 |
+
>>> from datetime import datetime
|
| 979 |
+
>>> pd.to_datetime(
|
| 980 |
+
... ["2020-01-01 01:00:00-01:00", datetime(2020, 1, 1, 3, 0)]
|
| 981 |
+
... ) # doctest: +SKIP
|
| 982 |
+
ValueError: Mixed timezones detected. Pass utc=True in to_datetime
|
| 983 |
+
or tz='UTC' in DatetimeIndex to convert to a common timezone.
|
| 984 |
+
|
| 985 |
+
|
|
| 986 |
+
|
| 987 |
+
Setting ``utc=True`` solves most of the above issues:
|
| 988 |
+
|
| 989 |
+
- Timezone-naive inputs are *localized* as UTC
|
| 990 |
+
|
| 991 |
+
>>> pd.to_datetime(["2018-10-26 12:00", "2018-10-26 13:00"], utc=True)
|
| 992 |
+
DatetimeIndex(['2018-10-26 12:00:00+00:00', '2018-10-26 13:00:00+00:00'],
|
| 993 |
+
dtype='datetime64[us, UTC]', freq=None)
|
| 994 |
+
|
| 995 |
+
- Timezone-aware inputs are *converted* to UTC (the output represents the
|
| 996 |
+
exact same datetime, but viewed from the UTC time offset `+00:00`).
|
| 997 |
+
|
| 998 |
+
>>> pd.to_datetime(["2018-10-26 12:00 -0530", "2018-10-26 12:00 -0500"], utc=True)
|
| 999 |
+
DatetimeIndex(['2018-10-26 17:30:00+00:00', '2018-10-26 17:00:00+00:00'],
|
| 1000 |
+
dtype='datetime64[us, UTC]', freq=None)
|
| 1001 |
+
|
| 1002 |
+
- Inputs can contain both string or datetime, the above
|
| 1003 |
+
rules still apply
|
| 1004 |
+
|
| 1005 |
+
>>> pd.to_datetime(["2018-10-26 12:00", datetime(2020, 1, 1, 18)], utc=True)
|
| 1006 |
+
DatetimeIndex(['2018-10-26 12:00:00+00:00', '2020-01-01 18:00:00+00:00'],
|
| 1007 |
+
dtype='datetime64[us, UTC]', freq=None)
|
| 1008 |
+
"""
|
| 1009 |
+
if exact is not lib.no_default and format in {"mixed", "ISO8601"}:
|
| 1010 |
+
raise ValueError("Cannot use 'exact' when 'format' is 'mixed' or 'ISO8601'")
|
| 1011 |
+
if arg is None:
|
| 1012 |
+
return NaT
|
| 1013 |
+
|
| 1014 |
+
if origin != "unix":
|
| 1015 |
+
arg = _adjust_to_origin(arg, origin, unit)
|
| 1016 |
+
|
| 1017 |
+
convert_listlike = partial(
|
| 1018 |
+
_convert_listlike_datetimes,
|
| 1019 |
+
utc=utc,
|
| 1020 |
+
unit=unit,
|
| 1021 |
+
dayfirst=dayfirst,
|
| 1022 |
+
yearfirst=yearfirst,
|
| 1023 |
+
errors=errors,
|
| 1024 |
+
exact=exact, # type: ignore[arg-type]
|
| 1025 |
+
)
|
| 1026 |
+
result: Timestamp | NaTType | Series | Index
|
| 1027 |
+
|
| 1028 |
+
if isinstance(arg, Timestamp):
|
| 1029 |
+
result = arg
|
| 1030 |
+
if utc:
|
| 1031 |
+
if arg.tz is not None:
|
| 1032 |
+
result = arg.tz_convert("utc")
|
| 1033 |
+
else:
|
| 1034 |
+
result = arg.tz_localize("utc")
|
| 1035 |
+
elif isinstance(arg, ABCSeries):
|
| 1036 |
+
cache_array = _maybe_cache(arg, format, cache, convert_listlike)
|
| 1037 |
+
if not cache_array.empty:
|
| 1038 |
+
result = arg.map(cache_array)
|
| 1039 |
+
else:
|
| 1040 |
+
values = convert_listlike(arg._values, format)
|
| 1041 |
+
result = arg._constructor(values, index=arg.index, name=arg.name)
|
| 1042 |
+
elif isinstance(arg, (ABCDataFrame, abc.MutableMapping)):
|
| 1043 |
+
result = _assemble_from_unit_mappings(arg, errors, utc)
|
| 1044 |
+
elif isinstance(arg, Index):
|
| 1045 |
+
cache_array = _maybe_cache(arg, format, cache, convert_listlike)
|
| 1046 |
+
if not cache_array.empty:
|
| 1047 |
+
result = _convert_and_box_cache(arg, cache_array, name=arg.name)
|
| 1048 |
+
else:
|
| 1049 |
+
result = convert_listlike(arg, format, name=arg.name)
|
| 1050 |
+
elif is_list_like(arg):
|
| 1051 |
+
try:
|
| 1052 |
+
# error: Argument 1 to "_maybe_cache" has incompatible type
|
| 1053 |
+
# "Union[float, str, datetime, List[Any], Tuple[Any, ...], ExtensionArray,
|
| 1054 |
+
# ndarray[Any, Any], Series]"; expected "Union[List[Any], Tuple[Any, ...],
|
| 1055 |
+
# Union[Union[ExtensionArray, ndarray[Any, Any]], Index, Series], Series]"
|
| 1056 |
+
argc = cast(
|
| 1057 |
+
Union[list, tuple, ExtensionArray, np.ndarray, "Series", Index], arg
|
| 1058 |
+
)
|
| 1059 |
+
cache_array = _maybe_cache(argc, format, cache, convert_listlike)
|
| 1060 |
+
except OutOfBoundsDatetime:
|
| 1061 |
+
# caching attempts to create a DatetimeIndex, which may raise
|
| 1062 |
+
# an OOB. If that's the desired behavior, then just reraise...
|
| 1063 |
+
if errors == "raise":
|
| 1064 |
+
raise
|
| 1065 |
+
# ... otherwise, continue without the cache.
|
| 1066 |
+
from pandas import Series
|
| 1067 |
+
|
| 1068 |
+
cache_array = Series([], dtype=object) # just an empty array
|
| 1069 |
+
if not cache_array.empty:
|
| 1070 |
+
result = _convert_and_box_cache(argc, cache_array)
|
| 1071 |
+
else:
|
| 1072 |
+
result = convert_listlike(argc, format)
|
| 1073 |
+
else:
|
| 1074 |
+
result = convert_listlike(np.array([arg]), format)[0]
|
| 1075 |
+
if isinstance(arg, bool) and isinstance(result, np.bool_):
|
| 1076 |
+
result = bool(result) # TODO: avoid this kludge.
|
| 1077 |
+
|
| 1078 |
+
# error: Incompatible return value type (got "Union[Timestamp, NaTType,
|
| 1079 |
+
# Series, Index]", expected "Union[DatetimeIndex, Series, float, str,
|
| 1080 |
+
# NaTType, None]")
|
| 1081 |
+
return result # type: ignore[return-value]
|
| 1082 |
+
|
| 1083 |
+
|
| 1084 |
+
# mappings for assembling units
|
| 1085 |
+
_unit_map = {
|
| 1086 |
+
"year": "year",
|
| 1087 |
+
"years": "year",
|
| 1088 |
+
"month": "month",
|
| 1089 |
+
"months": "month",
|
| 1090 |
+
"day": "day",
|
| 1091 |
+
"days": "day",
|
| 1092 |
+
"hour": "h",
|
| 1093 |
+
"hours": "h",
|
| 1094 |
+
"minute": "m",
|
| 1095 |
+
"minutes": "m",
|
| 1096 |
+
"second": "s",
|
| 1097 |
+
"seconds": "s",
|
| 1098 |
+
"ms": "ms",
|
| 1099 |
+
"millisecond": "ms",
|
| 1100 |
+
"milliseconds": "ms",
|
| 1101 |
+
"us": "us",
|
| 1102 |
+
"microsecond": "us",
|
| 1103 |
+
"microseconds": "us",
|
| 1104 |
+
"ns": "ns",
|
| 1105 |
+
"nanosecond": "ns",
|
| 1106 |
+
"nanoseconds": "ns",
|
| 1107 |
+
}
|
| 1108 |
+
|
| 1109 |
+
|
| 1110 |
+
def _assemble_from_unit_mappings(
|
| 1111 |
+
arg, errors: DateTimeErrorChoices, utc: bool
|
| 1112 |
+
) -> Series:
|
| 1113 |
+
"""
|
| 1114 |
+
assemble the unit specified fields from the arg (DataFrame)
|
| 1115 |
+
Return a Series for actual parsing
|
| 1116 |
+
|
| 1117 |
+
Parameters
|
| 1118 |
+
----------
|
| 1119 |
+
arg : DataFrame
|
| 1120 |
+
errors : {'raise', 'coerce'}, default 'raise'
|
| 1121 |
+
|
| 1122 |
+
- If :const:`'raise'`, then invalid parsing will raise an exception
|
| 1123 |
+
- If :const:`'coerce'`, then invalid parsing will be set as :const:`NaT`
|
| 1124 |
+
utc : bool
|
| 1125 |
+
Whether to convert/localize timestamps to UTC.
|
| 1126 |
+
|
| 1127 |
+
Returns
|
| 1128 |
+
-------
|
| 1129 |
+
Series
|
| 1130 |
+
"""
|
| 1131 |
+
from pandas import (
|
| 1132 |
+
DataFrame,
|
| 1133 |
+
to_numeric,
|
| 1134 |
+
to_timedelta,
|
| 1135 |
+
)
|
| 1136 |
+
|
| 1137 |
+
arg = DataFrame(arg)
|
| 1138 |
+
if not arg.columns.is_unique:
|
| 1139 |
+
raise ValueError("cannot assemble with duplicate keys")
|
| 1140 |
+
|
| 1141 |
+
# replace passed unit with _unit_map
|
| 1142 |
+
def f(value):
|
| 1143 |
+
if value in _unit_map:
|
| 1144 |
+
return _unit_map[value]
|
| 1145 |
+
|
| 1146 |
+
# m is case significant
|
| 1147 |
+
if value.lower() in _unit_map:
|
| 1148 |
+
return _unit_map[value.lower()]
|
| 1149 |
+
|
| 1150 |
+
return value
|
| 1151 |
+
|
| 1152 |
+
unit = {k: f(k) for k in arg.keys()}
|
| 1153 |
+
unit_rev = {v: k for k, v in unit.items()}
|
| 1154 |
+
|
| 1155 |
+
# we require at least Ymd
|
| 1156 |
+
required = ["year", "month", "day"]
|
| 1157 |
+
req = set(required) - set(unit_rev.keys())
|
| 1158 |
+
if len(req):
|
| 1159 |
+
_required = ",".join(sorted(req))
|
| 1160 |
+
raise ValueError(
|
| 1161 |
+
"to assemble mappings requires at least that "
|
| 1162 |
+
f"[year, month, day] be specified: [{_required}] is missing"
|
| 1163 |
+
)
|
| 1164 |
+
|
| 1165 |
+
# keys we don't recognize
|
| 1166 |
+
excess = set(unit_rev.keys()) - set(_unit_map.values())
|
| 1167 |
+
if len(excess):
|
| 1168 |
+
_excess = ",".join(sorted(excess))
|
| 1169 |
+
raise ValueError(
|
| 1170 |
+
f"extra keys have been passed to the datetime assemblage: [{_excess}]"
|
| 1171 |
+
)
|
| 1172 |
+
|
| 1173 |
+
def coerce(values):
|
| 1174 |
+
# we allow coercion to if errors allows
|
| 1175 |
+
values = to_numeric(values, errors=errors)
|
| 1176 |
+
|
| 1177 |
+
# prevent prevision issues in case of float32 # GH#60506
|
| 1178 |
+
if is_float_dtype(values.dtype):
|
| 1179 |
+
values = values.astype("float64")
|
| 1180 |
+
|
| 1181 |
+
# prevent overflow in case of int8 or int16
|
| 1182 |
+
if is_integer_dtype(values.dtype):
|
| 1183 |
+
values = values.astype("int64")
|
| 1184 |
+
return values
|
| 1185 |
+
|
| 1186 |
+
values = (
|
| 1187 |
+
coerce(arg[unit_rev["year"]]) * 10000
|
| 1188 |
+
+ coerce(arg[unit_rev["month"]]) * 100
|
| 1189 |
+
+ coerce(arg[unit_rev["day"]])
|
| 1190 |
+
)
|
| 1191 |
+
try:
|
| 1192 |
+
values = to_datetime(values, format="%Y%m%d", errors=errors, utc=utc)
|
| 1193 |
+
except (TypeError, ValueError) as err:
|
| 1194 |
+
raise ValueError(f"cannot assemble the datetimes: {err}") from err
|
| 1195 |
+
|
| 1196 |
+
units: list[UnitChoices] = ["h", "m", "s", "ms", "us", "ns"]
|
| 1197 |
+
for u in units:
|
| 1198 |
+
value = unit_rev.get(u)
|
| 1199 |
+
if value is not None and value in arg:
|
| 1200 |
+
try:
|
| 1201 |
+
values += to_timedelta(coerce(arg[value]), unit=u, errors=errors)
|
| 1202 |
+
except (TypeError, ValueError) as err:
|
| 1203 |
+
raise ValueError(
|
| 1204 |
+
f"cannot assemble the datetimes [{value}]: {err}"
|
| 1205 |
+
) from err
|
| 1206 |
+
return values
|
| 1207 |
+
|
| 1208 |
+
|
| 1209 |
+
__all__ = [
|
| 1210 |
+
"DateParseError",
|
| 1211 |
+
"should_cache",
|
| 1212 |
+
"to_datetime",
|
| 1213 |
+
]
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/numeric.py
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import (
|
| 4 |
+
TYPE_CHECKING,
|
| 5 |
+
Literal,
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
from pandas._libs import (
|
| 11 |
+
lib,
|
| 12 |
+
missing as libmissing,
|
| 13 |
+
)
|
| 14 |
+
from pandas._libs.tslibs import (
|
| 15 |
+
Timedelta,
|
| 16 |
+
Timestamp,
|
| 17 |
+
)
|
| 18 |
+
from pandas.util._decorators import set_module
|
| 19 |
+
from pandas.util._validators import check_dtype_backend
|
| 20 |
+
|
| 21 |
+
from pandas.core.dtypes.cast import maybe_downcast_numeric
|
| 22 |
+
from pandas.core.dtypes.common import (
|
| 23 |
+
ensure_object,
|
| 24 |
+
is_bool_dtype,
|
| 25 |
+
is_decimal,
|
| 26 |
+
is_integer_dtype,
|
| 27 |
+
is_number,
|
| 28 |
+
is_numeric_dtype,
|
| 29 |
+
is_scalar,
|
| 30 |
+
is_string_dtype,
|
| 31 |
+
needs_i8_conversion,
|
| 32 |
+
)
|
| 33 |
+
from pandas.core.dtypes.dtypes import ArrowDtype
|
| 34 |
+
from pandas.core.dtypes.generic import (
|
| 35 |
+
ABCIndex,
|
| 36 |
+
ABCSeries,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
from pandas.core.arrays import BaseMaskedArray
|
| 40 |
+
from pandas.core.arrays.string_ import StringDtype
|
| 41 |
+
|
| 42 |
+
if TYPE_CHECKING:
|
| 43 |
+
from pandas._typing import (
|
| 44 |
+
DateTimeErrorChoices,
|
| 45 |
+
DtypeBackend,
|
| 46 |
+
npt,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
@set_module("pandas")
|
| 51 |
+
def to_numeric(
|
| 52 |
+
arg,
|
| 53 |
+
errors: DateTimeErrorChoices = "raise",
|
| 54 |
+
downcast: Literal["integer", "signed", "unsigned", "float"] | None = None,
|
| 55 |
+
dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,
|
| 56 |
+
):
|
| 57 |
+
"""
|
| 58 |
+
Convert argument to a numeric type.
|
| 59 |
+
|
| 60 |
+
If the input is already of a numeric dtype, the dtype will be preserved.
|
| 61 |
+
For non-numeric inputs, the default return dtype is `float64` or `int64`
|
| 62 |
+
depending on the data supplied. Use the `downcast` parameter
|
| 63 |
+
to obtain other dtypes.
|
| 64 |
+
|
| 65 |
+
Please note that precision loss may occur if really large numbers
|
| 66 |
+
are passed in. Due to the internal limitations of `ndarray`, if
|
| 67 |
+
numbers smaller than `-9223372036854775808` (np.iinfo(np.int64).min)
|
| 68 |
+
or larger than `18446744073709551615` (np.iinfo(np.uint64).max) are
|
| 69 |
+
passed in, it is very likely they will be converted to float so that
|
| 70 |
+
they can be stored in an `ndarray`. These warnings apply similarly to
|
| 71 |
+
`Series` since it internally leverages `ndarray`.
|
| 72 |
+
|
| 73 |
+
Parameters
|
| 74 |
+
----------
|
| 75 |
+
arg : scalar, list, tuple, 1-d array, or Series
|
| 76 |
+
Argument to be converted.
|
| 77 |
+
|
| 78 |
+
errors : {'raise', 'coerce'}, default 'raise'
|
| 79 |
+
- If 'raise', then invalid parsing will raise an exception.
|
| 80 |
+
- If 'coerce', then invalid parsing will be set as NaN.
|
| 81 |
+
|
| 82 |
+
downcast : str, default None
|
| 83 |
+
Can be 'integer', 'signed', 'unsigned', or 'float'.
|
| 84 |
+
If not None, and if the data has been successfully cast to a
|
| 85 |
+
numerical dtype (or if the data was numeric to begin with),
|
| 86 |
+
downcast that resulting data to the smallest numerical dtype
|
| 87 |
+
possible according to the following rules:
|
| 88 |
+
|
| 89 |
+
- 'integer' or 'signed': smallest signed int dtype (min.: np.int8)
|
| 90 |
+
- 'unsigned': smallest unsigned int dtype (min.: np.uint8)
|
| 91 |
+
- 'float': smallest float dtype (min.: np.float32)
|
| 92 |
+
|
| 93 |
+
As this behaviour is separate from the core conversion to
|
| 94 |
+
numeric values, any errors raised during the downcasting
|
| 95 |
+
will be surfaced regardless of the value of the 'errors' input.
|
| 96 |
+
|
| 97 |
+
In addition, downcasting will only occur if the size
|
| 98 |
+
of the resulting data's dtype is strictly larger than
|
| 99 |
+
the dtype it is to be cast to, so if none of the dtypes
|
| 100 |
+
checked satisfy that specification, no downcasting will be
|
| 101 |
+
performed on the data.
|
| 102 |
+
|
| 103 |
+
dtype_backend : {'numpy_nullable', 'pyarrow'}
|
| 104 |
+
Back-end data type applied to the resultant :class:`DataFrame`
|
| 105 |
+
(still experimental). If not specified, the default behavior
|
| 106 |
+
is to not use nullable data types. If specified, the behavior
|
| 107 |
+
is as follows:
|
| 108 |
+
|
| 109 |
+
* ``"numpy_nullable"``: returns nullable-dtype-backed object
|
| 110 |
+
* ``"pyarrow"``: returns with pyarrow-backed nullable object
|
| 111 |
+
|
| 112 |
+
.. versionadded:: 2.0
|
| 113 |
+
|
| 114 |
+
Returns
|
| 115 |
+
-------
|
| 116 |
+
ret
|
| 117 |
+
Numeric if parsing succeeded.
|
| 118 |
+
Return type depends on input. Series if Series, otherwise ndarray.
|
| 119 |
+
|
| 120 |
+
Raises
|
| 121 |
+
------
|
| 122 |
+
ValueError
|
| 123 |
+
If the input contains non-numeric values and `errors='raise'`.
|
| 124 |
+
TypeError
|
| 125 |
+
If the input is not list-like, 1D, or scalar convertible to numeric,
|
| 126 |
+
such as nested lists or unsupported input types (e.g., dict).
|
| 127 |
+
|
| 128 |
+
See Also
|
| 129 |
+
--------
|
| 130 |
+
DataFrame.astype : Cast argument to a specified dtype.
|
| 131 |
+
to_datetime : Convert argument to datetime.
|
| 132 |
+
to_timedelta : Convert argument to timedelta.
|
| 133 |
+
numpy.ndarray.astype : Cast a numpy array to a specified type.
|
| 134 |
+
DataFrame.convert_dtypes : Convert dtypes.
|
| 135 |
+
|
| 136 |
+
Examples
|
| 137 |
+
--------
|
| 138 |
+
Take separate series and convert to numeric, coercing when told to
|
| 139 |
+
|
| 140 |
+
>>> s = pd.Series(["1.0", "2", -3])
|
| 141 |
+
>>> pd.to_numeric(s)
|
| 142 |
+
0 1.0
|
| 143 |
+
1 2.0
|
| 144 |
+
2 -3.0
|
| 145 |
+
dtype: float64
|
| 146 |
+
>>> pd.to_numeric(s, downcast="float")
|
| 147 |
+
0 1.0
|
| 148 |
+
1 2.0
|
| 149 |
+
2 -3.0
|
| 150 |
+
dtype: float32
|
| 151 |
+
>>> pd.to_numeric(s, downcast="signed")
|
| 152 |
+
0 1
|
| 153 |
+
1 2
|
| 154 |
+
2 -3
|
| 155 |
+
dtype: int8
|
| 156 |
+
>>> s = pd.Series(["apple", "1.0", "2", -3])
|
| 157 |
+
>>> pd.to_numeric(s, errors="coerce")
|
| 158 |
+
0 NaN
|
| 159 |
+
1 1.0
|
| 160 |
+
2 2.0
|
| 161 |
+
3 -3.0
|
| 162 |
+
dtype: float64
|
| 163 |
+
|
| 164 |
+
Downcasting of nullable integer and floating dtypes is supported:
|
| 165 |
+
|
| 166 |
+
>>> s = pd.Series([1, 2, 3], dtype="Int64")
|
| 167 |
+
>>> pd.to_numeric(s, downcast="integer")
|
| 168 |
+
0 1
|
| 169 |
+
1 2
|
| 170 |
+
2 3
|
| 171 |
+
dtype: Int8
|
| 172 |
+
>>> s = pd.Series([1.0, 2.1, 3.0], dtype="Float64")
|
| 173 |
+
>>> pd.to_numeric(s, downcast="float")
|
| 174 |
+
0 1.0
|
| 175 |
+
1 2.1
|
| 176 |
+
2 3.0
|
| 177 |
+
dtype: Float32
|
| 178 |
+
"""
|
| 179 |
+
if downcast not in (None, "integer", "signed", "unsigned", "float"):
|
| 180 |
+
raise ValueError("invalid downcasting method provided")
|
| 181 |
+
|
| 182 |
+
if errors not in ("raise", "coerce"):
|
| 183 |
+
raise ValueError("invalid error value specified")
|
| 184 |
+
|
| 185 |
+
check_dtype_backend(dtype_backend)
|
| 186 |
+
|
| 187 |
+
is_series = False
|
| 188 |
+
is_index = False
|
| 189 |
+
is_scalars = False
|
| 190 |
+
|
| 191 |
+
if isinstance(arg, ABCSeries):
|
| 192 |
+
is_series = True
|
| 193 |
+
values = arg.values
|
| 194 |
+
elif isinstance(arg, ABCIndex):
|
| 195 |
+
is_index = True
|
| 196 |
+
if needs_i8_conversion(arg.dtype):
|
| 197 |
+
values = arg.view("i8")
|
| 198 |
+
else:
|
| 199 |
+
values = arg.values
|
| 200 |
+
elif isinstance(arg, (list, tuple)):
|
| 201 |
+
values = np.array(arg, dtype="O")
|
| 202 |
+
elif is_scalar(arg):
|
| 203 |
+
if is_decimal(arg):
|
| 204 |
+
return float(arg)
|
| 205 |
+
if is_number(arg):
|
| 206 |
+
return arg
|
| 207 |
+
if isinstance(arg, (Timedelta, Timestamp)):
|
| 208 |
+
return arg._value
|
| 209 |
+
is_scalars = True
|
| 210 |
+
values = np.array([arg], dtype="O")
|
| 211 |
+
elif getattr(arg, "ndim", 1) > 1:
|
| 212 |
+
raise TypeError("arg must be a list, tuple, 1-d array, or Series")
|
| 213 |
+
else:
|
| 214 |
+
values = arg
|
| 215 |
+
|
| 216 |
+
# GH33013: for IntegerArray & FloatingArray extract non-null values for casting
|
| 217 |
+
# save mask to reconstruct the full array after casting
|
| 218 |
+
mask: npt.NDArray[np.bool_] | None = None
|
| 219 |
+
if isinstance(values, BaseMaskedArray):
|
| 220 |
+
mask = values._mask
|
| 221 |
+
values = values._data[~mask]
|
| 222 |
+
|
| 223 |
+
values_dtype = getattr(values, "dtype", None)
|
| 224 |
+
if isinstance(values_dtype, ArrowDtype):
|
| 225 |
+
mask = values.isna()
|
| 226 |
+
values = values.dropna().to_numpy()
|
| 227 |
+
new_mask: np.ndarray | None = None
|
| 228 |
+
if is_numeric_dtype(values_dtype):
|
| 229 |
+
pass
|
| 230 |
+
elif lib.is_np_dtype(values_dtype, "mM"):
|
| 231 |
+
values = values.view(np.int64)
|
| 232 |
+
else:
|
| 233 |
+
values = ensure_object(values)
|
| 234 |
+
coerce_numeric = errors != "raise"
|
| 235 |
+
values, new_mask = lib.maybe_convert_numeric( # type: ignore[call-overload]
|
| 236 |
+
values,
|
| 237 |
+
set(),
|
| 238 |
+
coerce_numeric=coerce_numeric,
|
| 239 |
+
convert_to_masked_nullable=dtype_backend is not lib.no_default
|
| 240 |
+
or (
|
| 241 |
+
isinstance(values_dtype, StringDtype)
|
| 242 |
+
and values_dtype.na_value is libmissing.NA
|
| 243 |
+
),
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
if new_mask is not None:
|
| 247 |
+
# Remove unnecessary values, is expected later anyway and enables
|
| 248 |
+
# downcasting
|
| 249 |
+
values = values[~new_mask]
|
| 250 |
+
elif (dtype_backend is not lib.no_default and new_mask is None) or (
|
| 251 |
+
isinstance(values_dtype, StringDtype) and values_dtype.na_value is libmissing.NA
|
| 252 |
+
):
|
| 253 |
+
new_mask = np.zeros(values.shape, dtype=np.bool_)
|
| 254 |
+
|
| 255 |
+
# attempt downcast only if the data has been successfully converted
|
| 256 |
+
# to a numerical dtype and if a downcast method has been specified
|
| 257 |
+
if downcast is not None and is_numeric_dtype(values.dtype):
|
| 258 |
+
typecodes: str | None = None
|
| 259 |
+
|
| 260 |
+
if downcast in ("integer", "signed"):
|
| 261 |
+
typecodes = np.typecodes["Integer"]
|
| 262 |
+
elif downcast == "unsigned" and (not len(values) or np.min(values) >= 0):
|
| 263 |
+
typecodes = np.typecodes["UnsignedInteger"]
|
| 264 |
+
elif downcast == "float":
|
| 265 |
+
typecodes = np.typecodes["Float"]
|
| 266 |
+
|
| 267 |
+
# pandas support goes only to np.float32,
|
| 268 |
+
# as float dtypes smaller than that are
|
| 269 |
+
# extremely rare and not well supported
|
| 270 |
+
float_32_char = np.dtype(np.float32).char
|
| 271 |
+
float_32_ind = typecodes.index(float_32_char)
|
| 272 |
+
typecodes = typecodes[float_32_ind:]
|
| 273 |
+
|
| 274 |
+
if typecodes is not None:
|
| 275 |
+
# from smallest to largest
|
| 276 |
+
for typecode in typecodes:
|
| 277 |
+
dtype = np.dtype(typecode)
|
| 278 |
+
if dtype.itemsize <= values.dtype.itemsize:
|
| 279 |
+
values = maybe_downcast_numeric(values, dtype)
|
| 280 |
+
|
| 281 |
+
# successful conversion
|
| 282 |
+
if values.dtype == dtype:
|
| 283 |
+
break
|
| 284 |
+
|
| 285 |
+
# GH33013: for IntegerArray, BooleanArray & FloatingArray need to reconstruct
|
| 286 |
+
# masked array
|
| 287 |
+
if (mask is not None or new_mask is not None) and not is_string_dtype(values.dtype):
|
| 288 |
+
if mask is None or (new_mask is not None and new_mask.shape == mask.shape):
|
| 289 |
+
# GH 52588
|
| 290 |
+
mask = new_mask
|
| 291 |
+
else:
|
| 292 |
+
mask = mask.copy()
|
| 293 |
+
assert isinstance(mask, np.ndarray)
|
| 294 |
+
data = np.zeros(mask.shape, dtype=values.dtype)
|
| 295 |
+
data[~mask] = values
|
| 296 |
+
|
| 297 |
+
from pandas.core.arrays import (
|
| 298 |
+
ArrowExtensionArray,
|
| 299 |
+
BooleanArray,
|
| 300 |
+
FloatingArray,
|
| 301 |
+
IntegerArray,
|
| 302 |
+
)
|
| 303 |
+
|
| 304 |
+
klass: type[IntegerArray | BooleanArray | FloatingArray]
|
| 305 |
+
if is_integer_dtype(data.dtype):
|
| 306 |
+
klass = IntegerArray
|
| 307 |
+
elif is_bool_dtype(data.dtype):
|
| 308 |
+
klass = BooleanArray
|
| 309 |
+
else:
|
| 310 |
+
klass = FloatingArray
|
| 311 |
+
values = klass(data, mask)
|
| 312 |
+
|
| 313 |
+
if dtype_backend == "pyarrow" or isinstance(values_dtype, ArrowDtype):
|
| 314 |
+
values = ArrowExtensionArray(values.__arrow_array__())
|
| 315 |
+
|
| 316 |
+
if is_series:
|
| 317 |
+
return arg._constructor(values, index=arg.index, name=arg.name)
|
| 318 |
+
elif is_index:
|
| 319 |
+
# because we want to coerce to numeric if possible,
|
| 320 |
+
# do not use _shallow_copy
|
| 321 |
+
from pandas import Index
|
| 322 |
+
|
| 323 |
+
return Index(values, name=arg.name)
|
| 324 |
+
elif is_scalars:
|
| 325 |
+
return values[0]
|
| 326 |
+
else:
|
| 327 |
+
return values
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/timedeltas.py
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
timedelta support tools
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from __future__ import annotations
|
| 6 |
+
|
| 7 |
+
from typing import (
|
| 8 |
+
TYPE_CHECKING,
|
| 9 |
+
Any,
|
| 10 |
+
overload,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
from pandas._libs import lib
|
| 16 |
+
from pandas._libs.tslibs import (
|
| 17 |
+
NaT,
|
| 18 |
+
NaTType,
|
| 19 |
+
)
|
| 20 |
+
from pandas._libs.tslibs.timedeltas import (
|
| 21 |
+
Timedelta,
|
| 22 |
+
disallow_ambiguous_unit,
|
| 23 |
+
parse_timedelta_unit,
|
| 24 |
+
)
|
| 25 |
+
from pandas.util._decorators import set_module
|
| 26 |
+
|
| 27 |
+
from pandas.core.dtypes.common import is_list_like
|
| 28 |
+
from pandas.core.dtypes.dtypes import ArrowDtype
|
| 29 |
+
from pandas.core.dtypes.generic import (
|
| 30 |
+
ABCIndex,
|
| 31 |
+
ABCSeries,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
from pandas.core.arrays.timedeltas import sequence_to_td64ns
|
| 35 |
+
|
| 36 |
+
if TYPE_CHECKING:
|
| 37 |
+
from collections.abc import Hashable
|
| 38 |
+
from datetime import timedelta
|
| 39 |
+
|
| 40 |
+
from pandas._libs.tslibs.timedeltas import UnitChoices
|
| 41 |
+
from pandas._typing import (
|
| 42 |
+
ArrayLike,
|
| 43 |
+
DateTimeErrorChoices,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
from pandas import (
|
| 47 |
+
Index,
|
| 48 |
+
Series,
|
| 49 |
+
TimedeltaIndex,
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
@overload
|
| 54 |
+
def to_timedelta(
|
| 55 |
+
arg: str | float | timedelta,
|
| 56 |
+
unit: UnitChoices | None = ...,
|
| 57 |
+
errors: DateTimeErrorChoices = ...,
|
| 58 |
+
) -> Timedelta: ...
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@overload
|
| 62 |
+
def to_timedelta(
|
| 63 |
+
arg: Series,
|
| 64 |
+
unit: UnitChoices | None = ...,
|
| 65 |
+
errors: DateTimeErrorChoices = ...,
|
| 66 |
+
) -> Series: ...
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
@overload
|
| 70 |
+
def to_timedelta(
|
| 71 |
+
arg: list | tuple | range | ArrayLike | Index,
|
| 72 |
+
unit: UnitChoices | None = ...,
|
| 73 |
+
errors: DateTimeErrorChoices = ...,
|
| 74 |
+
) -> TimedeltaIndex: ...
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
@set_module("pandas")
|
| 78 |
+
def to_timedelta(
|
| 79 |
+
arg: str
|
| 80 |
+
| int
|
| 81 |
+
| float
|
| 82 |
+
| timedelta
|
| 83 |
+
| list
|
| 84 |
+
| tuple
|
| 85 |
+
| range
|
| 86 |
+
| ArrayLike
|
| 87 |
+
| Index
|
| 88 |
+
| Series,
|
| 89 |
+
unit: UnitChoices | None = None,
|
| 90 |
+
errors: DateTimeErrorChoices = "raise",
|
| 91 |
+
) -> Timedelta | TimedeltaIndex | Series | NaTType | Any:
|
| 92 |
+
"""
|
| 93 |
+
Convert argument to timedelta.
|
| 94 |
+
|
| 95 |
+
Timedeltas are absolute differences in times, expressed in difference
|
| 96 |
+
units (e.g. days, hours, minutes, seconds). This method converts
|
| 97 |
+
an argument from a recognized timedelta format / value into
|
| 98 |
+
a Timedelta type.
|
| 99 |
+
|
| 100 |
+
Parameters
|
| 101 |
+
----------
|
| 102 |
+
arg : str, timedelta, list-like or Series
|
| 103 |
+
The data to be converted to timedelta.
|
| 104 |
+
|
| 105 |
+
.. versionchanged:: 2.0
|
| 106 |
+
Strings with units 'M', 'Y' and 'y' do not represent
|
| 107 |
+
unambiguous timedelta values and will raise an exception.
|
| 108 |
+
|
| 109 |
+
unit : str, optional
|
| 110 |
+
Denotes the unit of the arg for numeric `arg`. Defaults to ``"ns"``.
|
| 111 |
+
|
| 112 |
+
Possible values:
|
| 113 |
+
|
| 114 |
+
* 'W'
|
| 115 |
+
* 'D' / 'days' / 'day'
|
| 116 |
+
* 'hours' / 'hour' / 'hr' / 'h'
|
| 117 |
+
* 'm' / 'minute' / 'min' / 'minutes'
|
| 118 |
+
* 's' / 'seconds' / 'sec' / 'second'
|
| 119 |
+
* 'ms' / 'milliseconds' / 'millisecond' / 'milli' / 'millis'
|
| 120 |
+
* 'us' / 'microseconds' / 'microsecond' / 'micro' / 'micros'
|
| 121 |
+
* 'ns' / 'nanoseconds' / 'nano' / 'nanos' / 'nanosecond'
|
| 122 |
+
|
| 123 |
+
Must not be specified when `arg` contains strings and ``errors="raise"``.
|
| 124 |
+
|
| 125 |
+
errors : {'raise', 'coerce'}, default 'raise'
|
| 126 |
+
- If 'raise', then invalid parsing will raise an exception.
|
| 127 |
+
- If 'coerce', then invalid parsing will be set as NaT.
|
| 128 |
+
|
| 129 |
+
Returns
|
| 130 |
+
-------
|
| 131 |
+
timedelta
|
| 132 |
+
If parsing succeeded.
|
| 133 |
+
Return type depends on input:
|
| 134 |
+
|
| 135 |
+
- list-like: TimedeltaIndex of timedelta64 dtype
|
| 136 |
+
- Series: Series of timedelta64 dtype
|
| 137 |
+
- scalar: Timedelta
|
| 138 |
+
|
| 139 |
+
See Also
|
| 140 |
+
--------
|
| 141 |
+
DataFrame.astype : Cast argument to a specified dtype.
|
| 142 |
+
to_datetime : Convert argument to datetime.
|
| 143 |
+
convert_dtypes : Convert dtypes.
|
| 144 |
+
|
| 145 |
+
Notes
|
| 146 |
+
-----
|
| 147 |
+
If the precision is higher than nanoseconds, the precision of the duration is
|
| 148 |
+
truncated to nanoseconds for string inputs.
|
| 149 |
+
|
| 150 |
+
Examples
|
| 151 |
+
--------
|
| 152 |
+
Parsing a single string to a Timedelta:
|
| 153 |
+
|
| 154 |
+
>>> pd.to_timedelta("1 days 06:05:01.00003")
|
| 155 |
+
Timedelta('1 days 06:05:01.000030')
|
| 156 |
+
>>> pd.to_timedelta("15.5us")
|
| 157 |
+
Timedelta('0 days 00:00:00.000015500')
|
| 158 |
+
|
| 159 |
+
Parsing a list or array of strings:
|
| 160 |
+
|
| 161 |
+
>>> pd.to_timedelta(["1 days 06:05:01.00003", "15.5us", "nan"])
|
| 162 |
+
TimedeltaIndex(['1 days 06:05:01.000030', '0 days 00:00:00.000015500', NaT],
|
| 163 |
+
dtype='timedelta64[ns]', freq=None)
|
| 164 |
+
|
| 165 |
+
Converting numbers by specifying the `unit` keyword argument:
|
| 166 |
+
|
| 167 |
+
>>> pd.to_timedelta(np.arange(5), unit="s")
|
| 168 |
+
TimedeltaIndex(['0 days 00:00:00', '0 days 00:00:01', '0 days 00:00:02',
|
| 169 |
+
'0 days 00:00:03', '0 days 00:00:04'],
|
| 170 |
+
dtype='timedelta64[s]', freq=None)
|
| 171 |
+
>>> pd.to_timedelta(np.arange(5), unit="D")
|
| 172 |
+
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
|
| 173 |
+
dtype='timedelta64[s]', freq=None)
|
| 174 |
+
"""
|
| 175 |
+
if unit is not None:
|
| 176 |
+
unit = parse_timedelta_unit(unit)
|
| 177 |
+
disallow_ambiguous_unit(unit)
|
| 178 |
+
|
| 179 |
+
if errors not in ("raise", "coerce"):
|
| 180 |
+
raise ValueError("errors must be one of 'raise', or 'coerce'.")
|
| 181 |
+
|
| 182 |
+
if arg is None:
|
| 183 |
+
return NaT
|
| 184 |
+
elif isinstance(arg, ABCSeries):
|
| 185 |
+
values = _convert_listlike(arg._values, unit=unit, errors=errors)
|
| 186 |
+
return arg._constructor(values, index=arg.index, name=arg.name)
|
| 187 |
+
elif isinstance(arg, ABCIndex):
|
| 188 |
+
return _convert_listlike(arg, unit=unit, errors=errors, name=arg.name)
|
| 189 |
+
elif isinstance(arg, np.ndarray) and arg.ndim == 0:
|
| 190 |
+
# extract array scalar and process below
|
| 191 |
+
# error: Incompatible types in assignment (expression has type "object",
|
| 192 |
+
# variable has type "Union[str, int, float, timedelta, List[Any],
|
| 193 |
+
# Tuple[Any, ...], Union[Union[ExtensionArray, ndarray[Any, Any]], Index,
|
| 194 |
+
# Series]]") [assignment]
|
| 195 |
+
arg = lib.item_from_zerodim(arg) # type: ignore[assignment]
|
| 196 |
+
elif is_list_like(arg) and getattr(arg, "ndim", 1) == 1:
|
| 197 |
+
return _convert_listlike(arg, unit=unit, errors=errors)
|
| 198 |
+
elif getattr(arg, "ndim", 1) > 1:
|
| 199 |
+
raise TypeError(
|
| 200 |
+
"arg must be a string, timedelta, list, tuple, 1-d array, or Series"
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
if isinstance(arg, str) and unit is not None:
|
| 204 |
+
raise ValueError("unit must not be specified if the input is/contains a str")
|
| 205 |
+
|
| 206 |
+
# ...so it must be a scalar value. Return scalar.
|
| 207 |
+
return _coerce_scalar_to_timedelta_type(arg, unit=unit, errors=errors)
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def _coerce_scalar_to_timedelta_type(
|
| 211 |
+
r, unit: UnitChoices | None = "ns", errors: DateTimeErrorChoices = "raise"
|
| 212 |
+
) -> Timedelta | NaTType:
|
| 213 |
+
"""Convert string 'r' to a timedelta object."""
|
| 214 |
+
result: Timedelta | NaTType
|
| 215 |
+
|
| 216 |
+
try:
|
| 217 |
+
result = Timedelta(r, unit)
|
| 218 |
+
except ValueError:
|
| 219 |
+
if errors == "raise":
|
| 220 |
+
raise
|
| 221 |
+
# coerce
|
| 222 |
+
result = NaT
|
| 223 |
+
|
| 224 |
+
return result
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
def _convert_listlike(
|
| 228 |
+
arg,
|
| 229 |
+
unit: UnitChoices | None = None,
|
| 230 |
+
errors: DateTimeErrorChoices = "raise",
|
| 231 |
+
name: Hashable | None = None,
|
| 232 |
+
):
|
| 233 |
+
"""Convert a list of objects to a timedelta index object."""
|
| 234 |
+
arg_dtype = getattr(arg, "dtype", None)
|
| 235 |
+
if isinstance(arg, (list, tuple)) or arg_dtype is None:
|
| 236 |
+
arg = np.array(arg, dtype=object)
|
| 237 |
+
elif isinstance(arg_dtype, ArrowDtype) and arg_dtype.kind == "m":
|
| 238 |
+
return arg
|
| 239 |
+
|
| 240 |
+
td64arr = sequence_to_td64ns(arg, unit=unit, errors=errors, copy=False)[0]
|
| 241 |
+
|
| 242 |
+
from pandas import TimedeltaIndex
|
| 243 |
+
|
| 244 |
+
copy = td64arr is arg or np.may_share_memory(arg, td64arr)
|
| 245 |
+
value = TimedeltaIndex(td64arr, name=name, copy=copy)
|
| 246 |
+
return value
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/tools/times.py
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from datetime import (
|
| 4 |
+
datetime,
|
| 5 |
+
time,
|
| 6 |
+
)
|
| 7 |
+
from typing import TYPE_CHECKING
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
from pandas._libs.lib import is_list_like
|
| 12 |
+
|
| 13 |
+
from pandas.core.dtypes.generic import (
|
| 14 |
+
ABCIndex,
|
| 15 |
+
ABCSeries,
|
| 16 |
+
)
|
| 17 |
+
from pandas.core.dtypes.missing import notna
|
| 18 |
+
|
| 19 |
+
if TYPE_CHECKING:
|
| 20 |
+
from pandas._typing import DateTimeErrorChoices
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def to_time(
|
| 24 |
+
arg,
|
| 25 |
+
format: str | None = None,
|
| 26 |
+
infer_time_format: bool = False,
|
| 27 |
+
errors: DateTimeErrorChoices = "raise",
|
| 28 |
+
):
|
| 29 |
+
"""
|
| 30 |
+
Parse time strings to time objects using fixed strptime formats ("%H:%M",
|
| 31 |
+
"%H%M", "%I:%M%p", "%I%M%p", "%H:%M:%S", "%H%M%S", "%I:%M:%S%p",
|
| 32 |
+
"%I%M%S%p")
|
| 33 |
+
|
| 34 |
+
Use infer_time_format if all the strings are in the same format to speed
|
| 35 |
+
up conversion.
|
| 36 |
+
|
| 37 |
+
Parameters
|
| 38 |
+
----------
|
| 39 |
+
arg : string in time format, datetime.time, list, tuple, 1-d array, Series
|
| 40 |
+
format : str, default None
|
| 41 |
+
Format used to convert arg into a time object. If None, fixed formats
|
| 42 |
+
are used.
|
| 43 |
+
infer_time_format: bool, default False
|
| 44 |
+
Infer the time format based on the first non-NaN element. If all
|
| 45 |
+
strings are in the same format, this will speed up conversion.
|
| 46 |
+
errors : {'raise', 'coerce'}, default 'raise'
|
| 47 |
+
- If 'raise', then invalid parsing will raise an exception
|
| 48 |
+
- If 'coerce', then invalid parsing will be set as None
|
| 49 |
+
|
| 50 |
+
Returns
|
| 51 |
+
-------
|
| 52 |
+
datetime.time
|
| 53 |
+
"""
|
| 54 |
+
if errors not in ("raise", "coerce"):
|
| 55 |
+
raise ValueError("errors must be one of 'raise', or 'coerce'.")
|
| 56 |
+
|
| 57 |
+
def _convert_listlike(arg, format):
|
| 58 |
+
if isinstance(arg, (list, tuple)):
|
| 59 |
+
arg = np.array(arg, dtype="O")
|
| 60 |
+
|
| 61 |
+
elif getattr(arg, "ndim", 1) > 1:
|
| 62 |
+
raise TypeError(
|
| 63 |
+
"arg must be a string, datetime, list, tuple, 1-d array, or Series"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
arg = np.asarray(arg, dtype="O")
|
| 67 |
+
|
| 68 |
+
if infer_time_format and format is None:
|
| 69 |
+
format = _guess_time_format_for_array(arg)
|
| 70 |
+
|
| 71 |
+
times: list[time | None] = []
|
| 72 |
+
if format is not None:
|
| 73 |
+
for element in arg:
|
| 74 |
+
try:
|
| 75 |
+
times.append(datetime.strptime(element, format).time())
|
| 76 |
+
except (ValueError, TypeError) as err:
|
| 77 |
+
if errors == "raise":
|
| 78 |
+
msg = (
|
| 79 |
+
f"Cannot convert {element} to a time with given "
|
| 80 |
+
f"format {format}"
|
| 81 |
+
)
|
| 82 |
+
raise ValueError(msg) from err
|
| 83 |
+
times.append(None)
|
| 84 |
+
else:
|
| 85 |
+
formats = _time_formats[:]
|
| 86 |
+
format_found = False
|
| 87 |
+
for element in arg:
|
| 88 |
+
time_object = None
|
| 89 |
+
try:
|
| 90 |
+
time_object = time.fromisoformat(element)
|
| 91 |
+
except (ValueError, TypeError):
|
| 92 |
+
for time_format in formats:
|
| 93 |
+
try:
|
| 94 |
+
time_object = datetime.strptime(element, time_format).time()
|
| 95 |
+
if not format_found:
|
| 96 |
+
# Put the found format in front
|
| 97 |
+
fmt = formats.pop(formats.index(time_format))
|
| 98 |
+
formats.insert(0, fmt)
|
| 99 |
+
format_found = True
|
| 100 |
+
break
|
| 101 |
+
except (ValueError, TypeError):
|
| 102 |
+
continue
|
| 103 |
+
|
| 104 |
+
if time_object is not None:
|
| 105 |
+
times.append(time_object)
|
| 106 |
+
elif errors == "raise":
|
| 107 |
+
raise ValueError(f"Cannot convert arg {arg} to a time")
|
| 108 |
+
else:
|
| 109 |
+
times.append(None)
|
| 110 |
+
|
| 111 |
+
return times
|
| 112 |
+
|
| 113 |
+
if arg is None:
|
| 114 |
+
return arg
|
| 115 |
+
elif isinstance(arg, time):
|
| 116 |
+
return arg
|
| 117 |
+
elif isinstance(arg, ABCSeries):
|
| 118 |
+
values = _convert_listlike(arg._values, format)
|
| 119 |
+
return arg._constructor(values, index=arg.index, name=arg.name)
|
| 120 |
+
elif isinstance(arg, ABCIndex):
|
| 121 |
+
return _convert_listlike(arg, format)
|
| 122 |
+
elif is_list_like(arg):
|
| 123 |
+
return _convert_listlike(arg, format)
|
| 124 |
+
|
| 125 |
+
return _convert_listlike(np.array([arg]), format)[0]
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
# Fixed time formats for time parsing
|
| 129 |
+
_time_formats = [
|
| 130 |
+
"%H:%M",
|
| 131 |
+
"%H%M",
|
| 132 |
+
"%I:%M%p",
|
| 133 |
+
"%I%M%p",
|
| 134 |
+
"%H:%M:%S",
|
| 135 |
+
"%H%M%S",
|
| 136 |
+
"%I:%M:%S%p",
|
| 137 |
+
"%I%M%S%p",
|
| 138 |
+
]
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def _guess_time_format_for_array(arr):
|
| 142 |
+
# Try to guess the format based on the first non-NaN element
|
| 143 |
+
non_nan_elements = notna(arr).nonzero()[0]
|
| 144 |
+
if len(non_nan_elements):
|
| 145 |
+
element = arr[non_nan_elements[0]]
|
| 146 |
+
for time_format in _time_formats:
|
| 147 |
+
try:
|
| 148 |
+
datetime.strptime(element, time_format)
|
| 149 |
+
return time_format
|
| 150 |
+
except ValueError:
|
| 151 |
+
pass
|
| 152 |
+
|
| 153 |
+
return None
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/util/__init__.py
ADDED
|
File without changes
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/util/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (193 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/util/__pycache__/hashing.cpython-312.pyc
ADDED
|
Binary file (12.5 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/util/__pycache__/numba_.cpython-312.pyc
ADDED
|
Binary file (4.6 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/util/hashing.py
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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E_CHECKING
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
from pandas._libs.hashing import hash_object_array
|
| 13 |
+
|
| 14 |
+
from pandas.core.dtypes.common import is_list_like
|
| 15 |
+
from pandas.core.dtypes.dtypes import CategoricalDtype
|
| 16 |
+
from pandas.core.dtypes.generic import (
|
| 17 |
+
ABCDataFrame,
|
| 18 |
+
ABCExtensionArray,
|
| 19 |
+
ABCIndex,
|
| 20 |
+
ABCMultiIndex,
|
| 21 |
+
ABCSeries,
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
if TYPE_CHECKING:
|
| 25 |
+
from collections.abc import (
|
| 26 |
+
Hashable,
|
| 27 |
+
Iterable,
|
| 28 |
+
Iterator,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
from pandas._typing import (
|
| 32 |
+
ArrayLike,
|
| 33 |
+
npt,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
from pandas import (
|
| 37 |
+
DataFrame,
|
| 38 |
+
Index,
|
| 39 |
+
MultiIndex,
|
| 40 |
+
Series,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# 16 byte long hashing key
|
| 45 |
+
_default_hash_key = "0123456789123456"
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def combine_hash_arrays(
|
| 49 |
+
arrays: Iterator[np.ndarray], num_items: int
|
| 50 |
+
) -> npt.NDArray[np.uint64]:
|
| 51 |
+
"""
|
| 52 |
+
Parameters
|
| 53 |
+
----------
|
| 54 |
+
arrays : Iterator[np.ndarray]
|
| 55 |
+
num_items : int
|
| 56 |
+
|
| 57 |
+
Returns
|
| 58 |
+
-------
|
| 59 |
+
np.ndarray[uint64]
|
| 60 |
+
|
| 61 |
+
Should be the same as CPython's tupleobject.c
|
| 62 |
+
"""
|
| 63 |
+
try:
|
| 64 |
+
first = next(arrays)
|
| 65 |
+
except StopIteration:
|
| 66 |
+
return np.array([], dtype=np.uint64)
|
| 67 |
+
|
| 68 |
+
arrays = itertools.chain([first], arrays)
|
| 69 |
+
|
| 70 |
+
mult = np.uint64(1000003)
|
| 71 |
+
out = np.zeros_like(first) + np.uint64(0x345678)
|
| 72 |
+
last_i = 0
|
| 73 |
+
for i, a in enumerate(arrays):
|
| 74 |
+
inverse_i = num_items - i
|
| 75 |
+
out ^= a
|
| 76 |
+
out *= mult
|
| 77 |
+
mult += np.uint64(82520 + inverse_i + inverse_i)
|
| 78 |
+
last_i = i
|
| 79 |
+
assert last_i + 1 == num_items, "Fed in wrong num_items"
|
| 80 |
+
out += np.uint64(97531)
|
| 81 |
+
return out
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def hash_pandas_object(
|
| 85 |
+
obj: Index | DataFrame | Series,
|
| 86 |
+
index: bool = True,
|
| 87 |
+
encoding: str = "utf8",
|
| 88 |
+
hash_key: str | None = _default_hash_key,
|
| 89 |
+
categorize: bool = True,
|
| 90 |
+
) -> Series:
|
| 91 |
+
"""
|
| 92 |
+
Return a data hash of the Index/Series/DataFrame.
|
| 93 |
+
|
| 94 |
+
The hash is computed element-wise using the underlying data values,
|
| 95 |
+
and optionally includes the index when hashing a Series or DataFrame.
|
| 96 |
+
|
| 97 |
+
Parameters
|
| 98 |
+
----------
|
| 99 |
+
obj : Index, Series, or DataFrame
|
| 100 |
+
The pandas object to hash.
|
| 101 |
+
index : bool, default True
|
| 102 |
+
Include the index in the hash (if Series/DataFrame).
|
| 103 |
+
encoding : str, default 'utf8'
|
| 104 |
+
Encoding for data & key when strings.
|
| 105 |
+
hash_key : str, default _default_hash_key
|
| 106 |
+
Hash_key for string key to encode.
|
| 107 |
+
categorize : bool, default True
|
| 108 |
+
Whether to first categorize object arrays before hashing. This is more
|
| 109 |
+
efficient when the array contains duplicate values.
|
| 110 |
+
|
| 111 |
+
Returns
|
| 112 |
+
-------
|
| 113 |
+
Series of uint64
|
| 114 |
+
Same length as the object.
|
| 115 |
+
|
| 116 |
+
See Also
|
| 117 |
+
--------
|
| 118 |
+
util.hash_array : Return a hash of the given array.
|
| 119 |
+
util.hash_tuples : Hash a MultiIndex or listlike-of-tuples efficiently.
|
| 120 |
+
|
| 121 |
+
Examples
|
| 122 |
+
--------
|
| 123 |
+
>>> pd.util.hash_pandas_object(pd.Series([1, 2, 3]))
|
| 124 |
+
0 14639053686158035780
|
| 125 |
+
1 3869563279212530728
|
| 126 |
+
2 393322362522515241
|
| 127 |
+
dtype: uint64
|
| 128 |
+
"""
|
| 129 |
+
from pandas import Series
|
| 130 |
+
|
| 131 |
+
if hash_key is None:
|
| 132 |
+
hash_key = _default_hash_key
|
| 133 |
+
|
| 134 |
+
if isinstance(obj, ABCMultiIndex):
|
| 135 |
+
return Series(hash_tuples(obj, encoding, hash_key), dtype="uint64", copy=False)
|
| 136 |
+
|
| 137 |
+
elif isinstance(obj, ABCIndex):
|
| 138 |
+
h = hash_array(obj._values, encoding, hash_key, categorize).astype(
|
| 139 |
+
"uint64", copy=False
|
| 140 |
+
)
|
| 141 |
+
ser = Series(h, index=obj, dtype="uint64", copy=False)
|
| 142 |
+
|
| 143 |
+
elif isinstance(obj, ABCSeries):
|
| 144 |
+
h = hash_array(obj._values, encoding, hash_key, categorize).astype(
|
| 145 |
+
"uint64", copy=False
|
| 146 |
+
)
|
| 147 |
+
if index:
|
| 148 |
+
index_iter = (
|
| 149 |
+
hash_pandas_object(
|
| 150 |
+
obj.index,
|
| 151 |
+
index=False,
|
| 152 |
+
encoding=encoding,
|
| 153 |
+
hash_key=hash_key,
|
| 154 |
+
categorize=categorize,
|
| 155 |
+
)._values
|
| 156 |
+
for _ in [None]
|
| 157 |
+
)
|
| 158 |
+
arrays = itertools.chain([h], index_iter)
|
| 159 |
+
h = combine_hash_arrays(arrays, 2)
|
| 160 |
+
|
| 161 |
+
ser = Series(h, index=obj.index, dtype="uint64", copy=False)
|
| 162 |
+
|
| 163 |
+
elif isinstance(obj, ABCDataFrame):
|
| 164 |
+
hashes = (
|
| 165 |
+
hash_array(series._values, encoding, hash_key, categorize)
|
| 166 |
+
for _, series in obj.items()
|
| 167 |
+
)
|
| 168 |
+
num_items = len(obj.columns)
|
| 169 |
+
if index:
|
| 170 |
+
index_hash_generator = (
|
| 171 |
+
hash_pandas_object(
|
| 172 |
+
obj.index,
|
| 173 |
+
index=False,
|
| 174 |
+
encoding=encoding,
|
| 175 |
+
hash_key=hash_key,
|
| 176 |
+
categorize=categorize,
|
| 177 |
+
)._values
|
| 178 |
+
for _ in [None]
|
| 179 |
+
)
|
| 180 |
+
num_items += 1
|
| 181 |
+
|
| 182 |
+
# keep `hashes` specifically a generator to keep mypy happy
|
| 183 |
+
_hashes = itertools.chain(hashes, index_hash_generator)
|
| 184 |
+
hashes = (x for x in _hashes)
|
| 185 |
+
h = combine_hash_arrays(hashes, num_items)
|
| 186 |
+
|
| 187 |
+
ser = Series(h, index=obj.index, dtype="uint64", copy=False)
|
| 188 |
+
else:
|
| 189 |
+
raise TypeError(f"Unexpected type for hashing {type(obj)}")
|
| 190 |
+
|
| 191 |
+
return ser
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def hash_tuples(
|
| 195 |
+
vals: MultiIndex | Iterable[tuple[Hashable, ...]],
|
| 196 |
+
encoding: str = "utf8",
|
| 197 |
+
hash_key: str = _default_hash_key,
|
| 198 |
+
) -> npt.NDArray[np.uint64]:
|
| 199 |
+
"""
|
| 200 |
+
Hash a MultiIndex / listlike-of-tuples efficiently.
|
| 201 |
+
|
| 202 |
+
Parameters
|
| 203 |
+
----------
|
| 204 |
+
vals : MultiIndex or listlike-of-tuples
|
| 205 |
+
encoding : str, default 'utf8'
|
| 206 |
+
hash_key : str, default _default_hash_key
|
| 207 |
+
|
| 208 |
+
Returns
|
| 209 |
+
-------
|
| 210 |
+
ndarray[np.uint64] of hashed values
|
| 211 |
+
"""
|
| 212 |
+
if not is_list_like(vals):
|
| 213 |
+
raise TypeError("must be convertible to a list-of-tuples")
|
| 214 |
+
|
| 215 |
+
from pandas import (
|
| 216 |
+
Categorical,
|
| 217 |
+
MultiIndex,
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
if not isinstance(vals, ABCMultiIndex):
|
| 221 |
+
mi = MultiIndex.from_tuples(vals)
|
| 222 |
+
else:
|
| 223 |
+
mi = vals
|
| 224 |
+
|
| 225 |
+
# create a list-of-Categoricals
|
| 226 |
+
cat_vals = [
|
| 227 |
+
Categorical._simple_new(
|
| 228 |
+
mi.codes[level],
|
| 229 |
+
CategoricalDtype(categories=mi.levels[level], ordered=False),
|
| 230 |
+
)
|
| 231 |
+
for level in range(mi.nlevels)
|
| 232 |
+
]
|
| 233 |
+
|
| 234 |
+
# hash the list-of-ndarrays
|
| 235 |
+
hashes = (
|
| 236 |
+
cat._hash_pandas_object(encoding=encoding, hash_key=hash_key, categorize=False)
|
| 237 |
+
for cat in cat_vals
|
| 238 |
+
)
|
| 239 |
+
h = combine_hash_arrays(hashes, len(cat_vals))
|
| 240 |
+
|
| 241 |
+
return h
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
def hash_array(
|
| 245 |
+
vals: ArrayLike,
|
| 246 |
+
encoding: str = "utf8",
|
| 247 |
+
hash_key: str = _default_hash_key,
|
| 248 |
+
categorize: bool = True,
|
| 249 |
+
) -> npt.NDArray[np.uint64]:
|
| 250 |
+
"""
|
| 251 |
+
Given a 1d array, return an array of deterministic integers.
|
| 252 |
+
|
| 253 |
+
Parameters
|
| 254 |
+
----------
|
| 255 |
+
vals : ndarray or ExtensionArray
|
| 256 |
+
The input array to hash.
|
| 257 |
+
encoding : str, default 'utf8'
|
| 258 |
+
Encoding for data & key when strings.
|
| 259 |
+
hash_key : str, default _default_hash_key
|
| 260 |
+
Hash_key for string key to encode.
|
| 261 |
+
categorize : bool, default True
|
| 262 |
+
Whether to first categorize object arrays before hashing. This is more
|
| 263 |
+
efficient when the array contains duplicate values.
|
| 264 |
+
|
| 265 |
+
Returns
|
| 266 |
+
-------
|
| 267 |
+
ndarray[np.uint64, ndim=1]
|
| 268 |
+
Hashed values, same length as the vals.
|
| 269 |
+
|
| 270 |
+
See Also
|
| 271 |
+
--------
|
| 272 |
+
util.hash_pandas_object : Return a data hash of the Index/Series/DataFrame.
|
| 273 |
+
util.hash_tuples : Hash a MultiIndex / listlike-of-tuples efficiently.
|
| 274 |
+
|
| 275 |
+
Examples
|
| 276 |
+
--------
|
| 277 |
+
>>> pd.util.hash_array(np.array([1, 2, 3]))
|
| 278 |
+
array([ 6238072747940578789, 15839785061582574730, 2185194620014831856],
|
| 279 |
+
dtype=uint64)
|
| 280 |
+
"""
|
| 281 |
+
if not hasattr(vals, "dtype"):
|
| 282 |
+
raise TypeError("must pass an ndarray-like")
|
| 283 |
+
|
| 284 |
+
if isinstance(vals, ABCExtensionArray):
|
| 285 |
+
return vals._hash_pandas_object(
|
| 286 |
+
encoding=encoding, hash_key=hash_key, categorize=categorize
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
if not isinstance(vals, np.ndarray):
|
| 290 |
+
# GH#42003
|
| 291 |
+
raise TypeError(
|
| 292 |
+
"hash_array requires np.ndarray or ExtensionArray, not "
|
| 293 |
+
f"{type(vals).__name__}. Use hash_pandas_object instead."
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
return _hash_ndarray(vals, encoding, hash_key, categorize)
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
def _hash_ndarray(
|
| 300 |
+
vals: np.ndarray,
|
| 301 |
+
encoding: str = "utf8",
|
| 302 |
+
hash_key: str = _default_hash_key,
|
| 303 |
+
categorize: bool = True,
|
| 304 |
+
) -> npt.NDArray[np.uint64]:
|
| 305 |
+
"""
|
| 306 |
+
See hash_array.__doc__.
|
| 307 |
+
"""
|
| 308 |
+
dtype = vals.dtype
|
| 309 |
+
|
| 310 |
+
# _hash_ndarray only takes 64-bit values, so handle 128-bit by parts
|
| 311 |
+
if np.issubdtype(dtype, np.complex128):
|
| 312 |
+
hash_real = _hash_ndarray(vals.real, encoding, hash_key, categorize)
|
| 313 |
+
hash_imag = _hash_ndarray(vals.imag, encoding, hash_key, categorize)
|
| 314 |
+
return hash_real + 23 * hash_imag
|
| 315 |
+
|
| 316 |
+
# First, turn whatever array this is into unsigned 64-bit ints, if we can
|
| 317 |
+
# manage it.
|
| 318 |
+
if dtype == bool:
|
| 319 |
+
vals = vals.astype("u8")
|
| 320 |
+
elif issubclass(dtype.type, (np.datetime64, np.timedelta64)):
|
| 321 |
+
vals = vals.view("i8").astype("u8", copy=False)
|
| 322 |
+
elif issubclass(dtype.type, np.number) and dtype.itemsize <= 8:
|
| 323 |
+
vals = vals.view(f"u{vals.dtype.itemsize}").astype("u8")
|
| 324 |
+
else:
|
| 325 |
+
# With repeated values, its MUCH faster to categorize object dtypes,
|
| 326 |
+
# then hash and rename categories. We allow skipping the categorization
|
| 327 |
+
# when the values are known/likely to be unique.
|
| 328 |
+
if categorize:
|
| 329 |
+
from pandas import (
|
| 330 |
+
Categorical,
|
| 331 |
+
Index,
|
| 332 |
+
factorize,
|
| 333 |
+
)
|
| 334 |
+
|
| 335 |
+
codes, categories = factorize(vals, sort=False)
|
| 336 |
+
tdtype = CategoricalDtype(
|
| 337 |
+
categories=Index(categories, copy=False), ordered=False
|
| 338 |
+
)
|
| 339 |
+
cat = Categorical._simple_new(codes, tdtype)
|
| 340 |
+
return cat._hash_pandas_object(
|
| 341 |
+
encoding=encoding, hash_key=hash_key, categorize=False
|
| 342 |
+
)
|
| 343 |
+
|
| 344 |
+
try:
|
| 345 |
+
vals = hash_object_array(vals, hash_key, encoding)
|
| 346 |
+
except TypeError:
|
| 347 |
+
# we have mixed types
|
| 348 |
+
vals = hash_object_array(
|
| 349 |
+
vals.astype(str).astype(object), hash_key, encoding
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
+
# Then, redistribute these 64-bit ints within the space of 64-bit ints
|
| 353 |
+
vals ^= vals >> 30
|
| 354 |
+
vals *= np.uint64(0xBF58476D1CE4E5B9)
|
| 355 |
+
vals ^= vals >> 27
|
| 356 |
+
vals *= np.uint64(0x94D049BB133111EB)
|
| 357 |
+
vals ^= vals >> 31
|
| 358 |
+
# error: Incompatible return value type (got "Any | ndarray[tuple[int, ...],
|
| 359 |
+
# dtype[signedinteger[Any]]]", expected "ndarray[tuple[int, ...],
|
| 360 |
+
# dtype[unsignedinteger[_64Bit]]]")
|
| 361 |
+
return vals # type: ignore[return-value]
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/util/numba_.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Common utilities for Numba operations"""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import inspect
|
| 6 |
+
import types
|
| 7 |
+
from typing import TYPE_CHECKING
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
from pandas.compat._optional import import_optional_dependency
|
| 12 |
+
from pandas.errors import NumbaUtilError
|
| 13 |
+
|
| 14 |
+
if TYPE_CHECKING:
|
| 15 |
+
from collections.abc import Callable
|
| 16 |
+
|
| 17 |
+
GLOBAL_USE_NUMBA: bool = False
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def maybe_use_numba(engine: str | None) -> bool:
|
| 21 |
+
"""Signal whether to use numba routines."""
|
| 22 |
+
return engine == "numba" or (engine is None and GLOBAL_USE_NUMBA)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def set_use_numba(enable: bool = False) -> None:
|
| 26 |
+
global GLOBAL_USE_NUMBA
|
| 27 |
+
if enable:
|
| 28 |
+
import_optional_dependency("numba")
|
| 29 |
+
GLOBAL_USE_NUMBA = enable
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def get_jit_arguments(engine_kwargs: dict[str, bool] | None = None) -> dict[str, bool]:
|
| 33 |
+
"""
|
| 34 |
+
Return arguments to pass to numba.JIT, falling back on pandas default JIT settings.
|
| 35 |
+
|
| 36 |
+
Parameters
|
| 37 |
+
----------
|
| 38 |
+
engine_kwargs : dict, default None
|
| 39 |
+
user passed keyword arguments for numba.JIT
|
| 40 |
+
|
| 41 |
+
Returns
|
| 42 |
+
-------
|
| 43 |
+
dict[str, bool]
|
| 44 |
+
nopython, nogil, parallel
|
| 45 |
+
|
| 46 |
+
Raises
|
| 47 |
+
------
|
| 48 |
+
NumbaUtilError
|
| 49 |
+
"""
|
| 50 |
+
if engine_kwargs is None:
|
| 51 |
+
engine_kwargs = {}
|
| 52 |
+
|
| 53 |
+
nopython = engine_kwargs.get("nopython", True)
|
| 54 |
+
nogil = engine_kwargs.get("nogil", False)
|
| 55 |
+
parallel = engine_kwargs.get("parallel", False)
|
| 56 |
+
return {"nopython": nopython, "nogil": nogil, "parallel": parallel}
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def jit_user_function(func: Callable) -> Callable:
|
| 60 |
+
"""
|
| 61 |
+
If user function is not jitted already, mark the user's function
|
| 62 |
+
as jitable.
|
| 63 |
+
|
| 64 |
+
Parameters
|
| 65 |
+
----------
|
| 66 |
+
func : function
|
| 67 |
+
user defined function
|
| 68 |
+
|
| 69 |
+
Returns
|
| 70 |
+
-------
|
| 71 |
+
function
|
| 72 |
+
Numba JITed function, or function marked as JITable by numba
|
| 73 |
+
"""
|
| 74 |
+
if TYPE_CHECKING:
|
| 75 |
+
import numba
|
| 76 |
+
else:
|
| 77 |
+
numba = import_optional_dependency("numba")
|
| 78 |
+
|
| 79 |
+
if numba.extending.is_jitted(func):
|
| 80 |
+
# Don't jit a user passed jitted function
|
| 81 |
+
numba_func = func
|
| 82 |
+
elif getattr(np, func.__name__, False) is func or isinstance(
|
| 83 |
+
func, types.BuiltinFunctionType
|
| 84 |
+
):
|
| 85 |
+
# Not necessary to jit builtins or np functions
|
| 86 |
+
# This will mess up register_jitable
|
| 87 |
+
numba_func = func
|
| 88 |
+
else:
|
| 89 |
+
numba_func = numba.extending.register_jitable(func)
|
| 90 |
+
|
| 91 |
+
return numba_func
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
_sentinel = object()
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def prepare_function_arguments(
|
| 98 |
+
func: Callable, args: tuple, kwargs: dict, *, num_required_args: int
|
| 99 |
+
) -> tuple[tuple, dict]:
|
| 100 |
+
"""
|
| 101 |
+
Prepare arguments for jitted function. As numba functions do not support kwargs,
|
| 102 |
+
we try to move kwargs into args if possible.
|
| 103 |
+
|
| 104 |
+
Parameters
|
| 105 |
+
----------
|
| 106 |
+
func : function
|
| 107 |
+
User defined function
|
| 108 |
+
args : tuple
|
| 109 |
+
User input positional arguments
|
| 110 |
+
kwargs : dict
|
| 111 |
+
User input keyword arguments
|
| 112 |
+
num_required_args : int
|
| 113 |
+
The number of leading positional arguments we will pass to udf.
|
| 114 |
+
These are not supplied by the user.
|
| 115 |
+
e.g. for groupby we require "values", "index" as the first two arguments:
|
| 116 |
+
`numba_func(group, group_index, *args)`, in this case num_required_args=2.
|
| 117 |
+
See :func:`pandas.core.groupby.numba_.generate_numba_agg_func`
|
| 118 |
+
|
| 119 |
+
Returns
|
| 120 |
+
-------
|
| 121 |
+
tuple[tuple, dict]
|
| 122 |
+
args, kwargs
|
| 123 |
+
|
| 124 |
+
"""
|
| 125 |
+
if not kwargs:
|
| 126 |
+
return args, kwargs
|
| 127 |
+
|
| 128 |
+
# the udf should have this pattern: def udf(arg1, arg2, ..., *args, **kwargs):...
|
| 129 |
+
signature = inspect.signature(func)
|
| 130 |
+
arguments = signature.bind(*[_sentinel] * num_required_args, *args, **kwargs)
|
| 131 |
+
arguments.apply_defaults()
|
| 132 |
+
# Ref: https://peps.python.org/pep-0362/
|
| 133 |
+
# Arguments which could be passed as part of either *args or **kwargs
|
| 134 |
+
# will be included only in the BoundArguments.args attribute.
|
| 135 |
+
args = arguments.args
|
| 136 |
+
kwargs = arguments.kwargs
|
| 137 |
+
|
| 138 |
+
if kwargs:
|
| 139 |
+
# Note: in case numba supports keyword-only arguments in
|
| 140 |
+
# a future version, we should remove this check. But this
|
| 141 |
+
# seems unlikely to happen soon.
|
| 142 |
+
|
| 143 |
+
raise NumbaUtilError(
|
| 144 |
+
"numba does not support keyword-only arguments"
|
| 145 |
+
"https://github.com/numba/numba/issues/2916, "
|
| 146 |
+
"https://github.com/numba/numba/issues/6846"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
args = args[num_required_args:]
|
| 150 |
+
return args, kwargs
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__init__.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pandas.core.window.ewm import (
|
| 2 |
+
ExponentialMovingWindow,
|
| 3 |
+
ExponentialMovingWindowGroupby,
|
| 4 |
+
)
|
| 5 |
+
from pandas.core.window.expanding import (
|
| 6 |
+
Expanding,
|
| 7 |
+
ExpandingGroupby,
|
| 8 |
+
)
|
| 9 |
+
from pandas.core.window.rolling import (
|
| 10 |
+
Rolling,
|
| 11 |
+
RollingGroupby,
|
| 12 |
+
Window,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
__all__ = [
|
| 16 |
+
"Expanding",
|
| 17 |
+
"ExpandingGroupby",
|
| 18 |
+
"ExponentialMovingWindow",
|
| 19 |
+
"ExponentialMovingWindowGroupby",
|
| 20 |
+
"Rolling",
|
| 21 |
+
"RollingGroupby",
|
| 22 |
+
"Window",
|
| 23 |
+
]
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (572 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__pycache__/common.cpython-312.pyc
ADDED
|
Binary file (7.39 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__pycache__/ewm.cpython-312.pyc
ADDED
|
Binary file (43.4 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__pycache__/expanding.cpython-312.pyc
ADDED
|
Binary file (46.5 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__pycache__/numba_.cpython-312.pyc
ADDED
|
Binary file (12 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__pycache__/online.cpython-312.pyc
ADDED
|
Binary file (4.74 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/__pycache__/rolling.cpython-312.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:70088500f56df9b6682b05064ac45189360038bb0cfa7de4be5c66e63d10992d
|
| 3 |
+
size 125596
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/common.py
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Common utility functions for rolling operations"""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from collections import defaultdict
|
| 6 |
+
from typing import cast
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
from pandas.core.dtypes.generic import (
|
| 11 |
+
ABCDataFrame,
|
| 12 |
+
ABCSeries,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
from pandas.core.indexes.api import MultiIndex
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def flex_binary_moment(arg1, arg2, f, pairwise: bool = False):
|
| 19 |
+
if isinstance(arg1, ABCSeries) and isinstance(arg2, ABCSeries):
|
| 20 |
+
X, Y = prep_binary(arg1, arg2)
|
| 21 |
+
return f(X, Y)
|
| 22 |
+
|
| 23 |
+
elif isinstance(arg1, ABCDataFrame):
|
| 24 |
+
from pandas import DataFrame
|
| 25 |
+
|
| 26 |
+
def dataframe_from_int_dict(data, frame_template) -> DataFrame:
|
| 27 |
+
result = DataFrame(data, index=frame_template.index)
|
| 28 |
+
if len(result.columns) > 0:
|
| 29 |
+
result.columns = frame_template.columns[result.columns]
|
| 30 |
+
else:
|
| 31 |
+
result.columns = frame_template.columns.copy()
|
| 32 |
+
return result
|
| 33 |
+
|
| 34 |
+
results = {}
|
| 35 |
+
if isinstance(arg2, ABCDataFrame):
|
| 36 |
+
if pairwise is False:
|
| 37 |
+
if arg1 is arg2:
|
| 38 |
+
# special case in order to handle duplicate column names
|
| 39 |
+
for i in range(len(arg1.columns)):
|
| 40 |
+
results[i] = f(arg1.iloc[:, i], arg2.iloc[:, i])
|
| 41 |
+
return dataframe_from_int_dict(results, arg1)
|
| 42 |
+
else:
|
| 43 |
+
if not arg1.columns.is_unique:
|
| 44 |
+
raise ValueError("'arg1' columns are not unique")
|
| 45 |
+
if not arg2.columns.is_unique:
|
| 46 |
+
raise ValueError("'arg2' columns are not unique")
|
| 47 |
+
X, Y = arg1.align(arg2, join="outer")
|
| 48 |
+
X, Y = prep_binary(X, Y)
|
| 49 |
+
res_columns = arg1.columns.union(arg2.columns)
|
| 50 |
+
for col in res_columns:
|
| 51 |
+
if col in X and col in Y:
|
| 52 |
+
results[col] = f(X[col], Y[col])
|
| 53 |
+
return DataFrame(results, index=X.index, columns=res_columns)
|
| 54 |
+
elif pairwise is True:
|
| 55 |
+
results = defaultdict(dict)
|
| 56 |
+
for i in range(len(arg1.columns)):
|
| 57 |
+
for j in range(len(arg2.columns)):
|
| 58 |
+
if j < i and arg2 is arg1:
|
| 59 |
+
# Symmetric case
|
| 60 |
+
results[i][j] = results[j][i]
|
| 61 |
+
else:
|
| 62 |
+
results[i][j] = f(
|
| 63 |
+
*prep_binary(arg1.iloc[:, i], arg2.iloc[:, j])
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
from pandas import concat
|
| 67 |
+
|
| 68 |
+
result_index = arg1.index.union(arg2.index)
|
| 69 |
+
if len(result_index):
|
| 70 |
+
# construct result frame
|
| 71 |
+
result = concat(
|
| 72 |
+
[
|
| 73 |
+
concat(
|
| 74 |
+
[results[i][j] for j in range(len(arg2.columns))],
|
| 75 |
+
ignore_index=True,
|
| 76 |
+
)
|
| 77 |
+
for i in range(len(arg1.columns))
|
| 78 |
+
],
|
| 79 |
+
ignore_index=True,
|
| 80 |
+
axis=1,
|
| 81 |
+
)
|
| 82 |
+
result.columns = arg1.columns
|
| 83 |
+
|
| 84 |
+
# set the index and reorder
|
| 85 |
+
if arg2.columns.nlevels > 1:
|
| 86 |
+
# mypy needs to know columns is a MultiIndex, Index doesn't
|
| 87 |
+
# have levels attribute
|
| 88 |
+
arg2.columns = cast(MultiIndex, arg2.columns)
|
| 89 |
+
# GH 21157: Equivalent to MultiIndex.from_product(
|
| 90 |
+
# [result_index], <unique combinations of arg2.columns.levels>,
|
| 91 |
+
# )
|
| 92 |
+
# A normal MultiIndex.from_product will produce too many
|
| 93 |
+
# combinations.
|
| 94 |
+
result_level = np.tile(
|
| 95 |
+
result_index, len(result) // len(result_index)
|
| 96 |
+
)
|
| 97 |
+
arg2_levels = (
|
| 98 |
+
np.repeat(
|
| 99 |
+
arg2.columns.get_level_values(i),
|
| 100 |
+
len(result) // len(arg2.columns),
|
| 101 |
+
)
|
| 102 |
+
for i in range(arg2.columns.nlevels)
|
| 103 |
+
)
|
| 104 |
+
result_names = [*arg2.columns.names, result_index.name]
|
| 105 |
+
result.index = MultiIndex.from_arrays(
|
| 106 |
+
[*arg2_levels, result_level], names=result_names
|
| 107 |
+
)
|
| 108 |
+
# GH 34440
|
| 109 |
+
num_levels = len(
|
| 110 |
+
result.index.levels # pyright: ignore[reportAttributeAccessIssue]
|
| 111 |
+
)
|
| 112 |
+
new_order = [num_levels - 1, *range(num_levels - 1)]
|
| 113 |
+
result = result.reorder_levels(new_order).sort_index()
|
| 114 |
+
else:
|
| 115 |
+
result.index = MultiIndex.from_product(
|
| 116 |
+
[range(len(arg2.columns)), range(len(result_index))]
|
| 117 |
+
)
|
| 118 |
+
result = result.swaplevel(1, 0).sort_index()
|
| 119 |
+
result.index = MultiIndex.from_product(
|
| 120 |
+
[result_index, arg2.columns]
|
| 121 |
+
)
|
| 122 |
+
else:
|
| 123 |
+
# empty result
|
| 124 |
+
result = DataFrame(
|
| 125 |
+
index=MultiIndex(
|
| 126 |
+
levels=[arg1.index, arg2.columns], codes=[[], []]
|
| 127 |
+
),
|
| 128 |
+
columns=arg2.columns,
|
| 129 |
+
dtype="float64",
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
# reset our index names to arg1 names
|
| 133 |
+
# reset our column names to arg2 names
|
| 134 |
+
# careful not to mutate the original names
|
| 135 |
+
result.columns = result.columns.set_names(arg1.columns.names)
|
| 136 |
+
result.index = result.index.set_names(
|
| 137 |
+
result_index.names + arg2.columns.names
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
return result
|
| 141 |
+
else:
|
| 142 |
+
results = {
|
| 143 |
+
i: f(*prep_binary(arg1.iloc[:, i], arg2))
|
| 144 |
+
for i in range(len(arg1.columns))
|
| 145 |
+
}
|
| 146 |
+
return dataframe_from_int_dict(results, arg1)
|
| 147 |
+
|
| 148 |
+
else:
|
| 149 |
+
return flex_binary_moment(arg2, arg1, f)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def zsqrt(x):
|
| 153 |
+
with np.errstate(all="ignore"):
|
| 154 |
+
result = np.sqrt(x)
|
| 155 |
+
mask = x < 0
|
| 156 |
+
|
| 157 |
+
if isinstance(x, ABCDataFrame):
|
| 158 |
+
if mask._values.any():
|
| 159 |
+
result[mask] = 0
|
| 160 |
+
elif mask.any():
|
| 161 |
+
result[mask] = 0
|
| 162 |
+
|
| 163 |
+
return result
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def prep_binary(arg1, arg2):
|
| 167 |
+
# mask out values, this also makes a common index...
|
| 168 |
+
X = arg1 + 0 * arg2
|
| 169 |
+
Y = arg2 + 0 * arg1
|
| 170 |
+
|
| 171 |
+
return X, Y
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/doc.py
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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rolling/expanding/ewm"""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from textwrap import dedent
|
| 6 |
+
|
| 7 |
+
from pandas.core.shared_docs import _shared_docs
|
| 8 |
+
|
| 9 |
+
_shared_docs = dict(**_shared_docs)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def create_section_header(header: str) -> str:
|
| 13 |
+
"""Create numpydoc section header"""
|
| 14 |
+
return f"{header}\n{'-' * len(header)}\n"
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
template_header = "\nCalculate the {window_method} {aggregation_description}.\n\n"
|
| 18 |
+
|
| 19 |
+
template_returns = dedent(
|
| 20 |
+
"""
|
| 21 |
+
Series or DataFrame
|
| 22 |
+
Return type is the same as the original object with ``np.float64`` dtype.\n
|
| 23 |
+
"""
|
| 24 |
+
).replace("\n", "", 1)
|
| 25 |
+
|
| 26 |
+
template_see_also = dedent(
|
| 27 |
+
"""
|
| 28 |
+
Series.{window_method} : Calling {window_method} with Series data.
|
| 29 |
+
DataFrame.{window_method} : Calling {window_method} with DataFrames.
|
| 30 |
+
Series.{agg_method} : Aggregating {agg_method} for Series.
|
| 31 |
+
DataFrame.{agg_method} : Aggregating {agg_method} for DataFrame.\n
|
| 32 |
+
"""
|
| 33 |
+
).replace("\n", "", 1)
|
| 34 |
+
|
| 35 |
+
kwargs_numeric_only = dedent(
|
| 36 |
+
"""
|
| 37 |
+
numeric_only : bool, default False
|
| 38 |
+
Include only float, int, boolean columns.
|
| 39 |
+
|
| 40 |
+
.. versionadded:: 1.5.0\n
|
| 41 |
+
"""
|
| 42 |
+
).replace("\n", "", 1)
|
| 43 |
+
|
| 44 |
+
kwargs_scipy = dedent(
|
| 45 |
+
"""
|
| 46 |
+
**kwargs
|
| 47 |
+
Keyword arguments to configure the ``SciPy`` weighted window type.\n
|
| 48 |
+
"""
|
| 49 |
+
).replace("\n", "", 1)
|
| 50 |
+
|
| 51 |
+
window_apply_parameters = dedent(
|
| 52 |
+
"""
|
| 53 |
+
func : function
|
| 54 |
+
Must produce a single value from an ndarray input if ``raw=True``
|
| 55 |
+
or a single value from a Series if ``raw=False``. Can also accept a
|
| 56 |
+
Numba JIT function with ``engine='numba'`` specified.
|
| 57 |
+
|
| 58 |
+
raw : bool, default False
|
| 59 |
+
* ``False`` : passes each row or column as a Series to the
|
| 60 |
+
function.
|
| 61 |
+
* ``True`` : the passed function will receive ndarray
|
| 62 |
+
objects instead.
|
| 63 |
+
If you are just applying a NumPy reduction function this will
|
| 64 |
+
achieve much better performance.
|
| 65 |
+
|
| 66 |
+
engine : str, default None
|
| 67 |
+
* ``'cython'`` : Runs rolling apply through C-extensions from cython.
|
| 68 |
+
* ``'numba'`` : Runs rolling apply through JIT compiled code from numba.
|
| 69 |
+
Only available when ``raw`` is set to ``True``.
|
| 70 |
+
* ``None`` : Defaults to ``'cython'`` or globally setting ``compute.use_numba``
|
| 71 |
+
|
| 72 |
+
engine_kwargs : dict, default None
|
| 73 |
+
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
|
| 74 |
+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
|
| 75 |
+
and ``parallel`` dictionary keys. The values must either be ``True`` or
|
| 76 |
+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
|
| 77 |
+
``{{'nopython': True, 'nogil': False, 'parallel': False}}`` and will be
|
| 78 |
+
applied to both the ``func`` and the ``apply`` rolling aggregation.
|
| 79 |
+
|
| 80 |
+
args : tuple, default None
|
| 81 |
+
Positional arguments to be passed into func.
|
| 82 |
+
|
| 83 |
+
kwargs : dict, default None
|
| 84 |
+
Keyword arguments to be passed into func.\n
|
| 85 |
+
"""
|
| 86 |
+
).replace("\n", "", 1)
|
| 87 |
+
|
| 88 |
+
template_pipe = """
|
| 89 |
+
Apply a ``func`` with arguments to this %(klass)s object and return its result.
|
| 90 |
+
|
| 91 |
+
Use `.pipe` when you want to improve readability by chaining together
|
| 92 |
+
functions that expect Series, DataFrames, GroupBy, Rolling, Expanding or Resampler
|
| 93 |
+
objects.
|
| 94 |
+
Instead of writing
|
| 95 |
+
|
| 96 |
+
>>> h = lambda x, arg2, arg3: x + 1 - arg2 * arg3
|
| 97 |
+
>>> g = lambda x, arg1: x * 5 / arg1
|
| 98 |
+
>>> f = lambda x: x ** 4
|
| 99 |
+
>>> df = pd.DataFrame({'A': [1, 2, 3, 4]}, index=pd.date_range('2012-08-02', periods=4))
|
| 100 |
+
>>> h(g(f(df.rolling('2D')), arg1=1), arg2=2, arg3=3) # doctest: +SKIP
|
| 101 |
+
|
| 102 |
+
You can write
|
| 103 |
+
|
| 104 |
+
>>> (df.rolling('2D')
|
| 105 |
+
... .pipe(f)
|
| 106 |
+
... .pipe(g, arg1=1)
|
| 107 |
+
... .pipe(h, arg2=2, arg3=3)) # doctest: +SKIP
|
| 108 |
+
|
| 109 |
+
which is much more readable.
|
| 110 |
+
|
| 111 |
+
Parameters
|
| 112 |
+
----------
|
| 113 |
+
func : callable or tuple of (callable, str)
|
| 114 |
+
Function to apply to this %(klass)s object or, alternatively,
|
| 115 |
+
a `(callable, data_keyword)` tuple where `data_keyword` is a
|
| 116 |
+
string indicating the keyword of `callable` that expects the
|
| 117 |
+
%(klass)s object.
|
| 118 |
+
*args : iterable, optional
|
| 119 |
+
Positional arguments passed into `func`.
|
| 120 |
+
**kwargs : dict, optional
|
| 121 |
+
A dictionary of keyword arguments passed into `func`.
|
| 122 |
+
|
| 123 |
+
Returns
|
| 124 |
+
-------
|
| 125 |
+
%(klass)s
|
| 126 |
+
The original object with the function `func` applied.
|
| 127 |
+
|
| 128 |
+
See Also
|
| 129 |
+
--------
|
| 130 |
+
Series.pipe : Apply a function with arguments to a series.
|
| 131 |
+
DataFrame.pipe: Apply a function with arguments to a dataframe.
|
| 132 |
+
apply : Apply function to each group instead of to the
|
| 133 |
+
full %(klass)s object.
|
| 134 |
+
|
| 135 |
+
Notes
|
| 136 |
+
-----
|
| 137 |
+
See more `here
|
| 138 |
+
<https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#piping-function-calls>`_
|
| 139 |
+
|
| 140 |
+
Examples
|
| 141 |
+
--------
|
| 142 |
+
%(examples)s
|
| 143 |
+
"""
|
| 144 |
+
|
| 145 |
+
numba_notes = (
|
| 146 |
+
"See :ref:`window.numba_engine` and :ref:`enhancingperf.numba` for "
|
| 147 |
+
"extended documentation and performance considerations for the Numba engine.\n\n"
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def window_agg_numba_parameters(version: str = "1.3") -> str:
|
| 152 |
+
return (
|
| 153 |
+
dedent(
|
| 154 |
+
"""
|
| 155 |
+
engine : str, default None
|
| 156 |
+
* ``'cython'`` : Runs the operation through C-extensions from cython.
|
| 157 |
+
* ``'numba'`` : Runs the operation through JIT compiled code from numba.
|
| 158 |
+
* ``None`` : Defaults to ``'cython'`` or globally setting ``compute.use_numba``
|
| 159 |
+
|
| 160 |
+
.. versionadded:: {version}.0
|
| 161 |
+
|
| 162 |
+
engine_kwargs : dict, default None
|
| 163 |
+
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
|
| 164 |
+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
|
| 165 |
+
and ``parallel`` dictionary keys. The values must either be ``True`` or
|
| 166 |
+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
|
| 167 |
+
``{{'nopython': True, 'nogil': False, 'parallel': False}}``
|
| 168 |
+
|
| 169 |
+
.. versionadded:: {version}.0\n
|
| 170 |
+
"""
|
| 171 |
+
)
|
| 172 |
+
.replace("\n", "", 1)
|
| 173 |
+
.replace("{version}", version)
|
| 174 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/ewm.py
ADDED
|
@@ -0,0 +1,1175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import datetime
|
| 4 |
+
from functools import partial
|
| 5 |
+
from typing import (
|
| 6 |
+
TYPE_CHECKING,
|
| 7 |
+
cast,
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
from pandas._libs.tslibs import Timedelta
|
| 13 |
+
import pandas._libs.window.aggregations as window_aggregations
|
| 14 |
+
from pandas.util._decorators import set_module
|
| 15 |
+
|
| 16 |
+
from pandas.core.dtypes.common import (
|
| 17 |
+
is_datetime64_dtype,
|
| 18 |
+
is_numeric_dtype,
|
| 19 |
+
)
|
| 20 |
+
from pandas.core.dtypes.dtypes import DatetimeTZDtype
|
| 21 |
+
from pandas.core.dtypes.generic import ABCSeries
|
| 22 |
+
from pandas.core.dtypes.missing import isna
|
| 23 |
+
|
| 24 |
+
from pandas.core import common
|
| 25 |
+
from pandas.core.arrays.datetimelike import dtype_to_unit
|
| 26 |
+
from pandas.core.indexers.objects import (
|
| 27 |
+
BaseIndexer,
|
| 28 |
+
ExponentialMovingWindowIndexer,
|
| 29 |
+
GroupbyIndexer,
|
| 30 |
+
)
|
| 31 |
+
from pandas.core.util.numba_ import (
|
| 32 |
+
get_jit_arguments,
|
| 33 |
+
maybe_use_numba,
|
| 34 |
+
)
|
| 35 |
+
from pandas.core.window.common import zsqrt
|
| 36 |
+
from pandas.core.window.numba_ import (
|
| 37 |
+
generate_numba_ewm_func,
|
| 38 |
+
generate_numba_ewm_table_func,
|
| 39 |
+
)
|
| 40 |
+
from pandas.core.window.online import (
|
| 41 |
+
EWMMeanState,
|
| 42 |
+
generate_online_numba_ewma_func,
|
| 43 |
+
)
|
| 44 |
+
from pandas.core.window.rolling import (
|
| 45 |
+
BaseWindow,
|
| 46 |
+
BaseWindowGroupby,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
if TYPE_CHECKING:
|
| 50 |
+
from pandas._typing import (
|
| 51 |
+
TimedeltaConvertibleTypes,
|
| 52 |
+
TimeUnit,
|
| 53 |
+
npt,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
from pandas import (
|
| 57 |
+
DataFrame,
|
| 58 |
+
Series,
|
| 59 |
+
)
|
| 60 |
+
from pandas.core.generic import NDFrame
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def get_center_of_mass(
|
| 64 |
+
comass: float | None,
|
| 65 |
+
span: float | None,
|
| 66 |
+
halflife: float | None,
|
| 67 |
+
alpha: float | None,
|
| 68 |
+
) -> float:
|
| 69 |
+
valid_count = common.count_not_none(comass, span, halflife, alpha)
|
| 70 |
+
if valid_count > 1:
|
| 71 |
+
raise ValueError("comass, span, halflife, and alpha are mutually exclusive")
|
| 72 |
+
|
| 73 |
+
# Convert to center of mass; domain checks ensure 0 < alpha <= 1
|
| 74 |
+
if comass is not None:
|
| 75 |
+
if comass < 0:
|
| 76 |
+
raise ValueError("comass must satisfy: comass >= 0")
|
| 77 |
+
elif span is not None:
|
| 78 |
+
if span < 1:
|
| 79 |
+
raise ValueError("span must satisfy: span >= 1")
|
| 80 |
+
comass = (span - 1) / 2
|
| 81 |
+
elif halflife is not None:
|
| 82 |
+
if halflife <= 0:
|
| 83 |
+
raise ValueError("halflife must satisfy: halflife > 0")
|
| 84 |
+
decay = 1 - np.exp(np.log(0.5) / halflife)
|
| 85 |
+
comass = 1 / decay - 1
|
| 86 |
+
elif alpha is not None:
|
| 87 |
+
if alpha <= 0 or alpha > 1:
|
| 88 |
+
raise ValueError("alpha must satisfy: 0 < alpha <= 1")
|
| 89 |
+
comass = (1 - alpha) / alpha
|
| 90 |
+
else:
|
| 91 |
+
raise ValueError("Must pass one of comass, span, halflife, or alpha")
|
| 92 |
+
|
| 93 |
+
return float(comass)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def _calculate_deltas(
|
| 97 |
+
times: np.ndarray | NDFrame,
|
| 98 |
+
halflife: float | TimedeltaConvertibleTypes | None,
|
| 99 |
+
) -> npt.NDArray[np.float64]:
|
| 100 |
+
"""
|
| 101 |
+
Return the diff of the times divided by the half-life. These values are used in
|
| 102 |
+
the calculation of the ewm mean.
|
| 103 |
+
|
| 104 |
+
Parameters
|
| 105 |
+
----------
|
| 106 |
+
times : np.ndarray, Series
|
| 107 |
+
Times corresponding to the observations. Must be monotonically increasing
|
| 108 |
+
and ``datetime64[ns]`` dtype.
|
| 109 |
+
halflife : float, str, timedelta, optional
|
| 110 |
+
Half-life specifying the decay
|
| 111 |
+
|
| 112 |
+
Returns
|
| 113 |
+
-------
|
| 114 |
+
np.ndarray
|
| 115 |
+
Diff of the times divided by the half-life
|
| 116 |
+
"""
|
| 117 |
+
unit = dtype_to_unit(times.dtype)
|
| 118 |
+
unit = cast("TimeUnit", unit)
|
| 119 |
+
if isinstance(times, ABCSeries):
|
| 120 |
+
times = times._values
|
| 121 |
+
_times = np.asarray(times.view(np.int64), dtype=np.float64)
|
| 122 |
+
_halflife = float(Timedelta(halflife).as_unit(unit)._value)
|
| 123 |
+
return np.diff(_times) / _halflife
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
@set_module("pandas.api.typing")
|
| 127 |
+
class ExponentialMovingWindow(BaseWindow):
|
| 128 |
+
r"""
|
| 129 |
+
Provide exponentially weighted (EW) calculations.
|
| 130 |
+
|
| 131 |
+
Exactly one of ``com``, ``span``, ``halflife``, or ``alpha`` must be
|
| 132 |
+
provided if ``times`` is not provided. If ``times`` is provided and ``adjust=True``,
|
| 133 |
+
``halflife`` and one of ``com``, ``span`` or ``alpha`` may be provided.
|
| 134 |
+
If ``times`` is provided and ``adjust=False``, ``halflife`` must be the only
|
| 135 |
+
provided decay-specification parameter.
|
| 136 |
+
|
| 137 |
+
Parameters
|
| 138 |
+
----------
|
| 139 |
+
com : float, optional
|
| 140 |
+
Specify decay in terms of center of mass
|
| 141 |
+
|
| 142 |
+
:math:`\alpha = 1 / (1 + com)`, for :math:`com \geq 0`.
|
| 143 |
+
|
| 144 |
+
span : float, optional
|
| 145 |
+
Specify decay in terms of span
|
| 146 |
+
|
| 147 |
+
:math:`\alpha = 2 / (span + 1)`, for :math:`span \geq 1`.
|
| 148 |
+
|
| 149 |
+
halflife : float, str, timedelta, optional
|
| 150 |
+
Specify decay in terms of half-life
|
| 151 |
+
|
| 152 |
+
:math:`\alpha = 1 - \exp\left(-\ln(2) / halflife\right)`, for
|
| 153 |
+
:math:`halflife > 0`.
|
| 154 |
+
|
| 155 |
+
If ``times`` is specified, a timedelta convertible unit over which an
|
| 156 |
+
observation decays to half its value. Only applicable to ``mean()``,
|
| 157 |
+
and halflife value will not apply to the other functions.
|
| 158 |
+
|
| 159 |
+
alpha : float, optional
|
| 160 |
+
Specify smoothing factor :math:`\alpha` directly
|
| 161 |
+
|
| 162 |
+
:math:`0 < \alpha \leq 1`.
|
| 163 |
+
|
| 164 |
+
min_periods : int, default 0
|
| 165 |
+
Minimum number of observations in window required to have a value;
|
| 166 |
+
otherwise, result is ``np.nan``.
|
| 167 |
+
|
| 168 |
+
adjust : bool, default True
|
| 169 |
+
Divide by decaying adjustment factor in beginning periods to account
|
| 170 |
+
for imbalance in relative weightings (viewing EWMA as a moving average).
|
| 171 |
+
|
| 172 |
+
- When ``adjust=True`` (default), the EW function is calculated using weights
|
| 173 |
+
:math:`w_i = (1 - \alpha)^i`. For example, the EW moving average of the series
|
| 174 |
+
[:math:`x_0, x_1, ..., x_t`] would be:
|
| 175 |
+
|
| 176 |
+
.. math::
|
| 177 |
+
y_t = \frac{x_t + (1 - \alpha)x_{t-1} + (1 - \alpha)^2 x_{t-2} + ... + (1 -
|
| 178 |
+
\alpha)^t x_0}{1 + (1 - \alpha) + (1 - \alpha)^2 + ... + (1 - \alpha)^t}
|
| 179 |
+
|
| 180 |
+
- When ``adjust=False``, the exponentially weighted function is calculated
|
| 181 |
+
recursively:
|
| 182 |
+
|
| 183 |
+
.. math::
|
| 184 |
+
\begin{split}
|
| 185 |
+
y_0 &= x_0\\
|
| 186 |
+
y_t &= (1 - \alpha) y_{t-1} + \alpha x_t,
|
| 187 |
+
\end{split}
|
| 188 |
+
ignore_na : bool, default False
|
| 189 |
+
Ignore missing values when calculating weights.
|
| 190 |
+
|
| 191 |
+
- When ``ignore_na=False`` (default), weights are based on absolute positions.
|
| 192 |
+
For example, the weights of :math:`x_0` and :math:`x_2` used in calculating
|
| 193 |
+
the final weighted average of [:math:`x_0`, None, :math:`x_2`] are
|
| 194 |
+
:math:`(1-\alpha)^2` and :math:`1` if ``adjust=True``, and
|
| 195 |
+
:math:`(1-\alpha)^2` and :math:`\alpha` if ``adjust=False``.
|
| 196 |
+
|
| 197 |
+
- When ``ignore_na=True``, weights are based
|
| 198 |
+
on relative positions. For example, the weights of :math:`x_0` and :math:`x_2`
|
| 199 |
+
used in calculating the final weighted average of
|
| 200 |
+
[:math:`x_0`, None, :math:`x_2`] are :math:`1-\alpha` and :math:`1` if
|
| 201 |
+
``adjust=True``, and :math:`1-\alpha` and :math:`\alpha` if ``adjust=False``.
|
| 202 |
+
|
| 203 |
+
times : np.ndarray, Series, default None
|
| 204 |
+
|
| 205 |
+
Only applicable to ``mean()``.
|
| 206 |
+
|
| 207 |
+
Times corresponding to the observations. Must be monotonically increasing and
|
| 208 |
+
``datetime64[ns]`` dtype.
|
| 209 |
+
|
| 210 |
+
If 1-D array like, a sequence with the same shape as the observations.
|
| 211 |
+
|
| 212 |
+
method : str {'single', 'table'}, default 'single'
|
| 213 |
+
Execute the rolling operation per single column or row (``'single'``)
|
| 214 |
+
or over the entire object (``'table'``).
|
| 215 |
+
|
| 216 |
+
This argument is only implemented when specifying ``engine='numba'``
|
| 217 |
+
in the method call.
|
| 218 |
+
|
| 219 |
+
Only applicable to ``mean()``
|
| 220 |
+
|
| 221 |
+
Returns
|
| 222 |
+
-------
|
| 223 |
+
pandas.api.typing.ExponentialMovingWindow
|
| 224 |
+
An instance of ExponentialMovingWindow for further exponentially weighted (EW)
|
| 225 |
+
calculations, e.g. using the ``mean`` method.
|
| 226 |
+
|
| 227 |
+
See Also
|
| 228 |
+
--------
|
| 229 |
+
rolling : Provides rolling window calculations.
|
| 230 |
+
expanding : Provides expanding transformations.
|
| 231 |
+
|
| 232 |
+
Notes
|
| 233 |
+
-----
|
| 234 |
+
See :ref:`Windowing Operations <window.exponentially_weighted>`
|
| 235 |
+
for further usage details and examples.
|
| 236 |
+
|
| 237 |
+
Examples
|
| 238 |
+
--------
|
| 239 |
+
>>> df = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]})
|
| 240 |
+
>>> df
|
| 241 |
+
B
|
| 242 |
+
0 0.0
|
| 243 |
+
1 1.0
|
| 244 |
+
2 2.0
|
| 245 |
+
3 NaN
|
| 246 |
+
4 4.0
|
| 247 |
+
|
| 248 |
+
>>> df.ewm(com=0.5).mean()
|
| 249 |
+
B
|
| 250 |
+
0 0.000000
|
| 251 |
+
1 0.750000
|
| 252 |
+
2 1.615385
|
| 253 |
+
3 1.615385
|
| 254 |
+
4 3.670213
|
| 255 |
+
>>> df.ewm(alpha=2 / 3).mean()
|
| 256 |
+
B
|
| 257 |
+
0 0.000000
|
| 258 |
+
1 0.750000
|
| 259 |
+
2 1.615385
|
| 260 |
+
3 1.615385
|
| 261 |
+
4 3.670213
|
| 262 |
+
|
| 263 |
+
**adjust**
|
| 264 |
+
|
| 265 |
+
>>> df.ewm(com=0.5, adjust=True).mean()
|
| 266 |
+
B
|
| 267 |
+
0 0.000000
|
| 268 |
+
1 0.750000
|
| 269 |
+
2 1.615385
|
| 270 |
+
3 1.615385
|
| 271 |
+
4 3.670213
|
| 272 |
+
>>> df.ewm(com=0.5, adjust=False).mean()
|
| 273 |
+
B
|
| 274 |
+
0 0.000000
|
| 275 |
+
1 0.666667
|
| 276 |
+
2 1.555556
|
| 277 |
+
3 1.555556
|
| 278 |
+
4 3.650794
|
| 279 |
+
|
| 280 |
+
**ignore_na**
|
| 281 |
+
|
| 282 |
+
>>> df.ewm(com=0.5, ignore_na=True).mean()
|
| 283 |
+
B
|
| 284 |
+
0 0.000000
|
| 285 |
+
1 0.750000
|
| 286 |
+
2 1.615385
|
| 287 |
+
3 1.615385
|
| 288 |
+
4 3.225000
|
| 289 |
+
>>> df.ewm(com=0.5, ignore_na=False).mean()
|
| 290 |
+
B
|
| 291 |
+
0 0.000000
|
| 292 |
+
1 0.750000
|
| 293 |
+
2 1.615385
|
| 294 |
+
3 1.615385
|
| 295 |
+
4 3.670213
|
| 296 |
+
|
| 297 |
+
**times**
|
| 298 |
+
|
| 299 |
+
Exponentially weighted mean with weights calculated with a timedelta ``halflife``
|
| 300 |
+
relative to ``times``.
|
| 301 |
+
|
| 302 |
+
>>> times = ['2020-01-01', '2020-01-03', '2020-01-10', '2020-01-15', '2020-01-17']
|
| 303 |
+
>>> df.ewm(halflife='4 days', times=pd.DatetimeIndex(times)).mean()
|
| 304 |
+
B
|
| 305 |
+
0 0.000000
|
| 306 |
+
1 0.585786
|
| 307 |
+
2 1.523889
|
| 308 |
+
3 1.523889
|
| 309 |
+
4 3.233686
|
| 310 |
+
"""
|
| 311 |
+
|
| 312 |
+
_attributes = [
|
| 313 |
+
"com",
|
| 314 |
+
"span",
|
| 315 |
+
"halflife",
|
| 316 |
+
"alpha",
|
| 317 |
+
"min_periods",
|
| 318 |
+
"adjust",
|
| 319 |
+
"ignore_na",
|
| 320 |
+
"times",
|
| 321 |
+
"method",
|
| 322 |
+
]
|
| 323 |
+
|
| 324 |
+
def __init__(
|
| 325 |
+
self,
|
| 326 |
+
obj: NDFrame,
|
| 327 |
+
com: float | None = None,
|
| 328 |
+
span: float | None = None,
|
| 329 |
+
halflife: float | TimedeltaConvertibleTypes | None = None,
|
| 330 |
+
alpha: float | None = None,
|
| 331 |
+
min_periods: int | None = 0,
|
| 332 |
+
adjust: bool = True,
|
| 333 |
+
ignore_na: bool = False,
|
| 334 |
+
times: np.ndarray | NDFrame | None = None,
|
| 335 |
+
method: str = "single",
|
| 336 |
+
*,
|
| 337 |
+
selection=None,
|
| 338 |
+
) -> None:
|
| 339 |
+
super().__init__(
|
| 340 |
+
obj=obj,
|
| 341 |
+
min_periods=1 if min_periods is None else max(int(min_periods), 1),
|
| 342 |
+
on=None,
|
| 343 |
+
center=False,
|
| 344 |
+
closed=None,
|
| 345 |
+
method=method,
|
| 346 |
+
selection=selection,
|
| 347 |
+
)
|
| 348 |
+
self.com = com
|
| 349 |
+
self.span = span
|
| 350 |
+
self.halflife = halflife
|
| 351 |
+
self.alpha = alpha
|
| 352 |
+
self.adjust = adjust
|
| 353 |
+
self.ignore_na = ignore_na
|
| 354 |
+
self.times = times
|
| 355 |
+
if self.times is not None:
|
| 356 |
+
times_dtype = getattr(self.times, "dtype", None)
|
| 357 |
+
if not (
|
| 358 |
+
is_datetime64_dtype(times_dtype)
|
| 359 |
+
or isinstance(times_dtype, DatetimeTZDtype)
|
| 360 |
+
):
|
| 361 |
+
raise ValueError("times must be datetime64 dtype.")
|
| 362 |
+
if len(self.times) != len(obj):
|
| 363 |
+
raise ValueError("times must be the same length as the object.")
|
| 364 |
+
if not isinstance(self.halflife, (str, datetime.timedelta, np.timedelta64)):
|
| 365 |
+
raise ValueError("halflife must be a timedelta convertible object")
|
| 366 |
+
if isna(self.times).any():
|
| 367 |
+
raise ValueError("Cannot convert NaT values to integer")
|
| 368 |
+
self._deltas = _calculate_deltas(self.times, self.halflife)
|
| 369 |
+
# Halflife is no longer applicable when calculating COM
|
| 370 |
+
# But allow COM to still be calculated if the user passes other decay args
|
| 371 |
+
if common.count_not_none(self.com, self.span, self.alpha) > 0:
|
| 372 |
+
if not self.adjust:
|
| 373 |
+
raise NotImplementedError(
|
| 374 |
+
"None of com, span, or alpha can be specified if "
|
| 375 |
+
"times is provided and adjust=False"
|
| 376 |
+
)
|
| 377 |
+
self._com = get_center_of_mass(self.com, self.span, None, self.alpha)
|
| 378 |
+
else:
|
| 379 |
+
self._com = 1.0
|
| 380 |
+
else:
|
| 381 |
+
if self.halflife is not None and isinstance(
|
| 382 |
+
self.halflife, (str, datetime.timedelta, np.timedelta64)
|
| 383 |
+
):
|
| 384 |
+
raise ValueError(
|
| 385 |
+
"halflife can only be a timedelta convertible argument if "
|
| 386 |
+
"times is not None."
|
| 387 |
+
)
|
| 388 |
+
# Without times, points are equally spaced
|
| 389 |
+
self._deltas = np.ones(max(self.obj.shape[0] - 1, 0), dtype=np.float64)
|
| 390 |
+
self._com = get_center_of_mass(
|
| 391 |
+
# error: Argument 3 to "get_center_of_mass" has incompatible type
|
| 392 |
+
# "Union[float, Any, None, timedelta64, signedinteger[_64Bit]]";
|
| 393 |
+
# expected "Optional[float]"
|
| 394 |
+
self.com,
|
| 395 |
+
self.span,
|
| 396 |
+
self.halflife, # type: ignore[arg-type]
|
| 397 |
+
self.alpha,
|
| 398 |
+
)
|
| 399 |
+
|
| 400 |
+
def _check_window_bounds(
|
| 401 |
+
self, start: np.ndarray, end: np.ndarray, num_vals: int
|
| 402 |
+
) -> None:
|
| 403 |
+
# emw algorithms are iterative with each point
|
| 404 |
+
# ExponentialMovingWindowIndexer "bounds" are the entire window
|
| 405 |
+
pass
|
| 406 |
+
|
| 407 |
+
def _get_window_indexer(self) -> BaseIndexer:
|
| 408 |
+
"""
|
| 409 |
+
Return an indexer class that will compute the window start and end bounds
|
| 410 |
+
"""
|
| 411 |
+
return ExponentialMovingWindowIndexer()
|
| 412 |
+
|
| 413 |
+
def online(
|
| 414 |
+
self, engine: str = "numba", engine_kwargs=None
|
| 415 |
+
) -> OnlineExponentialMovingWindow:
|
| 416 |
+
"""
|
| 417 |
+
Return an ``OnlineExponentialMovingWindow`` object to calculate
|
| 418 |
+
exponentially moving window aggregations in an online method.
|
| 419 |
+
|
| 420 |
+
Parameters
|
| 421 |
+
----------
|
| 422 |
+
engine: str, default ``'numba'``
|
| 423 |
+
Execution engine to calculate online aggregations.
|
| 424 |
+
Applies to all supported aggregation methods.
|
| 425 |
+
|
| 426 |
+
engine_kwargs : dict, default None
|
| 427 |
+
Applies to all supported aggregation methods.
|
| 428 |
+
|
| 429 |
+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
|
| 430 |
+
and ``parallel`` dictionary keys. The values must either be ``True`` or
|
| 431 |
+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
|
| 432 |
+
``{'nopython': True, 'nogil': False, 'parallel': False}`` and will be
|
| 433 |
+
applied to the function
|
| 434 |
+
|
| 435 |
+
Returns
|
| 436 |
+
-------
|
| 437 |
+
OnlineExponentialMovingWindow
|
| 438 |
+
"""
|
| 439 |
+
return OnlineExponentialMovingWindow(
|
| 440 |
+
obj=self.obj,
|
| 441 |
+
com=self.com,
|
| 442 |
+
span=self.span,
|
| 443 |
+
halflife=self.halflife,
|
| 444 |
+
alpha=self.alpha,
|
| 445 |
+
min_periods=self.min_periods,
|
| 446 |
+
adjust=self.adjust,
|
| 447 |
+
ignore_na=self.ignore_na,
|
| 448 |
+
times=self.times,
|
| 449 |
+
engine=engine,
|
| 450 |
+
engine_kwargs=engine_kwargs,
|
| 451 |
+
selection=self._selection,
|
| 452 |
+
)
|
| 453 |
+
|
| 454 |
+
def aggregate(self, func=None, *args, **kwargs):
|
| 455 |
+
"""
|
| 456 |
+
Aggregate using one or more operations over the specified axis.
|
| 457 |
+
|
| 458 |
+
Parameters
|
| 459 |
+
----------
|
| 460 |
+
func : function, str, list or dict
|
| 461 |
+
Function to use for aggregating the data. If a function, must either
|
| 462 |
+
work when passed a Series/Dataframe or when passed to
|
| 463 |
+
Series/Dataframe.apply.
|
| 464 |
+
|
| 465 |
+
Accepted combinations are:
|
| 466 |
+
|
| 467 |
+
- function
|
| 468 |
+
- string function name
|
| 469 |
+
- list of functions and/or function names, e.g. ``[np.sum, 'mean']``
|
| 470 |
+
- dict of axis labels -> functions, function names or list of such.
|
| 471 |
+
*args
|
| 472 |
+
Positional arguments to pass to `func`.
|
| 473 |
+
**kwargs
|
| 474 |
+
Keyword arguments to pass to `func`.
|
| 475 |
+
|
| 476 |
+
Returns
|
| 477 |
+
-------
|
| 478 |
+
scalar, Series or DataFrame
|
| 479 |
+
|
| 480 |
+
The return can be:
|
| 481 |
+
|
| 482 |
+
* scalar : when Series.agg is called with single function
|
| 483 |
+
* Series : when DataFrame.agg is called with a single function
|
| 484 |
+
* DataFrame : when DataFrame.agg is called with several functions
|
| 485 |
+
|
| 486 |
+
See Also
|
| 487 |
+
--------
|
| 488 |
+
pandas.DataFrame.rolling.aggregate
|
| 489 |
+
|
| 490 |
+
Notes
|
| 491 |
+
-----
|
| 492 |
+
The aggregation operations are always performed over an axis, either the
|
| 493 |
+
index (default) or the column axis. This behavior is different from
|
| 494 |
+
`numpy` aggregation functions (`mean`, `median`, `prod`, `sum`, `std`,
|
| 495 |
+
`var`), where the default is to compute the aggregation of the flattened
|
| 496 |
+
array, e.g., ``numpy.mean(arr_2d)`` as opposed to
|
| 497 |
+
``numpy.mean(arr_2d, axis=0)``.
|
| 498 |
+
|
| 499 |
+
`agg` is an alias for `aggregate`. Use the alias.
|
| 500 |
+
|
| 501 |
+
Functions that mutate the passed object can produce unexpected
|
| 502 |
+
behavior or errors and are not supported. See :ref:`gotchas.udf-mutation`
|
| 503 |
+
for more details.
|
| 504 |
+
|
| 505 |
+
A passed user-defined-function will be passed a Series for evaluation.
|
| 506 |
+
|
| 507 |
+
If ``func`` defines an index relabeling, ``axis`` must be ``0`` or ``index``.
|
| 508 |
+
|
| 509 |
+
Examples
|
| 510 |
+
--------
|
| 511 |
+
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
|
| 512 |
+
>>> df
|
| 513 |
+
A B C
|
| 514 |
+
0 1 4 7
|
| 515 |
+
1 2 5 8
|
| 516 |
+
2 3 6 9
|
| 517 |
+
|
| 518 |
+
>>> df.ewm(alpha=0.5).mean()
|
| 519 |
+
A B C
|
| 520 |
+
0 1.000000 4.000000 7.000000
|
| 521 |
+
1 1.666667 4.666667 7.666667
|
| 522 |
+
2 2.428571 5.428571 8.428571
|
| 523 |
+
"""
|
| 524 |
+
return super().aggregate(func, *args, **kwargs)
|
| 525 |
+
|
| 526 |
+
agg = aggregate
|
| 527 |
+
|
| 528 |
+
def mean(
|
| 529 |
+
self,
|
| 530 |
+
numeric_only: bool = False,
|
| 531 |
+
engine=None,
|
| 532 |
+
engine_kwargs=None,
|
| 533 |
+
):
|
| 534 |
+
"""
|
| 535 |
+
Calculate the ewm (exponential weighted moment) mean.
|
| 536 |
+
|
| 537 |
+
Parameters
|
| 538 |
+
----------
|
| 539 |
+
numeric_only : bool, default False
|
| 540 |
+
Include only float, int, boolean columns.
|
| 541 |
+
|
| 542 |
+
engine : str, default None
|
| 543 |
+
* ``'cython'`` : Runs the operation through C-extensions from cython.
|
| 544 |
+
* ``'numba'`` : Runs the operation through JIT compiled code from numba.
|
| 545 |
+
* ``None`` : Defaults to ``'cython'`` or globally setting
|
| 546 |
+
``compute.use_numba``
|
| 547 |
+
|
| 548 |
+
engine_kwargs : dict, default None
|
| 549 |
+
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
|
| 550 |
+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
|
| 551 |
+
and ``parallel`` dictionary keys. The values must either be ``True`` or
|
| 552 |
+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
|
| 553 |
+
``{'nopython': True, 'nogil': False, 'parallel': False}``
|
| 554 |
+
|
| 555 |
+
Returns
|
| 556 |
+
-------
|
| 557 |
+
Series or DataFrame
|
| 558 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 559 |
+
|
| 560 |
+
See Also
|
| 561 |
+
--------
|
| 562 |
+
Series.ewm : Calling ewm with Series data.
|
| 563 |
+
DataFrame.ewm : Calling ewm with DataFrames.
|
| 564 |
+
Series.mean : Aggregating mean for Series.
|
| 565 |
+
DataFrame.mean : Aggregating mean for DataFrame.
|
| 566 |
+
|
| 567 |
+
Notes
|
| 568 |
+
-----
|
| 569 |
+
See :ref:`window.numba_engine` and :ref:`enhancingperf.numba` for
|
| 570 |
+
extended documentation and performance considerations for the Numba engine.
|
| 571 |
+
|
| 572 |
+
Examples
|
| 573 |
+
--------
|
| 574 |
+
>>> ser = pd.Series([1, 2, 3, 4])
|
| 575 |
+
>>> ser.ewm(alpha=0.2).mean()
|
| 576 |
+
0 1.000000
|
| 577 |
+
1 1.555556
|
| 578 |
+
2 2.147541
|
| 579 |
+
3 2.775068
|
| 580 |
+
dtype: float64
|
| 581 |
+
"""
|
| 582 |
+
if maybe_use_numba(engine):
|
| 583 |
+
if self.method == "single":
|
| 584 |
+
func = generate_numba_ewm_func
|
| 585 |
+
else:
|
| 586 |
+
func = generate_numba_ewm_table_func
|
| 587 |
+
ewm_func = func(
|
| 588 |
+
**get_jit_arguments(engine_kwargs),
|
| 589 |
+
com=self._com,
|
| 590 |
+
adjust=self.adjust,
|
| 591 |
+
ignore_na=self.ignore_na,
|
| 592 |
+
deltas=tuple(self._deltas),
|
| 593 |
+
normalize=True,
|
| 594 |
+
)
|
| 595 |
+
return self._apply(ewm_func, name="mean")
|
| 596 |
+
elif engine in ("cython", None):
|
| 597 |
+
if engine_kwargs is not None:
|
| 598 |
+
raise ValueError("cython engine does not accept engine_kwargs")
|
| 599 |
+
|
| 600 |
+
deltas = None if self.times is None else self._deltas
|
| 601 |
+
window_func = partial(
|
| 602 |
+
window_aggregations.ewm,
|
| 603 |
+
com=self._com,
|
| 604 |
+
adjust=self.adjust,
|
| 605 |
+
ignore_na=self.ignore_na,
|
| 606 |
+
deltas=deltas,
|
| 607 |
+
normalize=True,
|
| 608 |
+
)
|
| 609 |
+
return self._apply(window_func, name="mean", numeric_only=numeric_only)
|
| 610 |
+
else:
|
| 611 |
+
raise ValueError("engine must be either 'numba' or 'cython'")
|
| 612 |
+
|
| 613 |
+
def sum(
|
| 614 |
+
self,
|
| 615 |
+
numeric_only: bool = False,
|
| 616 |
+
engine=None,
|
| 617 |
+
engine_kwargs=None,
|
| 618 |
+
):
|
| 619 |
+
"""
|
| 620 |
+
Calculate the ewm (exponential weighted moment) sum.
|
| 621 |
+
|
| 622 |
+
Parameters
|
| 623 |
+
----------
|
| 624 |
+
numeric_only : bool, default False
|
| 625 |
+
Include only float, int, boolean columns.
|
| 626 |
+
engine : str, default None
|
| 627 |
+
* ``'cython'`` : Runs the operation through C-extensions from cython.
|
| 628 |
+
* ``'numba'`` : Runs the operation through JIT compiled code from numba.
|
| 629 |
+
* ``None`` : Defaults to ``'cython'`` or globally setting
|
| 630 |
+
``compute.use_numba``
|
| 631 |
+
engine_kwargs : dict, default None
|
| 632 |
+
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
|
| 633 |
+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
|
| 634 |
+
and ``parallel`` dictionary keys. The values must either be ``True`` or
|
| 635 |
+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
|
| 636 |
+
``{'nopython': True, 'nogil': False, 'parallel': False}``
|
| 637 |
+
|
| 638 |
+
Returns
|
| 639 |
+
-------
|
| 640 |
+
Series or DataFrame
|
| 641 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 642 |
+
|
| 643 |
+
See Also
|
| 644 |
+
--------
|
| 645 |
+
Series.ewm : Calling ewm with Series data.
|
| 646 |
+
DataFrame.ewm : Calling ewm with DataFrames.
|
| 647 |
+
Series.sum : Aggregating sum for Series.
|
| 648 |
+
DataFrame.sum : Aggregating sum for DataFrame.
|
| 649 |
+
|
| 650 |
+
Notes
|
| 651 |
+
-----
|
| 652 |
+
See :ref:`window.numba_engine` and :ref:`enhancingperf.numba` for extended
|
| 653 |
+
documentation and performance considerations for the Numba engine.
|
| 654 |
+
|
| 655 |
+
Examples
|
| 656 |
+
--------
|
| 657 |
+
>>> ser = pd.Series([1, 2, 3, 4])
|
| 658 |
+
>>> ser.ewm(alpha=0.2).sum()
|
| 659 |
+
0 1.000
|
| 660 |
+
1 2.800
|
| 661 |
+
2 5.240
|
| 662 |
+
3 8.192
|
| 663 |
+
dtype: float64
|
| 664 |
+
"""
|
| 665 |
+
if not self.adjust:
|
| 666 |
+
raise NotImplementedError("sum is not implemented with adjust=False")
|
| 667 |
+
if self.times is not None:
|
| 668 |
+
raise NotImplementedError("sum is not implemented with times")
|
| 669 |
+
if maybe_use_numba(engine):
|
| 670 |
+
if self.method == "single":
|
| 671 |
+
func = generate_numba_ewm_func
|
| 672 |
+
else:
|
| 673 |
+
func = generate_numba_ewm_table_func
|
| 674 |
+
ewm_func = func(
|
| 675 |
+
**get_jit_arguments(engine_kwargs),
|
| 676 |
+
com=self._com,
|
| 677 |
+
adjust=self.adjust,
|
| 678 |
+
ignore_na=self.ignore_na,
|
| 679 |
+
deltas=tuple(self._deltas),
|
| 680 |
+
normalize=False,
|
| 681 |
+
)
|
| 682 |
+
return self._apply(ewm_func, name="sum")
|
| 683 |
+
elif engine in ("cython", None):
|
| 684 |
+
if engine_kwargs is not None:
|
| 685 |
+
raise ValueError("cython engine does not accept engine_kwargs")
|
| 686 |
+
|
| 687 |
+
deltas = None if self.times is None else self._deltas
|
| 688 |
+
window_func = partial(
|
| 689 |
+
window_aggregations.ewm,
|
| 690 |
+
com=self._com,
|
| 691 |
+
adjust=self.adjust,
|
| 692 |
+
ignore_na=self.ignore_na,
|
| 693 |
+
deltas=deltas,
|
| 694 |
+
normalize=False,
|
| 695 |
+
)
|
| 696 |
+
return self._apply(window_func, name="sum", numeric_only=numeric_only)
|
| 697 |
+
else:
|
| 698 |
+
raise ValueError("engine must be either 'numba' or 'cython'")
|
| 699 |
+
|
| 700 |
+
def std(self, bias: bool = False, numeric_only: bool = False):
|
| 701 |
+
"""
|
| 702 |
+
Calculate the ewm (exponential weighted moment) standard deviation.
|
| 703 |
+
|
| 704 |
+
Parameters
|
| 705 |
+
----------
|
| 706 |
+
bias : bool, default False
|
| 707 |
+
Use a standard estimation bias correction.
|
| 708 |
+
numeric_only : bool, default False
|
| 709 |
+
Include only float, int, boolean columns.
|
| 710 |
+
|
| 711 |
+
Returns
|
| 712 |
+
-------
|
| 713 |
+
Series or DataFrame
|
| 714 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 715 |
+
|
| 716 |
+
See Also
|
| 717 |
+
--------
|
| 718 |
+
Series.ewm : Calling ewm with Series data.
|
| 719 |
+
DataFrame.ewm : Calling ewm with DataFrames.
|
| 720 |
+
Series.std : Aggregating std for Series.
|
| 721 |
+
DataFrame.std : Aggregating std for DataFrame.
|
| 722 |
+
|
| 723 |
+
Examples
|
| 724 |
+
--------
|
| 725 |
+
>>> ser = pd.Series([1, 2, 3, 4])
|
| 726 |
+
>>> ser.ewm(alpha=0.2).std()
|
| 727 |
+
0 NaN
|
| 728 |
+
1 0.707107
|
| 729 |
+
2 0.995893
|
| 730 |
+
3 1.277320
|
| 731 |
+
dtype: float64
|
| 732 |
+
"""
|
| 733 |
+
if (
|
| 734 |
+
numeric_only
|
| 735 |
+
and self._selected_obj.ndim == 1
|
| 736 |
+
and not is_numeric_dtype(self._selected_obj.dtype)
|
| 737 |
+
):
|
| 738 |
+
# Raise directly so error message says std instead of var
|
| 739 |
+
raise NotImplementedError(
|
| 740 |
+
f"{type(self).__name__}.std does not implement numeric_only"
|
| 741 |
+
)
|
| 742 |
+
if self.times is not None:
|
| 743 |
+
raise NotImplementedError("std is not implemented with times")
|
| 744 |
+
return zsqrt(self.var(bias=bias, numeric_only=numeric_only))
|
| 745 |
+
|
| 746 |
+
def var(self, bias: bool = False, numeric_only: bool = False):
|
| 747 |
+
"""
|
| 748 |
+
Calculate the ewm (exponential weighted moment) variance.
|
| 749 |
+
|
| 750 |
+
Parameters
|
| 751 |
+
----------
|
| 752 |
+
bias : bool, default False
|
| 753 |
+
Use a standard estimation bias correction.
|
| 754 |
+
numeric_only : bool, default False
|
| 755 |
+
Include only float, int, boolean columns.
|
| 756 |
+
|
| 757 |
+
Returns
|
| 758 |
+
-------
|
| 759 |
+
Series or DataFrame
|
| 760 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 761 |
+
|
| 762 |
+
See Also
|
| 763 |
+
--------
|
| 764 |
+
Series.ewm : Calling ewm with Series data.
|
| 765 |
+
DataFrame.ewm : Calling ewm with DataFrames.
|
| 766 |
+
Series.var : Aggregating var for Series.
|
| 767 |
+
DataFrame.var : Aggregating var for DataFrame.
|
| 768 |
+
|
| 769 |
+
Examples
|
| 770 |
+
--------
|
| 771 |
+
>>> ser = pd.Series([1, 2, 3, 4])
|
| 772 |
+
>>> ser.ewm(alpha=0.2).var()
|
| 773 |
+
0 NaN
|
| 774 |
+
1 0.500000
|
| 775 |
+
2 0.991803
|
| 776 |
+
3 1.631547
|
| 777 |
+
dtype: float64
|
| 778 |
+
"""
|
| 779 |
+
if self.times is not None:
|
| 780 |
+
raise NotImplementedError("var is not implemented with times")
|
| 781 |
+
window_func = window_aggregations.ewmcov
|
| 782 |
+
wfunc = partial(
|
| 783 |
+
window_func,
|
| 784 |
+
com=self._com,
|
| 785 |
+
adjust=self.adjust,
|
| 786 |
+
ignore_na=self.ignore_na,
|
| 787 |
+
bias=bias,
|
| 788 |
+
)
|
| 789 |
+
|
| 790 |
+
def var_func(values, begin, end, min_periods):
|
| 791 |
+
return wfunc(values, begin, end, min_periods, values)
|
| 792 |
+
|
| 793 |
+
return self._apply(var_func, name="var", numeric_only=numeric_only)
|
| 794 |
+
|
| 795 |
+
def cov(
|
| 796 |
+
self,
|
| 797 |
+
other: DataFrame | Series | None = None,
|
| 798 |
+
pairwise: bool | None = None,
|
| 799 |
+
bias: bool = False,
|
| 800 |
+
numeric_only: bool = False,
|
| 801 |
+
):
|
| 802 |
+
"""
|
| 803 |
+
Calculate the ewm (exponential weighted moment) sample covariance.
|
| 804 |
+
|
| 805 |
+
Parameters
|
| 806 |
+
----------
|
| 807 |
+
other : Series or DataFrame , optional
|
| 808 |
+
If not supplied then will default to self and produce pairwise
|
| 809 |
+
output.
|
| 810 |
+
pairwise : bool, default None
|
| 811 |
+
If False then only matching columns between self and other will be
|
| 812 |
+
used and the output will be a DataFrame.
|
| 813 |
+
If True then all pairwise combinations will be calculated and the
|
| 814 |
+
output will be a MultiIndex DataFrame in the case of DataFrame
|
| 815 |
+
inputs. In the case of missing elements, only complete pairwise
|
| 816 |
+
observations will be used.
|
| 817 |
+
bias : bool, default False
|
| 818 |
+
Use a standard estimation bias correction.
|
| 819 |
+
numeric_only : bool, default False
|
| 820 |
+
Include only float, int, boolean columns.
|
| 821 |
+
|
| 822 |
+
Returns
|
| 823 |
+
-------
|
| 824 |
+
Series or DataFrame
|
| 825 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 826 |
+
|
| 827 |
+
See Also
|
| 828 |
+
--------
|
| 829 |
+
Series.ewm : Calling ewm with Series data.
|
| 830 |
+
DataFrame.ewm : Calling ewm with DataFrames.
|
| 831 |
+
Series.cov : Aggregating cov for Series.
|
| 832 |
+
DataFrame.cov : Aggregating cov for DataFrame.
|
| 833 |
+
|
| 834 |
+
Examples
|
| 835 |
+
--------
|
| 836 |
+
>>> ser1 = pd.Series([1, 2, 3, 4])
|
| 837 |
+
>>> ser2 = pd.Series([10, 11, 13, 16])
|
| 838 |
+
>>> ser1.ewm(alpha=0.2).cov(ser2)
|
| 839 |
+
0 NaN
|
| 840 |
+
1 0.500000
|
| 841 |
+
2 1.524590
|
| 842 |
+
3 3.408836
|
| 843 |
+
dtype: float64
|
| 844 |
+
"""
|
| 845 |
+
if self.times is not None:
|
| 846 |
+
raise NotImplementedError("cov is not implemented with times")
|
| 847 |
+
|
| 848 |
+
from pandas import Series
|
| 849 |
+
|
| 850 |
+
self._validate_numeric_only("cov", numeric_only)
|
| 851 |
+
|
| 852 |
+
def cov_func(x, y):
|
| 853 |
+
x_array = self._prep_values(x)
|
| 854 |
+
y_array = self._prep_values(y)
|
| 855 |
+
window_indexer = self._get_window_indexer()
|
| 856 |
+
min_periods = (
|
| 857 |
+
self.min_periods
|
| 858 |
+
if self.min_periods is not None
|
| 859 |
+
else window_indexer.window_size
|
| 860 |
+
)
|
| 861 |
+
start, end = window_indexer.get_window_bounds(
|
| 862 |
+
num_values=len(x_array),
|
| 863 |
+
min_periods=min_periods,
|
| 864 |
+
center=self.center,
|
| 865 |
+
closed=self.closed,
|
| 866 |
+
step=self.step,
|
| 867 |
+
)
|
| 868 |
+
result = window_aggregations.ewmcov(
|
| 869 |
+
x_array,
|
| 870 |
+
start,
|
| 871 |
+
end,
|
| 872 |
+
# error: Argument 4 to "ewmcov" has incompatible type
|
| 873 |
+
# "Optional[int]"; expected "int"
|
| 874 |
+
self.min_periods, # type: ignore[arg-type]
|
| 875 |
+
y_array,
|
| 876 |
+
self._com,
|
| 877 |
+
self.adjust,
|
| 878 |
+
self.ignore_na,
|
| 879 |
+
bias,
|
| 880 |
+
)
|
| 881 |
+
return Series(result, index=x.index, name=x.name, copy=False)
|
| 882 |
+
|
| 883 |
+
return self._apply_pairwise(
|
| 884 |
+
self._selected_obj, other, pairwise, cov_func, numeric_only
|
| 885 |
+
)
|
| 886 |
+
|
| 887 |
+
def corr(
|
| 888 |
+
self,
|
| 889 |
+
other: DataFrame | Series | None = None,
|
| 890 |
+
pairwise: bool | None = None,
|
| 891 |
+
numeric_only: bool = False,
|
| 892 |
+
):
|
| 893 |
+
"""
|
| 894 |
+
Calculate the ewm (exponential weighted moment) sample correlation.
|
| 895 |
+
|
| 896 |
+
Parameters
|
| 897 |
+
----------
|
| 898 |
+
other : Series or DataFrame, optional
|
| 899 |
+
If not supplied then will default to self and produce pairwise
|
| 900 |
+
output.
|
| 901 |
+
pairwise : bool, default None
|
| 902 |
+
If False then only matching columns between self and other will be
|
| 903 |
+
used and the output will be a DataFrame.
|
| 904 |
+
If True then all pairwise combinations will be calculated and the
|
| 905 |
+
output will be a MultiIndex DataFrame in the case of DataFrame
|
| 906 |
+
inputs. In the case of missing elements, only complete pairwise
|
| 907 |
+
observations will be used.
|
| 908 |
+
numeric_only : bool, default False
|
| 909 |
+
Include only float, int, boolean columns.
|
| 910 |
+
|
| 911 |
+
Returns
|
| 912 |
+
-------
|
| 913 |
+
Series or DataFrame
|
| 914 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 915 |
+
|
| 916 |
+
See Also
|
| 917 |
+
--------
|
| 918 |
+
Series.ewm : Calling ewm with Series data.
|
| 919 |
+
DataFrame.ewm : Calling ewm with DataFrames.
|
| 920 |
+
Series.corr : Aggregating corr for Series.
|
| 921 |
+
DataFrame.corr : Aggregating corr for DataFrame.
|
| 922 |
+
|
| 923 |
+
Examples
|
| 924 |
+
--------
|
| 925 |
+
>>> ser1 = pd.Series([1, 2, 3, 4])
|
| 926 |
+
>>> ser2 = pd.Series([10, 11, 13, 16])
|
| 927 |
+
>>> ser1.ewm(alpha=0.2).corr(ser2)
|
| 928 |
+
0 NaN
|
| 929 |
+
1 1.000000
|
| 930 |
+
2 0.982821
|
| 931 |
+
3 0.977802
|
| 932 |
+
dtype: float64
|
| 933 |
+
"""
|
| 934 |
+
if self.times is not None:
|
| 935 |
+
raise NotImplementedError("corr is not implemented with times")
|
| 936 |
+
|
| 937 |
+
from pandas import Series
|
| 938 |
+
|
| 939 |
+
self._validate_numeric_only("corr", numeric_only)
|
| 940 |
+
|
| 941 |
+
def cov_func(x, y):
|
| 942 |
+
x_array = self._prep_values(x)
|
| 943 |
+
y_array = self._prep_values(y)
|
| 944 |
+
window_indexer = self._get_window_indexer()
|
| 945 |
+
min_periods = (
|
| 946 |
+
self.min_periods
|
| 947 |
+
if self.min_periods is not None
|
| 948 |
+
else window_indexer.window_size
|
| 949 |
+
)
|
| 950 |
+
start, end = window_indexer.get_window_bounds(
|
| 951 |
+
num_values=len(x_array),
|
| 952 |
+
min_periods=min_periods,
|
| 953 |
+
center=self.center,
|
| 954 |
+
closed=self.closed,
|
| 955 |
+
step=self.step,
|
| 956 |
+
)
|
| 957 |
+
|
| 958 |
+
def _cov(X, Y):
|
| 959 |
+
return window_aggregations.ewmcov(
|
| 960 |
+
X,
|
| 961 |
+
start,
|
| 962 |
+
end,
|
| 963 |
+
min_periods,
|
| 964 |
+
Y,
|
| 965 |
+
self._com,
|
| 966 |
+
self.adjust,
|
| 967 |
+
self.ignore_na,
|
| 968 |
+
True,
|
| 969 |
+
)
|
| 970 |
+
|
| 971 |
+
with np.errstate(all="ignore"):
|
| 972 |
+
cov = _cov(x_array, y_array)
|
| 973 |
+
x_var = _cov(x_array, x_array)
|
| 974 |
+
y_var = _cov(y_array, y_array)
|
| 975 |
+
result = cov / zsqrt(x_var * y_var)
|
| 976 |
+
return Series(result, index=x.index, name=x.name, copy=False)
|
| 977 |
+
|
| 978 |
+
return self._apply_pairwise(
|
| 979 |
+
self._selected_obj, other, pairwise, cov_func, numeric_only
|
| 980 |
+
)
|
| 981 |
+
|
| 982 |
+
|
| 983 |
+
@set_module("pandas.api.typing")
|
| 984 |
+
class ExponentialMovingWindowGroupby(BaseWindowGroupby, ExponentialMovingWindow):
|
| 985 |
+
"""
|
| 986 |
+
Provide an exponential moving window groupby implementation.
|
| 987 |
+
"""
|
| 988 |
+
|
| 989 |
+
_attributes = ExponentialMovingWindow._attributes + BaseWindowGroupby._attributes
|
| 990 |
+
|
| 991 |
+
def __init__(self, obj, *args, _grouper=None, **kwargs) -> None:
|
| 992 |
+
super().__init__(obj, *args, _grouper=_grouper, **kwargs)
|
| 993 |
+
|
| 994 |
+
if not obj.empty and self.times is not None:
|
| 995 |
+
# sort the times and recalculate the deltas according to the groups
|
| 996 |
+
groupby_order = np.concatenate(list(self._grouper.indices.values()))
|
| 997 |
+
self._deltas = _calculate_deltas(
|
| 998 |
+
self.times.take(groupby_order),
|
| 999 |
+
self.halflife,
|
| 1000 |
+
)
|
| 1001 |
+
|
| 1002 |
+
def _get_window_indexer(self) -> GroupbyIndexer:
|
| 1003 |
+
"""
|
| 1004 |
+
Return an indexer class that will compute the window start and end bounds
|
| 1005 |
+
|
| 1006 |
+
Returns
|
| 1007 |
+
-------
|
| 1008 |
+
GroupbyIndexer
|
| 1009 |
+
"""
|
| 1010 |
+
window_indexer = GroupbyIndexer(
|
| 1011 |
+
groupby_indices=self._grouper.indices,
|
| 1012 |
+
window_indexer=ExponentialMovingWindowIndexer,
|
| 1013 |
+
)
|
| 1014 |
+
return window_indexer
|
| 1015 |
+
|
| 1016 |
+
|
| 1017 |
+
class OnlineExponentialMovingWindow(ExponentialMovingWindow):
|
| 1018 |
+
def __init__(
|
| 1019 |
+
self,
|
| 1020 |
+
obj: NDFrame,
|
| 1021 |
+
com: float | None = None,
|
| 1022 |
+
span: float | None = None,
|
| 1023 |
+
halflife: float | TimedeltaConvertibleTypes | None = None,
|
| 1024 |
+
alpha: float | None = None,
|
| 1025 |
+
min_periods: int | None = 0,
|
| 1026 |
+
adjust: bool = True,
|
| 1027 |
+
ignore_na: bool = False,
|
| 1028 |
+
times: np.ndarray | NDFrame | None = None,
|
| 1029 |
+
engine: str = "numba",
|
| 1030 |
+
engine_kwargs: dict[str, bool] | None = None,
|
| 1031 |
+
*,
|
| 1032 |
+
selection=None,
|
| 1033 |
+
) -> None:
|
| 1034 |
+
if times is not None:
|
| 1035 |
+
raise NotImplementedError(
|
| 1036 |
+
"times is not implemented with online operations."
|
| 1037 |
+
)
|
| 1038 |
+
super().__init__(
|
| 1039 |
+
obj=obj,
|
| 1040 |
+
com=com,
|
| 1041 |
+
span=span,
|
| 1042 |
+
halflife=halflife,
|
| 1043 |
+
alpha=alpha,
|
| 1044 |
+
min_periods=min_periods,
|
| 1045 |
+
adjust=adjust,
|
| 1046 |
+
ignore_na=ignore_na,
|
| 1047 |
+
times=times,
|
| 1048 |
+
selection=selection,
|
| 1049 |
+
)
|
| 1050 |
+
self._mean = EWMMeanState(self._com, self.adjust, self.ignore_na, obj.shape)
|
| 1051 |
+
if maybe_use_numba(engine):
|
| 1052 |
+
self.engine = engine
|
| 1053 |
+
self.engine_kwargs = engine_kwargs
|
| 1054 |
+
else:
|
| 1055 |
+
raise ValueError("'numba' is the only supported engine")
|
| 1056 |
+
|
| 1057 |
+
def reset(self) -> None:
|
| 1058 |
+
"""
|
| 1059 |
+
Reset the state captured by `update` calls.
|
| 1060 |
+
"""
|
| 1061 |
+
self._mean.reset()
|
| 1062 |
+
|
| 1063 |
+
def aggregate(self, func=None, *args, **kwargs):
|
| 1064 |
+
raise NotImplementedError("aggregate is not implemented.")
|
| 1065 |
+
|
| 1066 |
+
def std(self, bias: bool = False, *args, **kwargs):
|
| 1067 |
+
raise NotImplementedError("std is not implemented.")
|
| 1068 |
+
|
| 1069 |
+
def corr(
|
| 1070 |
+
self,
|
| 1071 |
+
other: DataFrame | Series | None = None,
|
| 1072 |
+
pairwise: bool | None = None,
|
| 1073 |
+
numeric_only: bool = False,
|
| 1074 |
+
):
|
| 1075 |
+
raise NotImplementedError("corr is not implemented.")
|
| 1076 |
+
|
| 1077 |
+
def cov(
|
| 1078 |
+
self,
|
| 1079 |
+
other: DataFrame | Series | None = None,
|
| 1080 |
+
pairwise: bool | None = None,
|
| 1081 |
+
bias: bool = False,
|
| 1082 |
+
numeric_only: bool = False,
|
| 1083 |
+
):
|
| 1084 |
+
raise NotImplementedError("cov is not implemented.")
|
| 1085 |
+
|
| 1086 |
+
def var(self, bias: bool = False, numeric_only: bool = False):
|
| 1087 |
+
raise NotImplementedError("var is not implemented.")
|
| 1088 |
+
|
| 1089 |
+
def mean(self, *args, update=None, update_times=None, **kwargs):
|
| 1090 |
+
"""
|
| 1091 |
+
Calculate an online exponentially weighted mean.
|
| 1092 |
+
|
| 1093 |
+
Parameters
|
| 1094 |
+
----------
|
| 1095 |
+
update: DataFrame or Series, default None
|
| 1096 |
+
New values to continue calculating the
|
| 1097 |
+
exponentially weighted mean from the last values and weights.
|
| 1098 |
+
Values should be float64 dtype.
|
| 1099 |
+
|
| 1100 |
+
``update`` needs to be ``None`` the first time the
|
| 1101 |
+
exponentially weighted mean is calculated.
|
| 1102 |
+
|
| 1103 |
+
update_times: Series or 1-D np.ndarray, default None
|
| 1104 |
+
New times to continue calculating the
|
| 1105 |
+
exponentially weighted mean from the last values and weights.
|
| 1106 |
+
If ``None``, values are assumed to be evenly spaced
|
| 1107 |
+
in time.
|
| 1108 |
+
This feature is currently unsupported.
|
| 1109 |
+
|
| 1110 |
+
Returns
|
| 1111 |
+
-------
|
| 1112 |
+
DataFrame or Series
|
| 1113 |
+
|
| 1114 |
+
Examples
|
| 1115 |
+
--------
|
| 1116 |
+
>>> df = pd.DataFrame({"a": range(5), "b": range(5, 10)})
|
| 1117 |
+
>>> online_ewm = df.head(2).ewm(0.5).online()
|
| 1118 |
+
>>> online_ewm.mean()
|
| 1119 |
+
a b
|
| 1120 |
+
0 0.00 5.00
|
| 1121 |
+
1 0.75 5.75
|
| 1122 |
+
>>> online_ewm.mean(update=df.tail(3))
|
| 1123 |
+
a b
|
| 1124 |
+
2 1.615385 6.615385
|
| 1125 |
+
3 2.550000 7.550000
|
| 1126 |
+
4 3.520661 8.520661
|
| 1127 |
+
>>> online_ewm.reset()
|
| 1128 |
+
>>> online_ewm.mean()
|
| 1129 |
+
a b
|
| 1130 |
+
0 0.00 5.00
|
| 1131 |
+
1 0.75 5.75
|
| 1132 |
+
"""
|
| 1133 |
+
result_kwargs = {}
|
| 1134 |
+
is_frame = self._selected_obj.ndim == 2
|
| 1135 |
+
if update_times is not None:
|
| 1136 |
+
raise NotImplementedError("update_times is not implemented.")
|
| 1137 |
+
update_deltas = np.ones(
|
| 1138 |
+
max(self._selected_obj.shape[-1] - 1, 0), dtype=np.float64
|
| 1139 |
+
)
|
| 1140 |
+
if update is not None:
|
| 1141 |
+
if self._mean.last_ewm is None:
|
| 1142 |
+
raise ValueError(
|
| 1143 |
+
"Must call mean with update=None first before passing update"
|
| 1144 |
+
)
|
| 1145 |
+
result_from = 1
|
| 1146 |
+
result_kwargs["index"] = update.index
|
| 1147 |
+
if is_frame:
|
| 1148 |
+
last_value = self._mean.last_ewm[np.newaxis, :]
|
| 1149 |
+
result_kwargs["columns"] = update.columns
|
| 1150 |
+
else:
|
| 1151 |
+
last_value = self._mean.last_ewm
|
| 1152 |
+
result_kwargs["name"] = update.name
|
| 1153 |
+
np_array = np.concatenate((last_value, update.to_numpy()))
|
| 1154 |
+
else:
|
| 1155 |
+
result_from = 0
|
| 1156 |
+
result_kwargs["index"] = self._selected_obj.index
|
| 1157 |
+
if is_frame:
|
| 1158 |
+
result_kwargs["columns"] = self._selected_obj.columns
|
| 1159 |
+
else:
|
| 1160 |
+
result_kwargs["name"] = self._selected_obj.name
|
| 1161 |
+
np_array = self._selected_obj.astype(np.float64).to_numpy()
|
| 1162 |
+
ewma_func = generate_online_numba_ewma_func(
|
| 1163 |
+
**get_jit_arguments(self.engine_kwargs)
|
| 1164 |
+
)
|
| 1165 |
+
result = self._mean.run_ewm(
|
| 1166 |
+
np_array if is_frame else np_array[:, np.newaxis],
|
| 1167 |
+
update_deltas,
|
| 1168 |
+
self.min_periods,
|
| 1169 |
+
ewma_func,
|
| 1170 |
+
)
|
| 1171 |
+
if not is_frame:
|
| 1172 |
+
result = result.squeeze()
|
| 1173 |
+
result = result[result_from:]
|
| 1174 |
+
result = self._selected_obj._constructor(result, **result_kwargs)
|
| 1175 |
+
return result
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/expanding.py
ADDED
|
@@ -0,0 +1,1412 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import (
|
| 4 |
+
TYPE_CHECKING,
|
| 5 |
+
Any,
|
| 6 |
+
Concatenate,
|
| 7 |
+
Literal,
|
| 8 |
+
Self,
|
| 9 |
+
final,
|
| 10 |
+
overload,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
from pandas.util._decorators import set_module
|
| 14 |
+
|
| 15 |
+
from pandas.core.indexers.objects import (
|
| 16 |
+
BaseIndexer,
|
| 17 |
+
ExpandingIndexer,
|
| 18 |
+
GroupbyIndexer,
|
| 19 |
+
)
|
| 20 |
+
from pandas.core.window.rolling import (
|
| 21 |
+
BaseWindowGroupby,
|
| 22 |
+
RollingAndExpandingMixin,
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
if TYPE_CHECKING:
|
| 26 |
+
from collections.abc import Callable
|
| 27 |
+
|
| 28 |
+
from pandas._typing import (
|
| 29 |
+
P,
|
| 30 |
+
QuantileInterpolation,
|
| 31 |
+
T,
|
| 32 |
+
WindowingRankType,
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
from pandas import (
|
| 36 |
+
DataFrame,
|
| 37 |
+
Series,
|
| 38 |
+
)
|
| 39 |
+
from pandas.core.generic import NDFrame
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
@set_module("pandas.api.typing")
|
| 43 |
+
class Expanding(RollingAndExpandingMixin):
|
| 44 |
+
"""
|
| 45 |
+
Provide expanding window calculations.
|
| 46 |
+
|
| 47 |
+
An expanding window yields the value of an aggregation statistic with all the data
|
| 48 |
+
available up to that point in time.
|
| 49 |
+
|
| 50 |
+
Parameters
|
| 51 |
+
----------
|
| 52 |
+
min_periods : int, default 1
|
| 53 |
+
Minimum number of observations in window required to have a value;
|
| 54 |
+
otherwise, result is ``np.nan``.
|
| 55 |
+
|
| 56 |
+
method : str {'single', 'table'}, default 'single'
|
| 57 |
+
Execute the rolling operation per single column or row (``'single'``)
|
| 58 |
+
or over the entire object (``'table'``).
|
| 59 |
+
|
| 60 |
+
This argument is only implemented when specifying ``engine='numba'``
|
| 61 |
+
in the method call.
|
| 62 |
+
|
| 63 |
+
Returns
|
| 64 |
+
-------
|
| 65 |
+
pandas.api.typing.Expanding
|
| 66 |
+
An instance of Expanding for further expanding window calculations,
|
| 67 |
+
e.g. using the ``sum`` method.
|
| 68 |
+
|
| 69 |
+
See Also
|
| 70 |
+
--------
|
| 71 |
+
rolling : Provides rolling window calculations.
|
| 72 |
+
ewm : Provides exponential weighted functions.
|
| 73 |
+
|
| 74 |
+
Notes
|
| 75 |
+
-----
|
| 76 |
+
See :ref:`Windowing Operations <window.expanding>` for further usage details
|
| 77 |
+
and examples.
|
| 78 |
+
|
| 79 |
+
Examples
|
| 80 |
+
--------
|
| 81 |
+
>>> df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]})
|
| 82 |
+
>>> df
|
| 83 |
+
B
|
| 84 |
+
0 0.0
|
| 85 |
+
1 1.0
|
| 86 |
+
2 2.0
|
| 87 |
+
3 NaN
|
| 88 |
+
4 4.0
|
| 89 |
+
|
| 90 |
+
**min_periods**
|
| 91 |
+
|
| 92 |
+
Expanding sum with 1 vs 3 observations needed to calculate a value.
|
| 93 |
+
|
| 94 |
+
>>> df.expanding(1).sum()
|
| 95 |
+
B
|
| 96 |
+
0 0.0
|
| 97 |
+
1 1.0
|
| 98 |
+
2 3.0
|
| 99 |
+
3 3.0
|
| 100 |
+
4 7.0
|
| 101 |
+
>>> df.expanding(3).sum()
|
| 102 |
+
B
|
| 103 |
+
0 NaN
|
| 104 |
+
1 NaN
|
| 105 |
+
2 3.0
|
| 106 |
+
3 3.0
|
| 107 |
+
4 7.0
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
_attributes: list[str] = ["min_periods", "method"]
|
| 111 |
+
|
| 112 |
+
def __init__(
|
| 113 |
+
self,
|
| 114 |
+
obj: NDFrame,
|
| 115 |
+
min_periods: int = 1,
|
| 116 |
+
method: str = "single",
|
| 117 |
+
selection=None,
|
| 118 |
+
) -> None:
|
| 119 |
+
super().__init__(
|
| 120 |
+
obj=obj,
|
| 121 |
+
min_periods=min_periods,
|
| 122 |
+
method=method,
|
| 123 |
+
selection=selection,
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
def _get_window_indexer(self) -> BaseIndexer:
|
| 127 |
+
"""
|
| 128 |
+
Return an indexer class that will compute the window start and end bounds
|
| 129 |
+
"""
|
| 130 |
+
return ExpandingIndexer()
|
| 131 |
+
|
| 132 |
+
def aggregate(self, func=None, *args, **kwargs):
|
| 133 |
+
"""
|
| 134 |
+
Aggregate using one or more operations over the specified axis.
|
| 135 |
+
|
| 136 |
+
Parameters
|
| 137 |
+
----------
|
| 138 |
+
func : function, str, list or dict
|
| 139 |
+
Function to use for aggregating the data. If a function, must either
|
| 140 |
+
work when passed a Series/Dataframe or when passed to
|
| 141 |
+
Series/Dataframe.apply.
|
| 142 |
+
|
| 143 |
+
Accepted combinations are:
|
| 144 |
+
|
| 145 |
+
- function
|
| 146 |
+
- string function name
|
| 147 |
+
- list of functions and/or function names, e.g. ``[np.sum, 'mean']``
|
| 148 |
+
- dict of axis labels -> functions, function names or list of such.
|
| 149 |
+
|
| 150 |
+
*args
|
| 151 |
+
Positional arguments to pass to `func`.
|
| 152 |
+
**kwargs
|
| 153 |
+
Keyword arguments to pass to `func`.
|
| 154 |
+
|
| 155 |
+
Returns
|
| 156 |
+
-------
|
| 157 |
+
scalar, Series or DataFrame
|
| 158 |
+
|
| 159 |
+
The return can be:
|
| 160 |
+
|
| 161 |
+
* scalar : when Series.agg is called with single function
|
| 162 |
+
* Series : when DataFrame.agg is called with a single function
|
| 163 |
+
* DataFrame : when DataFrame.agg is called with several functions
|
| 164 |
+
|
| 165 |
+
See Also
|
| 166 |
+
--------
|
| 167 |
+
DataFrame.aggregate : Similar DataFrame method.
|
| 168 |
+
Series.aggregate : Similar Series method.
|
| 169 |
+
|
| 170 |
+
Notes
|
| 171 |
+
-----
|
| 172 |
+
The aggregation operations are always performed over an axis, either the
|
| 173 |
+
index (default) or the column axis. This behavior is different from
|
| 174 |
+
`numpy` aggregation functions (`mean`, `median`, `prod`, `sum`, `std`,
|
| 175 |
+
`var`), where the default is to compute the aggregation of the flattened
|
| 176 |
+
array, e.g., ``numpy.mean(arr_2d)`` as opposed to
|
| 177 |
+
``numpy.mean(arr_2d, axis=0)``.
|
| 178 |
+
|
| 179 |
+
`agg` is an alias for `aggregate`. Use the alias.
|
| 180 |
+
|
| 181 |
+
Functions that mutate the passed object can produce unexpected
|
| 182 |
+
behavior or errors and are not supported. See :ref:`gotchas.udf-mutation`
|
| 183 |
+
for more details.
|
| 184 |
+
|
| 185 |
+
A passed user-defined-function will be passed a Series for evaluation.
|
| 186 |
+
|
| 187 |
+
If ``func`` defines an index relabeling, ``axis`` must be ``0`` or ``index``.
|
| 188 |
+
|
| 189 |
+
Examples
|
| 190 |
+
--------
|
| 191 |
+
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
|
| 192 |
+
>>> df
|
| 193 |
+
A B C
|
| 194 |
+
0 1 4 7
|
| 195 |
+
1 2 5 8
|
| 196 |
+
2 3 6 9
|
| 197 |
+
|
| 198 |
+
>>> df.expanding(2).sum()
|
| 199 |
+
A B C
|
| 200 |
+
0 NaN NaN NaN
|
| 201 |
+
1 3.0 9.0 15.0
|
| 202 |
+
2 6.0 15.0 24.0
|
| 203 |
+
|
| 204 |
+
>>> df.expanding(2).agg({"A": "sum", "B": "min"})
|
| 205 |
+
A B
|
| 206 |
+
0 NaN NaN
|
| 207 |
+
1 3.0 4.0
|
| 208 |
+
2 6.0 4.0
|
| 209 |
+
"""
|
| 210 |
+
return super().aggregate(func, *args, **kwargs)
|
| 211 |
+
|
| 212 |
+
agg = aggregate
|
| 213 |
+
|
| 214 |
+
def count(self, numeric_only: bool = False):
|
| 215 |
+
"""
|
| 216 |
+
Calculate the expanding count of non NaN observations.
|
| 217 |
+
|
| 218 |
+
Parameters
|
| 219 |
+
----------
|
| 220 |
+
numeric_only : bool, default False
|
| 221 |
+
Include only float, int, boolean columns.
|
| 222 |
+
|
| 223 |
+
Returns
|
| 224 |
+
-------
|
| 225 |
+
Series or DataFrame
|
| 226 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 227 |
+
|
| 228 |
+
See Also
|
| 229 |
+
--------
|
| 230 |
+
Series.expanding : Calling expanding with Series data.
|
| 231 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 232 |
+
Series.count : Aggregating count for Series.
|
| 233 |
+
DataFrame.count : Aggregating count for DataFrame.
|
| 234 |
+
|
| 235 |
+
Examples
|
| 236 |
+
--------
|
| 237 |
+
>>> ser = pd.Series([1, 2, 3, 4], index=["a", "b", "c", "d"])
|
| 238 |
+
>>> ser.expanding().count()
|
| 239 |
+
a 1.0
|
| 240 |
+
b 2.0
|
| 241 |
+
c 3.0
|
| 242 |
+
d 4.0
|
| 243 |
+
dtype: float64
|
| 244 |
+
"""
|
| 245 |
+
return super().count(numeric_only=numeric_only)
|
| 246 |
+
|
| 247 |
+
def apply(
|
| 248 |
+
self,
|
| 249 |
+
func: Callable[..., Any],
|
| 250 |
+
raw: bool = False,
|
| 251 |
+
engine: Literal["cython", "numba"] | None = None,
|
| 252 |
+
engine_kwargs: dict[str, bool] | None = None,
|
| 253 |
+
args: tuple[Any, ...] | None = None,
|
| 254 |
+
kwargs: dict[str, Any] | None = None,
|
| 255 |
+
):
|
| 256 |
+
"""
|
| 257 |
+
Calculate the expanding custom aggregation function.
|
| 258 |
+
|
| 259 |
+
Parameters
|
| 260 |
+
----------
|
| 261 |
+
func : function
|
| 262 |
+
Must produce a single value from an ndarray input if ``raw=True``
|
| 263 |
+
or a single value from a Series if ``raw=False``. Can also accept a
|
| 264 |
+
Numba JIT function with ``engine='numba'`` specified.
|
| 265 |
+
|
| 266 |
+
raw : bool, default False
|
| 267 |
+
* ``False`` : passes each row or column as a Series to the
|
| 268 |
+
function.
|
| 269 |
+
* ``True`` : the passed function will receive ndarray objects instead.
|
| 270 |
+
|
| 271 |
+
If you are just applying a NumPy reduction function this will
|
| 272 |
+
achieve much better performance.
|
| 273 |
+
|
| 274 |
+
engine : str, default None
|
| 275 |
+
* ``'cython'`` : Runs rolling apply through C-extensions from cython.
|
| 276 |
+
* ``'numba'`` : Runs rolling apply through JIT compiled code from numba.
|
| 277 |
+
Only available when ``raw`` is set to ``True``.
|
| 278 |
+
* ``None`` : Defaults to ``'cython'`` or globally setting
|
| 279 |
+
``compute.use_numba``
|
| 280 |
+
|
| 281 |
+
engine_kwargs : dict, default None
|
| 282 |
+
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
|
| 283 |
+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
|
| 284 |
+
and ``parallel`` dictionary keys. The values must either be ``True`` or
|
| 285 |
+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
|
| 286 |
+
``{'nopython': True, 'nogil': False, 'parallel': False}`` and will be
|
| 287 |
+
applied to both the ``func`` and the ``apply`` rolling aggregation.
|
| 288 |
+
|
| 289 |
+
args : tuple, default None
|
| 290 |
+
Positional arguments to be passed into func.
|
| 291 |
+
|
| 292 |
+
kwargs : dict, default None
|
| 293 |
+
Keyword arguments to be passed into func.
|
| 294 |
+
|
| 295 |
+
Returns
|
| 296 |
+
-------
|
| 297 |
+
Series or DataFrame
|
| 298 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 299 |
+
|
| 300 |
+
See Also
|
| 301 |
+
--------
|
| 302 |
+
Series.expanding : Calling expanding with Series data.
|
| 303 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 304 |
+
Series.apply : Aggregating apply for Series.
|
| 305 |
+
DataFrame.apply : Aggregating apply for DataFrame.
|
| 306 |
+
|
| 307 |
+
Examples
|
| 308 |
+
--------
|
| 309 |
+
>>> ser = pd.Series([1, 2, 3, 4], index=["a", "b", "c", "d"])
|
| 310 |
+
>>> ser.expanding().apply(lambda s: s.max() - 2 * s.min())
|
| 311 |
+
a -1.0
|
| 312 |
+
b 0.0
|
| 313 |
+
c 1.0
|
| 314 |
+
d 2.0
|
| 315 |
+
dtype: float64
|
| 316 |
+
"""
|
| 317 |
+
return super().apply(
|
| 318 |
+
func,
|
| 319 |
+
raw=raw,
|
| 320 |
+
engine=engine,
|
| 321 |
+
engine_kwargs=engine_kwargs,
|
| 322 |
+
args=args,
|
| 323 |
+
kwargs=kwargs,
|
| 324 |
+
)
|
| 325 |
+
|
| 326 |
+
@overload
|
| 327 |
+
def pipe(
|
| 328 |
+
self,
|
| 329 |
+
func: Callable[Concatenate[Self, P], T],
|
| 330 |
+
*args: P.args,
|
| 331 |
+
**kwargs: P.kwargs,
|
| 332 |
+
) -> T: ...
|
| 333 |
+
|
| 334 |
+
@overload
|
| 335 |
+
def pipe(
|
| 336 |
+
self,
|
| 337 |
+
func: tuple[Callable[..., T], str],
|
| 338 |
+
*args: Any,
|
| 339 |
+
**kwargs: Any,
|
| 340 |
+
) -> T: ...
|
| 341 |
+
|
| 342 |
+
@final
|
| 343 |
+
def pipe(
|
| 344 |
+
self,
|
| 345 |
+
func: Callable[Concatenate[Self, P], T] | tuple[Callable[..., T], str],
|
| 346 |
+
*args: Any,
|
| 347 |
+
**kwargs: Any,
|
| 348 |
+
) -> T:
|
| 349 |
+
"""
|
| 350 |
+
Apply a ``func`` with arguments to this Expanding object and return its result.
|
| 351 |
+
|
| 352 |
+
Use `.pipe` when you want to improve readability by chaining together
|
| 353 |
+
functions that expect Series, DataFrames, GroupBy, Rolling, Expanding or
|
| 354 |
+
Resampler
|
| 355 |
+
objects.
|
| 356 |
+
Instead of writing
|
| 357 |
+
|
| 358 |
+
>>> h = lambda x, arg2, arg3: x + 1 - arg2 * arg3
|
| 359 |
+
>>> g = lambda x, arg1: x * 5 / arg1
|
| 360 |
+
>>> f = lambda x: x**4
|
| 361 |
+
>>> df = pd.DataFrame(
|
| 362 |
+
... {"A": [1, 2, 3, 4]}, index=pd.date_range("2012-08-02", periods=4)
|
| 363 |
+
... )
|
| 364 |
+
>>> h(g(f(df.rolling("2D")), arg1=1), arg2=2, arg3=3) # doctest: +SKIP
|
| 365 |
+
|
| 366 |
+
You can write
|
| 367 |
+
|
| 368 |
+
>>> (
|
| 369 |
+
... df.rolling("2D").pipe(f).pipe(g, arg1=1).pipe(h, arg2=2, arg3=3)
|
| 370 |
+
... ) # doctest: +SKIP
|
| 371 |
+
|
| 372 |
+
which is much more readable.
|
| 373 |
+
|
| 374 |
+
Parameters
|
| 375 |
+
----------
|
| 376 |
+
func : callable or tuple of (callable, str)
|
| 377 |
+
Function to apply to this Expanding object or, alternatively,
|
| 378 |
+
a `(callable, data_keyword)` tuple where `data_keyword` is a
|
| 379 |
+
string indicating the keyword of `callable` that expects the
|
| 380 |
+
Expanding object.
|
| 381 |
+
*args : iterable, optional
|
| 382 |
+
Positional arguments passed into `func`.
|
| 383 |
+
**kwargs : dict, optional
|
| 384 |
+
A dictionary of keyword arguments passed into `func`.
|
| 385 |
+
|
| 386 |
+
Returns
|
| 387 |
+
-------
|
| 388 |
+
Expanding
|
| 389 |
+
The original object with the function `func` applied.
|
| 390 |
+
|
| 391 |
+
See Also
|
| 392 |
+
--------
|
| 393 |
+
Series.pipe : Apply a function with arguments to a series.
|
| 394 |
+
DataFrame.pipe: Apply a function with arguments to a dataframe.
|
| 395 |
+
apply : Apply function to each group instead of to the
|
| 396 |
+
full Expanding object.
|
| 397 |
+
|
| 398 |
+
Notes
|
| 399 |
+
-----
|
| 400 |
+
See more `here
|
| 401 |
+
<https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#piping-function-calls>`_
|
| 402 |
+
|
| 403 |
+
Examples
|
| 404 |
+
--------
|
| 405 |
+
|
| 406 |
+
>>> df = pd.DataFrame(
|
| 407 |
+
... {"A": [1, 2, 3, 4]}, index=pd.date_range("2012-08-02", periods=4)
|
| 408 |
+
... )
|
| 409 |
+
>>> df
|
| 410 |
+
A
|
| 411 |
+
2012-08-02 1
|
| 412 |
+
2012-08-03 2
|
| 413 |
+
2012-08-04 3
|
| 414 |
+
2012-08-05 4
|
| 415 |
+
|
| 416 |
+
To get the difference between each expanding window's maximum and minimum
|
| 417 |
+
value in one pass, you can do
|
| 418 |
+
|
| 419 |
+
>>> df.expanding().pipe(lambda x: x.max() - x.min())
|
| 420 |
+
A
|
| 421 |
+
2012-08-02 0.0
|
| 422 |
+
2012-08-03 1.0
|
| 423 |
+
2012-08-04 2.0
|
| 424 |
+
2012-08-05 3.0
|
| 425 |
+
"""
|
| 426 |
+
return super().pipe(func, *args, **kwargs)
|
| 427 |
+
|
| 428 |
+
def sum(
|
| 429 |
+
self,
|
| 430 |
+
numeric_only: bool = False,
|
| 431 |
+
engine: Literal["cython", "numba"] | None = None,
|
| 432 |
+
engine_kwargs: dict[str, bool] | None = None,
|
| 433 |
+
):
|
| 434 |
+
"""
|
| 435 |
+
Calculate the expanding sum.
|
| 436 |
+
|
| 437 |
+
Parameters
|
| 438 |
+
----------
|
| 439 |
+
numeric_only : bool, default False
|
| 440 |
+
Include only float, int, boolean columns.
|
| 441 |
+
|
| 442 |
+
engine : str, default None
|
| 443 |
+
* ``'cython'`` : Runs the operation through C-extensions from cython.
|
| 444 |
+
* ``'numba'`` : Runs the operation through JIT compiled code from numba.
|
| 445 |
+
* ``None`` : Defaults to ``'cython'`` or globally setting
|
| 446 |
+
``compute.use_numba``
|
| 447 |
+
|
| 448 |
+
engine_kwargs : dict, default None
|
| 449 |
+
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
|
| 450 |
+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
|
| 451 |
+
and ``parallel`` dictionary keys. The values must either be ``True`` or
|
| 452 |
+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
|
| 453 |
+
``{'nopython': True, 'nogil': False, 'parallel': False}``
|
| 454 |
+
|
| 455 |
+
Returns
|
| 456 |
+
-------
|
| 457 |
+
Series or DataFrame
|
| 458 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 459 |
+
|
| 460 |
+
See Also
|
| 461 |
+
--------
|
| 462 |
+
Series.expanding : Calling expanding with Series data.
|
| 463 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 464 |
+
Series.sum : Aggregating sum for Series.
|
| 465 |
+
DataFrame.sum : Aggregating sum for DataFrame.
|
| 466 |
+
|
| 467 |
+
Notes
|
| 468 |
+
-----
|
| 469 |
+
See :ref:`window.numba_engine` and :ref:`enhancingperf.numba` for extended
|
| 470 |
+
documentation and performance considerations for the Numba engine.
|
| 471 |
+
|
| 472 |
+
Examples
|
| 473 |
+
--------
|
| 474 |
+
>>> ser = pd.Series([1, 2, 3, 4], index=["a", "b", "c", "d"])
|
| 475 |
+
>>> ser.expanding().sum()
|
| 476 |
+
a 1.0
|
| 477 |
+
b 3.0
|
| 478 |
+
c 6.0
|
| 479 |
+
d 10.0
|
| 480 |
+
dtype: float64
|
| 481 |
+
"""
|
| 482 |
+
return super().sum(
|
| 483 |
+
numeric_only=numeric_only,
|
| 484 |
+
engine=engine,
|
| 485 |
+
engine_kwargs=engine_kwargs,
|
| 486 |
+
)
|
| 487 |
+
|
| 488 |
+
def max(
|
| 489 |
+
self,
|
| 490 |
+
numeric_only: bool = False,
|
| 491 |
+
engine: Literal["cython", "numba"] | None = None,
|
| 492 |
+
engine_kwargs: dict[str, bool] | None = None,
|
| 493 |
+
):
|
| 494 |
+
"""
|
| 495 |
+
Calculate the expanding maximum.
|
| 496 |
+
|
| 497 |
+
Parameters
|
| 498 |
+
----------
|
| 499 |
+
numeric_only : bool, default False
|
| 500 |
+
Include only float, int, boolean columns.
|
| 501 |
+
|
| 502 |
+
engine : str, default None
|
| 503 |
+
* ``'cython'`` : Runs the operation through C-extensions from cython.
|
| 504 |
+
* ``'numba'`` : Runs the operation through JIT compiled code from numba.
|
| 505 |
+
* ``None`` : Defaults to ``'cython'`` or globally setting
|
| 506 |
+
``compute.use_numba``
|
| 507 |
+
|
| 508 |
+
engine_kwargs : dict, default None
|
| 509 |
+
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
|
| 510 |
+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
|
| 511 |
+
and ``parallel`` dictionary keys. The values must either be ``True`` or
|
| 512 |
+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
|
| 513 |
+
``{'nopython': True, 'nogil': False, 'parallel': False}``
|
| 514 |
+
|
| 515 |
+
Returns
|
| 516 |
+
-------
|
| 517 |
+
Series or DataFrame
|
| 518 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 519 |
+
|
| 520 |
+
See Also
|
| 521 |
+
--------
|
| 522 |
+
Series.expanding : Calling expanding with Series data.
|
| 523 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 524 |
+
Series.max : Aggregating max for Series.
|
| 525 |
+
DataFrame.max : Aggregating max for DataFrame.
|
| 526 |
+
|
| 527 |
+
Notes
|
| 528 |
+
-----
|
| 529 |
+
See :ref:`window.numba_engine` and :ref:`enhancingperf.numba` for extended
|
| 530 |
+
documentation and performance considerations for the Numba engine.
|
| 531 |
+
|
| 532 |
+
Examples
|
| 533 |
+
--------
|
| 534 |
+
>>> ser = pd.Series([3, 2, 1, 4], index=["a", "b", "c", "d"])
|
| 535 |
+
>>> ser.expanding().max()
|
| 536 |
+
a 3.0
|
| 537 |
+
b 3.0
|
| 538 |
+
c 3.0
|
| 539 |
+
d 4.0
|
| 540 |
+
dtype: float64
|
| 541 |
+
"""
|
| 542 |
+
return super().max(
|
| 543 |
+
numeric_only=numeric_only,
|
| 544 |
+
engine=engine,
|
| 545 |
+
engine_kwargs=engine_kwargs,
|
| 546 |
+
)
|
| 547 |
+
|
| 548 |
+
def min(
|
| 549 |
+
self,
|
| 550 |
+
numeric_only: bool = False,
|
| 551 |
+
engine: Literal["cython", "numba"] | None = None,
|
| 552 |
+
engine_kwargs: dict[str, bool] | None = None,
|
| 553 |
+
):
|
| 554 |
+
"""
|
| 555 |
+
Calculate the expanding minimum.
|
| 556 |
+
|
| 557 |
+
Parameters
|
| 558 |
+
----------
|
| 559 |
+
numeric_only : bool, default False
|
| 560 |
+
Include only float, int, boolean columns.
|
| 561 |
+
|
| 562 |
+
engine : str, default None
|
| 563 |
+
* ``'cython'`` : Runs the operation through C-extensions from cython.
|
| 564 |
+
* ``'numba'`` : Runs the operation through JIT compiled code from numba.
|
| 565 |
+
* ``None`` : Defaults to ``'cython'`` or globally setting
|
| 566 |
+
``compute.use_numba``
|
| 567 |
+
|
| 568 |
+
engine_kwargs : dict, default None
|
| 569 |
+
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
|
| 570 |
+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
|
| 571 |
+
and ``parallel`` dictionary keys. The values must either be ``True`` or
|
| 572 |
+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
|
| 573 |
+
``{'nopython': True, 'nogil': False, 'parallel': False}``
|
| 574 |
+
|
| 575 |
+
Returns
|
| 576 |
+
-------
|
| 577 |
+
Series or DataFrame
|
| 578 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 579 |
+
|
| 580 |
+
See Also
|
| 581 |
+
--------
|
| 582 |
+
Series.expanding : Calling expanding with Series data.
|
| 583 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 584 |
+
Series.min : Aggregating min for Series.
|
| 585 |
+
DataFrame.min : Aggregating min for DataFrame.
|
| 586 |
+
|
| 587 |
+
Notes
|
| 588 |
+
-----
|
| 589 |
+
See :ref:`window.numba_engine` and :ref:`enhancingperf.numba` for extended
|
| 590 |
+
documentation and performance considerations for the Numba engine.
|
| 591 |
+
|
| 592 |
+
Examples
|
| 593 |
+
--------
|
| 594 |
+
>>> ser = pd.Series([2, 3, 4, 1], index=["a", "b", "c", "d"])
|
| 595 |
+
>>> ser.expanding().min()
|
| 596 |
+
a 2.0
|
| 597 |
+
b 2.0
|
| 598 |
+
c 2.0
|
| 599 |
+
d 1.0
|
| 600 |
+
dtype: float64
|
| 601 |
+
"""
|
| 602 |
+
return super().min(
|
| 603 |
+
numeric_only=numeric_only,
|
| 604 |
+
engine=engine,
|
| 605 |
+
engine_kwargs=engine_kwargs,
|
| 606 |
+
)
|
| 607 |
+
|
| 608 |
+
def mean(
|
| 609 |
+
self,
|
| 610 |
+
numeric_only: bool = False,
|
| 611 |
+
engine: Literal["cython", "numba"] | None = None,
|
| 612 |
+
engine_kwargs: dict[str, bool] | None = None,
|
| 613 |
+
):
|
| 614 |
+
"""
|
| 615 |
+
Calculate the expanding mean.
|
| 616 |
+
|
| 617 |
+
Parameters
|
| 618 |
+
----------
|
| 619 |
+
numeric_only : bool, default False
|
| 620 |
+
Include only float, int, boolean columns.
|
| 621 |
+
|
| 622 |
+
engine : str, default None
|
| 623 |
+
* ``'cython'`` : Runs the operation through C-extensions from cython.
|
| 624 |
+
* ``'numba'`` : Runs the operation through JIT compiled code from numba.
|
| 625 |
+
* ``None`` : Defaults to ``'cython'`` or globally setting
|
| 626 |
+
``compute.use_numba``
|
| 627 |
+
|
| 628 |
+
engine_kwargs : dict, default None
|
| 629 |
+
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
|
| 630 |
+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
|
| 631 |
+
and ``parallel`` dictionary keys. The values must either be ``True`` or
|
| 632 |
+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
|
| 633 |
+
``{'nopython': True, 'nogil': False, 'parallel': False}``
|
| 634 |
+
|
| 635 |
+
Returns
|
| 636 |
+
-------
|
| 637 |
+
Series or DataFrame
|
| 638 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 639 |
+
|
| 640 |
+
See Also
|
| 641 |
+
--------
|
| 642 |
+
Series.expanding : Calling expanding with Series data.
|
| 643 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 644 |
+
Series.mean : Aggregating mean for Series.
|
| 645 |
+
DataFrame.mean : Aggregating mean for DataFrame.
|
| 646 |
+
|
| 647 |
+
Notes
|
| 648 |
+
-----
|
| 649 |
+
See :ref:`window.numba_engine` and :ref:`enhancingperf.numba` for extended
|
| 650 |
+
documentation and performance considerations for the Numba engine.
|
| 651 |
+
|
| 652 |
+
Examples
|
| 653 |
+
--------
|
| 654 |
+
>>> ser = pd.Series([1, 2, 3, 4], index=["a", "b", "c", "d"])
|
| 655 |
+
>>> ser.expanding().mean()
|
| 656 |
+
a 1.0
|
| 657 |
+
b 1.5
|
| 658 |
+
c 2.0
|
| 659 |
+
d 2.5
|
| 660 |
+
dtype: float64
|
| 661 |
+
"""
|
| 662 |
+
return super().mean(
|
| 663 |
+
numeric_only=numeric_only,
|
| 664 |
+
engine=engine,
|
| 665 |
+
engine_kwargs=engine_kwargs,
|
| 666 |
+
)
|
| 667 |
+
|
| 668 |
+
def median(
|
| 669 |
+
self,
|
| 670 |
+
numeric_only: bool = False,
|
| 671 |
+
engine: Literal["cython", "numba"] | None = None,
|
| 672 |
+
engine_kwargs: dict[str, bool] | None = None,
|
| 673 |
+
):
|
| 674 |
+
"""
|
| 675 |
+
Calculate the expanding median.
|
| 676 |
+
|
| 677 |
+
Parameters
|
| 678 |
+
----------
|
| 679 |
+
numeric_only : bool, default False
|
| 680 |
+
Include only float, int, boolean columns.
|
| 681 |
+
|
| 682 |
+
engine : str, default None
|
| 683 |
+
* ``'cython'`` : Runs the operation through C-extensions from cython.
|
| 684 |
+
* ``'numba'`` : Runs the operation through JIT compiled code from numba.
|
| 685 |
+
* ``None`` : Defaults to ``'cython'`` or globally setting
|
| 686 |
+
``compute.use_numba``
|
| 687 |
+
|
| 688 |
+
engine_kwargs : dict, default None
|
| 689 |
+
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
|
| 690 |
+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
|
| 691 |
+
and ``parallel`` dictionary keys. The values must either be ``True`` or
|
| 692 |
+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
|
| 693 |
+
``{'nopython': True, 'nogil': False, 'parallel': False}``
|
| 694 |
+
|
| 695 |
+
Returns
|
| 696 |
+
-------
|
| 697 |
+
Series or DataFrame
|
| 698 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 699 |
+
|
| 700 |
+
See Also
|
| 701 |
+
--------
|
| 702 |
+
Series.expanding : Calling expanding with Series data.
|
| 703 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 704 |
+
Series.median : Aggregating median for Series.
|
| 705 |
+
DataFrame.median : Aggregating median for DataFrame.
|
| 706 |
+
|
| 707 |
+
Notes
|
| 708 |
+
-----
|
| 709 |
+
See :ref:`window.numba_engine` and :ref:`enhancingperf.numba` for extended
|
| 710 |
+
documentation and performance considerations for the Numba engine.
|
| 711 |
+
|
| 712 |
+
Examples
|
| 713 |
+
--------
|
| 714 |
+
>>> ser = pd.Series([1, 2, 3, 4], index=["a", "b", "c", "d"])
|
| 715 |
+
>>> ser.expanding().median()
|
| 716 |
+
a 1.0
|
| 717 |
+
b 1.5
|
| 718 |
+
c 2.0
|
| 719 |
+
d 2.5
|
| 720 |
+
dtype: float64
|
| 721 |
+
"""
|
| 722 |
+
return super().median(
|
| 723 |
+
numeric_only=numeric_only,
|
| 724 |
+
engine=engine,
|
| 725 |
+
engine_kwargs=engine_kwargs,
|
| 726 |
+
)
|
| 727 |
+
|
| 728 |
+
def std(
|
| 729 |
+
self,
|
| 730 |
+
ddof: int = 1,
|
| 731 |
+
numeric_only: bool = False,
|
| 732 |
+
engine: Literal["cython", "numba"] | None = None,
|
| 733 |
+
engine_kwargs: dict[str, bool] | None = None,
|
| 734 |
+
):
|
| 735 |
+
"""
|
| 736 |
+
Calculate the expanding standard deviation.
|
| 737 |
+
|
| 738 |
+
Parameters
|
| 739 |
+
----------
|
| 740 |
+
ddof : int, default 1
|
| 741 |
+
Delta Degrees of Freedom. The divisor used in calculations
|
| 742 |
+
is ``N - ddof``, where ``N`` represents the number of elements.
|
| 743 |
+
|
| 744 |
+
numeric_only : bool, default False
|
| 745 |
+
Include only float, int, boolean columns.
|
| 746 |
+
|
| 747 |
+
engine : str, default None
|
| 748 |
+
* ``'cython'`` : Runs the operation through C-extensions from cython.
|
| 749 |
+
* ``'numba'`` : Runs the operation through JIT compiled code from numba.
|
| 750 |
+
* ``None`` : Defaults to ``'cython'`` or globally setting
|
| 751 |
+
``compute.use_numba``
|
| 752 |
+
|
| 753 |
+
engine_kwargs : dict, default None
|
| 754 |
+
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
|
| 755 |
+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
|
| 756 |
+
and ``parallel`` dictionary keys. The values must either be ``True`` or
|
| 757 |
+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
|
| 758 |
+
``{'nopython': True, 'nogil': False, 'parallel': False}``
|
| 759 |
+
|
| 760 |
+
Returns
|
| 761 |
+
-------
|
| 762 |
+
Series or DataFrame
|
| 763 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 764 |
+
|
| 765 |
+
See Also
|
| 766 |
+
--------
|
| 767 |
+
numpy.std : Equivalent method for NumPy array.
|
| 768 |
+
Series.expanding : Calling expanding with Series data.
|
| 769 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 770 |
+
Series.std : Aggregating std for Series.
|
| 771 |
+
DataFrame.std : Aggregating std for DataFrame.
|
| 772 |
+
|
| 773 |
+
Notes
|
| 774 |
+
-----
|
| 775 |
+
The default ``ddof`` of 1 used in :meth:`Series.std` is different
|
| 776 |
+
than the default ``ddof`` of 0 in :func:`numpy.std`.
|
| 777 |
+
|
| 778 |
+
A minimum of one period is required for the rolling calculation.
|
| 779 |
+
|
| 780 |
+
Examples
|
| 781 |
+
--------
|
| 782 |
+
>>> s = pd.Series([5, 5, 6, 7, 5, 5, 5])
|
| 783 |
+
|
| 784 |
+
>>> s.expanding(3).std()
|
| 785 |
+
0 NaN
|
| 786 |
+
1 NaN
|
| 787 |
+
2 0.577350
|
| 788 |
+
3 0.957427
|
| 789 |
+
4 0.894427
|
| 790 |
+
5 0.836660
|
| 791 |
+
6 0.786796
|
| 792 |
+
dtype: float64
|
| 793 |
+
"""
|
| 794 |
+
return super().std(
|
| 795 |
+
ddof=ddof,
|
| 796 |
+
numeric_only=numeric_only,
|
| 797 |
+
engine=engine,
|
| 798 |
+
engine_kwargs=engine_kwargs,
|
| 799 |
+
)
|
| 800 |
+
|
| 801 |
+
def var(
|
| 802 |
+
self,
|
| 803 |
+
ddof: int = 1,
|
| 804 |
+
numeric_only: bool = False,
|
| 805 |
+
engine: Literal["cython", "numba"] | None = None,
|
| 806 |
+
engine_kwargs: dict[str, bool] | None = None,
|
| 807 |
+
):
|
| 808 |
+
"""
|
| 809 |
+
Calculate the expanding variance.
|
| 810 |
+
|
| 811 |
+
Parameters
|
| 812 |
+
----------
|
| 813 |
+
ddof : int, default 1
|
| 814 |
+
Delta Degrees of Freedom. The divisor used in calculations
|
| 815 |
+
is ``N - ddof``, where ``N`` represents the number of elements.
|
| 816 |
+
|
| 817 |
+
numeric_only : bool, default False
|
| 818 |
+
Include only float, int, boolean columns.
|
| 819 |
+
|
| 820 |
+
engine : str, default None
|
| 821 |
+
* ``'cython'`` : Runs the operation through C-extensions from cython.
|
| 822 |
+
* ``'numba'`` : Runs the operation through JIT compiled code from numba.
|
| 823 |
+
* ``None`` : Defaults to ``'cython'`` or globally setting
|
| 824 |
+
``compute.use_numba``
|
| 825 |
+
|
| 826 |
+
engine_kwargs : dict, default None
|
| 827 |
+
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
|
| 828 |
+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
|
| 829 |
+
and ``parallel`` dictionary keys. The values must either be ``True`` or
|
| 830 |
+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
|
| 831 |
+
``{'nopython': True, 'nogil': False, 'parallel': False}``
|
| 832 |
+
|
| 833 |
+
Returns
|
| 834 |
+
-------
|
| 835 |
+
Series or DataFrame
|
| 836 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 837 |
+
|
| 838 |
+
See Also
|
| 839 |
+
--------
|
| 840 |
+
numpy.var : Equivalent method for NumPy array.
|
| 841 |
+
Series.expanding : Calling expanding with Series data.
|
| 842 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 843 |
+
Series.var : Aggregating var for Series.
|
| 844 |
+
DataFrame.var : Aggregating var for DataFrame.
|
| 845 |
+
|
| 846 |
+
Notes
|
| 847 |
+
-----
|
| 848 |
+
The default ``ddof`` of 1 used in :meth:`Series.var` is different
|
| 849 |
+
than the default ``ddof`` of 0 in :func:`numpy.var`.
|
| 850 |
+
|
| 851 |
+
A minimum of one period is required for the rolling calculation.
|
| 852 |
+
|
| 853 |
+
Examples
|
| 854 |
+
--------
|
| 855 |
+
>>> s = pd.Series([5, 5, 6, 7, 5, 5, 5])
|
| 856 |
+
|
| 857 |
+
>>> s.expanding(3).var()
|
| 858 |
+
0 NaN
|
| 859 |
+
1 NaN
|
| 860 |
+
2 0.333333
|
| 861 |
+
3 0.916667
|
| 862 |
+
4 0.800000
|
| 863 |
+
5 0.700000
|
| 864 |
+
6 0.619048
|
| 865 |
+
dtype: float64
|
| 866 |
+
"""
|
| 867 |
+
return super().var(
|
| 868 |
+
ddof=ddof,
|
| 869 |
+
numeric_only=numeric_only,
|
| 870 |
+
engine=engine,
|
| 871 |
+
engine_kwargs=engine_kwargs,
|
| 872 |
+
)
|
| 873 |
+
|
| 874 |
+
def sem(self, ddof: int = 1, numeric_only: bool = False):
|
| 875 |
+
"""
|
| 876 |
+
Calculate the expanding standard error of mean.
|
| 877 |
+
|
| 878 |
+
Parameters
|
| 879 |
+
----------
|
| 880 |
+
ddof : int, default 1
|
| 881 |
+
Delta Degrees of Freedom. The divisor used in calculations
|
| 882 |
+
is ``N - ddof``, where ``N`` represents the number of elements.
|
| 883 |
+
|
| 884 |
+
numeric_only : bool, default False
|
| 885 |
+
Include only float, int, boolean columns.
|
| 886 |
+
|
| 887 |
+
Returns
|
| 888 |
+
-------
|
| 889 |
+
Series or DataFrame
|
| 890 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 891 |
+
|
| 892 |
+
See Also
|
| 893 |
+
--------
|
| 894 |
+
Series.expanding : Calling expanding with Series data.
|
| 895 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 896 |
+
Series.sem : Aggregating sem for Series.
|
| 897 |
+
DataFrame.sem : Aggregating sem for DataFrame.
|
| 898 |
+
|
| 899 |
+
Notes
|
| 900 |
+
-----
|
| 901 |
+
A minimum of one period is required for the calculation.
|
| 902 |
+
|
| 903 |
+
Examples
|
| 904 |
+
--------
|
| 905 |
+
>>> s = pd.Series([0, 1, 2, 3])
|
| 906 |
+
|
| 907 |
+
>>> s.expanding().sem()
|
| 908 |
+
0 NaN
|
| 909 |
+
1 0.500000
|
| 910 |
+
2 0.577350
|
| 911 |
+
3 0.645497
|
| 912 |
+
dtype: float64
|
| 913 |
+
"""
|
| 914 |
+
return super().sem(ddof=ddof, numeric_only=numeric_only)
|
| 915 |
+
|
| 916 |
+
def skew(self, numeric_only: bool = False):
|
| 917 |
+
"""
|
| 918 |
+
Calculate the expanding unbiased skewness.
|
| 919 |
+
|
| 920 |
+
Parameters
|
| 921 |
+
----------
|
| 922 |
+
numeric_only : bool, default False
|
| 923 |
+
Include only float, int, boolean columns.
|
| 924 |
+
|
| 925 |
+
Returns
|
| 926 |
+
-------
|
| 927 |
+
Series or DataFrame
|
| 928 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 929 |
+
|
| 930 |
+
See Also
|
| 931 |
+
--------
|
| 932 |
+
scipy.stats.skew : Third moment of a probability density.
|
| 933 |
+
Series.expanding : Calling expanding with Series data.
|
| 934 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 935 |
+
Series.skew : Aggregating skew for Series.
|
| 936 |
+
DataFrame.skew : Aggregating skew for DataFrame.
|
| 937 |
+
|
| 938 |
+
Notes
|
| 939 |
+
-----
|
| 940 |
+
A minimum of three periods is required for the rolling calculation.
|
| 941 |
+
|
| 942 |
+
Examples
|
| 943 |
+
--------
|
| 944 |
+
>>> ser = pd.Series([-1, 0, 2, -1, 2], index=["a", "b", "c", "d", "e"])
|
| 945 |
+
>>> ser.expanding().skew()
|
| 946 |
+
a NaN
|
| 947 |
+
b NaN
|
| 948 |
+
c 0.935220
|
| 949 |
+
d 1.414214
|
| 950 |
+
e 0.315356
|
| 951 |
+
dtype: float64
|
| 952 |
+
"""
|
| 953 |
+
return super().skew(numeric_only=numeric_only)
|
| 954 |
+
|
| 955 |
+
def kurt(self, numeric_only: bool = False):
|
| 956 |
+
"""
|
| 957 |
+
Calculate the expanding Fisher's definition of kurtosis without bias.
|
| 958 |
+
|
| 959 |
+
Parameters
|
| 960 |
+
----------
|
| 961 |
+
numeric_only : bool, default False
|
| 962 |
+
Include only float, int, boolean columns.
|
| 963 |
+
|
| 964 |
+
Returns
|
| 965 |
+
-------
|
| 966 |
+
Series or DataFrame
|
| 967 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 968 |
+
|
| 969 |
+
See Also
|
| 970 |
+
--------
|
| 971 |
+
scipy.stats.kurtosis : Reference SciPy method.
|
| 972 |
+
Series.expanding : Calling expanding with Series data.
|
| 973 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 974 |
+
Series.kurt : Aggregating kurt for Series.
|
| 975 |
+
DataFrame.kurt : Aggregating kurt for DataFrame.
|
| 976 |
+
|
| 977 |
+
Notes
|
| 978 |
+
-----
|
| 979 |
+
A minimum of four periods is required for the calculation.
|
| 980 |
+
|
| 981 |
+
Examples
|
| 982 |
+
--------
|
| 983 |
+
The example below will show a rolling calculation with a window size of
|
| 984 |
+
four matching the equivalent function call using `scipy.stats`.
|
| 985 |
+
|
| 986 |
+
>>> arr = [1, 2, 3, 4, 999]
|
| 987 |
+
>>> import scipy.stats
|
| 988 |
+
>>> print(f"{scipy.stats.kurtosis(arr[:-1], bias=False):.6f}")
|
| 989 |
+
-1.200000
|
| 990 |
+
>>> print(f"{scipy.stats.kurtosis(arr, bias=False):.6f}")
|
| 991 |
+
4.999874
|
| 992 |
+
>>> s = pd.Series(arr)
|
| 993 |
+
>>> s.expanding(4).kurt()
|
| 994 |
+
0 NaN
|
| 995 |
+
1 NaN
|
| 996 |
+
2 NaN
|
| 997 |
+
3 -1.200000
|
| 998 |
+
4 4.999874
|
| 999 |
+
dtype: float64
|
| 1000 |
+
"""
|
| 1001 |
+
return super().kurt(numeric_only=numeric_only)
|
| 1002 |
+
|
| 1003 |
+
def first(self, numeric_only: bool = False):
|
| 1004 |
+
"""
|
| 1005 |
+
Calculate the expanding First (left-most) element of the window.
|
| 1006 |
+
|
| 1007 |
+
Parameters
|
| 1008 |
+
----------
|
| 1009 |
+
numeric_only : bool, default False
|
| 1010 |
+
Include only float, int, boolean columns.
|
| 1011 |
+
|
| 1012 |
+
Returns
|
| 1013 |
+
-------
|
| 1014 |
+
Series or DataFrame
|
| 1015 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 1016 |
+
|
| 1017 |
+
See Also
|
| 1018 |
+
--------
|
| 1019 |
+
GroupBy.first : Similar method for GroupBy objects.
|
| 1020 |
+
Expanding.last : Method to get the last element in each window.
|
| 1021 |
+
|
| 1022 |
+
Examples
|
| 1023 |
+
--------
|
| 1024 |
+
The example below will show an expanding calculation with a window size of
|
| 1025 |
+
three.
|
| 1026 |
+
|
| 1027 |
+
>>> s = pd.Series(range(5))
|
| 1028 |
+
>>> s.expanding(3).first()
|
| 1029 |
+
0 NaN
|
| 1030 |
+
1 NaN
|
| 1031 |
+
2 0.0
|
| 1032 |
+
3 0.0
|
| 1033 |
+
4 0.0
|
| 1034 |
+
dtype: float64
|
| 1035 |
+
"""
|
| 1036 |
+
return super().first(numeric_only=numeric_only)
|
| 1037 |
+
|
| 1038 |
+
def last(self, numeric_only: bool = False):
|
| 1039 |
+
"""
|
| 1040 |
+
Calculate the expanding Last (right-most) element of the window.
|
| 1041 |
+
|
| 1042 |
+
Parameters
|
| 1043 |
+
----------
|
| 1044 |
+
numeric_only : bool, default False
|
| 1045 |
+
Include only float, int, boolean columns.
|
| 1046 |
+
|
| 1047 |
+
Returns
|
| 1048 |
+
-------
|
| 1049 |
+
Series or DataFrame
|
| 1050 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 1051 |
+
|
| 1052 |
+
See Also
|
| 1053 |
+
--------
|
| 1054 |
+
GroupBy.last : Similar method for GroupBy objects.
|
| 1055 |
+
Expanding.first : Method to get the first element in each window.
|
| 1056 |
+
|
| 1057 |
+
Examples
|
| 1058 |
+
--------
|
| 1059 |
+
The example below will show an expanding calculation with a window size of
|
| 1060 |
+
three.
|
| 1061 |
+
|
| 1062 |
+
>>> s = pd.Series(range(5))
|
| 1063 |
+
>>> s.expanding(3).last()
|
| 1064 |
+
0 NaN
|
| 1065 |
+
1 NaN
|
| 1066 |
+
2 2.0
|
| 1067 |
+
3 3.0
|
| 1068 |
+
4 4.0
|
| 1069 |
+
dtype: float64
|
| 1070 |
+
"""
|
| 1071 |
+
return super().last(numeric_only=numeric_only)
|
| 1072 |
+
|
| 1073 |
+
def quantile(
|
| 1074 |
+
self,
|
| 1075 |
+
q: float,
|
| 1076 |
+
interpolation: QuantileInterpolation = "linear",
|
| 1077 |
+
numeric_only: bool = False,
|
| 1078 |
+
):
|
| 1079 |
+
"""
|
| 1080 |
+
Calculate the expanding quantile.
|
| 1081 |
+
|
| 1082 |
+
Parameters
|
| 1083 |
+
----------
|
| 1084 |
+
q : float
|
| 1085 |
+
Quantile to compute. 0 <= quantile <= 1.
|
| 1086 |
+
|
| 1087 |
+
interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'}
|
| 1088 |
+
This optional parameter specifies the interpolation method to use,
|
| 1089 |
+
when the desired quantile lies between two data points `i` and `j`:
|
| 1090 |
+
|
| 1091 |
+
* linear: `i + (j - i) * fraction`, where `fraction` is the
|
| 1092 |
+
fractional part of the index surrounded by `i` and `j`.
|
| 1093 |
+
* lower: `i`.
|
| 1094 |
+
* higher: `j`.
|
| 1095 |
+
* nearest: `i` or `j` whichever is nearest.
|
| 1096 |
+
* midpoint: (`i` + `j`) / 2.
|
| 1097 |
+
|
| 1098 |
+
numeric_only : bool, default False
|
| 1099 |
+
Include only float, int, boolean columns.
|
| 1100 |
+
|
| 1101 |
+
Returns
|
| 1102 |
+
-------
|
| 1103 |
+
Series or DataFrame
|
| 1104 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 1105 |
+
|
| 1106 |
+
See Also
|
| 1107 |
+
--------
|
| 1108 |
+
Series.expanding : Calling expanding with Series data.
|
| 1109 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 1110 |
+
Series.quantile : Aggregating quantile for Series.
|
| 1111 |
+
DataFrame.quantile : Aggregating quantile for DataFrame.
|
| 1112 |
+
|
| 1113 |
+
Examples
|
| 1114 |
+
--------
|
| 1115 |
+
>>> ser = pd.Series([1, 2, 3, 4, 5, 6], index=["a", "b", "c", "d", "e", "f"])
|
| 1116 |
+
>>> ser.expanding(min_periods=4).quantile(0.25)
|
| 1117 |
+
a NaN
|
| 1118 |
+
b NaN
|
| 1119 |
+
c NaN
|
| 1120 |
+
d 1.75
|
| 1121 |
+
e 2.00
|
| 1122 |
+
f 2.25
|
| 1123 |
+
dtype: float64
|
| 1124 |
+
"""
|
| 1125 |
+
return super().quantile(
|
| 1126 |
+
q=q,
|
| 1127 |
+
interpolation=interpolation,
|
| 1128 |
+
numeric_only=numeric_only,
|
| 1129 |
+
)
|
| 1130 |
+
|
| 1131 |
+
def rank(
|
| 1132 |
+
self,
|
| 1133 |
+
method: WindowingRankType = "average",
|
| 1134 |
+
ascending: bool = True,
|
| 1135 |
+
pct: bool = False,
|
| 1136 |
+
numeric_only: bool = False,
|
| 1137 |
+
):
|
| 1138 |
+
"""
|
| 1139 |
+
Calculate the expanding rank.
|
| 1140 |
+
|
| 1141 |
+
Parameters
|
| 1142 |
+
----------
|
| 1143 |
+
method : {'average', 'min', 'max'}, default 'average'
|
| 1144 |
+
How to rank the group of records that have the same value (i.e. ties):
|
| 1145 |
+
|
| 1146 |
+
* average: average rank of the group
|
| 1147 |
+
* min: lowest rank in the group
|
| 1148 |
+
* max: highest rank in the group
|
| 1149 |
+
|
| 1150 |
+
ascending : bool, default True
|
| 1151 |
+
Whether or not the elements should be ranked in ascending order.
|
| 1152 |
+
pct : bool, default False
|
| 1153 |
+
Whether or not to display the returned rankings in percentile
|
| 1154 |
+
form.
|
| 1155 |
+
numeric_only : bool, default False
|
| 1156 |
+
Include only float, int, boolean columns.
|
| 1157 |
+
|
| 1158 |
+
Returns
|
| 1159 |
+
-------
|
| 1160 |
+
Series or DataFrame
|
| 1161 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 1162 |
+
|
| 1163 |
+
See Also
|
| 1164 |
+
--------
|
| 1165 |
+
Series.expanding : Calling expanding with Series data.
|
| 1166 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 1167 |
+
Series.rank : Aggregating rank for Series.
|
| 1168 |
+
DataFrame.rank : Aggregating rank for DataFrame.
|
| 1169 |
+
|
| 1170 |
+
Examples
|
| 1171 |
+
--------
|
| 1172 |
+
>>> s = pd.Series([1, 4, 2, 3, 5, 3])
|
| 1173 |
+
>>> s.expanding().rank()
|
| 1174 |
+
0 1.0
|
| 1175 |
+
1 2.0
|
| 1176 |
+
2 2.0
|
| 1177 |
+
3 3.0
|
| 1178 |
+
4 5.0
|
| 1179 |
+
5 3.5
|
| 1180 |
+
dtype: float64
|
| 1181 |
+
|
| 1182 |
+
>>> s.expanding().rank(method="max")
|
| 1183 |
+
0 1.0
|
| 1184 |
+
1 2.0
|
| 1185 |
+
2 2.0
|
| 1186 |
+
3 3.0
|
| 1187 |
+
4 5.0
|
| 1188 |
+
5 4.0
|
| 1189 |
+
dtype: float64
|
| 1190 |
+
|
| 1191 |
+
>>> s.expanding().rank(method="min")
|
| 1192 |
+
0 1.0
|
| 1193 |
+
1 2.0
|
| 1194 |
+
2 2.0
|
| 1195 |
+
3 3.0
|
| 1196 |
+
4 5.0
|
| 1197 |
+
5 3.0
|
| 1198 |
+
dtype: float64
|
| 1199 |
+
"""
|
| 1200 |
+
return super().rank(
|
| 1201 |
+
method=method,
|
| 1202 |
+
ascending=ascending,
|
| 1203 |
+
pct=pct,
|
| 1204 |
+
numeric_only=numeric_only,
|
| 1205 |
+
)
|
| 1206 |
+
|
| 1207 |
+
def nunique(
|
| 1208 |
+
self,
|
| 1209 |
+
numeric_only: bool = False,
|
| 1210 |
+
):
|
| 1211 |
+
"""
|
| 1212 |
+
Calculate the expanding nunique.
|
| 1213 |
+
|
| 1214 |
+
.. versionadded:: 3.0.0
|
| 1215 |
+
|
| 1216 |
+
Parameters
|
| 1217 |
+
----------
|
| 1218 |
+
numeric_only : bool, default False
|
| 1219 |
+
Include only float, int, boolean columns.
|
| 1220 |
+
|
| 1221 |
+
Returns
|
| 1222 |
+
-------
|
| 1223 |
+
Series or DataFrame
|
| 1224 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 1225 |
+
|
| 1226 |
+
See Also
|
| 1227 |
+
--------
|
| 1228 |
+
Series.expanding : Calling expanding with Series data.
|
| 1229 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 1230 |
+
Series.nunique : Aggregating nunique for Series.
|
| 1231 |
+
DataFrame.nunique : Aggregating nunique for DataFrame.
|
| 1232 |
+
|
| 1233 |
+
Examples
|
| 1234 |
+
--------
|
| 1235 |
+
>>> s = pd.Series([1, 4, 2, 3, 5, 3])
|
| 1236 |
+
>>> s.expanding().nunique()
|
| 1237 |
+
0 1.0
|
| 1238 |
+
1 2.0
|
| 1239 |
+
2 3.0
|
| 1240 |
+
3 4.0
|
| 1241 |
+
4 5.0
|
| 1242 |
+
5 5.0
|
| 1243 |
+
dtype: float64
|
| 1244 |
+
"""
|
| 1245 |
+
return super().nunique(
|
| 1246 |
+
numeric_only=numeric_only,
|
| 1247 |
+
)
|
| 1248 |
+
|
| 1249 |
+
def cov(
|
| 1250 |
+
self,
|
| 1251 |
+
other: DataFrame | Series | None = None,
|
| 1252 |
+
pairwise: bool | None = None,
|
| 1253 |
+
ddof: int = 1,
|
| 1254 |
+
numeric_only: bool = False,
|
| 1255 |
+
):
|
| 1256 |
+
"""
|
| 1257 |
+
Calculate the expanding sample covariance.
|
| 1258 |
+
|
| 1259 |
+
Parameters
|
| 1260 |
+
----------
|
| 1261 |
+
other : Series or DataFrame, optional
|
| 1262 |
+
If not supplied then will default to self and produce pairwise
|
| 1263 |
+
output.
|
| 1264 |
+
pairwise : bool, default None
|
| 1265 |
+
If False then only matching columns between self and other will be
|
| 1266 |
+
used and the output will be a DataFrame.
|
| 1267 |
+
If True then all pairwise combinations will be calculated and the
|
| 1268 |
+
output will be a MultiIndexed DataFrame in the case of DataFrame
|
| 1269 |
+
inputs. In the case of missing elements, only complete pairwise
|
| 1270 |
+
observations will be used.
|
| 1271 |
+
ddof : int, default 1
|
| 1272 |
+
Delta Degrees of Freedom. The divisor used in calculations
|
| 1273 |
+
is ``N - ddof``, where ``N`` represents the number of elements.
|
| 1274 |
+
numeric_only : bool, default False
|
| 1275 |
+
Include only float, int, boolean columns.
|
| 1276 |
+
|
| 1277 |
+
Returns
|
| 1278 |
+
-------
|
| 1279 |
+
Series or DataFrame
|
| 1280 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 1281 |
+
|
| 1282 |
+
See Also
|
| 1283 |
+
--------
|
| 1284 |
+
Series.expanding : Calling expanding with Series data.
|
| 1285 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 1286 |
+
Series.cov : Aggregating cov for Series.
|
| 1287 |
+
DataFrame.cov : Aggregating cov for DataFrame.
|
| 1288 |
+
|
| 1289 |
+
Examples
|
| 1290 |
+
--------
|
| 1291 |
+
>>> ser1 = pd.Series([1, 2, 3, 4], index=["a", "b", "c", "d"])
|
| 1292 |
+
>>> ser2 = pd.Series([10, 11, 13, 16], index=["a", "b", "c", "d"])
|
| 1293 |
+
>>> ser1.expanding().cov(ser2)
|
| 1294 |
+
a NaN
|
| 1295 |
+
b 0.500000
|
| 1296 |
+
c 1.500000
|
| 1297 |
+
d 3.333333
|
| 1298 |
+
dtype: float64
|
| 1299 |
+
"""
|
| 1300 |
+
return super().cov(
|
| 1301 |
+
other=other,
|
| 1302 |
+
pairwise=pairwise,
|
| 1303 |
+
ddof=ddof,
|
| 1304 |
+
numeric_only=numeric_only,
|
| 1305 |
+
)
|
| 1306 |
+
|
| 1307 |
+
def corr(
|
| 1308 |
+
self,
|
| 1309 |
+
other: DataFrame | Series | None = None,
|
| 1310 |
+
pairwise: bool | None = None,
|
| 1311 |
+
ddof: int = 1,
|
| 1312 |
+
numeric_only: bool = False,
|
| 1313 |
+
):
|
| 1314 |
+
"""
|
| 1315 |
+
Calculate the expanding correlation.
|
| 1316 |
+
|
| 1317 |
+
Parameters
|
| 1318 |
+
----------
|
| 1319 |
+
other : Series or DataFrame, optional
|
| 1320 |
+
If not supplied then will default to self and produce pairwise
|
| 1321 |
+
output.
|
| 1322 |
+
pairwise : bool, default None
|
| 1323 |
+
If False then only matching columns between self and other will be
|
| 1324 |
+
used and the output will be a DataFrame.
|
| 1325 |
+
If True then all pairwise combinations will be calculated and the
|
| 1326 |
+
output will be a MultiIndexed DataFrame in the case of DataFrame
|
| 1327 |
+
inputs. In the case of missing elements, only complete pairwise
|
| 1328 |
+
observations will be used.
|
| 1329 |
+
ddof : int, default 1
|
| 1330 |
+
Delta Degrees of Freedom. The divisor used in calculations
|
| 1331 |
+
is ``N - ddof``, where ``N`` represents the number of elements.
|
| 1332 |
+
|
| 1333 |
+
numeric_only : bool, default False
|
| 1334 |
+
Include only float, int, boolean columns.
|
| 1335 |
+
|
| 1336 |
+
Returns
|
| 1337 |
+
-------
|
| 1338 |
+
Series or DataFrame
|
| 1339 |
+
Return type is the same as the original object with ``np.float64`` dtype.
|
| 1340 |
+
|
| 1341 |
+
See Also
|
| 1342 |
+
--------
|
| 1343 |
+
cov : Similar method to calculate covariance.
|
| 1344 |
+
numpy.corrcoef : NumPy Pearson's correlation calculation.
|
| 1345 |
+
Series.expanding : Calling expanding with Series data.
|
| 1346 |
+
DataFrame.expanding : Calling expanding with DataFrames.
|
| 1347 |
+
Series.corr : Aggregating corr for Series.
|
| 1348 |
+
DataFrame.corr : Aggregating corr for DataFrame.
|
| 1349 |
+
|
| 1350 |
+
Notes
|
| 1351 |
+
-----
|
| 1352 |
+
|
| 1353 |
+
This function uses Pearson's definition of correlation
|
| 1354 |
+
(https://en.wikipedia.org/wiki/Pearson_correlation_coefficient).
|
| 1355 |
+
|
| 1356 |
+
When `other` is not specified, the output will be self correlation (e.g.
|
| 1357 |
+
all 1's), except for :class:`~pandas.DataFrame` inputs with `pairwise`
|
| 1358 |
+
set to `True`.
|
| 1359 |
+
|
| 1360 |
+
Function will return ``NaN`` for correlations of equal valued sequences;
|
| 1361 |
+
this is the result of a 0/0 division error.
|
| 1362 |
+
|
| 1363 |
+
When `pairwise` is set to `False`, only matching columns between `self` and
|
| 1364 |
+
`other` will be used.
|
| 1365 |
+
|
| 1366 |
+
When `pairwise` is set to `True`, the output will be a MultiIndex DataFrame
|
| 1367 |
+
with the original index on the first level, and the `other` DataFrame
|
| 1368 |
+
columns on the second level.
|
| 1369 |
+
|
| 1370 |
+
In the case of missing elements, only complete pairwise observations
|
| 1371 |
+
will be used.
|
| 1372 |
+
|
| 1373 |
+
Examples
|
| 1374 |
+
--------
|
| 1375 |
+
>>> ser1 = pd.Series([1, 2, 3, 4], index=["a", "b", "c", "d"])
|
| 1376 |
+
>>> ser2 = pd.Series([10, 11, 13, 16], index=["a", "b", "c", "d"])
|
| 1377 |
+
>>> ser1.expanding().corr(ser2)
|
| 1378 |
+
a NaN
|
| 1379 |
+
b 1.000000
|
| 1380 |
+
c 0.981981
|
| 1381 |
+
d 0.975900
|
| 1382 |
+
dtype: float64
|
| 1383 |
+
"""
|
| 1384 |
+
return super().corr(
|
| 1385 |
+
other=other,
|
| 1386 |
+
pairwise=pairwise,
|
| 1387 |
+
ddof=ddof,
|
| 1388 |
+
numeric_only=numeric_only,
|
| 1389 |
+
)
|
| 1390 |
+
|
| 1391 |
+
|
| 1392 |
+
@set_module("pandas.api.typing")
|
| 1393 |
+
class ExpandingGroupby(BaseWindowGroupby, Expanding):
|
| 1394 |
+
"""
|
| 1395 |
+
Provide an expanding groupby implementation.
|
| 1396 |
+
"""
|
| 1397 |
+
|
| 1398 |
+
_attributes = Expanding._attributes + BaseWindowGroupby._attributes
|
| 1399 |
+
|
| 1400 |
+
def _get_window_indexer(self) -> GroupbyIndexer:
|
| 1401 |
+
"""
|
| 1402 |
+
Return an indexer class that will compute the window start and end bounds
|
| 1403 |
+
|
| 1404 |
+
Returns
|
| 1405 |
+
-------
|
| 1406 |
+
GroupbyIndexer
|
| 1407 |
+
"""
|
| 1408 |
+
window_indexer = GroupbyIndexer(
|
| 1409 |
+
groupby_indices=self._grouper.indices,
|
| 1410 |
+
window_indexer=ExpandingIndexer,
|
| 1411 |
+
)
|
| 1412 |
+
return window_indexer
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/numba_.py
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import functools
|
| 4 |
+
from typing import (
|
| 5 |
+
TYPE_CHECKING,
|
| 6 |
+
Any,
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
from pandas.compat._optional import import_optional_dependency
|
| 12 |
+
|
| 13 |
+
from pandas.core.util.numba_ import jit_user_function
|
| 14 |
+
|
| 15 |
+
if TYPE_CHECKING:
|
| 16 |
+
from collections.abc import Callable
|
| 17 |
+
|
| 18 |
+
from pandas._typing import Scalar
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@functools.cache
|
| 22 |
+
def generate_numba_apply_func(
|
| 23 |
+
func: Callable[..., Scalar],
|
| 24 |
+
nopython: bool,
|
| 25 |
+
nogil: bool,
|
| 26 |
+
parallel: bool,
|
| 27 |
+
):
|
| 28 |
+
"""
|
| 29 |
+
Generate a numba jitted apply function specified by values from engine_kwargs.
|
| 30 |
+
|
| 31 |
+
1. jit the user's function
|
| 32 |
+
2. Return a rolling apply function with the jitted function inline
|
| 33 |
+
|
| 34 |
+
Configurations specified in engine_kwargs apply to both the user's
|
| 35 |
+
function _AND_ the rolling apply function.
|
| 36 |
+
|
| 37 |
+
Parameters
|
| 38 |
+
----------
|
| 39 |
+
func : function
|
| 40 |
+
function to be applied to each window and will be JITed
|
| 41 |
+
nopython : bool
|
| 42 |
+
nopython to be passed into numba.jit
|
| 43 |
+
nogil : bool
|
| 44 |
+
nogil to be passed into numba.jit
|
| 45 |
+
parallel : bool
|
| 46 |
+
parallel to be passed into numba.jit
|
| 47 |
+
|
| 48 |
+
Returns
|
| 49 |
+
-------
|
| 50 |
+
Numba function
|
| 51 |
+
"""
|
| 52 |
+
numba_func = jit_user_function(func)
|
| 53 |
+
if TYPE_CHECKING:
|
| 54 |
+
import numba
|
| 55 |
+
else:
|
| 56 |
+
numba = import_optional_dependency("numba")
|
| 57 |
+
|
| 58 |
+
@numba.jit(nopython=nopython, nogil=nogil, parallel=parallel)
|
| 59 |
+
def roll_apply(
|
| 60 |
+
values: np.ndarray,
|
| 61 |
+
begin: np.ndarray,
|
| 62 |
+
end: np.ndarray,
|
| 63 |
+
minimum_periods: int,
|
| 64 |
+
*args: Any,
|
| 65 |
+
) -> np.ndarray:
|
| 66 |
+
result = np.empty(len(begin))
|
| 67 |
+
for i in numba.prange(len(result)):
|
| 68 |
+
start = begin[i]
|
| 69 |
+
stop = end[i]
|
| 70 |
+
window = values[start:stop]
|
| 71 |
+
count_nan = np.sum(np.isnan(window))
|
| 72 |
+
if len(window) - count_nan >= minimum_periods:
|
| 73 |
+
result[i] = numba_func(window, *args)
|
| 74 |
+
else:
|
| 75 |
+
result[i] = np.nan
|
| 76 |
+
return result
|
| 77 |
+
|
| 78 |
+
return roll_apply
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@functools.cache
|
| 82 |
+
def generate_numba_ewm_func(
|
| 83 |
+
nopython: bool,
|
| 84 |
+
nogil: bool,
|
| 85 |
+
parallel: bool,
|
| 86 |
+
com: float,
|
| 87 |
+
adjust: bool,
|
| 88 |
+
ignore_na: bool,
|
| 89 |
+
deltas: tuple,
|
| 90 |
+
normalize: bool,
|
| 91 |
+
):
|
| 92 |
+
"""
|
| 93 |
+
Generate a numba jitted ewm mean or sum function specified by values
|
| 94 |
+
from engine_kwargs.
|
| 95 |
+
|
| 96 |
+
Parameters
|
| 97 |
+
----------
|
| 98 |
+
nopython : bool
|
| 99 |
+
nopython to be passed into numba.jit
|
| 100 |
+
nogil : bool
|
| 101 |
+
nogil to be passed into numba.jit
|
| 102 |
+
parallel : bool
|
| 103 |
+
parallel to be passed into numba.jit
|
| 104 |
+
com : float
|
| 105 |
+
adjust : bool
|
| 106 |
+
ignore_na : bool
|
| 107 |
+
deltas : tuple
|
| 108 |
+
normalize : bool
|
| 109 |
+
|
| 110 |
+
Returns
|
| 111 |
+
-------
|
| 112 |
+
Numba function
|
| 113 |
+
"""
|
| 114 |
+
if TYPE_CHECKING:
|
| 115 |
+
import numba
|
| 116 |
+
else:
|
| 117 |
+
numba = import_optional_dependency("numba")
|
| 118 |
+
|
| 119 |
+
@numba.jit(nopython=nopython, nogil=nogil, parallel=parallel)
|
| 120 |
+
def ewm(
|
| 121 |
+
values: np.ndarray,
|
| 122 |
+
begin: np.ndarray,
|
| 123 |
+
end: np.ndarray,
|
| 124 |
+
minimum_periods: int,
|
| 125 |
+
) -> np.ndarray:
|
| 126 |
+
result = np.empty(len(values))
|
| 127 |
+
alpha = 1.0 / (1.0 + com)
|
| 128 |
+
old_wt_factor = 1.0 - alpha
|
| 129 |
+
new_wt = 1.0 if adjust else alpha
|
| 130 |
+
|
| 131 |
+
for i in numba.prange(len(begin)):
|
| 132 |
+
start = begin[i]
|
| 133 |
+
stop = end[i]
|
| 134 |
+
window = values[start:stop]
|
| 135 |
+
sub_result = np.empty(len(window))
|
| 136 |
+
|
| 137 |
+
weighted = window[0]
|
| 138 |
+
nobs = int(not np.isnan(weighted))
|
| 139 |
+
sub_result[0] = weighted if nobs >= minimum_periods else np.nan
|
| 140 |
+
old_wt = 1.0
|
| 141 |
+
|
| 142 |
+
for j in range(1, len(window)):
|
| 143 |
+
cur = window[j]
|
| 144 |
+
is_observation = not np.isnan(cur)
|
| 145 |
+
nobs += is_observation
|
| 146 |
+
if not np.isnan(weighted):
|
| 147 |
+
if is_observation or not ignore_na:
|
| 148 |
+
if normalize:
|
| 149 |
+
# note that len(deltas) = len(vals) - 1 and deltas[i]
|
| 150 |
+
# is to be used in conjunction with vals[i+1]
|
| 151 |
+
old_wt *= old_wt_factor ** deltas[start + j - 1]
|
| 152 |
+
if not adjust and com == 1:
|
| 153 |
+
# update in case of irregular-interval time series
|
| 154 |
+
new_wt = 1.0 - old_wt
|
| 155 |
+
else:
|
| 156 |
+
weighted = old_wt_factor * weighted
|
| 157 |
+
if is_observation:
|
| 158 |
+
if normalize:
|
| 159 |
+
# avoid numerical errors on constant series
|
| 160 |
+
if weighted != cur:
|
| 161 |
+
weighted = old_wt * weighted + new_wt * cur
|
| 162 |
+
if normalize:
|
| 163 |
+
weighted = weighted / (old_wt + new_wt)
|
| 164 |
+
if adjust:
|
| 165 |
+
old_wt += new_wt
|
| 166 |
+
else:
|
| 167 |
+
old_wt = 1.0
|
| 168 |
+
else:
|
| 169 |
+
weighted += cur
|
| 170 |
+
elif is_observation:
|
| 171 |
+
weighted = cur
|
| 172 |
+
|
| 173 |
+
sub_result[j] = weighted if nobs >= minimum_periods else np.nan
|
| 174 |
+
|
| 175 |
+
result[start:stop] = sub_result
|
| 176 |
+
|
| 177 |
+
return result
|
| 178 |
+
|
| 179 |
+
return ewm
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
@functools.cache
|
| 183 |
+
def generate_numba_table_func(
|
| 184 |
+
func: Callable[..., np.ndarray],
|
| 185 |
+
nopython: bool,
|
| 186 |
+
nogil: bool,
|
| 187 |
+
parallel: bool,
|
| 188 |
+
):
|
| 189 |
+
"""
|
| 190 |
+
Generate a numba jitted function to apply window calculations table-wise.
|
| 191 |
+
|
| 192 |
+
Func will be passed an M window size x N number of columns array, and
|
| 193 |
+
must return a 1 x N number of columns array.
|
| 194 |
+
|
| 195 |
+
1. jit the user's function
|
| 196 |
+
2. Return a rolling apply function with the jitted function inline
|
| 197 |
+
|
| 198 |
+
Parameters
|
| 199 |
+
----------
|
| 200 |
+
func : function
|
| 201 |
+
function to be applied to each window and will be JITed
|
| 202 |
+
nopython : bool
|
| 203 |
+
nopython to be passed into numba.jit
|
| 204 |
+
nogil : bool
|
| 205 |
+
nogil to be passed into numba.jit
|
| 206 |
+
parallel : bool
|
| 207 |
+
parallel to be passed into numba.jit
|
| 208 |
+
|
| 209 |
+
Returns
|
| 210 |
+
-------
|
| 211 |
+
Numba function
|
| 212 |
+
"""
|
| 213 |
+
numba_func = jit_user_function(func)
|
| 214 |
+
if TYPE_CHECKING:
|
| 215 |
+
import numba
|
| 216 |
+
else:
|
| 217 |
+
numba = import_optional_dependency("numba")
|
| 218 |
+
|
| 219 |
+
@numba.jit(nopython=nopython, nogil=nogil, parallel=parallel)
|
| 220 |
+
def roll_table(
|
| 221 |
+
values: np.ndarray,
|
| 222 |
+
begin: np.ndarray,
|
| 223 |
+
end: np.ndarray,
|
| 224 |
+
minimum_periods: int,
|
| 225 |
+
*args: Any,
|
| 226 |
+
):
|
| 227 |
+
result = np.empty((len(begin), values.shape[1]))
|
| 228 |
+
min_periods_mask = np.empty(result.shape)
|
| 229 |
+
for i in numba.prange(len(result)):
|
| 230 |
+
start = begin[i]
|
| 231 |
+
stop = end[i]
|
| 232 |
+
window = values[start:stop]
|
| 233 |
+
count_nan = np.sum(np.isnan(window), axis=0)
|
| 234 |
+
nan_mask = len(window) - count_nan >= minimum_periods
|
| 235 |
+
if nan_mask.any():
|
| 236 |
+
result[i, :] = numba_func(window, *args)
|
| 237 |
+
min_periods_mask[i, :] = nan_mask
|
| 238 |
+
result = np.where(min_periods_mask, result, np.nan)
|
| 239 |
+
return result
|
| 240 |
+
|
| 241 |
+
return roll_table
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
# This function will no longer be needed once numba supports
|
| 245 |
+
# axis for all np.nan* agg functions
|
| 246 |
+
# https://github.com/numba/numba/issues/1269
|
| 247 |
+
@functools.cache
|
| 248 |
+
def generate_manual_numpy_nan_agg_with_axis(nan_func):
|
| 249 |
+
if TYPE_CHECKING:
|
| 250 |
+
import numba
|
| 251 |
+
else:
|
| 252 |
+
numba = import_optional_dependency("numba")
|
| 253 |
+
|
| 254 |
+
@numba.jit(nopython=True, nogil=True, parallel=True)
|
| 255 |
+
def nan_agg_with_axis(table):
|
| 256 |
+
result = np.empty(table.shape[1])
|
| 257 |
+
for i in numba.prange(table.shape[1]):
|
| 258 |
+
partition = table[:, i]
|
| 259 |
+
result[i] = nan_func(partition)
|
| 260 |
+
return result
|
| 261 |
+
|
| 262 |
+
return nan_agg_with_axis
|
| 263 |
+
|
| 264 |
+
|
| 265 |
+
@functools.cache
|
| 266 |
+
def generate_numba_ewm_table_func(
|
| 267 |
+
nopython: bool,
|
| 268 |
+
nogil: bool,
|
| 269 |
+
parallel: bool,
|
| 270 |
+
com: float,
|
| 271 |
+
adjust: bool,
|
| 272 |
+
ignore_na: bool,
|
| 273 |
+
deltas: tuple,
|
| 274 |
+
normalize: bool,
|
| 275 |
+
):
|
| 276 |
+
"""
|
| 277 |
+
Generate a numba jitted ewm mean or sum function applied table wise specified
|
| 278 |
+
by values from engine_kwargs.
|
| 279 |
+
|
| 280 |
+
Parameters
|
| 281 |
+
----------
|
| 282 |
+
nopython : bool
|
| 283 |
+
nopython to be passed into numba.jit
|
| 284 |
+
nogil : bool
|
| 285 |
+
nogil to be passed into numba.jit
|
| 286 |
+
parallel : bool
|
| 287 |
+
parallel to be passed into numba.jit
|
| 288 |
+
com : float
|
| 289 |
+
adjust : bool
|
| 290 |
+
ignore_na : bool
|
| 291 |
+
deltas : tuple
|
| 292 |
+
normalize: bool
|
| 293 |
+
|
| 294 |
+
Returns
|
| 295 |
+
-------
|
| 296 |
+
Numba function
|
| 297 |
+
"""
|
| 298 |
+
if TYPE_CHECKING:
|
| 299 |
+
import numba
|
| 300 |
+
else:
|
| 301 |
+
numba = import_optional_dependency("numba")
|
| 302 |
+
|
| 303 |
+
@numba.jit(nopython=nopython, nogil=nogil, parallel=parallel)
|
| 304 |
+
def ewm_table(
|
| 305 |
+
values: np.ndarray,
|
| 306 |
+
begin: np.ndarray,
|
| 307 |
+
end: np.ndarray,
|
| 308 |
+
minimum_periods: int,
|
| 309 |
+
) -> np.ndarray:
|
| 310 |
+
alpha = 1.0 / (1.0 + com)
|
| 311 |
+
old_wt_factor = 1.0 - alpha
|
| 312 |
+
new_wt = 1.0 if adjust else alpha
|
| 313 |
+
old_wt = np.ones(values.shape[1])
|
| 314 |
+
|
| 315 |
+
result = np.empty(values.shape)
|
| 316 |
+
weighted = values[0].copy()
|
| 317 |
+
nobs = (~np.isnan(weighted)).astype(np.int64)
|
| 318 |
+
result[0] = np.where(nobs >= minimum_periods, weighted, np.nan)
|
| 319 |
+
for i in range(1, len(values)):
|
| 320 |
+
cur = values[i]
|
| 321 |
+
is_observations = ~np.isnan(cur)
|
| 322 |
+
nobs += is_observations.astype(np.int64)
|
| 323 |
+
for j in numba.prange(len(cur)):
|
| 324 |
+
if not np.isnan(weighted[j]):
|
| 325 |
+
if is_observations[j] or not ignore_na:
|
| 326 |
+
if normalize:
|
| 327 |
+
# note that len(deltas) = len(vals) - 1 and deltas[i]
|
| 328 |
+
# is to be used in conjunction with vals[i+1]
|
| 329 |
+
old_wt[j] *= old_wt_factor ** deltas[i - 1]
|
| 330 |
+
if not adjust and com == 1:
|
| 331 |
+
# update in case of irregular-interval time series
|
| 332 |
+
new_wt = 1.0 - old_wt[j]
|
| 333 |
+
else:
|
| 334 |
+
weighted[j] = old_wt_factor * weighted[j]
|
| 335 |
+
if is_observations[j]:
|
| 336 |
+
if normalize:
|
| 337 |
+
# avoid numerical errors on constant series
|
| 338 |
+
if weighted[j] != cur[j]:
|
| 339 |
+
weighted[j] = (
|
| 340 |
+
old_wt[j] * weighted[j] + new_wt * cur[j]
|
| 341 |
+
)
|
| 342 |
+
if normalize:
|
| 343 |
+
weighted[j] = weighted[j] / (old_wt[j] + new_wt)
|
| 344 |
+
if adjust:
|
| 345 |
+
old_wt[j] += new_wt
|
| 346 |
+
else:
|
| 347 |
+
old_wt[j] = 1.0
|
| 348 |
+
else:
|
| 349 |
+
weighted[j] += cur[j]
|
| 350 |
+
elif is_observations[j]:
|
| 351 |
+
weighted[j] = cur[j]
|
| 352 |
+
|
| 353 |
+
result[i] = np.where(nobs >= minimum_periods, weighted, np.nan)
|
| 354 |
+
|
| 355 |
+
return result
|
| 356 |
+
|
| 357 |
+
return ewm_table
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/online.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import TYPE_CHECKING
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
from pandas.compat._optional import import_optional_dependency
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def generate_online_numba_ewma_func(
|
| 11 |
+
nopython: bool,
|
| 12 |
+
nogil: bool,
|
| 13 |
+
parallel: bool,
|
| 14 |
+
):
|
| 15 |
+
"""
|
| 16 |
+
Generate a numba jitted groupby ewma function specified by values
|
| 17 |
+
from engine_kwargs.
|
| 18 |
+
|
| 19 |
+
Parameters
|
| 20 |
+
----------
|
| 21 |
+
nopython : bool
|
| 22 |
+
nopython to be passed into numba.jit
|
| 23 |
+
nogil : bool
|
| 24 |
+
nogil to be passed into numba.jit
|
| 25 |
+
parallel : bool
|
| 26 |
+
parallel to be passed into numba.jit
|
| 27 |
+
|
| 28 |
+
Returns
|
| 29 |
+
-------
|
| 30 |
+
Numba function
|
| 31 |
+
"""
|
| 32 |
+
if TYPE_CHECKING:
|
| 33 |
+
import numba
|
| 34 |
+
else:
|
| 35 |
+
numba = import_optional_dependency("numba")
|
| 36 |
+
|
| 37 |
+
@numba.jit(nopython=nopython, nogil=nogil, parallel=parallel)
|
| 38 |
+
def online_ewma(
|
| 39 |
+
values: np.ndarray,
|
| 40 |
+
deltas: np.ndarray,
|
| 41 |
+
minimum_periods: int,
|
| 42 |
+
old_wt_factor: float,
|
| 43 |
+
new_wt: float,
|
| 44 |
+
old_wt: np.ndarray,
|
| 45 |
+
adjust: bool,
|
| 46 |
+
ignore_na: bool,
|
| 47 |
+
):
|
| 48 |
+
"""
|
| 49 |
+
Compute online exponentially weighted mean per column over 2D values.
|
| 50 |
+
|
| 51 |
+
Takes the first observation as is, then computes the subsequent
|
| 52 |
+
exponentially weighted mean accounting minimum periods.
|
| 53 |
+
"""
|
| 54 |
+
result = np.empty(values.shape)
|
| 55 |
+
weighted_avg = values[0].copy()
|
| 56 |
+
nobs = (~np.isnan(weighted_avg)).astype(np.int64)
|
| 57 |
+
result[0] = np.where(nobs >= minimum_periods, weighted_avg, np.nan)
|
| 58 |
+
|
| 59 |
+
for i in range(1, len(values)):
|
| 60 |
+
cur = values[i]
|
| 61 |
+
is_observations = ~np.isnan(cur)
|
| 62 |
+
nobs += is_observations.astype(np.int64)
|
| 63 |
+
for j in numba.prange(len(cur)):
|
| 64 |
+
if not np.isnan(weighted_avg[j]):
|
| 65 |
+
if is_observations[j] or not ignore_na:
|
| 66 |
+
# note that len(deltas) = len(vals) - 1 and deltas[i] is to be
|
| 67 |
+
# used in conjunction with vals[i+1]
|
| 68 |
+
old_wt[j] *= old_wt_factor ** deltas[j - 1]
|
| 69 |
+
if is_observations[j]:
|
| 70 |
+
# avoid numerical errors on constant series
|
| 71 |
+
if weighted_avg[j] != cur[j]:
|
| 72 |
+
weighted_avg[j] = (
|
| 73 |
+
(old_wt[j] * weighted_avg[j]) + (new_wt * cur[j])
|
| 74 |
+
) / (old_wt[j] + new_wt)
|
| 75 |
+
if adjust:
|
| 76 |
+
old_wt[j] += new_wt
|
| 77 |
+
else:
|
| 78 |
+
old_wt[j] = 1.0
|
| 79 |
+
elif is_observations[j]:
|
| 80 |
+
weighted_avg[j] = cur[j]
|
| 81 |
+
|
| 82 |
+
result[i] = np.where(nobs >= minimum_periods, weighted_avg, np.nan)
|
| 83 |
+
|
| 84 |
+
return result, old_wt
|
| 85 |
+
|
| 86 |
+
return online_ewma
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
class EWMMeanState:
|
| 90 |
+
def __init__(self, com, adjust, ignore_na, shape) -> None:
|
| 91 |
+
alpha = 1.0 / (1.0 + com)
|
| 92 |
+
self.shape = shape
|
| 93 |
+
self.adjust = adjust
|
| 94 |
+
self.ignore_na = ignore_na
|
| 95 |
+
self.new_wt = 1.0 if adjust else alpha
|
| 96 |
+
self.old_wt_factor = 1.0 - alpha
|
| 97 |
+
self.old_wt = np.ones(self.shape[-1])
|
| 98 |
+
self.last_ewm = None
|
| 99 |
+
|
| 100 |
+
def run_ewm(self, weighted_avg, deltas, min_periods, ewm_func):
|
| 101 |
+
result, old_wt = ewm_func(
|
| 102 |
+
weighted_avg,
|
| 103 |
+
deltas,
|
| 104 |
+
min_periods,
|
| 105 |
+
self.old_wt_factor,
|
| 106 |
+
self.new_wt,
|
| 107 |
+
self.old_wt,
|
| 108 |
+
self.adjust,
|
| 109 |
+
self.ignore_na,
|
| 110 |
+
)
|
| 111 |
+
self.old_wt = old_wt
|
| 112 |
+
self.last_ewm = result[-1]
|
| 113 |
+
return result
|
| 114 |
+
|
| 115 |
+
def reset(self) -> None:
|
| 116 |
+
self.old_wt = np.ones(self.shape[-1])
|
| 117 |
+
self.last_ewm = None
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/core/window/rolling.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/errors/__init__.py
ADDED
|
@@ -0,0 +1,1087 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
| 12 |
+
from pandas._libs.tslibs import (
|
| 13 |
+
IncompatibleFrequency,
|
| 14 |
+
OutOfBoundsDatetime,
|
| 15 |
+
OutOfBoundsTimedelta,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
from pandas.util.version import InvalidVersion
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class IntCastingNaNError(ValueError):
|
| 22 |
+
"""
|
| 23 |
+
Exception raised when converting (``astype``) an array with NaN to an integer type.
|
| 24 |
+
|
| 25 |
+
This error occurs when attempting to cast a data structure containing non-finite
|
| 26 |
+
values (such as NaN or infinity) to an integer data type. Integer types do not
|
| 27 |
+
support non-finite values, so such conversions are explicitly disallowed to
|
| 28 |
+
prevent silent data corruption or unexpected behavior.
|
| 29 |
+
|
| 30 |
+
See Also
|
| 31 |
+
--------
|
| 32 |
+
DataFrame.astype : Method to cast a pandas DataFrame object to a specified dtype.
|
| 33 |
+
Series.astype : Method to cast a pandas Series object to a specified dtype.
|
| 34 |
+
|
| 35 |
+
Examples
|
| 36 |
+
--------
|
| 37 |
+
>>> pd.DataFrame(np.array([[1, np.nan], [2, 3]]), dtype="i8")
|
| 38 |
+
Traceback (most recent call last):
|
| 39 |
+
IntCastingNaNError: Cannot convert non-finite values (NA or inf) to integer
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class NullFrequencyError(ValueError):
|
| 44 |
+
"""
|
| 45 |
+
Exception raised when a ``freq`` cannot be null.
|
| 46 |
+
|
| 47 |
+
Particularly ``DatetimeIndex.shift``, ``TimedeltaIndex.shift``,
|
| 48 |
+
``PeriodIndex.shift``.
|
| 49 |
+
|
| 50 |
+
See Also
|
| 51 |
+
--------
|
| 52 |
+
Index.shift : Shift values of Index.
|
| 53 |
+
Series.shift : Shift values of Series.
|
| 54 |
+
|
| 55 |
+
Examples
|
| 56 |
+
--------
|
| 57 |
+
>>> df = pd.DatetimeIndex(["2011-01-01 10:00", "2011-01-01"], freq=None)
|
| 58 |
+
>>> df.shift(2)
|
| 59 |
+
Traceback (most recent call last):
|
| 60 |
+
NullFrequencyError: Cannot shift with no freq
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
class PerformanceWarning(Warning):
|
| 65 |
+
"""
|
| 66 |
+
Warning raised when there is a possible performance impact.
|
| 67 |
+
|
| 68 |
+
See Also
|
| 69 |
+
--------
|
| 70 |
+
DataFrame.set_index : Set the DataFrame index using existing columns.
|
| 71 |
+
DataFrame.loc : Access a group of rows and columns by label(s) \
|
| 72 |
+
or a boolean array.
|
| 73 |
+
|
| 74 |
+
Examples
|
| 75 |
+
--------
|
| 76 |
+
>>> df = pd.DataFrame(
|
| 77 |
+
... {"jim": [0, 0, 1, 1], "joe": ["x", "x", "z", "y"], "jolie": [1, 2, 3, 4]}
|
| 78 |
+
... )
|
| 79 |
+
>>> df = df.set_index(["jim", "joe"])
|
| 80 |
+
>>> df
|
| 81 |
+
jolie
|
| 82 |
+
jim joe
|
| 83 |
+
0 x 1
|
| 84 |
+
x 2
|
| 85 |
+
1 z 3
|
| 86 |
+
y 4
|
| 87 |
+
>>> df.loc[(1, "z")] # doctest: +SKIP
|
| 88 |
+
# PerformanceWarning: indexing past lexsort depth may impact performance.
|
| 89 |
+
df.loc[(1, 'z')]
|
| 90 |
+
jolie
|
| 91 |
+
jim joe
|
| 92 |
+
1 z 3
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
class PandasChangeWarning(Warning):
|
| 97 |
+
"""
|
| 98 |
+
Warning raised for any upcoming change.
|
| 99 |
+
|
| 100 |
+
See Also
|
| 101 |
+
--------
|
| 102 |
+
errors.PandasPendingDeprecationWarning : Class for deprecations that will raise a
|
| 103 |
+
PendingDeprecationWarning.
|
| 104 |
+
errors.PandasDeprecationWarning : Class for deprecations that will raise a
|
| 105 |
+
DeprecationWarning.
|
| 106 |
+
errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning.
|
| 107 |
+
|
| 108 |
+
Examples
|
| 109 |
+
--------
|
| 110 |
+
>>> pd.errors.PandasChangeWarning
|
| 111 |
+
<class 'pandas.errors.PandasChangeWarning'>
|
| 112 |
+
"""
|
| 113 |
+
|
| 114 |
+
@classmethod
|
| 115 |
+
@abc.abstractmethod
|
| 116 |
+
def version(cls) -> str:
|
| 117 |
+
"""Version where change will be enforced."""
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
class PandasPendingDeprecationWarning(PandasChangeWarning, PendingDeprecationWarning):
|
| 121 |
+
"""
|
| 122 |
+
Warning raised for an upcoming change that is a PendingDeprecationWarning.
|
| 123 |
+
|
| 124 |
+
See Also
|
| 125 |
+
--------
|
| 126 |
+
errors.PandasChangeWarning: Class for deprecations that will raise any warning.
|
| 127 |
+
errors.PandasDeprecationWarning : Class for deprecations that will raise a
|
| 128 |
+
DeprecationWarning.
|
| 129 |
+
errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning.
|
| 130 |
+
|
| 131 |
+
Examples
|
| 132 |
+
--------
|
| 133 |
+
>>> pd.errors.PandasPendingDeprecationWarning
|
| 134 |
+
<class 'pandas.errors.PandasPendingDeprecationWarning'>
|
| 135 |
+
"""
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
class PandasDeprecationWarning(PandasChangeWarning, DeprecationWarning):
|
| 139 |
+
"""
|
| 140 |
+
Warning raised for an upcoming change that is a DeprecationWarning.
|
| 141 |
+
|
| 142 |
+
See Also
|
| 143 |
+
--------
|
| 144 |
+
errors.PandasChangeWarning: Class for deprecations that will raise any warning.
|
| 145 |
+
errors.PandasPendingDeprecationWarning : Class for deprecations that will raise a
|
| 146 |
+
PendingDeprecationWarning.
|
| 147 |
+
errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning.
|
| 148 |
+
|
| 149 |
+
Examples
|
| 150 |
+
--------
|
| 151 |
+
>>> pd.errors.PandasDeprecationWarning
|
| 152 |
+
<class 'pandas.errors.PandasDeprecationWarning'>
|
| 153 |
+
"""
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
class PandasFutureWarning(PandasChangeWarning, FutureWarning):
|
| 157 |
+
"""
|
| 158 |
+
Warning raised for an upcoming change that is a FutureWarning.
|
| 159 |
+
|
| 160 |
+
See Also
|
| 161 |
+
--------
|
| 162 |
+
errors.PandasChangeWarning: Class for deprecations that will raise any warning.
|
| 163 |
+
errors.PandasPendingDeprecationWarning : Class for deprecations that will raise a
|
| 164 |
+
PendingDeprecationWarning.
|
| 165 |
+
errors.PandasDeprecationWarning : Class for deprecations that will raise a
|
| 166 |
+
DeprecationWarning.
|
| 167 |
+
|
| 168 |
+
Examples
|
| 169 |
+
--------
|
| 170 |
+
>>> pd.errors.PandasFutureWarning
|
| 171 |
+
<class 'pandas.errors.PandasFutureWarning'>
|
| 172 |
+
"""
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
class Pandas4Warning(PandasDeprecationWarning):
|
| 176 |
+
"""
|
| 177 |
+
Warning raised for an upcoming change that will be enforced in pandas 4.0.
|
| 178 |
+
|
| 179 |
+
See Also
|
| 180 |
+
--------
|
| 181 |
+
errors.PandasChangeWarning: Class for deprecations that will raise any warning.
|
| 182 |
+
errors.PandasPendingDeprecationWarning : Class for deprecations that will raise a
|
| 183 |
+
PendingDeprecationWarning.
|
| 184 |
+
errors.PandasDeprecationWarning : Class for deprecations that will raise a
|
| 185 |
+
DeprecationWarning.
|
| 186 |
+
errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning.
|
| 187 |
+
|
| 188 |
+
Examples
|
| 189 |
+
--------
|
| 190 |
+
>>> pd.errors.Pandas4Warning
|
| 191 |
+
<class 'pandas.errors.Pandas4Warning'>
|
| 192 |
+
"""
|
| 193 |
+
|
| 194 |
+
@classmethod
|
| 195 |
+
def version(cls) -> str:
|
| 196 |
+
"""Version where change will be enforced."""
|
| 197 |
+
return "4.0"
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
class Pandas5Warning(PandasPendingDeprecationWarning):
|
| 201 |
+
"""
|
| 202 |
+
Warning raised for an upcoming change that will be enforced in pandas 5.0.
|
| 203 |
+
|
| 204 |
+
See Also
|
| 205 |
+
--------
|
| 206 |
+
errors.PandasChangeWarning: Class for deprecations that will raise any warning.
|
| 207 |
+
errors.PandasPendingDeprecationWarning : Class for deprecations that will raise a
|
| 208 |
+
PendingDeprecationWarning.
|
| 209 |
+
errors.PandasDeprecationWarning : Class for deprecations that will raise a
|
| 210 |
+
DeprecationWarning.
|
| 211 |
+
errors.PandasFutureWarning : Class for deprecations that will raise a FutureWarning.
|
| 212 |
+
|
| 213 |
+
Examples
|
| 214 |
+
--------
|
| 215 |
+
>>> pd.errors.Pandas5Warning
|
| 216 |
+
<class 'pandas.errors.Pandas5Warning'>
|
| 217 |
+
"""
|
| 218 |
+
|
| 219 |
+
@classmethod
|
| 220 |
+
def version(cls) -> str:
|
| 221 |
+
"""Version where change will be enforced."""
|
| 222 |
+
return "5.0"
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
_CurrentDeprecationWarning = Pandas4Warning
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
class UnsupportedFunctionCall(ValueError):
|
| 229 |
+
"""
|
| 230 |
+
Exception raised when attempting to call a unsupported numpy function.
|
| 231 |
+
|
| 232 |
+
For example, ``np.cumsum(groupby_object)``.
|
| 233 |
+
|
| 234 |
+
See Also
|
| 235 |
+
--------
|
| 236 |
+
DataFrame.groupby : Group DataFrame using a mapper or by a Series of columns.
|
| 237 |
+
Series.groupby : Group Series using a mapper or by a Series of columns.
|
| 238 |
+
core.groupby.GroupBy.cumsum : Compute cumulative sum for each group.
|
| 239 |
+
|
| 240 |
+
Examples
|
| 241 |
+
--------
|
| 242 |
+
>>> df = pd.DataFrame(
|
| 243 |
+
... {"A": [0, 0, 1, 1], "B": ["x", "x", "z", "y"], "C": [1, 2, 3, 4]}
|
| 244 |
+
... )
|
| 245 |
+
>>> np.cumsum(df.groupby(["A"]))
|
| 246 |
+
Traceback (most recent call last):
|
| 247 |
+
UnsupportedFunctionCall: numpy operations are not valid with groupby.
|
| 248 |
+
Use .groupby(...).cumsum() instead
|
| 249 |
+
"""
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
class UnsortedIndexError(KeyError):
|
| 253 |
+
"""
|
| 254 |
+
Error raised when slicing a MultiIndex which has not been lexsorted.
|
| 255 |
+
|
| 256 |
+
Subclass of `KeyError`.
|
| 257 |
+
|
| 258 |
+
See Also
|
| 259 |
+
--------
|
| 260 |
+
DataFrame.sort_index : Sort a DataFrame by its index.
|
| 261 |
+
DataFrame.set_index : Set the DataFrame index using existing columns.
|
| 262 |
+
|
| 263 |
+
Examples
|
| 264 |
+
--------
|
| 265 |
+
>>> df = pd.DataFrame(
|
| 266 |
+
... {
|
| 267 |
+
... "cat": [0, 0, 1, 1],
|
| 268 |
+
... "color": ["white", "white", "brown", "black"],
|
| 269 |
+
... "lives": [4, 4, 3, 7],
|
| 270 |
+
... },
|
| 271 |
+
... )
|
| 272 |
+
>>> df = df.set_index(["cat", "color"])
|
| 273 |
+
>>> df
|
| 274 |
+
lives
|
| 275 |
+
cat color
|
| 276 |
+
0 white 4
|
| 277 |
+
white 4
|
| 278 |
+
1 brown 3
|
| 279 |
+
black 7
|
| 280 |
+
>>> df.loc[(0, "black") : (1, "white")]
|
| 281 |
+
Traceback (most recent call last):
|
| 282 |
+
UnsortedIndexError: 'Key length (2) was greater
|
| 283 |
+
than MultiIndex lexsort depth (1)'
|
| 284 |
+
"""
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
class ParserError(ValueError):
|
| 288 |
+
"""
|
| 289 |
+
Exception that is raised by an error encountered in parsing file contents.
|
| 290 |
+
|
| 291 |
+
This is a generic error raised for errors encountered when functions like
|
| 292 |
+
`read_csv` or `read_html` are parsing contents of a file.
|
| 293 |
+
|
| 294 |
+
See Also
|
| 295 |
+
--------
|
| 296 |
+
read_csv : Read CSV (comma-separated) file into a DataFrame.
|
| 297 |
+
read_html : Read HTML table into a DataFrame.
|
| 298 |
+
|
| 299 |
+
Examples
|
| 300 |
+
--------
|
| 301 |
+
>>> data = '''a,b,c
|
| 302 |
+
... cat,foo,bar
|
| 303 |
+
... dog,foo,"baz'''
|
| 304 |
+
>>> from io import StringIO
|
| 305 |
+
>>> pd.read_csv(StringIO(data), skipfooter=1, engine="python")
|
| 306 |
+
Traceback (most recent call last):
|
| 307 |
+
ParserError: ',' expected after '"'. Error could possibly be due
|
| 308 |
+
to parsing errors in the skipped footer rows
|
| 309 |
+
"""
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
class DtypeWarning(Warning):
|
| 313 |
+
"""
|
| 314 |
+
Warning raised when reading different dtypes in a column from a file.
|
| 315 |
+
|
| 316 |
+
Raised for a dtype incompatibility. This can happen whenever `read_csv`
|
| 317 |
+
or `read_table` encounter non-uniform dtypes in a column(s) of a given
|
| 318 |
+
CSV file.
|
| 319 |
+
|
| 320 |
+
See Also
|
| 321 |
+
--------
|
| 322 |
+
read_csv : Read CSV (comma-separated) file into a DataFrame.
|
| 323 |
+
read_table : Read general delimited file into a DataFrame.
|
| 324 |
+
|
| 325 |
+
Notes
|
| 326 |
+
-----
|
| 327 |
+
This warning is issued when dealing with larger files because the dtype
|
| 328 |
+
checking happens per chunk read.
|
| 329 |
+
|
| 330 |
+
Despite the warning, the CSV file is read with mixed types in a single
|
| 331 |
+
column which will be an object type. See the examples below to better
|
| 332 |
+
understand this issue.
|
| 333 |
+
|
| 334 |
+
Examples
|
| 335 |
+
--------
|
| 336 |
+
This example creates and reads a large CSV file with a column that contains
|
| 337 |
+
`int` and `str`.
|
| 338 |
+
|
| 339 |
+
>>> df = pd.DataFrame(
|
| 340 |
+
... {
|
| 341 |
+
... "a": (["1"] * 100000 + ["X"] * 100000 + ["1"] * 100000),
|
| 342 |
+
... "b": ["b"] * 300000,
|
| 343 |
+
... }
|
| 344 |
+
... ) # doctest: +SKIP
|
| 345 |
+
>>> df.to_csv("test.csv", index=False) # doctest: +SKIP
|
| 346 |
+
>>> df2 = pd.read_csv("test.csv") # doctest: +SKIP
|
| 347 |
+
... # DtypeWarning: Columns (0: a) have mixed types
|
| 348 |
+
|
| 349 |
+
Important to notice that ``df2`` will contain both `str` and `int` for the
|
| 350 |
+
same input, '1'.
|
| 351 |
+
|
| 352 |
+
>>> df2.iloc[262140, 0] # doctest: +SKIP
|
| 353 |
+
'1'
|
| 354 |
+
>>> type(df2.iloc[262140, 0]) # doctest: +SKIP
|
| 355 |
+
<class 'str'>
|
| 356 |
+
>>> df2.iloc[262150, 0] # doctest: +SKIP
|
| 357 |
+
1
|
| 358 |
+
>>> type(df2.iloc[262150, 0]) # doctest: +SKIP
|
| 359 |
+
<class 'int'>
|
| 360 |
+
|
| 361 |
+
One way to solve this issue is using the `dtype` parameter in the
|
| 362 |
+
`read_csv` and `read_table` functions to explicit the conversion:
|
| 363 |
+
|
| 364 |
+
>>> df2 = pd.read_csv("test.csv", sep=",", dtype={"a": str}) # doctest: +SKIP
|
| 365 |
+
|
| 366 |
+
No warning was issued.
|
| 367 |
+
"""
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
class EmptyDataError(ValueError):
|
| 371 |
+
"""
|
| 372 |
+
Exception raised in ``pd.read_csv`` when empty data or header is encountered.
|
| 373 |
+
|
| 374 |
+
This error is typically encountered when attempting to read an empty file or
|
| 375 |
+
an invalid file where no data or headers are present.
|
| 376 |
+
|
| 377 |
+
See Also
|
| 378 |
+
--------
|
| 379 |
+
read_csv : Read a comma-separated values (CSV) file into DataFrame.
|
| 380 |
+
errors.ParserError : Exception that is raised by an error encountered in parsing
|
| 381 |
+
file contents.
|
| 382 |
+
errors.DtypeWarning : Warning raised when reading different dtypes in a column
|
| 383 |
+
from a file.
|
| 384 |
+
|
| 385 |
+
Examples
|
| 386 |
+
--------
|
| 387 |
+
>>> from io import StringIO
|
| 388 |
+
>>> empty = StringIO()
|
| 389 |
+
>>> pd.read_csv(empty)
|
| 390 |
+
Traceback (most recent call last):
|
| 391 |
+
EmptyDataError: No columns to parse from file
|
| 392 |
+
"""
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
class ParserWarning(Warning):
|
| 396 |
+
"""
|
| 397 |
+
Warning raised when reading a file that doesn't use the default 'c' parser.
|
| 398 |
+
|
| 399 |
+
Raised by `pd.read_csv` and `pd.read_table` when it is necessary to change
|
| 400 |
+
parsers, generally from the default 'c' parser to 'python'.
|
| 401 |
+
|
| 402 |
+
It happens due to a lack of support or functionality for parsing a
|
| 403 |
+
particular attribute of a CSV file with the requested engine.
|
| 404 |
+
|
| 405 |
+
Currently, 'c' unsupported options include the following parameters:
|
| 406 |
+
|
| 407 |
+
1. `sep` other than a single character (e.g. regex separators)
|
| 408 |
+
2. `skipfooter` higher than 0
|
| 409 |
+
|
| 410 |
+
The warning can be avoided by adding `engine='python'` as a parameter in
|
| 411 |
+
`pd.read_csv` and `pd.read_table` methods.
|
| 412 |
+
|
| 413 |
+
See Also
|
| 414 |
+
--------
|
| 415 |
+
pd.read_csv : Read CSV (comma-separated) file into DataFrame.
|
| 416 |
+
pd.read_table : Read general delimited file into DataFrame.
|
| 417 |
+
|
| 418 |
+
Examples
|
| 419 |
+
--------
|
| 420 |
+
Using a `sep` in `pd.read_csv` other than a single character:
|
| 421 |
+
|
| 422 |
+
>>> import io
|
| 423 |
+
>>> csv = '''a;b;c
|
| 424 |
+
... 1;1,8
|
| 425 |
+
... 1;2,1'''
|
| 426 |
+
>>> df = pd.read_csv(io.StringIO(csv), sep="[;,]") # doctest: +SKIP
|
| 427 |
+
... # ParserWarning: Falling back to the 'python' engine...
|
| 428 |
+
|
| 429 |
+
Adding `engine='python'` to `pd.read_csv` removes the Warning:
|
| 430 |
+
|
| 431 |
+
>>> df = pd.read_csv(io.StringIO(csv), sep="[;,]", engine="python")
|
| 432 |
+
"""
|
| 433 |
+
|
| 434 |
+
|
| 435 |
+
class MergeError(ValueError):
|
| 436 |
+
"""
|
| 437 |
+
Exception raised when merging data.
|
| 438 |
+
|
| 439 |
+
Subclass of ``ValueError``.
|
| 440 |
+
|
| 441 |
+
See Also
|
| 442 |
+
--------
|
| 443 |
+
DataFrame.join : For joining DataFrames on their indexes.
|
| 444 |
+
merge : For merging two DataFrames on a common set of keys.
|
| 445 |
+
|
| 446 |
+
Examples
|
| 447 |
+
--------
|
| 448 |
+
>>> left = pd.DataFrame(
|
| 449 |
+
... {"a": ["a", "b", "b", "d"], "b": ["cat", "dog", "weasel", "horse"]},
|
| 450 |
+
... index=range(4),
|
| 451 |
+
... )
|
| 452 |
+
>>> right = pd.DataFrame(
|
| 453 |
+
... {"a": ["a", "b", "c", "d"], "c": ["meow", "bark", "chirp", "nay"]},
|
| 454 |
+
... index=range(4),
|
| 455 |
+
... ).set_index("a")
|
| 456 |
+
>>> left.join(
|
| 457 |
+
... right,
|
| 458 |
+
... on="a",
|
| 459 |
+
... validate="one_to_one",
|
| 460 |
+
... )
|
| 461 |
+
Traceback (most recent call last):
|
| 462 |
+
MergeError: Merge keys are not unique in left dataset; not a one-to-one merge
|
| 463 |
+
"""
|
| 464 |
+
|
| 465 |
+
|
| 466 |
+
class AbstractMethodError(NotImplementedError):
|
| 467 |
+
"""
|
| 468 |
+
Raise this error instead of NotImplementedError for abstract methods.
|
| 469 |
+
|
| 470 |
+
The `AbstractMethodError` is designed for use in classes that follow an abstract
|
| 471 |
+
base class pattern. By raising this error in the method, it ensures that a subclass
|
| 472 |
+
must implement the method to provide specific functionality. This is useful in a
|
| 473 |
+
framework or library where certain methods must be implemented by the user to
|
| 474 |
+
ensure correct behavior.
|
| 475 |
+
|
| 476 |
+
Parameters
|
| 477 |
+
----------
|
| 478 |
+
class_instance : object
|
| 479 |
+
The instance of the class where the abstract method is being called.
|
| 480 |
+
methodtype : str, default "method"
|
| 481 |
+
A string indicating the type of method that is abstract.
|
| 482 |
+
Must be one of {"method", "classmethod", "staticmethod", "property"}.
|
| 483 |
+
|
| 484 |
+
See Also
|
| 485 |
+
--------
|
| 486 |
+
api.extensions.ExtensionArray
|
| 487 |
+
An example of a pandas extension mechanism that requires implementing
|
| 488 |
+
specific abstract methods.
|
| 489 |
+
NotImplementedError
|
| 490 |
+
A built-in exception that can also be used for abstract methods but lacks
|
| 491 |
+
the specificity of `AbstractMethodError` in indicating the need for subclass
|
| 492 |
+
implementation.
|
| 493 |
+
|
| 494 |
+
Examples
|
| 495 |
+
--------
|
| 496 |
+
>>> class Foo:
|
| 497 |
+
... @classmethod
|
| 498 |
+
... def classmethod(cls):
|
| 499 |
+
... raise pd.errors.AbstractMethodError(cls, methodtype="classmethod")
|
| 500 |
+
...
|
| 501 |
+
... def method(self):
|
| 502 |
+
... raise pd.errors.AbstractMethodError(self)
|
| 503 |
+
>>> test = Foo.classmethod()
|
| 504 |
+
Traceback (most recent call last):
|
| 505 |
+
AbstractMethodError: This classmethod must be defined in the concrete class Foo
|
| 506 |
+
|
| 507 |
+
>>> test2 = Foo().method()
|
| 508 |
+
Traceback (most recent call last):
|
| 509 |
+
AbstractMethodError: This classmethod must be defined in the concrete class Foo
|
| 510 |
+
"""
|
| 511 |
+
|
| 512 |
+
def __init__(self, class_instance, methodtype: str = "method") -> None:
|
| 513 |
+
types = {"method", "classmethod", "staticmethod", "property"}
|
| 514 |
+
if methodtype not in types:
|
| 515 |
+
raise ValueError(
|
| 516 |
+
f"methodtype must be one of {types}, got {methodtype} instead."
|
| 517 |
+
)
|
| 518 |
+
self.methodtype = methodtype
|
| 519 |
+
self.class_instance = class_instance
|
| 520 |
+
|
| 521 |
+
def __str__(self) -> str:
|
| 522 |
+
if self.methodtype == "classmethod":
|
| 523 |
+
name = self.class_instance.__name__
|
| 524 |
+
else:
|
| 525 |
+
name = type(self.class_instance).__name__
|
| 526 |
+
return f"This {self.methodtype} must be defined in the concrete class {name}"
|
| 527 |
+
|
| 528 |
+
|
| 529 |
+
class NumbaUtilError(Exception):
|
| 530 |
+
"""
|
| 531 |
+
Error raised for unsupported Numba engine routines.
|
| 532 |
+
|
| 533 |
+
See Also
|
| 534 |
+
--------
|
| 535 |
+
DataFrame.groupby : Group DataFrame using a mapper or by a Series of columns.
|
| 536 |
+
Series.groupby : Group Series using a mapper or by a Series of columns.
|
| 537 |
+
DataFrame.agg : Aggregate using one or more operations over the specified axis.
|
| 538 |
+
Series.agg : Aggregate using one or more operations over the specified axis.
|
| 539 |
+
|
| 540 |
+
Examples
|
| 541 |
+
--------
|
| 542 |
+
>>> df = pd.DataFrame(
|
| 543 |
+
... {"key": ["a", "a", "b", "b"], "data": [1, 2, 3, 4]}, columns=["key", "data"]
|
| 544 |
+
... )
|
| 545 |
+
>>> def incorrect_function(x):
|
| 546 |
+
... return sum(x) * 2.7
|
| 547 |
+
>>> df.groupby("key").agg(incorrect_function, engine="numba")
|
| 548 |
+
Traceback (most recent call last):
|
| 549 |
+
NumbaUtilError: The first 2 arguments to incorrect_function
|
| 550 |
+
must be ['values', 'index']
|
| 551 |
+
"""
|
| 552 |
+
|
| 553 |
+
|
| 554 |
+
class DuplicateLabelError(ValueError):
|
| 555 |
+
"""
|
| 556 |
+
Error raised when an operation would introduce duplicate labels.
|
| 557 |
+
|
| 558 |
+
This error is typically encountered when performing operations on objects
|
| 559 |
+
with `allows_duplicate_labels=False` and the operation would result in
|
| 560 |
+
duplicate labels in the index. Duplicate labels can lead to ambiguities
|
| 561 |
+
in indexing and reduce data integrity.
|
| 562 |
+
|
| 563 |
+
See Also
|
| 564 |
+
--------
|
| 565 |
+
Series.set_flags : Return a new ``Series`` object with updated flags.
|
| 566 |
+
DataFrame.set_flags : Return a new ``DataFrame`` object with updated flags.
|
| 567 |
+
Series.reindex : Conform ``Series`` object to new index with optional filling logic.
|
| 568 |
+
DataFrame.reindex : Conform ``DataFrame`` object to new index with optional filling
|
| 569 |
+
logic.
|
| 570 |
+
|
| 571 |
+
Examples
|
| 572 |
+
--------
|
| 573 |
+
>>> s = pd.Series([0, 1, 2], index=["a", "b", "c"]).set_flags(
|
| 574 |
+
... allows_duplicate_labels=False
|
| 575 |
+
... )
|
| 576 |
+
>>> s.reindex(["a", "a", "b"])
|
| 577 |
+
Traceback (most recent call last):
|
| 578 |
+
...
|
| 579 |
+
DuplicateLabelError: Index has duplicates.
|
| 580 |
+
positions
|
| 581 |
+
label
|
| 582 |
+
a [0, 1]
|
| 583 |
+
"""
|
| 584 |
+
|
| 585 |
+
|
| 586 |
+
class InvalidIndexError(Exception):
|
| 587 |
+
"""
|
| 588 |
+
Exception raised when attempting to use an invalid index key.
|
| 589 |
+
|
| 590 |
+
This exception is triggered when a user attempts to access or manipulate
|
| 591 |
+
data in a pandas DataFrame or Series using an index key that is not valid
|
| 592 |
+
for the given object. This may occur in cases such as using a malformed
|
| 593 |
+
slice, a mismatched key for a ``MultiIndex``, or attempting to access an index
|
| 594 |
+
element that does not exist.
|
| 595 |
+
|
| 596 |
+
See Also
|
| 597 |
+
--------
|
| 598 |
+
MultiIndex : A multi-level, or hierarchical, index object for pandas objects.
|
| 599 |
+
|
| 600 |
+
Examples
|
| 601 |
+
--------
|
| 602 |
+
>>> idx = pd.MultiIndex.from_product([["x", "y"], [0, 1]])
|
| 603 |
+
>>> df = pd.DataFrame([[1, 1, 2, 2], [3, 3, 4, 4]], columns=idx)
|
| 604 |
+
>>> df
|
| 605 |
+
x y
|
| 606 |
+
0 1 0 1
|
| 607 |
+
0 1 1 2 2
|
| 608 |
+
1 3 3 4 4
|
| 609 |
+
>>> df[:, 0]
|
| 610 |
+
Traceback (most recent call last):
|
| 611 |
+
InvalidIndexError: (slice(None, None, None), 0)
|
| 612 |
+
"""
|
| 613 |
+
|
| 614 |
+
|
| 615 |
+
class DataError(Exception):
|
| 616 |
+
"""
|
| 617 |
+
Exception raised when performing an operation on non-numerical data.
|
| 618 |
+
|
| 619 |
+
For example, calling ``ohlc`` on a non-numerical column or a function
|
| 620 |
+
on a rolling window.
|
| 621 |
+
|
| 622 |
+
See Also
|
| 623 |
+
--------
|
| 624 |
+
Series.rolling : Provide rolling window calculations on Series object.
|
| 625 |
+
DataFrame.rolling : Provide rolling window calculations on DataFrame object.
|
| 626 |
+
|
| 627 |
+
Examples
|
| 628 |
+
--------
|
| 629 |
+
>>> ser = pd.Series(["a", "b", "c"])
|
| 630 |
+
>>> ser.rolling(2).sum()
|
| 631 |
+
Traceback (most recent call last):
|
| 632 |
+
DataError: No numeric types to aggregate
|
| 633 |
+
"""
|
| 634 |
+
|
| 635 |
+
|
| 636 |
+
class SpecificationError(Exception):
|
| 637 |
+
"""
|
| 638 |
+
Exception raised by ``agg`` when the functions are ill-specified.
|
| 639 |
+
|
| 640 |
+
The exception raised in two scenarios.
|
| 641 |
+
|
| 642 |
+
The first way is calling ``agg`` on a
|
| 643 |
+
Dataframe or Series using a nested renamer (dict-of-dict).
|
| 644 |
+
|
| 645 |
+
The second way is calling ``agg`` on a Dataframe with duplicated functions
|
| 646 |
+
names without assigning column name.
|
| 647 |
+
|
| 648 |
+
See Also
|
| 649 |
+
--------
|
| 650 |
+
DataFrame.agg : Aggregate using one or more operations over the specified axis.
|
| 651 |
+
Series.agg : Aggregate using one or more operations over the specified axis.
|
| 652 |
+
|
| 653 |
+
Examples
|
| 654 |
+
--------
|
| 655 |
+
>>> df = pd.DataFrame({"A": [1, 1, 1, 2, 2], "B": range(5), "C": range(5)})
|
| 656 |
+
>>> df.groupby("A").B.agg({"foo": "count"}) # doctest: +SKIP
|
| 657 |
+
... # SpecificationError: nested renamer is not supported
|
| 658 |
+
|
| 659 |
+
>>> df.groupby("A").agg({"B": {"foo": ["sum", "max"]}}) # doctest: +SKIP
|
| 660 |
+
... # SpecificationError: nested renamer is not supported
|
| 661 |
+
|
| 662 |
+
>>> df.groupby("A").agg(["min", "min"]) # doctest: +SKIP
|
| 663 |
+
... # SpecificationError: nested renamer is not supported
|
| 664 |
+
"""
|
| 665 |
+
|
| 666 |
+
|
| 667 |
+
class ChainedAssignmentError(Warning):
|
| 668 |
+
"""
|
| 669 |
+
Warning raised when trying to set using chained assignment.
|
| 670 |
+
|
| 671 |
+
With Copy-on-Write now always enabled, chained assignment can
|
| 672 |
+
never work. In such a situation, we are always setting into a temporary
|
| 673 |
+
object that is the result of an indexing operation (getitem), which under
|
| 674 |
+
Copy-on-Write always behaves as a copy. Thus, assigning through a chain
|
| 675 |
+
can never update the original Series or DataFrame.
|
| 676 |
+
|
| 677 |
+
For more information on Copy-on-Write,
|
| 678 |
+
see :ref:`the user guide<copy_on_write>`.
|
| 679 |
+
|
| 680 |
+
See Also
|
| 681 |
+
--------
|
| 682 |
+
DataFrame.loc : Access a group of rows and columns by label(s) or a boolean array.
|
| 683 |
+
DataFrame.iloc : Purely integer-location based indexing for selection by position.
|
| 684 |
+
Series.loc : Access a group of rows by label(s) or a boolean array.
|
| 685 |
+
|
| 686 |
+
Examples
|
| 687 |
+
--------
|
| 688 |
+
>>> df = pd.DataFrame({"A": [1, 1, 1, 2, 2]}, columns=["A"])
|
| 689 |
+
>>> df["A"][0:3] = 10 # doctest: +SKIP
|
| 690 |
+
... # ChainedAssignmentError: ...
|
| 691 |
+
"""
|
| 692 |
+
|
| 693 |
+
|
| 694 |
+
class NumExprClobberingError(NameError):
|
| 695 |
+
"""
|
| 696 |
+
Exception raised when trying to use a built-in numexpr name as a variable name.
|
| 697 |
+
|
| 698 |
+
``eval`` or ``query`` will throw the error if the engine is set
|
| 699 |
+
to 'numexpr'. 'numexpr' is the default engine value for these methods if the
|
| 700 |
+
numexpr package is installed.
|
| 701 |
+
|
| 702 |
+
See Also
|
| 703 |
+
--------
|
| 704 |
+
eval : Evaluate a Python expression as a string using various backends.
|
| 705 |
+
DataFrame.query : Query the columns of a DataFrame with a boolean expression.
|
| 706 |
+
|
| 707 |
+
Examples
|
| 708 |
+
--------
|
| 709 |
+
>>> df = pd.DataFrame({"abs": [1, 1, 1]})
|
| 710 |
+
>>> df.query("abs > 2") # doctest: +SKIP
|
| 711 |
+
... # NumExprClobberingError: Variables in expression "(abs) > (2)" overlap...
|
| 712 |
+
>>> sin, a = 1, 2
|
| 713 |
+
>>> pd.eval("sin + a", engine="numexpr") # doctest: +SKIP
|
| 714 |
+
... # NumExprClobberingError: Variables in expression "(sin) + (a)" overlap...
|
| 715 |
+
"""
|
| 716 |
+
|
| 717 |
+
|
| 718 |
+
class UndefinedVariableError(NameError):
|
| 719 |
+
"""
|
| 720 |
+
Exception raised by ``query`` or ``eval`` when using an undefined variable name.
|
| 721 |
+
|
| 722 |
+
It will also specify whether the undefined variable is local or not.
|
| 723 |
+
|
| 724 |
+
Parameters
|
| 725 |
+
----------
|
| 726 |
+
name : str
|
| 727 |
+
The name of the undefined variable.
|
| 728 |
+
is_local : bool or None, optional
|
| 729 |
+
Indicates whether the undefined variable is considered a local variable.
|
| 730 |
+
If ``True``, the error message specifies it as a local variable.
|
| 731 |
+
If ``False`` or ``None``, the variable is treated as a non-local name.
|
| 732 |
+
|
| 733 |
+
See Also
|
| 734 |
+
--------
|
| 735 |
+
DataFrame.query : Query the columns of a DataFrame with a boolean expression.
|
| 736 |
+
DataFrame.eval : Evaluate a string describing operations on DataFrame columns.
|
| 737 |
+
|
| 738 |
+
Examples
|
| 739 |
+
--------
|
| 740 |
+
>>> df = pd.DataFrame({"A": [1, 1, 1]})
|
| 741 |
+
>>> df.query("A > x") # doctest: +SKIP
|
| 742 |
+
... # UndefinedVariableError: name 'x' is not defined
|
| 743 |
+
>>> df.query("A > @y") # doctest: +SKIP
|
| 744 |
+
... # UndefinedVariableError: local variable 'y' is not defined
|
| 745 |
+
>>> pd.eval("x + 1") # doctest: +SKIP
|
| 746 |
+
... # UndefinedVariableError: name 'x' is not defined
|
| 747 |
+
"""
|
| 748 |
+
|
| 749 |
+
def __init__(self, name: str, is_local: bool | None = None) -> None:
|
| 750 |
+
base_msg = f"{name!r} is not defined"
|
| 751 |
+
if is_local:
|
| 752 |
+
msg = f"local variable {base_msg}"
|
| 753 |
+
else:
|
| 754 |
+
msg = f"name {base_msg}"
|
| 755 |
+
super().__init__(msg)
|
| 756 |
+
|
| 757 |
+
|
| 758 |
+
class IndexingError(Exception):
|
| 759 |
+
"""
|
| 760 |
+
Exception is raised when trying to index and there is a mismatch in dimensions.
|
| 761 |
+
|
| 762 |
+
Raised by properties like :attr:`.pandas.DataFrame.iloc` when
|
| 763 |
+
an indexer is out of bounds or :attr:`.pandas.DataFrame.loc` when its index is
|
| 764 |
+
unalignable to the frame index.
|
| 765 |
+
|
| 766 |
+
See Also
|
| 767 |
+
--------
|
| 768 |
+
DataFrame.iloc : Purely integer-location based indexing for \
|
| 769 |
+
selection by position.
|
| 770 |
+
DataFrame.loc : Access a group of rows and columns by label(s) \
|
| 771 |
+
or a boolean array.
|
| 772 |
+
|
| 773 |
+
Examples
|
| 774 |
+
--------
|
| 775 |
+
>>> df = pd.DataFrame({"A": [1, 1, 1]})
|
| 776 |
+
>>> df.loc[..., ..., "A"] # doctest: +SKIP
|
| 777 |
+
... # IndexingError: indexer may only contain one '...' entry
|
| 778 |
+
>>> df = pd.DataFrame({"A": [1, 1, 1]})
|
| 779 |
+
>>> df.loc[1, ..., ...] # doctest: +SKIP
|
| 780 |
+
... # IndexingError: Too many indexers
|
| 781 |
+
>>> df[pd.Series([True], dtype=bool)] # doctest: +SKIP
|
| 782 |
+
... # IndexingError: Unalignable boolean Series provided as indexer...
|
| 783 |
+
>>> s = pd.Series(range(2), index=pd.MultiIndex.from_product([["a", "b"], ["c"]]))
|
| 784 |
+
>>> s.loc["a", "c", "d"] # doctest: +SKIP
|
| 785 |
+
... # IndexingError: Too many indexers
|
| 786 |
+
"""
|
| 787 |
+
|
| 788 |
+
|
| 789 |
+
class PyperclipException(RuntimeError):
|
| 790 |
+
"""
|
| 791 |
+
Exception raised when clipboard functionality is unsupported.
|
| 792 |
+
|
| 793 |
+
Raised by ``to_clipboard()`` and ``read_clipboard()``.
|
| 794 |
+
"""
|
| 795 |
+
|
| 796 |
+
|
| 797 |
+
class PyperclipWindowsException(PyperclipException):
|
| 798 |
+
"""
|
| 799 |
+
Exception raised when clipboard functionality is unsupported by Windows.
|
| 800 |
+
|
| 801 |
+
Access to the clipboard handle would be denied due to some other
|
| 802 |
+
window process is accessing it.
|
| 803 |
+
"""
|
| 804 |
+
|
| 805 |
+
def __init__(self, message: str) -> None:
|
| 806 |
+
# attr only exists on Windows, so typing fails on other platforms
|
| 807 |
+
message += f" ({ctypes.WinError()})" # type: ignore[attr-defined]
|
| 808 |
+
super().__init__(message)
|
| 809 |
+
|
| 810 |
+
|
| 811 |
+
class CSSWarning(UserWarning):
|
| 812 |
+
"""
|
| 813 |
+
Warning is raised when converting css styling fails.
|
| 814 |
+
|
| 815 |
+
This can be due to the styling not having an equivalent value or because the
|
| 816 |
+
styling isn't properly formatted.
|
| 817 |
+
|
| 818 |
+
See Also
|
| 819 |
+
--------
|
| 820 |
+
DataFrame.style : Returns a Styler object for applying CSS-like styles.
|
| 821 |
+
io.formats.style.Styler : Helps style a DataFrame or Series according to the
|
| 822 |
+
data with HTML and CSS.
|
| 823 |
+
io.formats.style.Styler.to_excel : Export styled DataFrame to Excel.
|
| 824 |
+
io.formats.style.Styler.to_html : Export styled DataFrame to HTML.
|
| 825 |
+
|
| 826 |
+
Examples
|
| 827 |
+
--------
|
| 828 |
+
>>> df = pd.DataFrame({"A": [1, 1, 1]})
|
| 829 |
+
>>> df.style.map(lambda x: "background-color: blueGreenRed;").to_excel(
|
| 830 |
+
... "styled.xlsx"
|
| 831 |
+
... ) # doctest: +SKIP
|
| 832 |
+
CSSWarning: Unhandled color format: 'blueGreenRed'
|
| 833 |
+
>>> df.style.map(lambda x: "border: 1px solid red red;").to_excel(
|
| 834 |
+
... "styled.xlsx"
|
| 835 |
+
... ) # doctest: +SKIP
|
| 836 |
+
CSSWarning: Unhandled color format: 'blueGreenRed'
|
| 837 |
+
"""
|
| 838 |
+
|
| 839 |
+
|
| 840 |
+
class PossibleDataLossError(Exception):
|
| 841 |
+
"""
|
| 842 |
+
Exception raised when trying to open an HDFStore file when already opened.
|
| 843 |
+
|
| 844 |
+
This error is triggered when there is a potential risk of data loss due to
|
| 845 |
+
conflicting operations on an HDFStore file. It serves to prevent unintended
|
| 846 |
+
overwrites or data corruption by enforcing exclusive access to the file.
|
| 847 |
+
|
| 848 |
+
See Also
|
| 849 |
+
--------
|
| 850 |
+
HDFStore : Dict-like IO interface for storing pandas objects in PyTables.
|
| 851 |
+
HDFStore.open : Open an HDFStore file in the specified mode.
|
| 852 |
+
|
| 853 |
+
Examples
|
| 854 |
+
--------
|
| 855 |
+
>>> store = pd.HDFStore("my-store", "a") # doctest: +SKIP
|
| 856 |
+
>>> store.open("w") # doctest: +SKIP
|
| 857 |
+
"""
|
| 858 |
+
|
| 859 |
+
|
| 860 |
+
class ClosedFileError(Exception):
|
| 861 |
+
"""
|
| 862 |
+
Exception is raised when trying to perform an operation on a closed HDFStore file.
|
| 863 |
+
|
| 864 |
+
``ClosedFileError`` is specific to operations on ``HDFStore`` objects. Once an
|
| 865 |
+
HDFStore is closed, its resources are no longer available, and any further attempt
|
| 866 |
+
to access data or perform file operations will raise this exception.
|
| 867 |
+
|
| 868 |
+
See Also
|
| 869 |
+
--------
|
| 870 |
+
HDFStore.close : Closes the PyTables file handle.
|
| 871 |
+
HDFStore.open : Opens the file in the specified mode.
|
| 872 |
+
HDFStore.is_open : Returns a boolean indicating whether the file is open.
|
| 873 |
+
|
| 874 |
+
Examples
|
| 875 |
+
--------
|
| 876 |
+
>>> store = pd.HDFStore("my-store", "a") # doctest: +SKIP
|
| 877 |
+
>>> store.close() # doctest: +SKIP
|
| 878 |
+
>>> store.keys() # doctest: +SKIP
|
| 879 |
+
... # ClosedFileError: my-store file is not open!
|
| 880 |
+
"""
|
| 881 |
+
|
| 882 |
+
|
| 883 |
+
class IncompatibilityWarning(Warning):
|
| 884 |
+
"""
|
| 885 |
+
Warning raised when trying to use where criteria on an incompatible HDF5 file.
|
| 886 |
+
"""
|
| 887 |
+
|
| 888 |
+
|
| 889 |
+
class AttributeConflictWarning(Warning):
|
| 890 |
+
"""
|
| 891 |
+
Warning raised when index attributes conflict when using HDFStore.
|
| 892 |
+
|
| 893 |
+
Occurs when attempting to append an index with a different
|
| 894 |
+
name than the existing index on an HDFStore or attempting to append an index with a
|
| 895 |
+
different frequency than the existing index on an HDFStore.
|
| 896 |
+
|
| 897 |
+
See Also
|
| 898 |
+
--------
|
| 899 |
+
HDFStore : Dict-like IO interface for storing pandas objects in PyTables.
|
| 900 |
+
DataFrame.to_hdf : Write the contained data to an HDF5 file using HDFStore.
|
| 901 |
+
read_hdf : Read from an HDF5 file into a DataFrame.
|
| 902 |
+
|
| 903 |
+
Examples
|
| 904 |
+
--------
|
| 905 |
+
>>> idx1 = pd.Index(["a", "b"], name="name1")
|
| 906 |
+
>>> df1 = pd.DataFrame([[1, 2], [3, 4]], index=idx1)
|
| 907 |
+
>>> df1.to_hdf("file", "data", "w", append=True) # doctest: +SKIP
|
| 908 |
+
>>> idx2 = pd.Index(["c", "d"], name="name2")
|
| 909 |
+
>>> df2 = pd.DataFrame([[5, 6], [7, 8]], index=idx2)
|
| 910 |
+
>>> df2.to_hdf("file", "data", "a", append=True) # doctest: +SKIP
|
| 911 |
+
AttributeConflictWarning: the [index_name] attribute of the existing index is
|
| 912 |
+
[name1] which conflicts with the new [name2]...
|
| 913 |
+
"""
|
| 914 |
+
|
| 915 |
+
|
| 916 |
+
class DatabaseError(OSError):
|
| 917 |
+
"""
|
| 918 |
+
Error is raised when executing SQL with bad syntax or SQL that throws an error.
|
| 919 |
+
|
| 920 |
+
Raised by :func:`.pandas.read_sql` when a bad SQL statement is passed in.
|
| 921 |
+
|
| 922 |
+
See Also
|
| 923 |
+
--------
|
| 924 |
+
read_sql : Read SQL query or database table into a DataFrame.
|
| 925 |
+
|
| 926 |
+
Examples
|
| 927 |
+
--------
|
| 928 |
+
>>> from sqlite3 import connect
|
| 929 |
+
>>> conn = connect(":memory:")
|
| 930 |
+
>>> pd.read_sql("select * test", conn) # doctest: +SKIP
|
| 931 |
+
"""
|
| 932 |
+
|
| 933 |
+
|
| 934 |
+
class PossiblePrecisionLoss(Warning):
|
| 935 |
+
"""
|
| 936 |
+
Warning raised by to_stata on a column with a value outside or equal to int64.
|
| 937 |
+
|
| 938 |
+
When the column value is outside or equal to the int64 value the column is
|
| 939 |
+
converted to a float64 dtype.
|
| 940 |
+
|
| 941 |
+
See Also
|
| 942 |
+
--------
|
| 943 |
+
DataFrame.to_stata : Export DataFrame object to Stata dta format.
|
| 944 |
+
|
| 945 |
+
Examples
|
| 946 |
+
--------
|
| 947 |
+
>>> df = pd.DataFrame({"s": pd.Series([1, 2**53], dtype=np.int64)})
|
| 948 |
+
>>> df.to_stata("test") # doctest: +SKIP
|
| 949 |
+
"""
|
| 950 |
+
|
| 951 |
+
|
| 952 |
+
class ValueLabelTypeMismatch(Warning):
|
| 953 |
+
"""
|
| 954 |
+
Warning raised by to_stata on a category column that contains non-string values.
|
| 955 |
+
|
| 956 |
+
When exporting data to Stata format using the `to_stata` method, category columns
|
| 957 |
+
must have string values as labels. If a category column contains non-string values
|
| 958 |
+
(e.g., integers, floats, or other types), this warning is raised to indicate that
|
| 959 |
+
the Stata file may not correctly represent the data.
|
| 960 |
+
|
| 961 |
+
See Also
|
| 962 |
+
--------
|
| 963 |
+
DataFrame.to_stata : Export DataFrame object to Stata dta format.
|
| 964 |
+
Series.cat : Accessor for categorical properties of the Series values.
|
| 965 |
+
|
| 966 |
+
Examples
|
| 967 |
+
--------
|
| 968 |
+
>>> df = pd.DataFrame({"categories": pd.Series(["a", 2], dtype="category")})
|
| 969 |
+
>>> df.to_stata("test") # doctest: +SKIP
|
| 970 |
+
"""
|
| 971 |
+
|
| 972 |
+
|
| 973 |
+
class InvalidColumnName(Warning):
|
| 974 |
+
"""
|
| 975 |
+
Warning raised by to_stata the column contains a non-valid stata name.
|
| 976 |
+
|
| 977 |
+
Because the column name is an invalid Stata variable, the name needs to be
|
| 978 |
+
converted.
|
| 979 |
+
|
| 980 |
+
See Also
|
| 981 |
+
--------
|
| 982 |
+
DataFrame.to_stata : Export DataFrame object to Stata dta format.
|
| 983 |
+
|
| 984 |
+
Examples
|
| 985 |
+
--------
|
| 986 |
+
>>> df = pd.DataFrame({"0categories": pd.Series([2, 2])})
|
| 987 |
+
>>> df.to_stata("test") # doctest: +SKIP
|
| 988 |
+
"""
|
| 989 |
+
|
| 990 |
+
|
| 991 |
+
class CategoricalConversionWarning(Warning):
|
| 992 |
+
"""
|
| 993 |
+
Warning is raised when reading a partial labeled Stata file using an iterator.
|
| 994 |
+
|
| 995 |
+
This warning helps ensure data integrity and alerts users to potential issues
|
| 996 |
+
during the incremental reading of Stata files with labeled data, allowing for
|
| 997 |
+
additional checks and adjustments as necessary.
|
| 998 |
+
|
| 999 |
+
See Also
|
| 1000 |
+
--------
|
| 1001 |
+
read_stata : Read a Stata file into a DataFrame.
|
| 1002 |
+
Categorical : Represents a categorical variable in pandas.
|
| 1003 |
+
|
| 1004 |
+
Examples
|
| 1005 |
+
--------
|
| 1006 |
+
>>> from pandas.io.stata import StataReader
|
| 1007 |
+
>>> with StataReader("dta_file", chunksize=2) as reader: # doctest: +SKIP
|
| 1008 |
+
... for i, block in enumerate(reader):
|
| 1009 |
+
... print(i, block)
|
| 1010 |
+
... # CategoricalConversionWarning: One or more series with value labels...
|
| 1011 |
+
"""
|
| 1012 |
+
|
| 1013 |
+
|
| 1014 |
+
class LossySetitemError(Exception):
|
| 1015 |
+
"""
|
| 1016 |
+
Raised when trying to do a __setitem__ on an np.ndarray that is not lossless.
|
| 1017 |
+
|
| 1018 |
+
Notes
|
| 1019 |
+
-----
|
| 1020 |
+
This is an internal error.
|
| 1021 |
+
"""
|
| 1022 |
+
|
| 1023 |
+
|
| 1024 |
+
class NoBufferPresent(Exception):
|
| 1025 |
+
"""
|
| 1026 |
+
Exception is raised in _get_data_buffer to signal that there is no requested buffer.
|
| 1027 |
+
"""
|
| 1028 |
+
|
| 1029 |
+
|
| 1030 |
+
class InvalidComparison(Exception):
|
| 1031 |
+
"""
|
| 1032 |
+
Exception is raised by _validate_comparison_value to indicate an invalid comparison.
|
| 1033 |
+
|
| 1034 |
+
Notes
|
| 1035 |
+
-----
|
| 1036 |
+
This is an internal error.
|
| 1037 |
+
"""
|
| 1038 |
+
|
| 1039 |
+
|
| 1040 |
+
__all__ = [
|
| 1041 |
+
"AbstractMethodError",
|
| 1042 |
+
"AttributeConflictWarning",
|
| 1043 |
+
"CSSWarning",
|
| 1044 |
+
"CategoricalConversionWarning",
|
| 1045 |
+
"ChainedAssignmentError",
|
| 1046 |
+
"ClosedFileError",
|
| 1047 |
+
"DataError",
|
| 1048 |
+
"DatabaseError",
|
| 1049 |
+
"DtypeWarning",
|
| 1050 |
+
"DuplicateLabelError",
|
| 1051 |
+
"EmptyDataError",
|
| 1052 |
+
"IncompatibilityWarning",
|
| 1053 |
+
"IncompatibleFrequency",
|
| 1054 |
+
"IndexingError",
|
| 1055 |
+
"IntCastingNaNError",
|
| 1056 |
+
"InvalidColumnName",
|
| 1057 |
+
"InvalidComparison",
|
| 1058 |
+
"InvalidIndexError",
|
| 1059 |
+
"InvalidVersion",
|
| 1060 |
+
"LossySetitemError",
|
| 1061 |
+
"MergeError",
|
| 1062 |
+
"NoBufferPresent",
|
| 1063 |
+
"NullFrequencyError",
|
| 1064 |
+
"NumExprClobberingError",
|
| 1065 |
+
"NumbaUtilError",
|
| 1066 |
+
"OptionError",
|
| 1067 |
+
"OutOfBoundsDatetime",
|
| 1068 |
+
"OutOfBoundsTimedelta",
|
| 1069 |
+
"Pandas4Warning",
|
| 1070 |
+
"Pandas5Warning",
|
| 1071 |
+
"PandasChangeWarning",
|
| 1072 |
+
"PandasDeprecationWarning",
|
| 1073 |
+
"PandasFutureWarning",
|
| 1074 |
+
"PandasPendingDeprecationWarning",
|
| 1075 |
+
"ParserError",
|
| 1076 |
+
"ParserWarning",
|
| 1077 |
+
"PerformanceWarning",
|
| 1078 |
+
"PossibleDataLossError",
|
| 1079 |
+
"PossiblePrecisionLoss",
|
| 1080 |
+
"PyperclipException",
|
| 1081 |
+
"PyperclipWindowsException",
|
| 1082 |
+
"SpecificationError",
|
| 1083 |
+
"UndefinedVariableError",
|
| 1084 |
+
"UnsortedIndexError",
|
| 1085 |
+
"UnsupportedFunctionCall",
|
| 1086 |
+
"ValueLabelTypeMismatch",
|
| 1087 |
+
]
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/errors/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (39.4 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/errors/__pycache__/cow.cpython-312.pyc
ADDED
|
Binary file (2.13 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/errors/cow.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
_chained_assignment_msg = (
|
| 2 |
+
"A value is being set on a copy of a DataFrame or Series "
|
| 3 |
+
"through chained assignment.\n"
|
| 4 |
+
"Such chained assignment never works to update the original DataFrame or "
|
| 5 |
+
"Series, because the intermediate object on which we are setting values "
|
| 6 |
+
"always behaves as a copy (due to Copy-on-Write).\n\n"
|
| 7 |
+
"Try using '.loc[row_indexer, col_indexer] = value' instead, to perform "
|
| 8 |
+
"the assignment in a single step.\n\n"
|
| 9 |
+
"See the documentation for a more detailed explanation: "
|
| 10 |
+
"https://pandas.pydata.org/pandas-docs/stable/user_guide/"
|
| 11 |
+
"copy_on_write.html#chained-assignment"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
_chained_assignment_method_msg = (
|
| 16 |
+
"A value is being set on a copy of a DataFrame or Series "
|
| 17 |
+
"through chained assignment using an inplace method.\n"
|
| 18 |
+
"Such inplace method never works to update the original DataFrame or Series, "
|
| 19 |
+
"because the intermediate object on which we are setting values always "
|
| 20 |
+
"behaves as a copy (due to Copy-on-Write).\n\n"
|
| 21 |
+
"For example, when doing 'df[col].method(value, inplace=True)', try "
|
| 22 |
+
"using 'df.method({col: value}, inplace=True)' instead, to perform "
|
| 23 |
+
"the operation inplace on the original object, or try to avoid an inplace "
|
| 24 |
+
"operation using 'df[col] = df[col].method(value)'.\n\n"
|
| 25 |
+
"See the documentation for a more detailed explanation: "
|
| 26 |
+
"https://pandas.pydata.org/pandas-docs/stable/user_guide/"
|
| 27 |
+
"copy_on_write.html"
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
_chained_assignment_method_update_msg = (
|
| 32 |
+
"A value is being set on a copy of a DataFrame or Series "
|
| 33 |
+
"through chained assignment using an inplace method.\n"
|
| 34 |
+
"Such inplace method never works to update the original DataFrame or Series, "
|
| 35 |
+
"because the intermediate object on which we are setting values always "
|
| 36 |
+
"behaves as a copy (due to Copy-on-Write).\n\n"
|
| 37 |
+
"For example, when doing 'df[col].update(other)', try "
|
| 38 |
+
"using 'df.update({col: other})' instead, to perform "
|
| 39 |
+
"the operation inplace on the original object.\n\n"
|
| 40 |
+
"See the documentation for a more detailed explanation: "
|
| 41 |
+
"https://pandas.pydata.org/pandas-docs/stable/user_guide/"
|
| 42 |
+
"copy_on_write.html"
|
| 43 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ruff: noqa: TC004
|
| 2 |
+
from typing import TYPE_CHECKING
|
| 3 |
+
|
| 4 |
+
if TYPE_CHECKING:
|
| 5 |
+
# import modules that have public classes/functions
|
| 6 |
+
from pandas.io import (
|
| 7 |
+
formats,
|
| 8 |
+
json,
|
| 9 |
+
stata,
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
# mark only those modules as public
|
| 13 |
+
__all__ = ["formats", "json", "stata"]
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (351 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/_util.cpython-312.pyc
ADDED
|
Binary file (7.84 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/api.cpython-312.pyc
ADDED
|
Binary file (1.39 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/clipboards.cpython-312.pyc
ADDED
|
Binary file (7.06 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/common.cpython-312.pyc
ADDED
|
Binary file (44 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/feather_format.cpython-312.pyc
ADDED
|
Binary file (7.71 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/html.cpython-312.pyc
ADDED
|
Binary file (46.1 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/iceberg.cpython-312.pyc
ADDED
|
Binary file (5.42 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/pandas/io/__pycache__/orc.cpython-312.pyc
ADDED
|
Binary file (9.63 kB). View file
|
|
|