From 09b150f89042a11b278b954631a83fbd86c9aa79 Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Fri, 7 Jun 2024 12:40:56 +0100 Subject: [PATCH] DOC: Fix many aliases --- doc/source/conf.py | 9 ++++++ doc/source/nitpick-exceptions | 5 +++ linearmodels/asset_pricing/covariance.py | 13 ++++---- linearmodels/asset_pricing/model.py | 17 ++++++---- linearmodels/iv/_utility.py | 6 ++-- linearmodels/iv/absorbing.py | 6 ++-- linearmodels/iv/data.py | 3 +- linearmodels/iv/gmm.py | 15 +++++---- linearmodels/iv/model.py | 17 +++++----- linearmodels/iv/results.py | 8 +++-- linearmodels/panel/covariance.py | 3 +- linearmodels/panel/data.py | 22 +++++++------ linearmodels/panel/model.py | 41 +++++++++++++++--------- linearmodels/panel/results.py | 13 ++++---- linearmodels/panel/utility.py | 12 ++++--- linearmodels/shared/utility.py | 5 ++- linearmodels/system/_utility.py | 10 +++--- linearmodels/system/covariance.py | 27 ++++++++-------- linearmodels/system/gmm.py | 7 ++-- linearmodels/system/model.py | 15 +++++---- linearmodels/system/results.py | 7 ++-- linearmodels/tests/panel/_utility.py | 8 ++++- linearmodels/typing/__init__.py | 7 ++-- linearmodels/typing/data.py | 4 +-- 24 files changed, 171 insertions(+), 109 deletions(-) create mode 100644 doc/source/nitpick-exceptions diff --git a/doc/source/conf.py b/doc/source/conf.py index e234d4bf16..b83894b86b 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -23,6 +23,15 @@ # More warnings nitpicky = True +nitpick_ignore = [] + +for line in open('nitpick-exceptions'): + if line.strip() == "" or line.startswith("#"): + continue + dtype, target = line.split(None, 1) + target = target.strip() + nitpick_ignore.append((dtype, target)) + # The short X.Y version full_version = parse(linearmodels.__version__) diff --git a/doc/source/nitpick-exceptions b/doc/source/nitpick-exceptions new file mode 100644 index 0000000000..7c04d51c06 --- /dev/null +++ b/doc/source/nitpick-exceptions @@ -0,0 +1,5 @@ +py:class ArrayLike +py:class IVDataLike +py:class Float64Array +py:class IntArray + diff --git a/linearmodels/asset_pricing/covariance.py b/linearmodels/asset_pricing/covariance.py index 0bdb03ff3d..756768381c 100644 --- a/linearmodels/asset_pricing/covariance.py +++ b/linearmodels/asset_pricing/covariance.py @@ -4,7 +4,8 @@ from __future__ import annotations -from numpy import empty, ndarray +import numpy +from numpy import empty from numpy.linalg import inv from linearmodels.iv.covariance import ( @@ -19,7 +20,7 @@ class _HACMixin: def __init__(self, kernel: str, bandwidth: float | None) -> None: self._kernel: str | None = None self._bandwidth: float | None = None # pragma: no cover - self._moments: ndarray = empty((0,)) # pragma: no cover + self._moments: numpy.ndarray = empty((0,)) # pragma: no cover self._check_kernel(kernel) self._check_bandwidth(bandwidth) @@ -98,8 +99,8 @@ def __init__( self, xe: Float64Array, *, - jacobian: ndarray | None = None, - inv_jacobian: ndarray | None = None, + jacobian: numpy.ndarray | None = None, + inv_jacobian: numpy.ndarray | None = None, center: bool = True, debiased: bool = False, df: int = 0, @@ -231,8 +232,8 @@ def __init__( self, xe: Float64Array, *, - jacobian: ndarray | None = None, - inv_jacobian: ndarray | None = None, + jacobian: numpy.ndarray | None = None, + inv_jacobian: numpy.ndarray | None = None, kernel: str | None = None, bandwidth: float | None = None, center: bool = True, diff --git a/linearmodels/asset_pricing/model.py b/linearmodels/asset_pricing/model.py index 97319c5bec..49937c3885 100644 --- a/linearmodels/asset_pricing/model.py +++ b/linearmodels/asset_pricing/model.py @@ -10,6 +10,7 @@ from formulaic.materializers.types import NAAction import numpy as np from numpy.linalg import lstsq +import pandas from pandas import DataFrame from scipy.optimize import minimize @@ -134,7 +135,7 @@ def formula(self, value: str | None) -> None: @staticmethod def _prepare_data_from_formula( - formula: str, data: DataFrame, portfolios: DataFrame | None + formula: str, data: pandas.DataFrame, portfolios: pandas.DataFrame | None ) -> tuple[DataFrame, DataFrame, str]: orig_formula = formula na_action = NAAction("raise") @@ -203,7 +204,11 @@ def __init__(self, portfolios: IVDataLike, factors: IVDataLike): @classmethod def from_formula( - cls, formula: str, data: DataFrame, *, portfolios: DataFrame | None = None + cls, + formula: str, + data: pandas.DataFrame, + *, + portfolios: pandas.DataFrame | None = None, ) -> TradedFactorModel: """ Parameters @@ -517,9 +522,9 @@ def __init__( def from_formula( cls, formula: str, - data: DataFrame, + data: pandas.DataFrame, *, - portfolios: DataFrame | None = None, + portfolios: pandas.DataFrame | None = None, risk_free: bool = False, sigma: ArrayLike | None = None, ) -> LinearFactorModel: @@ -818,9 +823,9 @@ def __init__( def from_formula( cls, formula: str, - data: DataFrame, + data: pandas.DataFrame, *, - portfolios: DataFrame | None = None, + portfolios: pandas.DataFrame | None = None, risk_free: bool = False, ) -> LinearFactorModelGMM: """ diff --git a/linearmodels/iv/_utility.py b/linearmodels/iv/_utility.py index 67438f99f2..81d041fbe3 100644 --- a/linearmodels/iv/_utility.py +++ b/linearmodels/iv/_utility.py @@ -7,7 +7,9 @@ from formulaic.formula import Formula from formulaic.materializers.types import NAAction as fNAAction from formulaic.utils.context import capture_context +import numpy import numpy as np +import pandas from pandas import DataFrame from linearmodels.typing import Float64Array @@ -91,7 +93,7 @@ class IVFormulaParser: def __init__( self, formula: str, - data: DataFrame, + data: pandas.DataFrame, eval_env: int = 2, context: Mapping[str, Any] | None = None, ): @@ -222,5 +224,5 @@ def components(self) -> dict[str, str]: return self._components @staticmethod - def _empty_check(arr: DataFrame) -> DataFrame | None: + def _empty_check(arr: pandas.DataFrame) -> DataFrame | None: return None if arr.shape[1] == 0 else arr diff --git a/linearmodels/iv/absorbing.py b/linearmodels/iv/absorbing.py index 1a203169df..ae88f0ab7f 100644 --- a/linearmodels/iv/absorbing.py +++ b/linearmodels/iv/absorbing.py @@ -8,7 +8,6 @@ from numpy import ( any as npany, arange, - array, asarray, ascontiguousarray, average, @@ -31,6 +30,7 @@ zeros, ) from numpy.linalg import lstsq +import pandas from pandas import Categorical, CategoricalDtype, DataFrame, Series import scipy.sparse as sp from scipy.sparse.linalg import lsmr @@ -211,7 +211,9 @@ def category_product(cats: AnyPandas) -> Series: return Series(Categorical(codes), index=cats.index) -def category_interaction(cat: Series, precondition: bool = True) -> sp.csc_matrix: +def category_interaction( + cat: pandas.Series, precondition: bool = True +) -> sp.csc_matrix: """ Parameters ---------- diff --git a/linearmodels/iv/data.py b/linearmodels/iv/data.py index d9e6908e14..a50cb4d1f8 100644 --- a/linearmodels/iv/data.py +++ b/linearmodels/iv/data.py @@ -8,6 +8,7 @@ from typing import Any, Union import numpy as np +import pandas import pandas as pd from pandas.api.types import is_numeric_dtype, is_string_dtype @@ -26,7 +27,7 @@ def convert_columns(s: pd.Series, drop_first: bool) -> AnyPandas: return s -def expand_categoricals(x: pd.DataFrame, drop_first: bool) -> pd.DataFrame: +def expand_categoricals(x: pandas.DataFrame, drop_first: bool) -> pd.DataFrame: if x.shape[1] == 0: return x return pd.concat( diff --git a/linearmodels/iv/gmm.py b/linearmodels/iv/gmm.py index 339d65ef13..cdb9313f1d 100644 --- a/linearmodels/iv/gmm.py +++ b/linearmodels/iv/gmm.py @@ -6,7 +6,8 @@ from typing import Any -from numpy import asarray, ndarray, unique +import numpy +from numpy import asarray, unique from numpy.linalg import inv from linearmodels.iv.covariance import ( @@ -75,7 +76,7 @@ def weight_matrix( return w @property - def config(self) -> dict[str, str | bool | ndarray | int | None]: + def config(self) -> dict[str, str | bool | numpy.ndarray | int | None]: """ Weight estimator configuration @@ -239,7 +240,7 @@ def weight_matrix( return s @property - def config(self) -> dict[str, str | bool | ndarray | int | None]: + def config(self) -> dict[str, str | bool | numpy.ndarray | int | None]: """ Weight estimator configuration @@ -324,7 +325,7 @@ def weight_matrix( return s @property - def config(self) -> dict[str, str | bool | ndarray | int | None]: + def config(self) -> dict[str, str | bool | numpy.ndarray | int | None]: """ Weight estimator configuration @@ -466,7 +467,9 @@ def cov(self) -> Float64Array: return (c + c.T) / 2 @property - def config(self) -> dict[str, str | bool | ndarray | int | None]: - conf: dict[str, str | bool | ndarray | int | None] = {"debiased": self.debiased} + def config(self) -> dict[str, str | bool | numpy.ndarray | int | None]: + conf: dict[str, str | bool | numpy.ndarray | int | None] = { + "debiased": self.debiased + } conf.update(self._cov_config) return conf diff --git a/linearmodels/iv/model.py b/linearmodels/iv/model.py index 2d1c722c72..daa6d45157 100644 --- a/linearmodels/iv/model.py +++ b/linearmodels/iv/model.py @@ -26,6 +26,7 @@ squeeze, ) from numpy.linalg import eigvalsh, inv, matrix_rank, pinv +import pandas from pandas import DataFrame, Series, concat from scipy.optimize import minimize @@ -243,7 +244,7 @@ def predict( *, exog: IVDataLike | None = None, endog: IVDataLike | None = None, - data: DataFrame | None = None, + data: pandas.DataFrame | None = None, eval_env: int = 4, ) -> DataFrame: """ @@ -776,7 +777,7 @@ def __init__( @staticmethod def from_formula( formula: str, - data: DataFrame, + data: pandas.DataFrame, *, weights: IVDataLike | None = None, fuller: float = 0, @@ -889,7 +890,7 @@ def __init__( @staticmethod def from_formula( - formula: str, data: DataFrame, *, weights: IVDataLike | None = None + formula: str, data: pandas.DataFrame, *, weights: IVDataLike | None = None ) -> IV2SLS: """ Parameters @@ -1106,7 +1107,7 @@ def __init__( @staticmethod def from_formula( formula: str, - data: DataFrame, + data: pandas.DataFrame, *, weights: IVDataLike | None = None, weight_type: str = "robust", @@ -1214,7 +1215,7 @@ def fit( Convergence criteria. Measured as covariance normalized change in parameters across iterations where the covariance estimator is based on the first step parameter estimates. - initial_weight : ndarray + initial_weight : numpy.ndarray Initial weighting matrix to use in the first step. If not specified, uses the average outer-product of the set containing the exogenous variables and instruments. @@ -1379,7 +1380,7 @@ def __init__( @staticmethod def from_formula( formula: str, - data: DataFrame, + data: pandas.DataFrame, *, weights: IVDataLike | None = None, weight_type: str = "robust", @@ -1536,7 +1537,7 @@ def estimate_parameters( def fit( self, *, - starting: Float64Array | Series | None = None, + starting: Float64Array | pandas.Series | None = None, display: bool = False, cov_type: str = "robust", debiased: bool = False, @@ -1664,7 +1665,7 @@ def __init__( def _gmm_model_from_formula( cls: type[IVGMM] | type[IVGMMCUE], formula: str, - data: DataFrame, + data: pandas.DataFrame, weights: IVDataLike | None, weight_type: str, **weight_config: Any, diff --git a/linearmodels/iv/results.py b/linearmodels/iv/results.py index 3916e40591..4afa175690 100644 --- a/linearmodels/iv/results.py +++ b/linearmodels/iv/results.py @@ -11,6 +11,7 @@ from functools import cached_property from typing import Any, Union +import numpy from numpy import ( array, asarray, @@ -25,6 +26,7 @@ squeeze, ) from numpy.linalg import inv +import pandas from pandas import DataFrame, Series, concat, to_numeric import scipy.stats as stats from statsmodels.iolib.summary import SimpleTable, fmt_2cols, fmt_params @@ -407,8 +409,8 @@ def _update_extra_text(self, extra_text: list[str]) -> list[str]: def wald_test( self, - restriction: DataFrame | ndarray | None = None, - value: Series | ndarray | None = None, + restriction: pandas.DataFrame | numpy.ndarray | None = None, + value: pandas.Series | numpy.ndarray | None = None, *, formula: str | list[str] | dict[str, float] | None = None, ) -> WaldTestStatistic: @@ -524,7 +526,7 @@ def predict( exog: ArrayLike | None = None, endog: ArrayLike | None = None, *, - data: DataFrame | None = None, + data: pandas.DataFrame | None = None, fitted: bool = True, idiosyncratic: bool = False, missing: bool = False, diff --git a/linearmodels/panel/covariance.py b/linearmodels/panel/covariance.py index 05a9cef3c5..b6d735a879 100644 --- a/linearmodels/panel/covariance.py +++ b/linearmodels/panel/covariance.py @@ -5,6 +5,7 @@ import numpy as np from numpy.linalg import inv +import pandas from pandas import DataFrame, MultiIndex from linearmodels.iv.covariance import ( @@ -669,7 +670,7 @@ def __init__( y: Float64Array, x: Float64Array, params: Float64Array, - all_params: DataFrame, + all_params: pandas.DataFrame, *, debiased: bool = False, bandwidth: float | None = None, diff --git a/linearmodels/panel/data.py b/linearmodels/panel/data.py index 6ea1135731..974db5aec2 100644 --- a/linearmodels/panel/data.py +++ b/linearmodels/panel/data.py @@ -8,6 +8,7 @@ import numpy as np from numpy.linalg import lstsq +import pandas import pandas as pd from pandas import ( Categorical, @@ -53,7 +54,7 @@ class _Panel: into a minimal pandas Panel-like object """ - def __init__(self, df: DataFrame): + def __init__(self, df: pandas.DataFrame): self._items = df.columns index = df.index assert isinstance(index, MultiIndex) @@ -109,7 +110,7 @@ def to_frame(self) -> DataFrame: return self._frame -def convert_columns(s: Series, drop_first: bool) -> AnyPandas: +def convert_columns(s: pandas.Series, drop_first: bool) -> AnyPandas: if is_string_dtype(s.dtype) and s.map(lambda v: isinstance(v, str)).all(): s = s.astype("category") @@ -121,7 +122,7 @@ def convert_columns(s: Series, drop_first: bool) -> AnyPandas: return s -def expand_categoricals(x: DataFrame, drop_first: bool) -> DataFrame: +def expand_categoricals(x: pandas.DataFrame, drop_first: bool) -> DataFrame: return concat( [convert_columns(x[c], drop_first) for c in x.columns], axis=1, sort=False ) @@ -291,7 +292,7 @@ def values3d(self) -> AnyArray: """NumPy ndarray view of panel""" return self.panel.values - def drop(self, locs: Series | BoolArray) -> None: + def drop(self, locs: pandas.Series | BoolArray) -> None: """ Drop observations from the panel. @@ -463,7 +464,10 @@ def general_demean( weight_sum: dict[int, Series | DataFrame] = {} def weighted_group_mean( - df: DataFrame, weights: DataFrame, root_w: Float64Array, level: int + df: pandas.DataFrame, + weights: pandas.DataFrame, + root_w: Float64Array, + level: int, ) -> Float64Array: scaled_df = cast(DataFrame, root_w * df) num = scaled_df.groupby(level=level).transform("sum") @@ -476,7 +480,7 @@ def weighted_group_mean( return np.asarray(num) / denom def demean_pass( - frame: DataFrame, weights: DataFrame, root_w: Float64Array + frame: pandas.DataFrame, weights: pandas.DataFrame, root_w: Float64Array ) -> DataFrame: levels = groups.shape[1] for level in range(levels): @@ -498,7 +502,7 @@ def demean_pass( wframe.index = init_index.index previous = wframe - current: DataFrame = demean_pass(previous, weights_df, root_w) + current: pandas.DataFrame = demean_pass(previous, weights_df, root_w) if groups.shape[1] == 1: current.index = self._frame.index return PanelData(current) @@ -660,7 +664,7 @@ def copy(self) -> PanelData: def mean( self, group: str = "entity", weights: PanelData | None = None - ) -> DataFrame: + ) -> pandas.DataFrame: """ Compute data mean by either entity or time group @@ -715,7 +719,7 @@ def first_difference(self) -> PanelData: return PanelData(diffs_frame) @staticmethod - def _minimize_multiindex(df: DataFrame) -> DataFrame: + def _minimize_multiindex(df: pandas.DataFrame) -> DataFrame: index_cols = list(df.index.names) orig_names = index_cols[:] for i, col in enumerate(index_cols): diff --git a/linearmodels/panel/model.py b/linearmodels/panel/model.py index 8f5f8a3ee5..30b393a52d 100644 --- a/linearmodels/panel/model.py +++ b/linearmodels/panel/model.py @@ -10,6 +10,7 @@ from formulaic.parser.algos.tokenize import tokenize from formulaic.utils.context import capture_context import numpy as np +import pandas from pandas import Categorical, DataFrame, Index, MultiIndex, Series, get_dummies from scipy.linalg import lstsq as sp_lstsq from scipy.sparse import csc_matrix, diags @@ -110,7 +111,11 @@ class FInfo(NamedTuple): def _deferred_f( - params: Series, cov: DataFrame, debiased: bool, df_resid: int, f_info: FInfo + params: pandas.Series, + cov: pandas.DataFrame, + debiased: bool, + df_resid: int, + f_info: FInfo, ) -> InvalidTestStatistic | WaldTestStatistic: if f_info.is_invalid: assert f_info.invalid_test_stat is not None @@ -724,8 +729,10 @@ def not_null(self) -> BoolArray: def _setup_clusters( self, - cov_config: Mapping[str, bool | float | str | IntArray | DataFrame | PanelData], - ) -> dict[str, bool | float | str | IntArray | DataFrame | PanelData]: + cov_config: Mapping[ + str, bool | float | str | IntArray | pandas.DataFrame | PanelData + ], + ) -> dict[str, bool | float | str | IntArray | pandas.DataFrame | PanelData]: cov_config_upd = dict(cov_config) cluster_types = ("clusters", "cluster_entity", "cluster_time") common = set(cov_config.keys()).intersection(cluster_types) @@ -735,7 +742,7 @@ def _setup_clusters( cov_config_upd = {k: v for k, v in cov_config.items()} clusters = get_panel_data_like(cov_config, "clusters") - clusters_frame: DataFrame | None = None + clusters_frame: pandas.DataFrame | None = None if clusters is not None: formatted_clusters = self.reformat_clusters(clusters) for col in formatted_clusters.dataframe: @@ -947,7 +954,7 @@ def fit( *, cov_type: str = "unadjusted", debiased: bool = True, - **cov_config: bool | float | str | IntArray | DataFrame | PanelData, + **cov_config: bool | float | str | IntArray | pandas.DataFrame | PanelData, ) -> PanelResults: """ Estimate model parameters @@ -1720,7 +1727,7 @@ def _is_effect_nested(effects: NumericArray, clusters: NumericArray) -> bool: def _determine_df_adjustment( self, cov_type: str, - **cov_config: bool | float | str | IntArray | DataFrame | PanelData, + **cov_config: bool | float | str | IntArray | pandas.DataFrame | PanelData, ) -> bool: if cov_type != "clustered" or not self._has_effect: return True @@ -1748,7 +1755,7 @@ def fit( debiased: bool = True, auto_df: bool = True, count_effects: bool = True, - **cov_config: bool | float | str | IntArray | DataFrame | PanelData, + **cov_config: bool | float | str | IntArray | pandas.DataFrame | PanelData, ) -> PanelEffectsResults: """ Estimate model parameters @@ -2065,8 +2072,10 @@ def __init__( def _setup_clusters( self, - cov_config: Mapping[str, bool | float | str | IntArray | DataFrame | PanelData], - ) -> dict[str, bool | float | str | IntArray | DataFrame | PanelData]: + cov_config: Mapping[ + str, bool | float | str | IntArray | pandas.DataFrame | PanelData + ], + ) -> dict[str, bool | float | str | IntArray | pandas.DataFrame | PanelData]: """Return covariance estimator reformat clusters""" cov_config_upd = dict(cov_config) if "clusters" not in cov_config: @@ -2100,7 +2109,7 @@ def fit( reweight: bool = False, cov_type: str = "unadjusted", debiased: bool = True, - **cov_config: bool | float | str | IntArray | DataFrame | PanelData, + **cov_config: bool | float | str | IntArray | pandas.DataFrame | PanelData, ) -> PanelResults: """ Estimate model parameters @@ -2325,8 +2334,10 @@ def __init__( def _setup_clusters( self, - cov_config: Mapping[str, bool | float | str | IntArray | DataFrame | PanelData], - ) -> dict[str, bool | float | str | IntArray | DataFrame | PanelData]: + cov_config: Mapping[ + str, bool | float | str | IntArray | pandas.DataFrame | PanelData + ], + ) -> dict[str, bool | float | str | IntArray | pandas.DataFrame | PanelData]: cov_config_upd = dict(cov_config).copy() cluster_types = ("clusters", "cluster_entity") common = set(cov_config.keys()).intersection(cluster_types) @@ -2381,7 +2392,7 @@ def fit( *, cov_type: str = "unadjusted", debiased: bool = True, - **cov_config: bool | float | str | IntArray | DataFrame | PanelData, + **cov_config: bool | float | str | IntArray | pandas.DataFrame | PanelData, ) -> PanelResults: """ Estimate model parameters @@ -2684,7 +2695,7 @@ def fit( small_sample: bool = False, cov_type: str = "unadjusted", debiased: bool = True, - **cov_config: bool | float | str | IntArray | DataFrame | PanelData, + **cov_config: bool | float | str | IntArray | pandas.DataFrame | PanelData, ) -> RandomEffectsResults: """ Estimate model parameters @@ -2920,7 +2931,7 @@ def _validate_blocks(self) -> None: wx[self._not_null], index=exog.notnull().index, columns=exog.columns ) - def validate_block(ex: Float64Array | DataFrame) -> bool: + def validate_block(ex: Float64Array | pandas.DataFrame) -> bool: _ex = np.asarray(ex, dtype=float) def _mr(ex: Float64Array) -> int: diff --git a/linearmodels/panel/results.py b/linearmodels/panel/results.py index b7a10c477b..d000e301c1 100644 --- a/linearmodels/panel/results.py +++ b/linearmodels/panel/results.py @@ -9,6 +9,7 @@ from formulaic.utils.context import capture_context import numpy as np +import pandas from pandas import DataFrame, Series, concat from scipy import stats from statsmodels.iolib.summary import SimpleTable, fmt_2cols, fmt_params @@ -476,7 +477,7 @@ def resids(self) -> Series: def _out_of_sample( self, exog: ArrayLike | None, - data: DataFrame | None, + data: pandas.DataFrame | None, missing: bool, context: Mapping[str, Any] | None = None, ) -> DataFrame: @@ -495,7 +496,7 @@ def predict( self, exog: ArrayLike | None = None, *, - data: DataFrame | None = None, + data: pandas.DataFrame | None = None, fitted: bool = True, effects: bool = False, idiosyncratic: bool = False, @@ -551,7 +552,7 @@ def predict( out.append(self.idiosyncratic) if len(out) == 0: raise ValueError("At least one output must be selected") - out_df: DataFrame = concat(out, axis=1) + out_df: pandas.DataFrame = concat(out, axis=1) if missing: index = self._original_index out_df = out_df.reindex(index) @@ -666,8 +667,8 @@ def loglik(self) -> float: def wald_test( self, - restriction: Float64Array | DataFrame | None = None, - value: Float64Array | Series | None = None, + restriction: Float64Array | pandas.DataFrame | None = None, + value: Float64Array | pandas.Series | None = None, *, formula: str | list[str] | None = None, ) -> WaldTestStatistic: @@ -811,7 +812,7 @@ def included_effects(self) -> list[str]: return effects @property - def other_info(self) -> DataFrame | None: + def other_info(self) -> pandas.DataFrame | None: """Statistics on observations per group for other effects""" return self._other_info diff --git a/linearmodels/panel/utility.py b/linearmodels/panel/utility.py index ec5a1c8054..d6b4526374 100644 --- a/linearmodels/panel/utility.py +++ b/linearmodels/panel/utility.py @@ -6,6 +6,8 @@ from typing import NamedTuple, TypeVar, cast import numpy as np +import numpy.random +import pandas from pandas import DataFrame, concat, date_range import scipy.sparse as sp @@ -513,10 +515,10 @@ class PanelModelData(NamedTuple): DataFrame containing cluster ids. """ - data: DataFrame - weights: DataFrame - other_effects: DataFrame - clusters: DataFrame + data: pandas.DataFrame + weights: pandas.DataFrame + other_effects: pandas.DataFrame + clusters: pandas.DataFrame def generate_panel_data( @@ -527,7 +529,7 @@ def generate_panel_data( missing: float = 0, other_effects: int = 2, ncats: int | list[int] = 4, - rng: np.random.RandomState | None = None, + rng: numpy.random.RandomState | None = None, ) -> PanelModelData: """ Simulate panel data for testing diff --git a/linearmodels/shared/utility.py b/linearmodels/shared/utility.py index c65a3193d6..be087042bc 100644 --- a/linearmodels/shared/utility.py +++ b/linearmodels/shared/utility.py @@ -13,6 +13,7 @@ from typing import Any, Callable, Protocol, TypeVar, cast import numpy as np +import pandas from pandas import DataFrame, Index, MultiIndex, Series from linearmodels.typing import AnyArray, Label @@ -143,7 +144,9 @@ def __iter__(self) -> Iterator[Label]: return self.__private_dict__.__iter__() -def ensure_unique_column(col_name: str, df: DataFrame, addition: str = "_") -> str: +def ensure_unique_column( + col_name: str, df: pandas.DataFrame, addition: str = "_" +) -> str: while col_name in df: col_name = addition + col_name + addition return col_name diff --git a/linearmodels/system/_utility.py b/linearmodels/system/_utility.py index d1b199dc69..ecdd2e1429 100644 --- a/linearmodels/system/_utility.py +++ b/linearmodels/system/_utility.py @@ -2,8 +2,10 @@ from typing import cast +import numpy import numpy as np from numpy.linalg import inv, matrix_rank +import pandas import pandas as pd from linearmodels.typing import ArraySequence, Float64Array @@ -226,8 +228,8 @@ class LinearConstraint: def __init__( self, - r: pd.DataFrame | np.ndarray, - q: pd.Series | np.ndarray | None = None, + r: pandas.DataFrame | numpy.ndarray, + q: pandas.Series | numpy.ndarray | None = None, num_params: int | None = None, require_pandas: bool = True, ) -> None: @@ -297,7 +299,7 @@ def _compute_transform(self) -> None: self._computed = True @property - def r(self) -> pd.DataFrame: + def r(self) -> pandas.DataFrame: """Constraint loading matrix""" return self._r_pd @@ -352,6 +354,6 @@ def a(self) -> Float64Array: return self._a @property - def q(self) -> pd.Series | np.ndarray: + def q(self) -> pandas.Series | numpy.ndarray: """Constrain target values""" return self._q_pd diff --git a/linearmodels/system/covariance.py b/linearmodels/system/covariance.py index 1dcc054809..d4a0eabe87 100644 --- a/linearmodels/system/covariance.py +++ b/linearmodels/system/covariance.py @@ -2,6 +2,7 @@ from typing import cast +import numpy from numpy import asarray, empty, eye, ndarray, ones, sqrt, vstack, zeros from numpy.linalg import inv @@ -62,7 +63,7 @@ class HomoskedasticCovariance: def __init__( self, - x: list[ndarray], + x: list[numpy.ndarray], eps: Float64Array, sigma: Float64Array, full_sigma: Float64Array, @@ -204,7 +205,7 @@ class HeteroskedasticCovariance(HomoskedasticCovariance): def __init__( self, - x: list[ndarray], + x: list[numpy.ndarray], eps: Float64Array, sigma: Float64Array, full_sigma: Float64Array, @@ -352,7 +353,7 @@ class KernelCovariance(HeteroskedasticCovariance, _HACMixin): def __init__( self, - x: list[ndarray], + x: list[numpy.ndarray], eps: Float64Array, sigma: Float64Array, full_sigma: Float64Array, @@ -446,7 +447,7 @@ class ClusteredCovariance(HeteroskedasticCovariance): def __init__( self, - x: list[ndarray], + x: list[numpy.ndarray], eps: Float64Array, sigma: Float64Array, full_sigma: Float64Array, @@ -570,12 +571,12 @@ class GMMHomoskedasticCovariance: def __init__( self, - x: list[ndarray], - z: list[ndarray], + x: list[numpy.ndarray], + z: list[numpy.ndarray], eps: Float64Array, w: Float64Array, *, - sigma: ndarray | None = None, + sigma: numpy.ndarray | None = None, debiased: bool = False, constraints: LinearConstraint | None = None, ) -> None: @@ -692,12 +693,12 @@ class GMMHeteroskedasticCovariance(GMMHomoskedasticCovariance): def __init__( self, - x: list[ndarray], - z: list[ndarray], + x: list[numpy.ndarray], + z: list[numpy.ndarray], eps: Float64Array, w: Float64Array, *, - sigma: ndarray | None = None, + sigma: numpy.ndarray | None = None, debiased: bool = False, constraints: LinearConstraint | None = None, ) -> None: @@ -768,12 +769,12 @@ class GMMKernelCovariance(GMMHeteroskedasticCovariance, _HACMixin): def __init__( self, - x: list[ndarray], - z: list[ndarray], + x: list[numpy.ndarray], + z: list[numpy.ndarray], eps: Float64Array, w: Float64Array, *, - sigma: ndarray | None = None, + sigma: numpy.ndarray | None = None, debiased: bool = False, constraints: LinearConstraint | None = None, kernel: str = "bartlett", diff --git a/linearmodels/system/gmm.py b/linearmodels/system/gmm.py index e1561eb1f8..b06decec47 100644 --- a/linearmodels/system/gmm.py +++ b/linearmodels/system/gmm.py @@ -7,6 +7,7 @@ from collections.abc import Sequence from typing import cast +import numpy from numpy import array, empty, ndarray, repeat, sqrt, zeros_like from linearmodels.asset_pricing.covariance import _HACMixin @@ -100,7 +101,7 @@ def weight_matrix( z: Sequence[Float64Array], eps: Float64Array, *, - sigma: ndarray, + sigma: numpy.ndarray, ) -> Float64Array: """ Construct a GMM weight matrix for a model. @@ -176,7 +177,7 @@ def weight_matrix( z: Sequence[Float64Array], eps: Float64Array, *, - sigma: ndarray | None = None, + sigma: numpy.ndarray | None = None, ) -> Float64Array: """ Construct a GMM weight matrix for a model. @@ -294,7 +295,7 @@ def weight_matrix( z: Sequence[Float64Array], eps: Float64Array, *, - sigma: ndarray | None = None, + sigma: numpy.ndarray | None = None, ) -> Float64Array: """ Construct a GMM weight matrix for a model. diff --git a/linearmodels/system/model.py b/linearmodels/system/model.py index 27d775a9de..982a65b16d 100644 --- a/linearmodels/system/model.py +++ b/linearmodels/system/model.py @@ -25,6 +25,7 @@ from formulaic.utils.context import capture_context import numpy as np from numpy.linalg import inv, lstsq, matrix_rank, solve +import pandas from pandas import DataFrame, Index, Series, concat from linearmodels.iv._utility import IVFormulaParser @@ -154,7 +155,7 @@ class SystemFormulaParser: def __init__( self, formula: Mapping[str, str] | str, - data: DataFrame, + data: pandas.DataFrame, weights: Mapping[str, ArrayLike] | None = None, eval_env: int = 2, context: Mapping[str, Any] | None = None, @@ -582,7 +583,7 @@ def predict( params: ArrayLike, *, equations: Mapping[str, Mapping[str, ArrayLike]] | None = None, - data: DataFrame | None = None, + data: pandas.DataFrame | None = None, eval_env: int = 1, ) -> DataFrame: """ @@ -1171,7 +1172,9 @@ def constraints(self) -> LinearConstraint | None: """ return self._constraints - def add_constraints(self, r: DataFrame, q: Series | None = None) -> None: + def add_constraints( + self, r: pandas.DataFrame, q: pandas.Series | None = None + ) -> None: r""" Add parameter constraints to a model. @@ -1494,7 +1497,7 @@ def multivariate_iv( def from_formula( cls, formula: str | dict[str, str], - data: DataFrame, + data: pandas.DataFrame, *, sigma: ArrayLike | None = None, weights: Mapping[str, ArrayLike] | None = None, @@ -1714,7 +1717,7 @@ def multivariate_ls(cls, dependent: ArrayLike, exog: ArrayLike) -> SUR: def from_formula( cls, formula: str | dict[str, str], - data: DataFrame, + data: pandas.DataFrame, *, sigma: ArrayLike | None = None, weights: Mapping[str, ArrayLike] | None = None, @@ -2104,7 +2107,7 @@ def _finalize_results( def from_formula( cls, formula: str | dict[str, str], - data: DataFrame, + data: pandas.DataFrame, *, weights: dict[str, ArrayLike] | None = None, weight_type: str = "robust", diff --git a/linearmodels/system/results.py b/linearmodels/system/results.py index 472cdcaab6..1654f91c6a 100644 --- a/linearmodels/system/results.py +++ b/linearmodels/system/results.py @@ -7,6 +7,7 @@ from typing import Any, Union import numpy as np +import pandas from pandas import DataFrame, Series, concat from scipy import stats from statsmodels.iolib.summary import SimpleTable, fmt_2cols @@ -244,7 +245,7 @@ def fitted_values(self) -> DataFrame: def _out_of_sample( self, equations: dict[str, dict[str, ArrayLike]] | None, - data: DataFrame | None, + data: pandas.DataFrame | None, missing: bool, dataframe: bool, ) -> dict[str, Series] | DataFrame: @@ -272,12 +273,12 @@ def predict( self, equations: dict[str, dict[str, ArrayLike]] | None = None, *, - data: DataFrame | None = None, + data: pandas.DataFrame | None = None, fitted: bool = True, idiosyncratic: bool = False, missing: bool = False, dataframe: bool = False, - ) -> DataFrame | dict: + ) -> pandas.DataFrame | dict: """ In- and out-of-sample predictions diff --git a/linearmodels/tests/panel/_utility.py b/linearmodels/tests/panel/_utility.py index 9825f4c967..78dd6d005f 100644 --- a/linearmodels/tests/panel/_utility.py +++ b/linearmodels/tests/panel/_utility.py @@ -8,6 +8,7 @@ from numpy.linalg import lstsq from numpy.random import RandomState, standard_normal from numpy.testing import assert_allclose +import pandas from pandas import Categorical, DataFrame, Series, date_range, get_dummies from pandas.testing import assert_frame_equal, assert_series_equal @@ -27,7 +28,12 @@ def lsdv( - y: DataFrame, x: DataFrame, has_const=False, entity=False, time=False, general=None + y: pandas.DataFrame, + x: pandas.DataFrame, + has_const=False, + entity=False, + time=False, + general=None, ): nvar = x.shape[1] temp = x.reset_index() diff --git a/linearmodels/typing/__init__.py b/linearmodels/typing/__init__.py index b7d36e229a..a63caf11ab 100644 --- a/linearmodels/typing/__init__.py +++ b/linearmodels/typing/__init__.py @@ -6,10 +6,7 @@ import numpy as np from pandas import DataFrame, Series -from .data import ArrayLike - -NP_GTE_121 = np.lib.NumpyVersion(np.__version__) >= np.lib.NumpyVersion("1.21.0") - +from .data import ArrayLike as ArrayLike __all__ = [ "ArrayLike", @@ -35,7 +32,7 @@ AnyPandas = Union[Series, DataFrame] Label = Optional[Hashable] -if NP_GTE_121 and TYPE_CHECKING: +if TYPE_CHECKING: Float64Array = np.ndarray[Any, np.dtype[np.float64]] # pragma: no cover Int64Array = np.ndarray[Any, np.dtype[np.int64]] # pragma: no cover Int32Array = np.ndarray[Any, np.dtype[np.int32]] # pragma: no cover diff --git a/linearmodels/typing/data.py b/linearmodels/typing/data.py index 37996db3ed..0edec8caf1 100644 --- a/linearmodels/typing/data.py +++ b/linearmodels/typing/data.py @@ -16,11 +16,9 @@ ArrayLike = Union[np.ndarray, pd.DataFrame, pd.Series] # type: ignore -NP_GTE_121 = np.lib.NumpyVersion(np.__version__) >= np.lib.NumpyVersion("1.21.0") - NDArray = Union[np.ndarray] -if NP_GTE_121 and TYPE_CHECKING: +if TYPE_CHECKING: Float64Array = np.ndarray[Any, np.dtype[np.float64]] # pragma: no cover Int64Array = np.ndarray[Any, np.dtype[np.int64]] # pragma: no cover Int32Array = np.ndarray[Any, np.dtype[np.int32]] # pragma: no cover