本文整理匯總了Python中pandas.period_range方法的典型用法代碼示例。如果您正苦於以下問題:Python pandas.period_range方法的具體用法?Python pandas.period_range怎麽用?Python pandas.period_range使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas
的用法示例。
在下文中一共展示了pandas.period_range方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_union_misc
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import period_range [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)
示例2: test_intersection
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import period_range [as 別名]
def test_intersection(self, sort):
index = period_range('1/1/2000', '1/20/2000', freq='D')
result = index[:-5].intersection(index[10:], sort=sort)
tm.assert_index_equal(result, index[10:-5])
# not in order
left = _permute(index[:-5])
right = _permute(index[10:])
result = left.intersection(right, sort=sort)
if sort is None:
tm.assert_index_equal(result, index[10:-5])
assert tm.equalContents(result, index[10:-5])
# 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.intersection(index2, sort=sort)
index3 = period_range('1/1/2000', '1/20/2000', freq='2D')
with pytest.raises(period.IncompatibleFrequency):
index.intersection(index3, sort=sort)
示例3: test_getitem_seconds
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import period_range [as 別名]
def test_getitem_seconds(self):
# GH#6716
didx = pd.date_range(start='2013/01/01 09:00:00', freq='S',
periods=4000)
pidx = period_range(start='2013/01/01 09:00:00', freq='S',
periods=4000)
for idx in [didx, pidx]:
# getitem against index should raise ValueError
values = ['2014', '2013/02', '2013/01/02', '2013/02/01 9H',
'2013/02/01 09:00']
for v in values:
# GH7116
# these show deprecations as we are trying
# to slice with non-integer indexers
# with pytest.raises(IndexError):
# idx[v]
continue
s = Series(np.random.rand(len(idx)), index=idx)
tm.assert_series_equal(s['2013/01/01 10:00'], s[3600:3660])
tm.assert_series_equal(s['2013/01/01 9H'], s[:3600])
for d in ['2013/01/01', '2013/01', '2013']:
tm.assert_series_equal(s[d], s)
示例4: test_where_other
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import period_range [as 別名]
def test_where_other(self):
i = period_range('20130101', periods=5, freq='D')
for arr in [np.nan, pd.NaT]:
result = i.where(notna(i), other=np.nan)
expected = i
tm.assert_index_equal(result, expected)
i2 = i.copy()
i2 = pd.PeriodIndex([pd.NaT, pd.NaT] + i[2:].tolist(),
freq='D')
result = i.where(notna(i2), i2)
tm.assert_index_equal(result, i2)
i2 = i.copy()
i2 = pd.PeriodIndex([pd.NaT, pd.NaT] + i[2:].tolist(),
freq='D')
result = i.where(notna(i2), i2.values)
tm.assert_index_equal(result, i2)
示例5: test_dti_to_period
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import period_range [as 別名]
def test_dti_to_period(self):
dti = pd.date_range(start='1/1/2005', end='12/1/2005', freq='M')
pi1 = dti.to_period()
pi2 = dti.to_period(freq='D')
pi3 = dti.to_period(freq='3D')
assert pi1[0] == Period('Jan 2005', freq='M')
assert pi2[0] == Period('1/31/2005', freq='D')
assert pi3[0] == Period('1/31/2005', freq='3D')
assert pi1[-1] == Period('Nov 2005', freq='M')
assert pi2[-1] == Period('11/30/2005', freq='D')
assert pi3[-1], Period('11/30/2005', freq='3D')
tm.assert_index_equal(pi1, period_range('1/1/2005', '11/1/2005',
freq='M'))
tm.assert_index_equal(pi2, period_range('1/1/2005', '11/1/2005',
freq='M').asfreq('D'))
tm.assert_index_equal(pi3, period_range('1/1/2005', '11/1/2005',
freq='M').asfreq('3D'))
示例6: test_to_timestamp_pi_combined
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import period_range [as 別名]
def test_to_timestamp_pi_combined(self):
idx = period_range(start='2011', periods=2, freq='1D1H', name='idx')
result = idx.to_timestamp()
expected = DatetimeIndex(['2011-01-01 00:00', '2011-01-02 01:00'],
name='idx')
tm.assert_index_equal(result, expected)
result = idx.to_timestamp(how='E')
expected = DatetimeIndex(['2011-01-02 00:59:59',
'2011-01-03 01:59:59'],
name='idx')
expected = expected + Timedelta(1, 's') - Timedelta(1, 'ns')
tm.assert_index_equal(result, expected)
result = idx.to_timestamp(how='E', freq='H')
expected = DatetimeIndex(['2011-01-02 00:00', '2011-01-03 01:00'],
name='idx')
expected = expected + Timedelta(1, 'h') - Timedelta(1, 'ns')
tm.assert_index_equal(result, expected)
示例7: test_astype_conversion
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import period_range [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)
示例8: test_slice_with_negative_step
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import period_range [as 別名]
def test_slice_with_negative_step(self):
ts = Series(np.arange(20),
period_range('2014-01', periods=20, freq='M'))
SLC = pd.IndexSlice
def assert_slices_equivalent(l_slc, i_slc):
tm.assert_series_equal(ts[l_slc], ts.iloc[i_slc])
tm.assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])
tm.assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])
assert_slices_equivalent(SLC[Period('2014-10')::-1], SLC[9::-1])
assert_slices_equivalent(SLC['2014-10'::-1], SLC[9::-1])
assert_slices_equivalent(SLC[:Period('2014-10'):-1], SLC[:8:-1])
assert_slices_equivalent(SLC[:'2014-10':-1], SLC[:8:-1])
assert_slices_equivalent(SLC['2015-02':'2014-10':-1], SLC[13:8:-1])
assert_slices_equivalent(SLC[Period('2015-02'):Period('2014-10'):-1],
SLC[13:8:-1])
assert_slices_equivalent(SLC['2015-02':Period('2014-10'):-1],
SLC[13:8:-1])
assert_slices_equivalent(SLC[Period('2015-02'):'2014-10':-1],
SLC[13:8:-1])
assert_slices_equivalent(SLC['2014-10':'2015-02':-1], SLC[:0])
示例9: test_range_slice_day
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import period_range [as 別名]
def test_range_slice_day(self):
# GH#6716
didx = pd.date_range(start='2013/01/01', freq='D', periods=400)
pidx = period_range(start='2013/01/01', freq='D', periods=400)
for idx in [didx, pidx]:
# slices against index should raise IndexError
values = ['2014', '2013/02', '2013/01/02', '2013/02/01 9H',
'2013/02/01 09:00']
for v in values:
with pytest.raises(TypeError):
idx[v:]
s = Series(np.random.rand(len(idx)), index=idx)
tm.assert_series_equal(s['2013/01/02':], s[1:])
tm.assert_series_equal(s['2013/01/02':'2013/01/05'], s[1:5])
tm.assert_series_equal(s['2013/02':], s[31:])
tm.assert_series_equal(s['2014':], s[365:])
invalid = ['2013/02/01 9H', '2013/02/01 09:00']
for v in invalid:
with pytest.raises(TypeError):
idx[v:]
示例10: test_range_slice_seconds
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import period_range [as 別名]
def test_range_slice_seconds(self):
# GH#6716
didx = pd.date_range(start='2013/01/01 09:00:00', freq='S',
periods=4000)
pidx = period_range(start='2013/01/01 09:00:00', freq='S',
periods=4000)
for idx in [didx, pidx]:
# slices against index should raise IndexError
values = ['2014', '2013/02', '2013/01/02', '2013/02/01 9H',
'2013/02/01 09:00']
for v in values:
with pytest.raises(TypeError):
idx[v:]
s = Series(np.random.rand(len(idx)), index=idx)
tm.assert_series_equal(s['2013/01/01 09:05':'2013/01/01 09:10'],
s[300:660])
tm.assert_series_equal(s['2013/01/01 10:00':'2013/01/01 10:05'],
s[3600:3960])
tm.assert_series_equal(s['2013/01/01 10H':], s[3600:])
tm.assert_series_equal(s[:'2013/01/01 09:30'], s[:1860])
for d in ['2013/01/01', '2013/01', '2013']:
tm.assert_series_equal(s[d:], s)
示例11: test_range_slice_outofbounds
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import period_range [as 別名]
def test_range_slice_outofbounds(self):
# GH#5407
didx = pd.date_range(start='2013/10/01', freq='D', periods=10)
pidx = period_range(start='2013/10/01', freq='D', periods=10)
for idx in [didx, pidx]:
df = DataFrame(dict(units=[100 + i for i in range(10)]), index=idx)
empty = DataFrame(index=idx.__class__([], freq='D'),
columns=['units'])
empty['units'] = empty['units'].astype('int64')
tm.assert_frame_equal(df['2013/09/01':'2013/09/30'], empty)
tm.assert_frame_equal(df['2013/09/30':'2013/10/02'], df.iloc[:2])
tm.assert_frame_equal(df['2013/10/01':'2013/10/02'], df.iloc[:2])
tm.assert_frame_equal(df['2013/10/02':'2013/09/30'], empty)
tm.assert_frame_equal(df['2013/10/15':'2013/10/17'], empty)
tm.assert_frame_equal(df['2013-06':'2013-09'], empty)
tm.assert_frame_equal(df['2013-11':'2013-12'], empty)
示例12: test_difference_freq
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import period_range [as 別名]
def test_difference_freq(self, sort):
# GH14323: difference of Period MUST preserve frequency
# but the ability to union results must be preserved
index = period_range("20160920", "20160925", freq="D")
other = period_range("20160921", "20160924", freq="D")
expected = PeriodIndex(["20160920", "20160925"], freq='D')
idx_diff = index.difference(other, sort)
tm.assert_index_equal(idx_diff, expected)
tm.assert_attr_equal('freq', idx_diff, expected)
other = period_range("20160922", "20160925", freq="D")
idx_diff = index.difference(other, sort)
expected = PeriodIndex(["20160920", "20160921"], freq='D')
tm.assert_index_equal(idx_diff, expected)
tm.assert_attr_equal('freq', idx_diff, expected)
示例13: test_is_
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import period_range [as 別名]
def test_is_(self):
create_index = lambda: period_range(freq='A', start='1/1/2001',
end='12/1/2009')
index = create_index()
assert index.is_(index)
assert not index.is_(create_index())
assert index.is_(index.view())
assert index.is_(index.view().view().view().view().view())
assert index.view().is_(index)
ind2 = index.view()
index.name = "Apple"
assert ind2.is_(index)
assert not index.is_(index[:])
assert not index.is_(index.asfreq('M'))
assert not index.is_(index.asfreq('A'))
assert not index.is_(index - 2)
assert not index.is_(index - 0)
示例14: test_pindex_multiples
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import period_range [as 別名]
def test_pindex_multiples(self):
with tm.assert_produces_warning(FutureWarning):
pi = PeriodIndex(start='1/1/11', end='12/31/11', freq='2M')
expected = PeriodIndex(['2011-01', '2011-03', '2011-05', '2011-07',
'2011-09', '2011-11'], freq='2M')
tm.assert_index_equal(pi, expected)
assert pi.freq == offsets.MonthEnd(2)
assert pi.freqstr == '2M'
pi = period_range(start='1/1/11', end='12/31/11', freq='2M')
tm.assert_index_equal(pi, expected)
assert pi.freq == offsets.MonthEnd(2)
assert pi.freqstr == '2M'
pi = period_range(start='1/1/11', periods=6, freq='2M')
tm.assert_index_equal(pi, expected)
assert pi.freq == offsets.MonthEnd(2)
assert pi.freqstr == '2M'
示例15: test_drop_duplicates
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import period_range [as 別名]
def test_drop_duplicates(self):
# to check Index/Series compat
base = pd.period_range('2011-01-01', '2011-01-31', freq='D',
name='idx')
idx = base.append(base[:5])
res = idx.drop_duplicates()
tm.assert_index_equal(res, base)
res = Series(idx).drop_duplicates()
tm.assert_series_equal(res, Series(base))
res = idx.drop_duplicates(keep='last')
exp = base[5:].append(base[:5])
tm.assert_index_equal(res, exp)
res = Series(idx).drop_duplicates(keep='last')
tm.assert_series_equal(res, Series(exp, index=np.arange(5, 36)))
res = idx.drop_duplicates(keep=False)
tm.assert_index_equal(res, base[5:])
res = Series(idx).drop_duplicates(keep=False)
tm.assert_series_equal(res, Series(base[5:], index=np.arange(5, 31)))