dycw-utilities 0.159.4__py3-none-any.whl → 0.159.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dycw-utilities
3
- Version: 0.159.4
3
+ Version: 0.159.5
4
4
  Author-email: Derek Wan <d.wan@icloud.com>
5
5
  License-File: LICENSE
6
6
  Requires-Python: >=3.12
@@ -1,4 +1,4 @@
1
- utilities/__init__.py,sha256=8PwC9PN_0tTaC-HGuAmY0sqVwC7bKnFBwSDdjEvRwnk,60
1
+ utilities/__init__.py,sha256=y1aZeGKpHWH6gkJPUx6DJDCZYIPbZ7BynHdMXlgmmME,60
2
2
  utilities/altair.py,sha256=92E2lCdyHY4Zb-vCw6rEJIsWdKipuu-Tu2ab1ufUfAk,9079
3
3
  utilities/asyncio.py,sha256=PUedzQ5deqlSECQ33sam9cRzI9TnygHz3FdOqWJWPTM,15288
4
4
  utilities/atomicwrites.py,sha256=tPo6r-Rypd9u99u66B9z86YBPpnLrlHtwox_8Z7T34Y,5790
@@ -45,7 +45,7 @@ utilities/parse.py,sha256=JcJn5yXKhIWXBCwgBdPsyu7Hvcuw6kyEdqvaebCaI9k,17951
45
45
  utilities/pathlib.py,sha256=qGuU8XPmdgGpy8tOMUgelfXx3kxI8h9IaV3TI_06QGE,8428
46
46
  utilities/pickle.py,sha256=MBT2xZCsv0pH868IXLGKnlcqNx2IRVKYNpRcqiQQqxw,653
47
47
  utilities/platform.py,sha256=pTn7gw6N4T6LdKrf0virwarof_mze9WtoQlrGMzhGVI,2798
48
- utilities/polars.py,sha256=4Qc9lv0SZEpjXFKANUQBb7nA55sC4zaHtlZEJXTBALg,76896
48
+ utilities/polars.py,sha256=QTHk58M2dwIOCSR1JYttlrblUAw8Ihn7M8gAH4CtrGU,79542
49
49
  utilities/polars_ols.py,sha256=Uc9V5kvlWZ5cU93lKZ-cfAKdVFFw81tqwLW9PxtUvMs,5618
50
50
  utilities/postgres.py,sha256=ynCTTaF-bVEOSW-KEAR-dlLh_hYjeVVjm__-4pEU8Zk,12269
51
51
  utilities/pottery.py,sha256=ggMN72Y7wx7Js8VN6eyNyodpm8TIYqZHGghkDPXIVWk,3949
@@ -87,8 +87,8 @@ utilities/zoneinfo.py,sha256=FBMcUQ4662Aq8SsuCL1OAhDQiyANmVjtb-C30DRrWoE,1966
87
87
  utilities/pytest_plugins/__init__.py,sha256=U4S_2y3zgLZVfMenHRaJFBW8yqh2mUBuI291LGQVOJ8,35
88
88
  utilities/pytest_plugins/pytest_randomly.py,sha256=B1qYVlExGOxTywq2r1SMi5o7btHLk2PNdY_b1p98dkE,409
89
89
  utilities/pytest_plugins/pytest_regressions.py,sha256=9v8kAXDM2ycIXJBimoiF4EgrwbUvxTycFWJiGR_GHhM,1466
90
- dycw_utilities-0.159.4.dist-info/METADATA,sha256=WLxPLavB-QLjzJhQr7T5-LbVJNRA_PlRiBtCJTT9FmQ,1643
91
- dycw_utilities-0.159.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
92
- dycw_utilities-0.159.4.dist-info/entry_points.txt,sha256=BOD_SoDxwsfJYOLxhrSXhHP_T7iw-HXI9f2WVkzYxvQ,135
93
- dycw_utilities-0.159.4.dist-info/licenses/LICENSE,sha256=gppZp16M6nSVpBbUBrNL6JuYfvKwZiKgV7XoKKsHzqo,1066
94
- dycw_utilities-0.159.4.dist-info/RECORD,,
90
+ dycw_utilities-0.159.5.dist-info/METADATA,sha256=HkJFj45zxD0WbiOwR0QfrBq9RjjFHmI8aieoNk49Voo,1643
91
+ dycw_utilities-0.159.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
92
+ dycw_utilities-0.159.5.dist-info/entry_points.txt,sha256=BOD_SoDxwsfJYOLxhrSXhHP_T7iw-HXI9f2WVkzYxvQ,135
93
+ dycw_utilities-0.159.5.dist-info/licenses/LICENSE,sha256=gppZp16M6nSVpBbUBrNL6JuYfvKwZiKgV7XoKKsHzqo,1066
94
+ dycw_utilities-0.159.5.dist-info/RECORD,,
utilities/__init__.py CHANGED
@@ -1,3 +1,3 @@
1
1
  from __future__ import annotations
