<commit_before>"""
checks module
    This module is used to check the time series.

"""
import pandas as pd


def check_oseries(oseries, freq, fillnan='drop'):
    """Check the observed time series before running a simulation.

    Parameters
    ----------
    oseries: pd.Series
        Pandas series object containing the observed time series.
    freq: optional[str]
        String containing the desired frequency. The required string format is found
        at http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset
        -aliases
    fillnan: optional[str or float]
        Methods or float number to fill nan-values. Default values is
        'drop'. Currently supported options are: 'interpolate', float,
        'mean' and, 'drop'. Interpolation is performed with a standard linear
        interpolation.

    Returns
    -------
    oseries: pd.Series
        Pandas series object checked for nan-values and with the required frequency.

    """
    assert isinstance(oseries, pd.Series), 'Expected a Pandas Series object, ' \
                                           'got %s' % type(oseries)

    # make a deep copy to preserve original imported data
    oseries = oseries.loc[oseries.first_valid_index():oseries.last_valid_index(
    )].copy(deep=True)

    # Deal with frequency of the time series
    if freq:
        print Warning
        oseries = oseries.resample(freq)

    # Handle nan-values in oseries
    if oseries.hasnans:
        print '%i nan-value(s) in the oseries was/were found and handled/filled ' \
              'with: %s' % (oseries.isnull().values.sum(), fillnan)
        if fillnan == 'drop':
            oseries.dropna(inplace=True)  # Default option
        elif fillnan == 'mean':
            oseries.fillna(oseries.mean(), inplace=True)
        elif fillnan == 'interpolate':
            oseries.interpolate(method='time', inplace=True)
        elif type(fillnan) == float:
            oseries.fillna(fillnan, inplace=True)
        else:
            print 'User-defined option for fillnan %s isinstance() not supported' \
                  % fillnan

    # Drop dubplicate indexes
    if not oseries.index.is_unique:
        #oseries.drop_duplicates(inplace=True) # does not look at index...
        grouped = oseries.groupby(level=0)
        oseries = grouped.first()

    return oseries


def check_tseries(stress, freq, fillnan, name=''):
    """ Check the stress series when creating a time series model.

    Parameters
    ----------
    stress: pd.Series
        Pandas series object containing the stress time series.
    freq: str
        String containing the desired frequency. The required string format is found
        at http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset
        -aliases
    fillnan: optional: str or float
        Methods or float number to fill nan-values. Default values is
        'mean'. Currently supported options are: 'interpolate', float,
        and 'mean'. Interpolation is performed with a standard linear
        interpolation.

    Returns
    -------
    the corrected stress as pd.Series:
        - nan-values dropped at begin and end.<commit_msg>changed first to mean for duplicate values<commit_after>"""
checks module
    This module is used to check the time series.

"""
import pandas as pd


def check_oseries(oseries, freq, fillnan='drop'):
    """Check the observed time series before running a simulation.

    Parameters
    ----------
    oseries: pd.Series
        Pandas series object containing the observed time series.
    freq: optional[str]
        String containing the desired frequency. The required string format is found
        at http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset
        -aliases
    fillnan: optional[str or float]
        Methods or float number to fill nan-values. Default values is
        'drop'. Currently supported options are: 'interpolate', float,
        'mean' and, 'drop'. Interpolation is performed with a standard linear
        interpolation.

    Returns
    -------
    oseries: pd.Series
        Pandas series object checked for nan-values and with the required frequency.

    """
    assert isinstance(oseries, pd.Series), 'Expected a Pandas Series object, ' \
                                           'got %s' % type(oseries)

    # make a deep copy to preserve original imported data
    oseries = oseries.loc[oseries.first_valid_index():oseries.last_valid_index(
    )].copy(deep=True)

    # Deal with frequency of the time series
    if freq:
        print Warning
        oseries = oseries.resample(freq)

    # Handle nan-values in oseries
    if oseries.hasnans:
        print '%i nan-value(s) in the oseries was/were found and handled/filled ' \
              'with: %s' % (oseries.isnull().values.sum(), fillnan)
        if fillnan == 'drop':
            oseries.dropna(inplace=True)  # Default option
        elif fillnan == 'mean':
            oseries.fillna(oseries.mean(), inplace=True)
        elif fillnan == 'interpolate':
            oseries.interpolate(method='time', inplace=True)
        elif type(fillnan) == float:
            oseries.fillna(fillnan, inplace=True)
        else:
            print 'User-defined option for fillnan %s isinstance() not supported' \
                  % fillnan

    # Drop dubplicate indexes
    if not oseries.index.is_unique:
        print 'duplicate time-indexes were found in the oseries. Values were averaged.'
        grouped = oseries.groupby(level=0)
        oseries = grouped.mean()

    return oseries


def check_tseries(stress, freq, fillnan, name=''):
    """ Check the stress series when creating a time series model.

    Parameters
    ----------
    stress: pd.Series
        Pandas series object containing the stress time series.
    freq: str
        String containing the desired frequency. The required string format is found
        at http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset
        -aliases
    fillnan: optional: str or float
        Methods or float number to fill nan-values. Default values is
        'mean'. Currently supported options are: 'interpolate', float,
        and 'mean'. Interpolation is performed with a standard linear
        interpolation.

    Returns
    -------
    the corrected stress as pd.Series:
        - nan-values dropped at begin and end.
{'fraction_non_alphanumeric': 0.0918639285154413, 'fraction_numerical': 0.00031352876626430476, 'mean_word_length': 2.372093023255814, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4284529', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1692, 'n_words': 598}