
    Djv?                         d dl mZ d dlZd dlZddlmZ ddl	m
Z
 g dZd Zd	 Zed
dd
fdZed
d
fdZd Zd ZddZd Zd ZddZd Zy)    )
validationN   )DTYPE   )C_intgrt_vec)	as_seriesccheck_endog
check_exogdiffdiff_invis_iterablec                     t        | t        j                        r| S t        j                  t        j                  |       fi |S )a  Cast as pandas Series.

    Cast an iterable to a Pandas Series object. Note that the index
    will simply be a positional ``arange`` and cannot be set in this
    function.

    Parameters
    ----------
    x : array-like, shape=(n_samples,)
        The 1d array on which to compute the auto correlation.

    Examples
    --------
    >>> as_series([1, 2, 3])
    0    1
    1    2
    2    3
    dtype: int64

    >>> as_series(as_series((1, 2, 3)))
    0    1
    1    2
    2    3
    dtype: int64

    >>> import pandas as pd
    >>> as_series(pd.Series([4, 5, 6], index=['a', 'b', 'c']))
    a    4
    b    5
    c    6
    dtype: int64

    Returns
    -------
    s : pd.Series
        A pandas Series object.
    )
isinstancepdSeriesskvalcolumn_or_1d)xkwargss     XC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\pmdarima/utils/array.pyr   r      s7    L !RYY99U''*5f55    c                     | syt        |       dk(  r;| d   }t        |      rt        j                  |      S t        j                  |g      S t        j                  | D cg c]  }t        |      r|n|g c}      S c c}w )a  Imitates the ``c`` function from R.

    Since this whole library is aimed at re-creating in
    Python what R has already done so well, the ``c`` function was created to
    wrap ``numpy.concatenate`` and mimic the R functionality. Similar to R,
    this works with scalars, iterables, and any mix therein.

    Note that using the ``c`` function on multi-nested lists or iterables
    will fail!

    Examples
    --------
    Using ``c`` with varargs will yield a single array:

    >>> c(1, 2, 3, 4)
    array([1, 2, 3, 4])

    Using ``c`` with nested lists and scalars will also yield a single array:

    >>> c([1, 2], 4, c(5, 4))
    array([1, 2, 4, 5, 4])

    However, using ``c`` with multi-level lists will fail!

    >>> c([1, 2, 3], [[1, 2]])  # doctest: +SKIP
    ValueError: all the input arrays must have same number of dimensions

    References
    ----------
    .. [1] https://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html
    Nr   r   )lenr   npasarrayconcatenate)argselementas      r   r	   r	   E   sy    B  4yA~q' w::g&& zz7)$$0 >>FAA1QC7FGGFs    A>TFc                 6   t        j                  | d|||      }t        j                  |      }|s|S t        | t        j
                        r| j                         } t        | t        j                        r!t	        j                  || j                        }|S )a+  Wrapper for ``check_array`` and ``column_or_1d`` from sklearn

    Parameters
    ----------
    y : array-like, shape=(n_samples,)
        The 1d endogenous array.

    dtype : string, type or None (default=np.float64)
        Data type of result. If None, the dtype of the input is preserved.
        If "numeric", dtype is preserved unless array.dtype is object.

    copy : bool, optional (default=False)
        Whether a forced copy will be triggered. If copy=False, a copy might
        still be triggered by a conversion.

    force_all_finite : bool, optional (default=False)
        Whether to raise an error on np.inf and np.nan in an array. The
        possibilities are:

        - True: Force all values of array to be finite.
        - False: accept both np.inf and np.nan in array.

    preserve_series : bool, optional
        Whether to preserve a ``pd.Series`` object. Will also attempt to
        squeeze a dataframe into a ``pd.Series``.

    Returns
    -------
    y : np.ndarray or pd.Series, shape=(n_samples,)
        A 1d numpy ndarray
    F)	ensure_2dforce_all_finitecopydtype)index)	r   check_arrayr   r   r   	DataFramesqueezer   r&   )yr%   r$   r#   preserve_seriesendogs         r   r
   r
      s    L 	)E u%E !R\\"IIK!RYY		%qww/Lr   c                 ~   t        | d      r| j                  dk7  rt        d      t        | t        j
                        r`|r|| j                  |      } |rG| j                  t        j                         j                         j                         rt        d      | S t        j                  | dt        ||      S )a  A wrapper for ``check_array`` for 2D arrays

    Parameters
    ----------
    X : array-like, shape=(n_samples, n_features)
        The exogenous array. If a Pandas frame, a Pandas frame will be returned
        as well. Otherwise, a numpy array will be returned.

    dtype : string, type or None (default=np.float64)
        Data type of result. If None, the dtype of the input is preserved.
        If "numeric", dtype is preserved unless array.dtype is object.

    copy : bool, optional (default=True)
        Whether a forced copy will be triggered. If copy=False, a copy might
        still be triggered by a conversion.

    force_all_finite : bool, optional (default=True)
        Whether to raise an error on np.inf and np.nan in an array. The
        possibilities are:

        - True: Force all values of array to be finite.
        - False: accept both np.inf and np.nan in array.

    Returns
    -------
    X : pd.DataFrame or np.ndarray, shape=(n_samples, n_features)
        Either a 2-d numpy array or pd.DataFrame
    ndimr   z Must be a 2-d array or dataframez$Found non-finite values in dataframeT)r"   r%   r$   r#   )hasattrr.   
ValueErrorr   r   r(   astypeapplyr   isfiniteanyr   r'   r   )Xr%   r$   r#   s       r   r   r      s    : q&affk;<<!R\\"E%A!''"++"6!6 ; ; = A A CCDD 	) r   c                 T    | j                   d   }t        ||      }| || | d ||z
   z
  S )Nr   shapemin)r   lagns      r   _diff_vectorr<      s5    	
A
a+CS!9q1S5z!!r   c                 l    | j                   \  }}t        ||      }| ||d d f   | d ||z
  d d f   z
  S )Nr7   )r   r:   m_s       r   _diff_matrixr@     sA    77DAq
a+CS!VQY<!GaeGQJ-''r   c                    t        d ||fD              rt        d      t        j                  | dt        d      } | j
                  dk(  rt        nt        }| }t        |      D ]  } |||      }|j                  d   r|c S  |S )a  Difference an array.

    A python implementation of the R ``diff`` function [1]. This computes lag
    differences from an array given a ``lag`` and ``differencing`` term.

    If ``x`` is a vector of length :math:`n`, ``lag=1`` and ``differences=1``,
    then the computed result is equal to the successive differences
    ``x[lag:n] - x[:n-lag]``.

    Examples
    --------
    Where ``lag=1`` and ``differences=1``:

    >>> x = c(10, 4, 2, 9, 34)
    >>> diff(x, 1, 1)
    array([ -6.,  -2.,   7.,  25.], dtype=float32)

    Where ``lag=1`` and ``differences=2``:

    >>> x = c(10, 4, 2, 9, 34)
    >>> diff(x, 1, 2)
    array([  4.,   9.,  18.], dtype=float32)

    Where ``lag=3`` and ``differences=1``:

    >>> x = c(10, 4, 2, 9, 34)
    >>> diff(x, 3, 1)
    array([ -1.,  30.], dtype=float32)

    Where ``lag=6`` (larger than the array is) and ``differences=1``:

    >>> x = c(10, 4, 2, 9, 34)
    >>> diff(x, 6, 1)
    array([], dtype=float32)

    For a 2d array with ``lag=1`` and ``differences=1``:

    >>> import numpy as np
    >>>
    >>> x = np.arange(1, 10).reshape((3, 3)).T
    >>> diff(x, 1, 1)
    array([[ 1.,  1.,  1.],
           [ 1.,  1.,  1.]], dtype=float32)

    Parameters
    ----------
    x : array-like, shape=(n_samples, [n_features])
        The array to difference.

    lag : int, optional (default=1)
        An integer > 0 indicating which lag to use.

    differences : int, optional (default=1)
        An integer > 0 indicating the order of the difference.

    Returns
    -------
    res : np.ndarray, shape=(n_samples, [n_features])
        The result of the differenced arrays.

    References
    ----------
    .. [1] https://stat.ethz.ch/R-manual/R-devel/library/base/html/diff.html
    c              3   &   K   | ]	  }|d k    ywr   N .0vs     r   	<genexpr>zdiff.<locals>.<genexpr>J       
-Q1q5
-   3lag and differences must be positive (> 0) integersF)r"   r%   r$   r   r   )
r4   r0   r   r'   r   r.   r<   r@   ranger8   )r   r:   differencesfunresis         r   r   r   	  s    B 
-3,
--NOO!uEFA&&A+,<C
C ; #smyy|J	 Jr   c                 T   |t        j                  ||z  t              }n4t        |t        ddd      }|j                  d   ||z  k7  rt        d      |dk(  r!t        j                  t        | ||            S t        t        | ||dz
  t        ||d      	      |d|d | 	      S )
Nr%   F)r%   r$   r#   r+   r   z#"xi" does not have the right lengthr   )r   xir:   )r   r:   rM   r   r:   rM   rS   )
r   zerosr   r
   r8   
IndexErrorr   r   r   r   rT   s       r   _diff_inv_vectorrW   [  s     
zXXcK'u5"!
 88A;#++BCCazz,rs;<<
 q!<>$3x
 	
r   c           	         | j                   \  }}t        j                  |||z  z   |ft              }|dk\  r|!t        j                  ||z  |ft              }n=t	        j
                  |t        ddd      }|j                   ||z  |fk7  rt        d      t        |      D ]%  }t        | d d |f   |||d d |f         |d d |f<   ' |S )NrR   r   FTr%   r$   r#   r"   z""xi" does not have the right shape)	r8   r   rU   r   r   r'   rV   rL   rW   )r   r:   rM   rS   r;   r>   r*   rP   s           r   _diff_inv_matrixrZ   {  s    77DAq
!cK''+59AAv:3,a0>B""!&B xxC+-q11 !EFF q 	LA&qAw["QT(KAadG	L Hr   c                    t        j                  | t        ddd      } t        d ||fD              rt	        d      | j
                  dk(  rt        | |||      S | j
                  dk(  rt        | |||      S t	        d      )a  
    Inverse the difference of an array.

    A python implementation of the R ``diffinv`` function [1]. This computes
    the inverse of lag differences from an array given a ``lag``
    and ``differencing`` term.

    If ``x`` is a vector of length :math:`n`, ``lag=1`` and ``differences=1``,
    then the computed result is equal to the cumulative sum plus left-padding
    of zeros equal to ``lag * differences``.

    Examples
    --------
    Where ``lag=1`` and ``differences=1``:

    >>> x = c(10, 4, 2, 9, 34)
    >>> diff_inv(x, 1, 1)
    array([ 0., 10., 14., 16., 25., 59.])

    Where ``lag=1`` and ``differences=2``:

    >>> x = c(10, 4, 2, 9, 34)
    >>> diff_inv(x, 1, 2)
    array([  0.,   0.,  10.,  24.,  40.,  65., 124.])

    Where ``lag=3`` and ``differences=1``:

    >>> x = c(10, 4, 2, 9, 34)
    >>> diff_inv(x, 3, 1)
    array([ 0.,  0.,  0., 10.,  4.,  2., 19., 38.])

    Where ``lag=6`` (larger than the array is) and ``differences=1``:

    >>> x = c(10, 4, 2, 9, 34)
    >>> diff_inv(x, 6, 1)
    array([ 0.,  0.,  0.,  0.,  0.,  0., 10.,  4.,  2.,  9., 34.])

    For a 2d array with ``lag=1`` and ``differences=1``:

    >>> import numpy as np
    >>>
    >>> x = np.arange(1, 10).reshape((3, 3)).T
    >>> diff_inv(x, 1, 1)
    array([[ 0.,  0.,  0.],
           [ 1.,  4.,  7.],
           [ 3.,  9., 15.],
           [ 6., 15., 24.]])

    Parameters
    ----------
    x : array-like, shape=(n_samples, [n_features])
        The array to difference.

    lag : int, optional (default=1)
        An integer > 0 indicating which lag to use.

    differences : int, optional (default=1)
        An integer > 0 indicating the order of the difference.

    Returns
    -------
    res : np.ndarray, shape=(n_samples, [n_features])
        The result of the inverse of the difference arrays.

    References
    ----------
    .. [1] https://stat.ethz.ch/R-manual/R-devel/library/stats/html/diffinv.html
    FrY   c              3   &   K   | ]	  }|d k    ywrC   rD   rE   s     r   rH   zdiff_inv.<locals>.<genexpr>  rI   rJ   rK   r   r   z9only vector and matrix inverse differencing are supported)r   r'   r   r4   r0   r.   rW   rZ   rT   s       r   r   r     s    J 			A 
-3,
--NOOvv{3R88	
13R88
 % & &r   c                 <    t        | t              ryt        | d      S )a  Test a variable for iterability.

    Determine whether an object ``x`` is iterable. In Python 2, this
    was as simple as checking for the ``__iter__`` attribute. However, in
    Python 3, strings became iterable. Therefore, this function checks for the
    ``__iter__`` attribute, returning True if present (except for strings,
    for which it will return False).

    Parameters
    ----------
    x : str, iterable or object
        The object in question.

    Examples
    --------
    Strings and other objects are not iterable:

    >>> x = "not me"
    >>> y = 123
    >>> any(is_iterable(v) for v in (x, y))
    False

    Tuples, lists and other structures with ``__iter__`` are:

    >>> x = ('a', 'tuple')
    >>> y = ['a', 'list']
    >>> all(is_iterable(v) for v in (x, y))
    True

    This even applies to numpy arrays:

    >>> import numpy as np
    >>> is_iterable(np.arange(10))
    True

    Returns
    -------
    isiter : bool
        True if iterable, else False.
    F__iter__)r   strr/   )r   s    r   r   r     s    R !S1j!!r   )r   r   )r   r   N)sklearn.utilsr   r   numpyr   pandasr   compatr   _arrayr   __all__r   r	   r
   r   r<   r@   r   rW   rZ   r   r   rD   r   r   <module>rf      sv    .     (6VEHT 	9x D4 /d"(Od
@4W&t+"r   