Python pandas.core.dtypes.dtypes.PeriodDtype() Examples

The following are 18 code examples of pandas.core.dtypes.dtypes.PeriodDtype(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pandas.core.dtypes.dtypes , or try the search function .
Example #1
Source File: period.py    From recruit with Apache License 2.0 6 votes vote down vote up
def _from_sequence(cls, scalars, dtype=None, copy=False):
        # type: (Sequence[Optional[Period]], PeriodDtype, bool) -> PeriodArray
        if dtype:
            freq = dtype.freq
        else:
            freq = None

        if isinstance(scalars, cls):
            validate_dtype_freq(scalars.dtype, freq)
            if copy:
                scalars = scalars.copy()
            return scalars

        periods = np.asarray(scalars, dtype=object)
        if copy:
            periods = periods.copy()

        freq = freq or libperiod.extract_freq(periods)
        ordinals = libperiod.extract_ordinals(periods, freq)
        return cls(ordinals, freq=freq) 
Example #2
Source File: period.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def _from_sequence(cls, scalars, dtype=None, copy=False):
        # type: (Sequence[Optional[Period]], PeriodDtype, bool) -> PeriodArray
        if dtype:
            freq = dtype.freq
        else:
            freq = None

        if isinstance(scalars, cls):
            validate_dtype_freq(scalars.dtype, freq)
            if copy:
                scalars = scalars.copy()
            return scalars

        periods = np.asarray(scalars, dtype=object)
        if copy:
            periods = periods.copy()

        freq = freq or libperiod.extract_freq(periods)
        ordinals = libperiod.extract_ordinals(periods, freq)
        return cls(ordinals, freq=freq) 
Example #3
Source File: test_period.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_astype_period():
    arr = period_array(['2000', '2001', None], freq='D')
    result = arr.astype(PeriodDtype("M"))
    expected = period_array(['2000', '2001', None], freq='M')
    tm.assert_period_array_equal(result, expected) 
Example #4
Source File: test_construction.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_constructor_cast_object(self):
        s = Series(period_range('1/1/2000', periods=10),
                   dtype=PeriodDtype("D"))
        exp = Series(period_range('1/1/2000', periods=10))
        tm.assert_series_equal(s, exp) 
Example #5
Source File: test_period.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_astype_period():
    arr = period_array(['2000', '2001', None], freq='D')
    result = arr.astype(PeriodDtype("M"))
    expected = period_array(['2000', '2001', None], freq='M')
    tm.assert_period_array_equal(result, expected) 
Example #6
Source File: test_period.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_registered():
    assert PeriodDtype in registry.dtypes
    result = registry.find("Period[D]")
    expected = PeriodDtype("D")
    assert result == expected

# ----------------------------------------------------------------------------
# period_array 
Example #7
Source File: test_period.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def dtype():
    return PeriodDtype(freq='D') 
Example #8
Source File: period.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def validate_dtype_freq(dtype, freq):
    """
    If both a dtype and a freq are available, ensure they match.  If only
    dtype is available, extract the implied freq.

    Parameters
    ----------
    dtype : dtype
    freq : DateOffset or None

    Returns
    -------
    freq : DateOffset

    Raises
    ------
    ValueError : non-period dtype
    IncompatibleFrequency : mismatch between dtype and freq
    """
    if freq is not None:
        freq = frequencies.to_offset(freq)

    if dtype is not None:
        dtype = pandas_dtype(dtype)
        if not is_period_dtype(dtype):
            raise ValueError('dtype must be PeriodDtype')
        if freq is None:
            freq = dtype.freq
        elif freq != dtype.freq:
            raise IncompatibleFrequency('specified freq and dtype '
                                        'are different')
    return freq 
Example #9
Source File: test_construction.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_constructor_cast_object(self):
        s = Series(period_range('1/1/2000', periods=10),
                   dtype=PeriodDtype("D"))
        exp = Series(period_range('1/1/2000', periods=10))
        tm.assert_series_equal(s, exp) 
Example #10
Source File: test_period.py    From recruit with Apache License 2.0 5 votes vote down vote up
def dtype():
    return PeriodDtype(freq='D') 
Example #11
Source File: test_period.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_registered():
    assert PeriodDtype in registry.dtypes
    result = registry.find("Period[D]")
    expected = PeriodDtype("D")
    assert result == expected

# ----------------------------------------------------------------------------
# period_array 
Example #12
Source File: test_period.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def dtype():
    return PeriodDtype(freq='D') 
Example #13
Source File: period.py    From recruit with Apache License 2.0 5 votes vote down vote up
def validate_dtype_freq(dtype, freq):
    """
    If both a dtype and a freq are available, ensure they match.  If only
    dtype is available, extract the implied freq.

    Parameters
    ----------
    dtype : dtype
    freq : DateOffset or None

    Returns
    -------
    freq : DateOffset

    Raises
    ------
    ValueError : non-period dtype
    IncompatibleFrequency : mismatch between dtype and freq
    """
    if freq is not None:
        freq = frequencies.to_offset(freq)

    if dtype is not None:
        dtype = pandas_dtype(dtype)
        if not is_period_dtype(dtype):
            raise ValueError('dtype must be PeriodDtype')
        if freq is None:
            freq = dtype.freq
        elif freq != dtype.freq:
            raise IncompatibleFrequency('specified freq and dtype '
                                        'are different')
    return freq 
Example #14
Source File: test_construction.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_constructor_cast_object(self):
        s = Series(period_range('1/1/2000', periods=10),
                   dtype=PeriodDtype("D"))
        exp = Series(period_range('1/1/2000', periods=10))
        tm.assert_series_equal(s, exp) 
Example #15
Source File: test_period.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_astype_period():
    arr = period_array(['2000', '2001', None], freq='D')
    result = arr.astype(PeriodDtype("M"))
    expected = period_array(['2000', '2001', None], freq='M')
    tm.assert_period_array_equal(result, expected) 
Example #16
Source File: test_period.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_registered():
    assert PeriodDtype in registry.dtypes
    result = registry.find("Period[D]")
    expected = PeriodDtype("D")
    assert result == expected

# ----------------------------------------------------------------------------
# period_array 
Example #17
Source File: period.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 4 votes vote down vote up
def _period_array_cmp(cls, op):
    """
    Wrap comparison operations to convert Period-like to PeriodDtype
    """
    opname = '__{name}__'.format(name=op.__name__)
    nat_result = True if opname == '__ne__' else False

    def wrapper(self, other):
        op = getattr(self.asi8, opname)

        if isinstance(other, (ABCDataFrame, ABCSeries, ABCIndexClass)):
            return NotImplemented

        if is_list_like(other) and len(other) != len(self):
            raise ValueError("Lengths must match")

        if isinstance(other, Period):
            self._check_compatible_with(other)

            result = op(other.ordinal)
        elif isinstance(other, cls):
            self._check_compatible_with(other)

            result = op(other.asi8)

            mask = self._isnan | other._isnan
            if mask.any():
                result[mask] = nat_result

            return result
        elif other is NaT:
            result = np.empty(len(self.asi8), dtype=bool)
            result.fill(nat_result)
        else:
            other = Period(other, freq=self.freq)
            result = op(other.ordinal)

        if self._hasnans:
            result[self._isnan] = nat_result

        return result

    return compat.set_function_name(wrapper, opname, cls) 
Example #18
Source File: period.py    From recruit with Apache License 2.0 4 votes vote down vote up
def _period_array_cmp(cls, op):
    """
    Wrap comparison operations to convert Period-like to PeriodDtype
    """
    opname = '__{name}__'.format(name=op.__name__)
    nat_result = True if opname == '__ne__' else False

    def wrapper(self, other):
        op = getattr(self.asi8, opname)

        if isinstance(other, (ABCDataFrame, ABCSeries, ABCIndexClass)):
            return NotImplemented

        if is_list_like(other) and len(other) != len(self):
            raise ValueError("Lengths must match")

        if isinstance(other, Period):
            self._check_compatible_with(other)

            result = op(other.ordinal)
        elif isinstance(other, cls):
            self._check_compatible_with(other)

            result = op(other.asi8)

            mask = self._isnan | other._isnan
            if mask.any():
                result[mask] = nat_result

            return result
        elif other is NaT:
            result = np.empty(len(self.asi8), dtype=bool)
            result.fill(nat_result)
        else:
            other = Period(other, freq=self.freq)
            result = op(other.ordinal)

        if self._hasnans:
            result[self._isnan] = nat_result

        return result

    return compat.set_function_name(wrapper, opname, cls)