Repository navigation
feat(core): deprecate vdims (#1011) and kdims (#1010) #1303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
9a3ec68
vdims
priyam0k e7a2ce2
style: fix ruff check and format issues on touched files
priyam0k faa4437
fix(estimator): use columns_label for parameter DataFrames to fix doc…
priyam0k 829f40d
feat: deprecate kdims in favor of Triangle.index and Triangle.key_lab…
priyam0k 7a8a05e
style: address N802 and formatting on touched files after main rebase
priyam0k b16bdc2
fix(core): preserve legacy pickle compatibility for kdims/vdims migra…
priyam0k 3190c05
refactor(core): store columns as pd.Index and index as pd.DataFrame d…
priyam0k 8d79a31
Fix Triangle index copy isolation, JSON I/O, and bootstrap dtypes
priyam0k ac714ef
style: run ruff format on changed core files
priyam0k e341766
fix kdims key labels
priyam0k 3a49f9f
test: improve patch coverage for dims deprecation and concat
priyam0k 75d4522
Implement TriangleAxis for columns
priyam0k df582a6
Fix sort_index mutation
priyam0k 022497c
Address review comments
priyam0k c519555
Use integer axis keys
priyam0k 401be44
Move axis helpers
priyam0k 6a3b33a
Fix trailing newline
priyam0k ac32453
Add patch coverage tests
priyam0k 0e55f6c
Validate boolean series length
priyam0k b2ec506
Format core typing
priyam0k f8f01ea
Fix duplicate column assignment
priyam0k 8244066
Implement index axis descriptor
priyam0k 7899ce7
Drop Triangle._index alias
priyam0k 0bb1b65
Address review requested renames
priyam0k 091dc01
Fix concat nan index
priyam0k 7e5ff84
Address review cleanup requests
priyam0k 60bbd28
Split axis test imports
priyam0k 814a2cb
Group axis test imports
priyam0k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| """Axis descriptor for Triangle dimensions, analogous to Pandas AxisProperty.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any, Callable | ||
| import pandas as pd | ||
|
|
||
| from chainladder.utils.utility_functions import _get_axis_name, _get_axis_number | ||
|
|
||
| if TYPE_CHECKING: | ||
| from chainladder.core.triangle import Triangle | ||
|
|
||
|
|
||
| class TriangleAxis: | ||
| """ | ||
| Generalized descriptor for representing a Triangle dimension, | ||
| analogous to Pandas AxisProperty. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| axis : int or str | ||
| Axis number (0..3) or canonical name ('index', 'columns', 'origin', | ||
| 'development') used to store and access raw axis data in ``obj._axes``. | ||
| fget : callable, optional | ||
| Transformation function taking ``(obj, raw)`` and returning the | ||
| public axis representation. If None, the raw value is returned. | ||
| fset : callable, optional | ||
| Transformation function taking ``(obj, value)`` and returning the | ||
| raw axis representation to be stored in ``obj._axes``. If None, | ||
| the value is stored as-is. | ||
| doc : str, optional | ||
| Docstring for the descriptor attribute on the class. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| axis: int | str, | ||
| *, | ||
| fget: Callable[[Triangle, Any], Any] | None = None, | ||
| fset: Callable[[Triangle, Any], Any] | None = None, | ||
| doc: str | None = None, | ||
|
genedan marked this conversation as resolved.
Resolved
|
||
| ) -> None: | ||
| """ | ||
| Initialize a TriangleAxis descriptor. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| axis : int or str | ||
| Axis number or name mapped to an integer in ``obj._axes``. | ||
| fget : callable, optional | ||
| Getter callable mapping ``(obj, raw)`` to public representation. | ||
| fset : callable, optional | ||
| Setter callable mapping ``(obj, value)`` to internal representation. | ||
| doc : str, optional | ||
| Docstring for the attribute. | ||
| """ | ||
| self.axis: int = _get_axis_number(axis) | ||
| self.key: int = self.axis | ||
| self.fget = fget # raw -> public; identity if None | ||
| self.fset = fset # (obj, public) -> raw; identity if None | ||
| self.__doc__ = doc | ||
|
|
||
| def __get__(self, obj: Triangle | None, objtype: type | None = None) -> Any: | ||
| """ | ||
| Access the axis value on a Triangle instance or return descriptor on class. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| obj : Triangle or None | ||
| Instance of Triangle or None if accessed via class. | ||
| objtype : type, optional | ||
| Type of the owning class. | ||
|
|
||
| Returns | ||
| ------- | ||
| Any | ||
| The descriptor itself if accessed on class, or the transformed | ||
| axis value if accessed on an instance. | ||
|
|
||
| Raises | ||
| ------ | ||
| AttributeError | ||
| If the axis key has not been initialized in ``obj._axes``. | ||
| """ | ||
| if obj is None: | ||
| return self | ||
| if not hasattr(obj, "_axes") or self.axis not in obj._axes: | ||
| axis_name = _get_axis_name(self.axis) | ||
| raise AttributeError( | ||
| f"'{type(obj).__name__}' object has no attribute '{axis_name}'" | ||
| ) | ||
| raw = obj._axes[self.axis] | ||
| return self.fget(obj, raw) if self.fget else raw | ||
|
|
||
| def __set__(self, obj: Triangle, value: Any) -> None: | ||
| """ | ||
| Set the axis value on a Triangle instance. | ||
|
|
||
| Transforms ``value`` using ``fset`` if defined, stores the raw value | ||
| in ``obj._axes[self.axis]``, and updates slicers if necessary. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| obj : Triangle | ||
| Instance of Triangle. | ||
| value : Any | ||
| New axis value provided by the user. | ||
| """ | ||
| raw = self.fset(obj, value) if self.fset else value | ||
| if not hasattr(obj, "_axes"): | ||
| obj._axes = {} | ||
| obj._axes[self.axis] = raw | ||
| if hasattr(obj, "virtual_columns"): | ||
| obj._set_slicers() | ||
|
|
||
|
|
||
| def _set_columns(obj: Triangle, value: Any) -> pd.Index: | ||
| """ | ||
| Validate and transform assigned columns into a pandas Index. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| obj : Triangle | ||
| The Triangle instance being updated. | ||
| value : Any | ||
| The new column labels. Can be a string, list, or pandas Index. | ||
|
|
||
| Returns | ||
| ------- | ||
| pd.Index | ||
| A pandas Index named ``"columns"``. | ||
| """ | ||
| if isinstance(value, str): | ||
| value = [value] | ||
| if hasattr(obj, "values") and obj.values is not None: | ||
| obj._len_check(range(obj.values.shape[1]), value) | ||
| elif hasattr(obj, "_axes") and (1 in obj._axes or "columns" in obj._axes): | ||
| obj._len_check(obj.columns, value) | ||
| return pd.Index(value, name="columns") | ||
|
|
||
|
|
||
| def _set_index(obj: Triangle, value: Any) -> pd.DataFrame: | ||
| """ | ||
| Validate and transform assigned index into a pandas DataFrame. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| obj : Triangle | ||
| The Triangle instance being updated. | ||
| value : Any | ||
| The new index labels. Must be a pandas DataFrame. | ||
|
|
||
| Returns | ||
| ------- | ||
| pd.DataFrame | ||
| A reset-index pandas DataFrame. | ||
| """ | ||
| if not isinstance(value, pd.DataFrame): | ||
| raise TypeError("index must be a pandas DataFrame") | ||
| if hasattr(obj, "values") and obj.values is not None: | ||
| obj._len_check(range(obj.values.shape[0]), value) | ||
| elif hasattr(obj, "_axes") and (0 in obj._axes or "index" in obj._axes): | ||
| obj._len_check(obj.index, value) | ||
| return value.copy().reset_index(drop=True) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.