本文整理汇总了Python中pandas.PeriodIndex方法的典型用法代码示例。如果您正苦于以下问题:Python pandas.PeriodIndex方法的具体用法?Python pandas.PeriodIndex怎么用?Python pandas.PeriodIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas
的用法示例。
在下文中一共展示了pandas.PeriodIndex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_to_period_nofreq
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import PeriodIndex [as 别名]
def test_to_period_nofreq(self):
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-04'])
with pytest.raises(ValueError):
idx.to_period()
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03'],
freq='infer')
assert idx.freqstr == 'D'
expected = pd.PeriodIndex(['2000-01-01', '2000-01-02',
'2000-01-03'], freq='D')
tm.assert_index_equal(idx.to_period(), expected)
# GH 7606
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03'])
assert idx.freqstr is None
tm.assert_index_equal(idx.to_period(), expected)
示例2: test_union_misc
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import PeriodIndex [as 别名]
def test_union_misc(self, sort):
index = period_range('1/1/2000', '1/20/2000', freq='D')
result = index[:-5].union(index[10:], sort=sort)
tm.assert_index_equal(result, index)
# not in order
result = _permute(index[:-5]).union(_permute(index[10:]), sort=sort)
if sort is None:
tm.assert_index_equal(result, index)
assert tm.equalContents(result, index)
# raise if different frequencies
index = period_range('1/1/2000', '1/20/2000', freq='D')
index2 = period_range('1/1/2000', '1/20/2000', freq='W-WED')
with pytest.raises(period.IncompatibleFrequency):
index.union(index2, sort=sort)
msg = 'can only call with other PeriodIndex-ed objects'
with pytest.raises(ValueError, match=msg):
index.join(index.to_timestamp())
index3 = period_range('1/1/2000', '1/20/2000', freq='2D')
with pytest.raises(period.IncompatibleFrequency):
index.join(index3)
示例3: test_is_monotonic_decreasing
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import PeriodIndex [as 别名]
def test_is_monotonic_decreasing(self):
# GH 17717
p0 = pd.Period('2017-09-01')
p1 = pd.Period('2017-09-02')
p2 = pd.Period('2017-09-03')
idx_inc0 = pd.PeriodIndex([p0, p1, p2])
idx_inc1 = pd.PeriodIndex([p0, p1, p1])
idx_dec0 = pd.PeriodIndex([p2, p1, p0])
idx_dec1 = pd.PeriodIndex([p2, p1, p1])
idx = pd.PeriodIndex([p1, p2, p0])
assert idx_inc0.is_monotonic_decreasing is False
assert idx_inc1.is_monotonic_decreasing is False
assert idx_dec0.is_monotonic_decreasing is True
assert idx_dec1.is_monotonic_decreasing is True
assert idx.is_monotonic_decreasing is False
示例4: test_contains
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import PeriodIndex [as 别名]
def test_contains(self):
# GH 17717
p0 = pd.Period('2017-09-01')
p1 = pd.Period('2017-09-02')
p2 = pd.Period('2017-09-03')
p3 = pd.Period('2017-09-04')
ps0 = [p0, p1, p2]
idx0 = pd.PeriodIndex(ps0)
for p in ps0:
assert idx0.contains(p)
assert p in idx0
assert idx0.contains(str(p))
assert str(p) in idx0
assert idx0.contains('2017-09-01 00:00:01')
assert '2017-09-01 00:00:01' in idx0
assert idx0.contains('2017-09')
assert '2017-09' in idx0
assert not idx0.contains(p3)
assert p3 not in idx0
示例5: test_get_indexer_non_unique
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import PeriodIndex [as 别名]
def test_get_indexer_non_unique(self):
# GH 17717
p1 = pd.Period('2017-09-02')
p2 = pd.Period('2017-09-03')
p3 = pd.Period('2017-09-04')
p4 = pd.Period('2017-09-05')
idx1 = pd.PeriodIndex([p1, p2, p1])
idx2 = pd.PeriodIndex([p2, p1, p3, p4])
result = idx1.get_indexer_non_unique(idx2)
expected_indexer = np.array([1, 0, 2, -1, -1], dtype=np.intp)
expected_missing = np.array([2, 3], dtype=np.int64)
tm.assert_numpy_array_equal(result[0], expected_indexer)
tm.assert_numpy_array_equal(result[1], expected_missing)
# TODO: This method came from test_period; de-dup with version above
示例6: test_construction_base_constructor
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import PeriodIndex [as 别名]
def test_construction_base_constructor(self):
# GH 13664
arr = [pd.Period('2011-01', freq='M'), pd.NaT,
pd.Period('2011-03', freq='M')]
tm.assert_index_equal(pd.Index(arr), pd.PeriodIndex(arr))
tm.assert_index_equal(pd.Index(np.array(arr)),
pd.PeriodIndex(np.array(arr)))
arr = [np.nan, pd.NaT, pd.Period('2011-03', freq='M')]
tm.assert_index_equal(pd.Index(arr), pd.PeriodIndex(arr))
tm.assert_index_equal(pd.Index(np.array(arr)),
pd.PeriodIndex(np.array(arr)))
arr = [pd.Period('2011-01', freq='M'), pd.NaT,
pd.Period('2011-03', freq='D')]
tm.assert_index_equal(pd.Index(arr), pd.Index(arr, dtype=object))
tm.assert_index_equal(pd.Index(np.array(arr)),
pd.Index(np.array(arr), dtype=object))
示例7: test_constructor_incompat_freq
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import PeriodIndex [as 别名]
def test_constructor_incompat_freq(self):
msg = "Input has different freq=D from PeriodIndex\\(freq=M\\)"
with pytest.raises(period.IncompatibleFrequency, match=msg):
PeriodIndex([Period('2011-01', freq='M'), pd.NaT,
Period('2011-01', freq='D')])
with pytest.raises(period.IncompatibleFrequency, match=msg):
PeriodIndex(np.array([Period('2011-01', freq='M'), pd.NaT,
Period('2011-01', freq='D')]))
# first element is pd.NaT
with pytest.raises(period.IncompatibleFrequency, match=msg):
PeriodIndex([pd.NaT, Period('2011-01', freq='M'),
Period('2011-01', freq='D')])
with pytest.raises(period.IncompatibleFrequency, match=msg):
PeriodIndex(np.array([pd.NaT, Period('2011-01', freq='M'),
Period('2011-01', freq='D')]))
示例8: test_map_with_string_constructor
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import PeriodIndex [as 别名]
def test_map_with_string_constructor(self):
raw = [2005, 2007, 2009]
index = PeriodIndex(raw, freq='A')
types = str,
if PY3:
# unicode
types += text_type,
for t in types:
expected = Index(lmap(t, raw))
res = index.map(t)
# should return an Index
assert isinstance(res, Index)
# preserve element types
assert all(isinstance(resi, t) for resi in res)
# lastly, values should compare equal
tm.assert_index_equal(res, expected)
示例9: test_searchsorted
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import PeriodIndex [as 别名]
def test_searchsorted(self, freq):
pidx = pd.PeriodIndex(['2014-01-01', '2014-01-02', '2014-01-03',
'2014-01-04', '2014-01-05'], freq=freq)
p1 = pd.Period('2014-01-01', freq=freq)
assert pidx.searchsorted(p1) == 0
p2 = pd.Period('2014-01-04', freq=freq)
assert pidx.searchsorted(p2) == 3
msg = "Input has different freq=H from PeriodIndex"
with pytest.raises(period.IncompatibleFrequency, match=msg):
pidx.searchsorted(pd.Period('2014-01-01', freq='H'))
msg = "Input has different freq=5D from PeriodIndex"
with pytest.raises(period.IncompatibleFrequency, match=msg):
pidx.searchsorted(pd.Period('2014-01-01', freq='5D'))
示例10: test_to_timestamp_pi_nat
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import PeriodIndex [as 别名]
def test_to_timestamp_pi_nat(self):
# GH#7228
index = PeriodIndex(['NaT', '2011-01', '2011-02'], freq='M',
name='idx')
result = index.to_timestamp('D')
expected = DatetimeIndex([pd.NaT, datetime(2011, 1, 1),
datetime(2011, 2, 1)], name='idx')
tm.assert_index_equal(result, expected)
assert result.name == 'idx'
result2 = result.to_period(freq='M')
tm.assert_index_equal(result2, index)
assert result2.name == 'idx'
result3 = result.to_period(freq='3M')
exp = PeriodIndex(['NaT', '2011-01', '2011-02'],
freq='3M', name='idx')
tm.assert_index_equal(result3, exp)
assert result3.freqstr == '3M'
msg = ('Frequency must be positive, because it'
' represents span: -2A')
with pytest.raises(ValueError, match=msg):
result.to_period(freq='-2A')
示例11: test_period_astype_to_timestamp
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import PeriodIndex [as 别名]
def test_period_astype_to_timestamp(self):
pi = pd.PeriodIndex(['2011-01', '2011-02', '2011-03'], freq='M')
exp = pd.DatetimeIndex(['2011-01-01', '2011-02-01', '2011-03-01'])
tm.assert_index_equal(pi.astype('datetime64[ns]'), exp)
exp = pd.DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31'])
exp = exp + Timedelta(1, 'D') - Timedelta(1, 'ns')
tm.assert_index_equal(pi.astype('datetime64[ns]', how='end'), exp)
exp = pd.DatetimeIndex(['2011-01-01', '2011-02-01', '2011-03-01'],
tz='US/Eastern')
res = pi.astype('datetime64[ns, US/Eastern]')
tm.assert_index_equal(pi.astype('datetime64[ns, US/Eastern]'), exp)
exp = pd.DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31'],
tz='US/Eastern')
exp = exp + Timedelta(1, 'D') - Timedelta(1, 'ns')
res = pi.astype('datetime64[ns, US/Eastern]', how='end')
tm.assert_index_equal(res, exp)
示例12: test_astype_conversion
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import PeriodIndex [as 别名]
def test_astype_conversion(self):
# GH#13149, GH#13209
idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D')
result = idx.astype(object)
expected = Index([Period('2016-05-16', freq='D')] +
[Period(NaT, freq='D')] * 3, dtype='object')
tm.assert_index_equal(result, expected)
result = idx.astype(np.int64)
expected = Int64Index([16937] + [-9223372036854775808] * 3,
dtype=np.int64)
tm.assert_index_equal(result, expected)
result = idx.astype(str)
expected = Index(str(x) for x in idx)
tm.assert_index_equal(result, expected)
idx = period_range('1990', '2009', freq='A')
result = idx.astype('i8')
tm.assert_index_equal(result, Index(idx.asi8))
tm.assert_numpy_array_equal(result.values, idx.asi8)
示例13: test_astype_object
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import PeriodIndex [as 别名]
def test_astype_object(self):
idx = pd.PeriodIndex([], freq='M')
exp = np.array([], dtype=object)
tm.assert_numpy_array_equal(idx.astype(object).values, exp)
tm.assert_numpy_array_equal(idx._mpl_repr(), exp)
idx = pd.PeriodIndex(['2011-01', pd.NaT], freq='M')
exp = np.array([pd.Period('2011-01', freq='M'), pd.NaT], dtype=object)
tm.assert_numpy_array_equal(idx.astype(object).values, exp)
tm.assert_numpy_array_equal(idx._mpl_repr(), exp)
exp = np.array([pd.Period('2011-01-01', freq='D'), pd.NaT],
dtype=object)
idx = pd.PeriodIndex(['2011-01-01', pd.NaT], freq='D')
tm.assert_numpy_array_equal(idx.astype(object).values, exp)
tm.assert_numpy_array_equal(idx._mpl_repr(), exp)
# TODO: de-duplicate this version (from test_ops) with the one above
# (from test_period)
示例14: test_shift_corner_cases
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import PeriodIndex [as 别名]
def test_shift_corner_cases(self):
# GH#9903
idx = pd.PeriodIndex([], name='xxx', freq='H')
with pytest.raises(TypeError):
# period shift doesn't accept freq
idx.shift(1, freq='H')
tm.assert_index_equal(idx.shift(0), idx)
tm.assert_index_equal(idx.shift(3), idx)
idx = pd.PeriodIndex(['2011-01-01 10:00', '2011-01-01 11:00'
'2011-01-01 12:00'], name='xxx', freq='H')
tm.assert_index_equal(idx.shift(0), idx)
exp = pd.PeriodIndex(['2011-01-01 13:00', '2011-01-01 14:00'
'2011-01-01 15:00'], name='xxx', freq='H')
tm.assert_index_equal(idx.shift(3), exp)
exp = pd.PeriodIndex(['2011-01-01 07:00', '2011-01-01 08:00'
'2011-01-01 09:00'], name='xxx', freq='H')
tm.assert_index_equal(idx.shift(-3), exp)
示例15: test_fillna_period
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import PeriodIndex [as 别名]
def test_fillna_period(self):
# GH 11343
idx = pd.PeriodIndex(['2011-01-01 09:00', pd.NaT,
'2011-01-01 11:00'], freq='H')
exp = pd.PeriodIndex(['2011-01-01 09:00', '2011-01-01 10:00',
'2011-01-01 11:00'], freq='H')
tm.assert_index_equal(
idx.fillna(pd.Period('2011-01-01 10:00', freq='H')), exp)
exp = pd.Index([pd.Period('2011-01-01 09:00', freq='H'), 'x',
pd.Period('2011-01-01 11:00', freq='H')], dtype=object)
tm.assert_index_equal(idx.fillna('x'), exp)
exp = pd.Index([pd.Period('2011-01-01 09:00', freq='H'),
pd.Period('2011-01-01', freq='D'),
pd.Period('2011-01-01 11:00', freq='H')], dtype=object)
tm.assert_index_equal(idx.fillna(
pd.Period('2011-01-01', freq='D')), exp)