2
2
 
3
- __version__ = "0.159.4"
3
+ __version__ = "0.159.5"
utilities/polars.py CHANGED
@@ -57,6 +57,7 @@ import utilities.math
57
57
  from utilities.dataclasses import yield_fields
58
58
  from utilities.errors import ImpossibleCaseError
59
59
  from utilities.functions import (
60
+ get_class_name,
60
61
  is_dataclass_class,
61
62
  is_dataclass_instance,
62
63
  make_isinstance,
@@ -1324,10 +1325,27 @@ class _FiniteEWMWeightsError(Exception):
1324
1325
  ##
1325
1326
 
1326
1327
 
1327
- def first_true_horizontal(df: DataFrame, /) -> Series:
1328
+ @overload
1329
+ def first_true_horizontal(column: Series, /) -> Series: ...
1330
+ @overload
1331
+ def first_true_horizontal(column1: Series, column2: Series, /) -> Series: ...
1332
+ @overload
1333
+ def first_true_horizontal(
1334
+ column1: Series, column2: Series, column3: Series, /
1335
+ ) -> Series: ...
1336
+ @overload
1337
+ def first_true_horizontal(
1338
+ column1: Series, column2: Series, column3: Series, column4: Series, /
1339
+ ) -> Series: ...
1340
+ @overload
1341
+ def first_true_horizontal(*columns: Series) -> Series: ...
1342
+ @overload
1343
+ def first_true_horizontal(*columns: IntoExprColumn) -> ExprOrSeries: ...
1344
+ def first_true_horizontal(*columns: IntoExprColumn) -> ExprOrSeries:
1328
1345
  """Get the index of the first true in each row."""
1329
- expr = when(any_horizontal(pl.all())).then(concat_list(pl.all()).list.arg_max())
1330
- return one_column(df.select(expr))
1346
+ columns2 = ensure_expr_or_series_many(*columns)
1347
+ expr = when(any_horizontal(*columns2)).then(concat_list(*columns2).list.arg_max())
1348
+ return try_reify_expr(expr, *columns2)
1331
1349
 
1332
1350
 
1333
1351
  ##
@@ -1972,19 +1990,31 @@ def normal(
1972
1990
  ##
1973
1991
 
1974
1992
 
1993
+ @overload
1994
+ def number_of_decimals(
1995
+ series: ExprLike, /, *, max_decimals: int = MAX_DECIMALS
1996
+ ) -> Expr: ...
1997
+ @overload
1975
1998
  def number_of_decimals(
1976
1999
  series: Series, /, *, max_decimals: int = MAX_DECIMALS
1977
- ) -> Series:
2000
+ ) -> Series: ...
2001
+ @overload
2002
+ def number_of_decimals(
2003
+ series: IntoExprColumn, /, *, max_decimals: int = MAX_DECIMALS
2004
+ ) -> ExprOrSeries: ...
2005
+ def number_of_decimals(
2006
+ series: IntoExprColumn, /, *, max_decimals: int = MAX_DECIMALS
2007
+ ) -> ExprOrSeries:
1978
2008
  """Get the number of decimals."""
2009
+ series = ensure_expr_or_series(series)
1979
2010
  frac = series - series.floor()
1980
- results = [
2011
+ results = (
1981
2012
  _number_of_decimals_check_scale(frac, s) for s in range(max_decimals + 1)
1982
- ]
1983
- df_results = concat_series(*results)
1984
- return first_true_horizontal(df_results)
2013
+ )
2014
+ return first_true_horizontal(*results)
1985
2015
 
1986
2016
 
1987
- def _number_of_decimals_check_scale(frac: Series, scale: int, /) -> Series:
2017
+ def _number_of_decimals_check_scale(frac: ExprOrSeries, scale: int, /) -> ExprOrSeries:
1988
2018
  scaled = 10**scale * frac
1989
2019
  return is_close(scaled, scaled.round()).alias(str(scale))
1990
2020
 
@@ -2346,19 +2376,63 @@ def round_to_float(
2346
2376
  ) -> Expr: ...
2347
2377
  @overload
2348
2378
  def round_to_float(
2349
- x: Series, y: float, /, *, mode: RoundMode = "half_to_even"
2379
+ x: Series, y: float | ExprOrSeries, /, *, mode: RoundMode = "half_to_even"
2380
+ ) -> Series: ...
2381
+ @overload
2382
+ def round_to_float(
2383
+ x: ExprLike, y: Series, /, *, mode: RoundMode = "half_to_even"
2350
2384
  ) -> Series: ...
2351
2385
  @overload
2352
2386
  def round_to_float(
2353
- x: IntoExprColumn, y: float, /, *, mode: RoundMode = "half_to_even"
2387
+ x: ExprLike, y: Expr, /, *, mode: RoundMode = "half_to_even"
2388
+ ) -> Expr: ...
2389
+ @overload
2390
+ def round_to_float(
2391
+ x: IntoExprColumn, y: float | Series, /, *, mode: RoundMode = "half_to_even"
2354
2392
  ) -> ExprOrSeries: ...
2355
2393
  def round_to_float(
2356
- x: IntoExprColumn, y: float, /, *, mode: RoundMode = "half_to_even"
2394
+ x: IntoExprColumn, y: float | IntoExprColumn, /, *, mode: RoundMode = "half_to_even"
2357
2395
  ) -> ExprOrSeries:
2358
2396
  """Round a column to the nearest multiple of another float."""
2359
2397
  x = ensure_expr_or_series(x)
2360
- z = (x / y).round(mode=mode) * y
2361
- return z.round(decimals=utilities.math.number_of_decimals(y) + 1)
2398
+ y = y if isinstance(y, int | float) else ensure_expr_or_series(y)
2399
+ match x, y:
2400
+ case Expr() | Series(), int() | float():
2401
+ z = (x / y).round(mode=mode) * y
2402
+ return z.round(decimals=utilities.math.number_of_decimals(y) + 1)
2403
+ case Series(), Expr() | Series():
2404
+ df = (
2405
+ x.to_frame()
2406
+ .with_columns(y)
2407
+ .with_columns(number_of_decimals(y).alias("_decimals"))
2408
+ .with_row_index(name="_index")
2409
+ .group_by("_decimals")
2410
+ .map_groups(_round_to_float_one)
2411
+ .sort("_index")
2412
+ )
2413
+ return df[df.columns[1]]
2414
+ case Expr(), Series():
2415
+ df = y.to_frame().with_columns(x)
2416
+ return round_to_float(df[df.columns[1]], df[df.columns[0]], mode=mode)
2417
+ case Expr(), Expr() | str():
2418
+ raise RoundToFloatError(x=x, y=y)
2419
+ case never:
2420
+ assert_never(never)
2421
+
2422
+
2423
+ def _round_to_float_one(df: DataFrame, /) -> DataFrame:
2424
+ decimals: int = df["_decimals"].unique().item()
2425
+ return df.with_columns(col(df.columns[1]).round(decimals=decimals))
2426
+
2427
+
2428
+ @dataclass(kw_only=True, slots=True)
2429
+ class RoundToFloatError(Exception):
2430
+ x: IntoExprColumn
2431
+ y: IntoExprColumn
2432
+
2433
+ @override
2434
+ def __str__(self) -> str:
2435
+ return f"At least 1 of the dividend and/or divisor must be a Series; got {get_class_name(self.x)!r} and {get_class_name(self.y)!r}"
2362
2436
 
2363
2437
 
2364
2438
  ##
@@ -2571,6 +2645,7 @@ __all__ = [
2571
2645
  "OneColumnEmptyError",
2572
2646
  "OneColumnError",
2573
2647
  "OneColumnNonUniqueError",
2648
+ "RoundToFloatError",
2574
2649
  "SetFirstRowAsColumnsError",
2575
2650
  "TimePeriodDType",
2576
2651
  "acf",