Repository navigation
TYP: Annotate fitted_values, estimated_effects and idiosyncratic as D… - #842
Merged
Merged
Conversation
…ataFrame The three properties of PanelResults were annotated as Series, but every panel estimator builds them as one-column DataFrames (named "fitted_values", "estimated_effects" and "idiosyncratic"), and predict concatenates them along the columns. Annotate them with what they return. Annotations only; no behavior change.
…ights Every array_like parameter converts a list or a tuple to an array on entry, but ArrayLike and IVDataLike only listed ndarray, Series, DataFrame and DataArray. Add list and tuple to ArrayLike, which IVDataLike builds on. PanelDataLike was built on ArrayLike. It describes dependent and exog, which do not accept a list or a tuple since the layout of the data would not be known, so it now uses a new DataContainer alias (ndarray, Series, DataFrame and DataArray). PanelArrayLike (PanelData or ArrayLike) is for the panel inputs that are array_like: weights, other_effects and clusters. Other changes that follow from this: * as_array_like is overloaded, so that type checkers know that it returns an ndarray for a list or a tuple and the container for a container. Before, it returned Any, and the parameter kept the type that was declared, which includes the list and the tuple that had been converted. * get_array_like and get_panel_data_like return containers only, since they reject a list. * in_2core_graph and in_2core_graph_slow need an array with a shape, so they are annotated as taking a DataFrame or an ndarray, which is what their docstrings say. * predict of the panel and IV results is annotated as the model methods are: PanelDataLike for the panel exog, and IVDataLike for exog and endog and DataFrame for the data of a formula in the IV results. * flake8 ignores E704 in shared/inputs.py, as it does in the other modules that have overloads. Annotations only; no behavior change.
* entity_ids and time_ids of PanelData are integer arrays of any width (the codes of a categorical can be int8, int16 or int32), and are annotated as AnyIntArray, but the covariance estimators, panel_structure_stats and the list of effects in the fast path annotated them as IntArray (np.int_). * IVData.ndarray is float64, except for the categoricals that the absorbing estimator keeps as they are, which it only reads through .pandas. It was annotated as NumericArray, so that every use as float64 data was an error for a type checker. Annotate it as Float64Array. * NumericArray listed signed and unsigned integers separately, which type checkers do not match to the integer type that NumPy functions return. Use the integer type. * IVSystemGMM.fit and _finalize_results annotated the covariance options as bool or float, but the documented ``kernel`` option is a str. Annotations only; no behavior change.
mypy reports these assignments because the first assignment of a name fixes its type: an integer array that becomes a float array, a 2-d array that is replaced by an array of any shape, a float that becomes an array or NaN. Put the annotation that the later assignments need on the first one. Also: * the compat module for SciPy assigns the matrix classes to the names of the array classes when SciPy 1 is installed, and shared.inputs assigns np.VisibleDeprecationWarning when NumPy is older than 1.25. The stubs for SciPy 2 and NumPy 2 reject both, so each assignment has a type: ignore with its error codes and a comment that says why. * the estimator of the first-stage score covariance is one of three weight matrix classes, which are not subclasses of each other. * _validate_sigma returns a 2-d array, so it is annotated as one. Annotations and comments only; no behavior change.
| if TYPE_CHECKING: | ||
| from xarray import DataArray | ||
|
|
||
| from linearmodels.iv.data import IVData |
| from xarray import DataArray | ||
|
|
||
| from linearmodels.iv.data import IVData | ||
| from linearmodels.panel.data import PanelData |
| from pandas.api.types import is_complex_dtype | ||
|
|
||
| if TYPE_CHECKING: | ||
| from xarray import DataArray |
| if TYPE_CHECKING: | ||
| from xarray import DataArray | ||
|
|
||
| from linearmodels.iv.data import IVData |
| from xarray import DataArray | ||
|
|
||
| from linearmodels.iv.data import IVData | ||
| from linearmodels.panel.data import PanelData |
| @overload | ||
| def as_array_like( # numpydoc ignore=GL08 | ||
| value: list[Any] | tuple[Any, ...], name: str | ||
| ) -> np.ndarray: ... |
| @overload | ||
| def as_array_like( # numpydoc ignore=GL08 | ||
| value: _Container, name: str | ||
| ) -> _Container: ... |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #842 +/- ##
==========================================
- Coverage 99.68% 99.68% -0.01%
==========================================
Files 198 198
Lines 37694 37703 +9
Branches 2426 2428 +2
==========================================
+ Hits 37577 37584 +7
Misses 51 51
- Partials 66 68 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
…ataFrame
The three properties of PanelResults were annotated as Series, but every
panel estimator builds them as one-column DataFrames (named "fitted_values",
"estimated_effects" and "idiosyncratic"), and predict concatenates them along
the columns. Annotate them with what they return.
Annotations only; no behavior change.