本文整理汇总了Python中pandas.core.dtypes.dtypes.PeriodDtype方法的典型用法代码示例。如果您正苦于以下问题:Python dtypes.PeriodDtype方法的具体用法?Python dtypes.PeriodDtype怎么用?Python dtypes.PeriodDtype使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.core.dtypes.dtypes
的用法示例。
在下文中一共展示了dtypes.PeriodDtype方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _from_sequence
# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import PeriodDtype [as 别名]
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)
示例2: dtype
# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import PeriodDtype [as 别名]
def dtype():
return PeriodDtype(freq='D')
示例3: test_registered
# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import PeriodDtype [as 别名]
def test_registered():
assert PeriodDtype in registry.dtypes
result = registry.find("Period[D]")
expected = PeriodDtype("D")
assert result == expected
# ----------------------------------------------------------------------------
# period_array
示例4: test_astype_period
# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import PeriodDtype [as 别名]
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)
示例5: test_constructor_cast_object
# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import PeriodDtype [as 别名]
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)
示例6: validate_dtype_freq
# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import PeriodDtype [as 别名]
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
示例7: _period_array_cmp
# 需要导入模块: from pandas.core.dtypes import dtypes [as 别名]
# 或者: from pandas.core.dtypes.dtypes import PeriodDtype [as 别名]
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)