pruned venvs
This commit is contained in:
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -1,47 +0,0 @@
|
||||
import pytest
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
import pandas.util.testing as tm
|
||||
from pandas.core.indexes.api import Index, MultiIndex
|
||||
from pandas.compat import lzip, long
|
||||
|
||||
|
||||
@pytest.fixture(params=[tm.makeUnicodeIndex(100),
|
||||
tm.makeStringIndex(100),
|
||||
tm.makeDateIndex(100),
|
||||
tm.makePeriodIndex(100),
|
||||
tm.makeTimedeltaIndex(100),
|
||||
tm.makeIntIndex(100),
|
||||
tm.makeUIntIndex(100),
|
||||
tm.makeFloatIndex(100),
|
||||
Index([True, False]),
|
||||
tm.makeCategoricalIndex(100),
|
||||
Index([]),
|
||||
MultiIndex.from_tuples(lzip(
|
||||
['foo', 'bar', 'baz'], [1, 2, 3])),
|
||||
Index([0, 0, 1, 1, 2, 2])],
|
||||
ids=lambda x: type(x).__name__)
|
||||
def indices(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(params=[1, np.array(1, dtype=np.int64)])
|
||||
def one(request):
|
||||
# zero-dim integer array behaves like an integer
|
||||
return request.param
|
||||
|
||||
|
||||
zeros = [box([0] * 5, dtype=dtype)
|
||||
for box in [pd.Index, np.array]
|
||||
for dtype in [np.int64, np.uint64, np.float64]]
|
||||
zeros.extend([np.array(0, dtype=dtype)
|
||||
for dtype in [np.int64, np.uint64, np.float64]])
|
||||
zeros.extend([0, 0.0, long(0)])
|
||||
|
||||
|
||||
@pytest.fixture(params=zeros)
|
||||
def zero(request):
|
||||
# For testing division by (or of) zero for Index with length 5, this
|
||||
# gives several scalar-zeros and length-5 vector-zeros
|
||||
return request.param
|
||||
@@ -1,90 +0,0 @@
|
||||
""" generic datetimelike tests """
|
||||
import pytest
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from .common import Base
|
||||
import pandas.util.testing as tm
|
||||
|
||||
|
||||
class DatetimeLike(Base):
|
||||
|
||||
def test_can_hold_identifiers(self):
|
||||
idx = self.create_index()
|
||||
key = idx[0]
|
||||
assert idx._can_hold_identifiers_and_holds_name(key) is False
|
||||
|
||||
def test_shift_identity(self):
|
||||
|
||||
idx = self.create_index()
|
||||
tm.assert_index_equal(idx, idx.shift(0))
|
||||
|
||||
def test_str(self):
|
||||
|
||||
# test the string repr
|
||||
idx = self.create_index()
|
||||
idx.name = 'foo'
|
||||
assert not "length=%s" % len(idx) in str(idx)
|
||||
assert "'foo'" in str(idx)
|
||||
assert idx.__class__.__name__ in str(idx)
|
||||
|
||||
if hasattr(idx, 'tz'):
|
||||
if idx.tz is not None:
|
||||
assert idx.tz in str(idx)
|
||||
if hasattr(idx, 'freq'):
|
||||
assert "freq='%s'" % idx.freqstr in str(idx)
|
||||
|
||||
def test_view(self, indices):
|
||||
super(DatetimeLike, self).test_view(indices)
|
||||
|
||||
i = self.create_index()
|
||||
|
||||
i_view = i.view('i8')
|
||||
result = self._holder(i)
|
||||
tm.assert_index_equal(result, i)
|
||||
|
||||
i_view = i.view(self._holder)
|
||||
result = self._holder(i)
|
||||
tm.assert_index_equal(result, i_view)
|
||||
|
||||
def test_map_callable(self):
|
||||
|
||||
expected = self.index + 1
|
||||
result = self.index.map(lambda x: x + 1)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# map to NaT
|
||||
result = self.index.map(lambda x: pd.NaT if x == self.index[0] else x)
|
||||
expected = pd.Index([pd.NaT] + self.index[1:].tolist())
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"mapper",
|
||||
[
|
||||
lambda values, index: {i: e for e, i in zip(values, index)},
|
||||
lambda values, index: pd.Series(values, index)])
|
||||
def test_map_dictlike(self, mapper):
|
||||
expected = self.index + 1
|
||||
|
||||
# don't compare the freqs
|
||||
if isinstance(expected, pd.DatetimeIndex):
|
||||
expected.freq = None
|
||||
|
||||
result = self.index.map(mapper(expected, self.index))
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
expected = pd.Index([pd.NaT] + self.index[1:].tolist())
|
||||
result = self.index.map(mapper(expected, self.index))
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# empty map; these map to np.nan because we cannot know
|
||||
# to re-infer things
|
||||
expected = pd.Index([np.nan] * len(self.index))
|
||||
result = self.index.map(mapper([], []))
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_asobject_deprecated(self):
|
||||
# GH18572
|
||||
d = self.create_index()
|
||||
with tm.assert_produces_warning(FutureWarning):
|
||||
i = d.asobject
|
||||
assert isinstance(i, pd.Index)
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
-1053
File diff suppressed because it is too large
Load Diff
-349
@@ -1,349 +0,0 @@
|
||||
import pytest
|
||||
|
||||
import pytz
|
||||
import dateutil
|
||||
import numpy as np
|
||||
|
||||
from datetime import datetime
|
||||
from dateutil.tz import tzlocal
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
from pandas import (DatetimeIndex, date_range, Series, NaT, Index, Timestamp,
|
||||
Int64Index, Period)
|
||||
|
||||
|
||||
class TestDatetimeIndex(object):
|
||||
|
||||
def test_astype(self):
|
||||
# GH 13149, GH 13209
|
||||
idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN])
|
||||
|
||||
result = idx.astype(object)
|
||||
expected = Index([Timestamp('2016-05-16')] + [NaT] * 3, dtype=object)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = idx.astype(int)
|
||||
expected = Int64Index([1463356800000000000] +
|
||||
[-9223372036854775808] * 3, dtype=np.int64)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
rng = date_range('1/1/2000', periods=10)
|
||||
result = rng.astype('i8')
|
||||
tm.assert_index_equal(result, Index(rng.asi8))
|
||||
tm.assert_numpy_array_equal(result.values, rng.asi8)
|
||||
|
||||
def test_astype_with_tz(self):
|
||||
|
||||
# with tz
|
||||
rng = date_range('1/1/2000', periods=10, tz='US/Eastern')
|
||||
result = rng.astype('datetime64[ns]')
|
||||
expected = (date_range('1/1/2000', periods=10,
|
||||
tz='US/Eastern')
|
||||
.tz_convert('UTC').tz_localize(None))
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# BUG#10442 : testing astype(str) is correct for Series/DatetimeIndex
|
||||
result = pd.Series(pd.date_range('2012-01-01', periods=3)).astype(str)
|
||||
expected = pd.Series(
|
||||
['2012-01-01', '2012-01-02', '2012-01-03'], dtype=object)
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
result = Series(pd.date_range('2012-01-01', periods=3,
|
||||
tz='US/Eastern')).astype(str)
|
||||
expected = Series(['2012-01-01 00:00:00-05:00',
|
||||
'2012-01-02 00:00:00-05:00',
|
||||
'2012-01-03 00:00:00-05:00'],
|
||||
dtype=object)
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
# GH 18951: tz-aware to tz-aware
|
||||
idx = date_range('20170101', periods=4, tz='US/Pacific')
|
||||
result = idx.astype('datetime64[ns, US/Eastern]')
|
||||
expected = date_range('20170101 03:00:00', periods=4, tz='US/Eastern')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# GH 18951: tz-naive to tz-aware
|
||||
idx = date_range('20170101', periods=4)
|
||||
result = idx.astype('datetime64[ns, US/Eastern]')
|
||||
expected = date_range('20170101', periods=4, tz='US/Eastern')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_astype_str_compat(self):
|
||||
# GH 13149, GH 13209
|
||||
# verify that we are returning NaT as a string (and not unicode)
|
||||
|
||||
idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN])
|
||||
result = idx.astype(str)
|
||||
expected = Index(['2016-05-16', 'NaT', 'NaT', 'NaT'], dtype=object)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_astype_str(self):
|
||||
# test astype string - #10442
|
||||
result = date_range('2012-01-01', periods=4,
|
||||
name='test_name').astype(str)
|
||||
expected = Index(['2012-01-01', '2012-01-02', '2012-01-03',
|
||||
'2012-01-04'], name='test_name', dtype=object)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# test astype string with tz and name
|
||||
result = date_range('2012-01-01', periods=3, name='test_name',
|
||||
tz='US/Eastern').astype(str)
|
||||
expected = Index(['2012-01-01 00:00:00-05:00',
|
||||
'2012-01-02 00:00:00-05:00',
|
||||
'2012-01-03 00:00:00-05:00'],
|
||||
name='test_name', dtype=object)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# test astype string with freqH and name
|
||||
result = date_range('1/1/2011', periods=3, freq='H',
|
||||
name='test_name').astype(str)
|
||||
expected = Index(['2011-01-01 00:00:00', '2011-01-01 01:00:00',
|
||||
'2011-01-01 02:00:00'],
|
||||
name='test_name', dtype=object)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# test astype string with freqH and timezone
|
||||
result = date_range('3/6/2012 00:00', periods=2, freq='H',
|
||||
tz='Europe/London', name='test_name').astype(str)
|
||||
expected = Index(['2012-03-06 00:00:00+00:00',
|
||||
'2012-03-06 01:00:00+00:00'],
|
||||
dtype=object, name='test_name')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_astype_datetime64(self):
|
||||
# GH 13149, GH 13209
|
||||
idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN])
|
||||
|
||||
result = idx.astype('datetime64[ns]')
|
||||
tm.assert_index_equal(result, idx)
|
||||
assert result is not idx
|
||||
|
||||
result = idx.astype('datetime64[ns]', copy=False)
|
||||
tm.assert_index_equal(result, idx)
|
||||
assert result is idx
|
||||
|
||||
idx_tz = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN], tz='EST')
|
||||
result = idx_tz.astype('datetime64[ns]')
|
||||
expected = DatetimeIndex(['2016-05-16 05:00:00', 'NaT', 'NaT', 'NaT'],
|
||||
dtype='datetime64[ns]')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_astype_object(self):
|
||||
rng = date_range('1/1/2000', periods=20)
|
||||
|
||||
casted = rng.astype('O')
|
||||
exp_values = list(rng)
|
||||
|
||||
tm.assert_index_equal(casted, Index(exp_values, dtype=np.object_))
|
||||
assert casted.tolist() == exp_values
|
||||
|
||||
@pytest.mark.parametrize('tz', [None, 'Asia/Tokyo'])
|
||||
def test_astype_object_tz(self, tz):
|
||||
idx = pd.date_range(start='2013-01-01', periods=4, freq='M',
|
||||
name='idx', tz=tz)
|
||||
expected_list = [Timestamp('2013-01-31', tz=tz),
|
||||
Timestamp('2013-02-28', tz=tz),
|
||||
Timestamp('2013-03-31', tz=tz),
|
||||
Timestamp('2013-04-30', tz=tz)]
|
||||
expected = pd.Index(expected_list, dtype=object, name='idx')
|
||||
result = idx.astype(object)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert idx.tolist() == expected_list
|
||||
|
||||
def test_astype_object_with_nat(self):
|
||||
idx = DatetimeIndex([datetime(2013, 1, 1), datetime(2013, 1, 2),
|
||||
pd.NaT, datetime(2013, 1, 4)], name='idx')
|
||||
expected_list = [Timestamp('2013-01-01'),
|
||||
Timestamp('2013-01-02'), pd.NaT,
|
||||
Timestamp('2013-01-04')]
|
||||
expected = pd.Index(expected_list, dtype=object, name='idx')
|
||||
result = idx.astype(object)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert idx.tolist() == expected_list
|
||||
|
||||
@pytest.mark.parametrize('dtype', [
|
||||
float, 'timedelta64', 'timedelta64[ns]', 'datetime64',
|
||||
'datetime64[D]'])
|
||||
def test_astype_raises(self, dtype):
|
||||
# GH 13149, GH 13209
|
||||
idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN])
|
||||
msg = 'Cannot cast DatetimeIndex to dtype'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
idx.astype(dtype)
|
||||
|
||||
def test_index_convert_to_datetime_array(self):
|
||||
def _check_rng(rng):
|
||||
converted = rng.to_pydatetime()
|
||||
assert isinstance(converted, np.ndarray)
|
||||
for x, stamp in zip(converted, rng):
|
||||
assert isinstance(x, datetime)
|
||||
assert x == stamp.to_pydatetime()
|
||||
assert x.tzinfo == stamp.tzinfo
|
||||
|
||||
rng = date_range('20090415', '20090519')
|
||||
rng_eastern = date_range('20090415', '20090519', tz='US/Eastern')
|
||||
rng_utc = date_range('20090415', '20090519', tz='utc')
|
||||
|
||||
_check_rng(rng)
|
||||
_check_rng(rng_eastern)
|
||||
_check_rng(rng_utc)
|
||||
|
||||
def test_index_convert_to_datetime_array_explicit_pytz(self):
|
||||
def _check_rng(rng):
|
||||
converted = rng.to_pydatetime()
|
||||
assert isinstance(converted, np.ndarray)
|
||||
for x, stamp in zip(converted, rng):
|
||||
assert isinstance(x, datetime)
|
||||
assert x == stamp.to_pydatetime()
|
||||
assert x.tzinfo == stamp.tzinfo
|
||||
|
||||
rng = date_range('20090415', '20090519')
|
||||
rng_eastern = date_range('20090415', '20090519',
|
||||
tz=pytz.timezone('US/Eastern'))
|
||||
rng_utc = date_range('20090415', '20090519', tz=pytz.utc)
|
||||
|
||||
_check_rng(rng)
|
||||
_check_rng(rng_eastern)
|
||||
_check_rng(rng_utc)
|
||||
|
||||
def test_index_convert_to_datetime_array_dateutil(self):
|
||||
def _check_rng(rng):
|
||||
converted = rng.to_pydatetime()
|
||||
assert isinstance(converted, np.ndarray)
|
||||
for x, stamp in zip(converted, rng):
|
||||
assert isinstance(x, datetime)
|
||||
assert x == stamp.to_pydatetime()
|
||||
assert x.tzinfo == stamp.tzinfo
|
||||
|
||||
rng = date_range('20090415', '20090519')
|
||||
rng_eastern = date_range('20090415', '20090519',
|
||||
tz='dateutil/US/Eastern')
|
||||
rng_utc = date_range('20090415', '20090519', tz=dateutil.tz.tzutc())
|
||||
|
||||
_check_rng(rng)
|
||||
_check_rng(rng_eastern)
|
||||
_check_rng(rng_utc)
|
||||
|
||||
|
||||
class TestToPeriod(object):
|
||||
|
||||
def setup_method(self, method):
|
||||
data = [Timestamp('2007-01-01 10:11:12.123456Z'),
|
||||
Timestamp('2007-01-01 10:11:13.789123Z')]
|
||||
self.index = DatetimeIndex(data)
|
||||
|
||||
def test_to_period_millisecond(self):
|
||||
index = self.index
|
||||
|
||||
period = index.to_period(freq='L')
|
||||
assert 2 == len(period)
|
||||
assert period[0] == Period('2007-01-01 10:11:12.123Z', 'L')
|
||||
assert period[1] == Period('2007-01-01 10:11:13.789Z', 'L')
|
||||
|
||||
def test_to_period_microsecond(self):
|
||||
index = self.index
|
||||
|
||||
period = index.to_period(freq='U')
|
||||
assert 2 == len(period)
|
||||
assert period[0] == Period('2007-01-01 10:11:12.123456Z', 'U')
|
||||
assert period[1] == Period('2007-01-01 10:11:13.789123Z', 'U')
|
||||
|
||||
def test_to_period_tz_pytz(self):
|
||||
from pytz import utc as UTC
|
||||
|
||||
xp = date_range('1/1/2000', '4/1/2000').to_period()
|
||||
|
||||
ts = date_range('1/1/2000', '4/1/2000', tz='US/Eastern')
|
||||
|
||||
result = ts.to_period()[0]
|
||||
expected = ts[0].to_period()
|
||||
|
||||
assert result == expected
|
||||
tm.assert_index_equal(ts.to_period(), xp)
|
||||
|
||||
ts = date_range('1/1/2000', '4/1/2000', tz=UTC)
|
||||
|
||||
result = ts.to_period()[0]
|
||||
expected = ts[0].to_period()
|
||||
|
||||
assert result == expected
|
||||
tm.assert_index_equal(ts.to_period(), xp)
|
||||
|
||||
ts = date_range('1/1/2000', '4/1/2000', tz=tzlocal())
|
||||
|
||||
result = ts.to_period()[0]
|
||||
expected = ts[0].to_period()
|
||||
|
||||
assert result == expected
|
||||
tm.assert_index_equal(ts.to_period(), xp)
|
||||
|
||||
def test_to_period_tz_explicit_pytz(self):
|
||||
xp = date_range('1/1/2000', '4/1/2000').to_period()
|
||||
|
||||
ts = date_range('1/1/2000', '4/1/2000', tz=pytz.timezone('US/Eastern'))
|
||||
|
||||
result = ts.to_period()[0]
|
||||
expected = ts[0].to_period()
|
||||
|
||||
assert result == expected
|
||||
tm.assert_index_equal(ts.to_period(), xp)
|
||||
|
||||
ts = date_range('1/1/2000', '4/1/2000', tz=pytz.utc)
|
||||
|
||||
result = ts.to_period()[0]
|
||||
expected = ts[0].to_period()
|
||||
|
||||
assert result == expected
|
||||
tm.assert_index_equal(ts.to_period(), xp)
|
||||
|
||||
ts = date_range('1/1/2000', '4/1/2000', tz=tzlocal())
|
||||
|
||||
result = ts.to_period()[0]
|
||||
expected = ts[0].to_period()
|
||||
|
||||
assert result == expected
|
||||
tm.assert_index_equal(ts.to_period(), xp)
|
||||
|
||||
def test_to_period_tz_dateutil(self):
|
||||
xp = date_range('1/1/2000', '4/1/2000').to_period()
|
||||
|
||||
ts = date_range('1/1/2000', '4/1/2000', tz='dateutil/US/Eastern')
|
||||
|
||||
result = ts.to_period()[0]
|
||||
expected = ts[0].to_period()
|
||||
|
||||
assert result == expected
|
||||
tm.assert_index_equal(ts.to_period(), xp)
|
||||
|
||||
ts = date_range('1/1/2000', '4/1/2000', tz=dateutil.tz.tzutc())
|
||||
|
||||
result = ts.to_period()[0]
|
||||
expected = ts[0].to_period()
|
||||
|
||||
assert result == expected
|
||||
tm.assert_index_equal(ts.to_period(), xp)
|
||||
|
||||
ts = date_range('1/1/2000', '4/1/2000', tz=tzlocal())
|
||||
|
||||
result = ts.to_period()[0]
|
||||
expected = ts[0].to_period()
|
||||
|
||||
assert result == expected
|
||||
tm.assert_index_equal(ts.to_period(), xp)
|
||||
|
||||
def test_to_period_nofreq(self):
|
||||
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-04'])
|
||||
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)
|
||||
-622
@@ -1,622 +0,0 @@
|
||||
import pytest
|
||||
|
||||
import pytz
|
||||
import numpy as np
|
||||
from datetime import timedelta
|
||||
|
||||
import pandas as pd
|
||||
from pandas import offsets
|
||||
import pandas.util.testing as tm
|
||||
from pandas._libs.tslib import OutOfBoundsDatetime
|
||||
from pandas._libs.tslibs import conversion
|
||||
from pandas import (DatetimeIndex, Index, Timestamp, datetime, date_range,
|
||||
to_datetime)
|
||||
|
||||
|
||||
class TestDatetimeIndex(object):
|
||||
|
||||
def test_construction_caching(self):
|
||||
|
||||
df = pd.DataFrame({'dt': pd.date_range('20130101', periods=3),
|
||||
'dttz': pd.date_range('20130101', periods=3,
|
||||
tz='US/Eastern'),
|
||||
'dt_with_null': [pd.Timestamp('20130101'), pd.NaT,
|
||||
pd.Timestamp('20130103')],
|
||||
'dtns': pd.date_range('20130101', periods=3,
|
||||
freq='ns')})
|
||||
assert df.dttz.dtype.tz.zone == 'US/Eastern'
|
||||
|
||||
def test_construction_with_alt(self):
|
||||
|
||||
i = pd.date_range('20130101', periods=5, freq='H', tz='US/Eastern')
|
||||
i2 = DatetimeIndex(i, dtype=i.dtype)
|
||||
tm.assert_index_equal(i, i2)
|
||||
assert i.tz.zone == 'US/Eastern'
|
||||
|
||||
i2 = DatetimeIndex(i.tz_localize(None).asi8, tz=i.dtype.tz)
|
||||
tm.assert_index_equal(i, i2)
|
||||
assert i.tz.zone == 'US/Eastern'
|
||||
|
||||
i2 = DatetimeIndex(i.tz_localize(None).asi8, dtype=i.dtype)
|
||||
tm.assert_index_equal(i, i2)
|
||||
assert i.tz.zone == 'US/Eastern'
|
||||
|
||||
i2 = DatetimeIndex(
|
||||
i.tz_localize(None).asi8, dtype=i.dtype, tz=i.dtype.tz)
|
||||
tm.assert_index_equal(i, i2)
|
||||
assert i.tz.zone == 'US/Eastern'
|
||||
|
||||
# localize into the provided tz
|
||||
i2 = DatetimeIndex(i.tz_localize(None).asi8, tz='UTC')
|
||||
expected = i.tz_localize(None).tz_localize('UTC')
|
||||
tm.assert_index_equal(i2, expected)
|
||||
|
||||
# incompat tz/dtype
|
||||
pytest.raises(ValueError, lambda: DatetimeIndex(
|
||||
i.tz_localize(None).asi8, dtype=i.dtype, tz='US/Pacific'))
|
||||
|
||||
def test_construction_index_with_mixed_timezones(self):
|
||||
# gh-11488: no tz results in DatetimeIndex
|
||||
result = Index([Timestamp('2011-01-01'),
|
||||
Timestamp('2011-01-02')], name='idx')
|
||||
exp = DatetimeIndex([Timestamp('2011-01-01'),
|
||||
Timestamp('2011-01-02')], name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
assert result.tz is None
|
||||
|
||||
# same tz results in DatetimeIndex
|
||||
result = Index([Timestamp('2011-01-01 10:00', tz='Asia/Tokyo'),
|
||||
Timestamp('2011-01-02 10:00', tz='Asia/Tokyo')],
|
||||
name='idx')
|
||||
exp = DatetimeIndex(
|
||||
[Timestamp('2011-01-01 10:00'), Timestamp('2011-01-02 10:00')
|
||||
], tz='Asia/Tokyo', name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
assert result.tz is not None
|
||||
assert result.tz == exp.tz
|
||||
|
||||
# same tz results in DatetimeIndex (DST)
|
||||
result = Index([Timestamp('2011-01-01 10:00', tz='US/Eastern'),
|
||||
Timestamp('2011-08-01 10:00', tz='US/Eastern')],
|
||||
name='idx')
|
||||
exp = DatetimeIndex([Timestamp('2011-01-01 10:00'),
|
||||
Timestamp('2011-08-01 10:00')],
|
||||
tz='US/Eastern', name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
assert result.tz is not None
|
||||
assert result.tz == exp.tz
|
||||
|
||||
# Different tz results in Index(dtype=object)
|
||||
result = Index([Timestamp('2011-01-01 10:00'),
|
||||
Timestamp('2011-01-02 10:00', tz='US/Eastern')],
|
||||
name='idx')
|
||||
exp = Index([Timestamp('2011-01-01 10:00'),
|
||||
Timestamp('2011-01-02 10:00', tz='US/Eastern')],
|
||||
dtype='object', name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert not isinstance(result, DatetimeIndex)
|
||||
|
||||
result = Index([Timestamp('2011-01-01 10:00', tz='Asia/Tokyo'),
|
||||
Timestamp('2011-01-02 10:00', tz='US/Eastern')],
|
||||
name='idx')
|
||||
exp = Index([Timestamp('2011-01-01 10:00', tz='Asia/Tokyo'),
|
||||
Timestamp('2011-01-02 10:00', tz='US/Eastern')],
|
||||
dtype='object', name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert not isinstance(result, DatetimeIndex)
|
||||
|
||||
# length = 1
|
||||
result = Index([Timestamp('2011-01-01')], name='idx')
|
||||
exp = DatetimeIndex([Timestamp('2011-01-01')], name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
assert result.tz is None
|
||||
|
||||
# length = 1 with tz
|
||||
result = Index(
|
||||
[Timestamp('2011-01-01 10:00', tz='Asia/Tokyo')], name='idx')
|
||||
exp = DatetimeIndex([Timestamp('2011-01-01 10:00')], tz='Asia/Tokyo',
|
||||
name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
assert result.tz is not None
|
||||
assert result.tz == exp.tz
|
||||
|
||||
def test_construction_index_with_mixed_timezones_with_NaT(self):
|
||||
# see gh-11488
|
||||
result = Index([pd.NaT, Timestamp('2011-01-01'),
|
||||
pd.NaT, Timestamp('2011-01-02')], name='idx')
|
||||
exp = DatetimeIndex([pd.NaT, Timestamp('2011-01-01'),
|
||||
pd.NaT, Timestamp('2011-01-02')], name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
assert result.tz is None
|
||||
|
||||
# Same tz results in DatetimeIndex
|
||||
result = Index([pd.NaT, Timestamp('2011-01-01 10:00', tz='Asia/Tokyo'),
|
||||
pd.NaT, Timestamp('2011-01-02 10:00',
|
||||
tz='Asia/Tokyo')],
|
||||
name='idx')
|
||||
exp = DatetimeIndex([pd.NaT, Timestamp('2011-01-01 10:00'),
|
||||
pd.NaT, Timestamp('2011-01-02 10:00')],
|
||||
tz='Asia/Tokyo', name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
assert result.tz is not None
|
||||
assert result.tz == exp.tz
|
||||
|
||||
# same tz results in DatetimeIndex (DST)
|
||||
result = Index([Timestamp('2011-01-01 10:00', tz='US/Eastern'),
|
||||
pd.NaT,
|
||||
Timestamp('2011-08-01 10:00', tz='US/Eastern')],
|
||||
name='idx')
|
||||
exp = DatetimeIndex([Timestamp('2011-01-01 10:00'), pd.NaT,
|
||||
Timestamp('2011-08-01 10:00')],
|
||||
tz='US/Eastern', name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
assert result.tz is not None
|
||||
assert result.tz == exp.tz
|
||||
|
||||
# different tz results in Index(dtype=object)
|
||||
result = Index([pd.NaT, Timestamp('2011-01-01 10:00'),
|
||||
pd.NaT, Timestamp('2011-01-02 10:00',
|
||||
tz='US/Eastern')],
|
||||
name='idx')
|
||||
exp = Index([pd.NaT, Timestamp('2011-01-01 10:00'),
|
||||
pd.NaT, Timestamp('2011-01-02 10:00', tz='US/Eastern')],
|
||||
dtype='object', name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert not isinstance(result, DatetimeIndex)
|
||||
|
||||
result = Index([pd.NaT, Timestamp('2011-01-01 10:00', tz='Asia/Tokyo'),
|
||||
pd.NaT, Timestamp('2011-01-02 10:00',
|
||||
tz='US/Eastern')], name='idx')
|
||||
exp = Index([pd.NaT, Timestamp('2011-01-01 10:00', tz='Asia/Tokyo'),
|
||||
pd.NaT, Timestamp('2011-01-02 10:00', tz='US/Eastern')],
|
||||
dtype='object', name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert not isinstance(result, DatetimeIndex)
|
||||
|
||||
# all NaT
|
||||
result = Index([pd.NaT, pd.NaT], name='idx')
|
||||
exp = DatetimeIndex([pd.NaT, pd.NaT], name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
assert result.tz is None
|
||||
|
||||
# all NaT with tz
|
||||
result = Index([pd.NaT, pd.NaT], tz='Asia/Tokyo', name='idx')
|
||||
exp = DatetimeIndex([pd.NaT, pd.NaT], tz='Asia/Tokyo', name='idx')
|
||||
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
assert result.tz is not None
|
||||
assert result.tz == exp.tz
|
||||
|
||||
def test_construction_dti_with_mixed_timezones(self):
|
||||
# GH 11488 (not changed, added explicit tests)
|
||||
|
||||
# no tz results in DatetimeIndex
|
||||
result = DatetimeIndex(
|
||||
[Timestamp('2011-01-01'), Timestamp('2011-01-02')], name='idx')
|
||||
exp = DatetimeIndex(
|
||||
[Timestamp('2011-01-01'), Timestamp('2011-01-02')], name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
|
||||
# same tz results in DatetimeIndex
|
||||
result = DatetimeIndex([Timestamp('2011-01-01 10:00', tz='Asia/Tokyo'),
|
||||
Timestamp('2011-01-02 10:00',
|
||||
tz='Asia/Tokyo')],
|
||||
name='idx')
|
||||
exp = DatetimeIndex([Timestamp('2011-01-01 10:00'),
|
||||
Timestamp('2011-01-02 10:00')],
|
||||
tz='Asia/Tokyo', name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
|
||||
# same tz results in DatetimeIndex (DST)
|
||||
result = DatetimeIndex([Timestamp('2011-01-01 10:00', tz='US/Eastern'),
|
||||
Timestamp('2011-08-01 10:00',
|
||||
tz='US/Eastern')],
|
||||
name='idx')
|
||||
exp = DatetimeIndex([Timestamp('2011-01-01 10:00'),
|
||||
Timestamp('2011-08-01 10:00')],
|
||||
tz='US/Eastern', name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
|
||||
# different tz coerces tz-naive to tz-awareIndex(dtype=object)
|
||||
result = DatetimeIndex([Timestamp('2011-01-01 10:00'),
|
||||
Timestamp('2011-01-02 10:00',
|
||||
tz='US/Eastern')], name='idx')
|
||||
exp = DatetimeIndex([Timestamp('2011-01-01 05:00'),
|
||||
Timestamp('2011-01-02 10:00')],
|
||||
tz='US/Eastern', name='idx')
|
||||
tm.assert_index_equal(result, exp, exact=True)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
|
||||
# tz mismatch affecting to tz-aware raises TypeError/ValueError
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
DatetimeIndex([Timestamp('2011-01-01 10:00', tz='Asia/Tokyo'),
|
||||
Timestamp('2011-01-02 10:00', tz='US/Eastern')],
|
||||
name='idx')
|
||||
|
||||
with tm.assert_raises_regex(TypeError,
|
||||
'data is already tz-aware'):
|
||||
DatetimeIndex([Timestamp('2011-01-01 10:00'),
|
||||
Timestamp('2011-01-02 10:00', tz='US/Eastern')],
|
||||
tz='Asia/Tokyo', name='idx')
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
DatetimeIndex([Timestamp('2011-01-01 10:00', tz='Asia/Tokyo'),
|
||||
Timestamp('2011-01-02 10:00', tz='US/Eastern')],
|
||||
tz='US/Eastern', name='idx')
|
||||
|
||||
with tm.assert_raises_regex(TypeError,
|
||||
'data is already tz-aware'):
|
||||
# passing tz should results in DatetimeIndex, then mismatch raises
|
||||
# TypeError
|
||||
Index([pd.NaT, Timestamp('2011-01-01 10:00'),
|
||||
pd.NaT, Timestamp('2011-01-02 10:00', tz='US/Eastern')],
|
||||
tz='Asia/Tokyo', name='idx')
|
||||
|
||||
def test_construction_base_constructor(self):
|
||||
arr = [pd.Timestamp('2011-01-01'), pd.NaT, pd.Timestamp('2011-01-03')]
|
||||
tm.assert_index_equal(pd.Index(arr), pd.DatetimeIndex(arr))
|
||||
tm.assert_index_equal(pd.Index(np.array(arr)),
|
||||
pd.DatetimeIndex(np.array(arr)))
|
||||
|
||||
arr = [np.nan, pd.NaT, pd.Timestamp('2011-01-03')]
|
||||
tm.assert_index_equal(pd.Index(arr), pd.DatetimeIndex(arr))
|
||||
tm.assert_index_equal(pd.Index(np.array(arr)),
|
||||
pd.DatetimeIndex(np.array(arr)))
|
||||
|
||||
def test_construction_outofbounds(self):
|
||||
# GH 13663
|
||||
dates = [datetime(3000, 1, 1), datetime(4000, 1, 1),
|
||||
datetime(5000, 1, 1), datetime(6000, 1, 1)]
|
||||
exp = Index(dates, dtype=object)
|
||||
# coerces to object
|
||||
tm.assert_index_equal(Index(dates), exp)
|
||||
|
||||
with pytest.raises(OutOfBoundsDatetime):
|
||||
# can't create DatetimeIndex
|
||||
DatetimeIndex(dates)
|
||||
|
||||
def test_construction_with_ndarray(self):
|
||||
# GH 5152
|
||||
dates = [datetime(2013, 10, 7),
|
||||
datetime(2013, 10, 8),
|
||||
datetime(2013, 10, 9)]
|
||||
data = DatetimeIndex(dates, freq=pd.tseries.frequencies.BDay()).values
|
||||
result = DatetimeIndex(data, freq=pd.tseries.frequencies.BDay())
|
||||
expected = DatetimeIndex(['2013-10-07',
|
||||
'2013-10-08',
|
||||
'2013-10-09'],
|
||||
freq='B')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_constructor_coverage(self):
|
||||
rng = date_range('1/1/2000', periods=10.5)
|
||||
exp = date_range('1/1/2000', periods=10)
|
||||
tm.assert_index_equal(rng, exp)
|
||||
|
||||
msg = 'periods must be a number, got foo'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
DatetimeIndex(start='1/1/2000', periods='foo', freq='D')
|
||||
|
||||
pytest.raises(ValueError, DatetimeIndex, start='1/1/2000',
|
||||
end='1/10/2000')
|
||||
|
||||
pytest.raises(ValueError, DatetimeIndex, '1/1/2000')
|
||||
|
||||
# generator expression
|
||||
gen = (datetime(2000, 1, 1) + timedelta(i) for i in range(10))
|
||||
result = DatetimeIndex(gen)
|
||||
expected = DatetimeIndex([datetime(2000, 1, 1) + timedelta(i)
|
||||
for i in range(10)])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# NumPy string array
|
||||
strings = np.array(['2000-01-01', '2000-01-02', '2000-01-03'])
|
||||
result = DatetimeIndex(strings)
|
||||
expected = DatetimeIndex(strings.astype('O'))
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
from_ints = DatetimeIndex(expected.asi8)
|
||||
tm.assert_index_equal(from_ints, expected)
|
||||
|
||||
# string with NaT
|
||||
strings = np.array(['2000-01-01', '2000-01-02', 'NaT'])
|
||||
result = DatetimeIndex(strings)
|
||||
expected = DatetimeIndex(strings.astype('O'))
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
from_ints = DatetimeIndex(expected.asi8)
|
||||
tm.assert_index_equal(from_ints, expected)
|
||||
|
||||
# non-conforming
|
||||
pytest.raises(ValueError, DatetimeIndex,
|
||||
['2000-01-01', '2000-01-02', '2000-01-04'], freq='D')
|
||||
|
||||
pytest.raises(ValueError, DatetimeIndex, start='2011-01-01',
|
||||
freq='b')
|
||||
pytest.raises(ValueError, DatetimeIndex, end='2011-01-01',
|
||||
freq='B')
|
||||
pytest.raises(ValueError, DatetimeIndex, periods=10, freq='D')
|
||||
|
||||
@pytest.mark.parametrize('freq', ['AS', 'W-SUN'])
|
||||
def test_constructor_datetime64_tzformat(self, freq):
|
||||
# see GH#6572: ISO 8601 format results in pytz.FixedOffset
|
||||
idx = date_range('2013-01-01T00:00:00-05:00',
|
||||
'2016-01-01T23:59:59-05:00', freq=freq)
|
||||
expected = date_range('2013-01-01T00:00:00', '2016-01-01T23:59:59',
|
||||
freq=freq, tz=pytz.FixedOffset(-300))
|
||||
tm.assert_index_equal(idx, expected)
|
||||
# Unable to use `US/Eastern` because of DST
|
||||
expected_i8 = date_range('2013-01-01T00:00:00',
|
||||
'2016-01-01T23:59:59', freq=freq,
|
||||
tz='America/Lima')
|
||||
tm.assert_numpy_array_equal(idx.asi8, expected_i8.asi8)
|
||||
|
||||
idx = date_range('2013-01-01T00:00:00+09:00',
|
||||
'2016-01-01T23:59:59+09:00', freq=freq)
|
||||
expected = date_range('2013-01-01T00:00:00', '2016-01-01T23:59:59',
|
||||
freq=freq, tz=pytz.FixedOffset(540))
|
||||
tm.assert_index_equal(idx, expected)
|
||||
expected_i8 = date_range('2013-01-01T00:00:00',
|
||||
'2016-01-01T23:59:59', freq=freq,
|
||||
tz='Asia/Tokyo')
|
||||
tm.assert_numpy_array_equal(idx.asi8, expected_i8.asi8)
|
||||
|
||||
# Non ISO 8601 format results in dateutil.tz.tzoffset
|
||||
idx = date_range('2013/1/1 0:00:00-5:00', '2016/1/1 23:59:59-5:00',
|
||||
freq=freq)
|
||||
expected = date_range('2013-01-01T00:00:00', '2016-01-01T23:59:59',
|
||||
freq=freq, tz=pytz.FixedOffset(-300))
|
||||
tm.assert_index_equal(idx, expected)
|
||||
# Unable to use `US/Eastern` because of DST
|
||||
expected_i8 = date_range('2013-01-01T00:00:00',
|
||||
'2016-01-01T23:59:59', freq=freq,
|
||||
tz='America/Lima')
|
||||
tm.assert_numpy_array_equal(idx.asi8, expected_i8.asi8)
|
||||
|
||||
idx = date_range('2013/1/1 0:00:00+9:00',
|
||||
'2016/1/1 23:59:59+09:00', freq=freq)
|
||||
expected = date_range('2013-01-01T00:00:00', '2016-01-01T23:59:59',
|
||||
freq=freq, tz=pytz.FixedOffset(540))
|
||||
tm.assert_index_equal(idx, expected)
|
||||
expected_i8 = date_range('2013-01-01T00:00:00',
|
||||
'2016-01-01T23:59:59', freq=freq,
|
||||
tz='Asia/Tokyo')
|
||||
tm.assert_numpy_array_equal(idx.asi8, expected_i8.asi8)
|
||||
|
||||
def test_constructor_dtype(self):
|
||||
|
||||
# passing a dtype with a tz should localize
|
||||
idx = DatetimeIndex(['2013-01-01', '2013-01-02'],
|
||||
dtype='datetime64[ns, US/Eastern]')
|
||||
expected = DatetimeIndex(['2013-01-01', '2013-01-02']
|
||||
).tz_localize('US/Eastern')
|
||||
tm.assert_index_equal(idx, expected)
|
||||
|
||||
idx = DatetimeIndex(['2013-01-01', '2013-01-02'],
|
||||
tz='US/Eastern')
|
||||
tm.assert_index_equal(idx, expected)
|
||||
|
||||
# if we already have a tz and its not the same, then raise
|
||||
idx = DatetimeIndex(['2013-01-01', '2013-01-02'],
|
||||
dtype='datetime64[ns, US/Eastern]')
|
||||
|
||||
pytest.raises(ValueError,
|
||||
lambda: DatetimeIndex(idx,
|
||||
dtype='datetime64[ns]'))
|
||||
|
||||
# this is effectively trying to convert tz's
|
||||
pytest.raises(TypeError,
|
||||
lambda: DatetimeIndex(idx,
|
||||
dtype='datetime64[ns, CET]'))
|
||||
pytest.raises(ValueError,
|
||||
lambda: DatetimeIndex(
|
||||
idx, tz='CET',
|
||||
dtype='datetime64[ns, US/Eastern]'))
|
||||
result = DatetimeIndex(idx, dtype='datetime64[ns, US/Eastern]')
|
||||
tm.assert_index_equal(idx, result)
|
||||
|
||||
def test_constructor_name(self):
|
||||
idx = DatetimeIndex(start='2000-01-01', periods=1, freq='A',
|
||||
name='TEST')
|
||||
assert idx.name == 'TEST'
|
||||
|
||||
def test_000constructor_resolution(self):
|
||||
# 2252
|
||||
t1 = Timestamp((1352934390 * 1000000000) + 1000000 + 1000 + 1)
|
||||
idx = DatetimeIndex([t1])
|
||||
|
||||
assert idx.nanosecond[0] == t1.nanosecond
|
||||
|
||||
def test_disallow_setting_tz(self):
|
||||
# GH 3746
|
||||
dti = DatetimeIndex(['2010'], tz='UTC')
|
||||
with pytest.raises(AttributeError):
|
||||
dti.tz = pytz.timezone('US/Pacific')
|
||||
|
||||
@pytest.mark.parametrize('tz', [
|
||||
None, 'America/Los_Angeles', pytz.timezone('America/Los_Angeles'),
|
||||
Timestamp('2000', tz='America/Los_Angeles').tz])
|
||||
def test_constructor_start_end_with_tz(self, tz):
|
||||
# GH 18595
|
||||
start = Timestamp('2013-01-01 06:00:00', tz='America/Los_Angeles')
|
||||
end = Timestamp('2013-01-02 06:00:00', tz='America/Los_Angeles')
|
||||
result = DatetimeIndex(freq='D', start=start, end=end, tz=tz)
|
||||
expected = DatetimeIndex(['2013-01-01 06:00:00',
|
||||
'2013-01-02 06:00:00'],
|
||||
tz='America/Los_Angeles')
|
||||
tm.assert_index_equal(result, expected)
|
||||
# Especially assert that the timezone is consistent for pytz
|
||||
assert pytz.timezone('America/Los_Angeles') is result.tz
|
||||
|
||||
@pytest.mark.parametrize('tz', ['US/Pacific', 'US/Eastern', 'Asia/Tokyo'])
|
||||
def test_constructor_with_non_normalized_pytz(self, tz):
|
||||
# GH 18595
|
||||
non_norm_tz = Timestamp('2010', tz=tz).tz
|
||||
result = DatetimeIndex(['2010'], tz=non_norm_tz)
|
||||
assert pytz.timezone(tz) is result.tz
|
||||
|
||||
def test_constructor_timestamp_near_dst(self):
|
||||
# GH 20854
|
||||
ts = [Timestamp('2016-10-30 03:00:00+0300', tz='Europe/Helsinki'),
|
||||
Timestamp('2016-10-30 03:00:00+0200', tz='Europe/Helsinki')]
|
||||
result = DatetimeIndex(ts)
|
||||
expected = DatetimeIndex([ts[0].to_pydatetime(),
|
||||
ts[1].to_pydatetime()])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
|
||||
class TestTimeSeries(object):
|
||||
|
||||
def test_dti_constructor_preserve_dti_freq(self):
|
||||
rng = date_range('1/1/2000', '1/2/2000', freq='5min')
|
||||
|
||||
rng2 = DatetimeIndex(rng)
|
||||
assert rng.freq == rng2.freq
|
||||
|
||||
def test_dti_constructor_years_only(self, tz_naive_fixture):
|
||||
tz = tz_naive_fixture
|
||||
# GH 6961
|
||||
rng1 = date_range('2014', '2015', freq='M', tz=tz)
|
||||
expected1 = date_range('2014-01-31', '2014-12-31', freq='M', tz=tz)
|
||||
|
||||
rng2 = date_range('2014', '2015', freq='MS', tz=tz)
|
||||
expected2 = date_range('2014-01-01', '2015-01-01', freq='MS', tz=tz)
|
||||
|
||||
rng3 = date_range('2014', '2020', freq='A', tz=tz)
|
||||
expected3 = date_range('2014-12-31', '2019-12-31', freq='A', tz=tz)
|
||||
|
||||
rng4 = date_range('2014', '2020', freq='AS', tz=tz)
|
||||
expected4 = date_range('2014-01-01', '2020-01-01', freq='AS', tz=tz)
|
||||
|
||||
for rng, expected in [(rng1, expected1), (rng2, expected2),
|
||||
(rng3, expected3), (rng4, expected4)]:
|
||||
tm.assert_index_equal(rng, expected)
|
||||
|
||||
@pytest.mark.parametrize('dtype', [np.int64, np.int32, np.int16, np.int8])
|
||||
def test_dti_constructor_small_int(self, dtype):
|
||||
# GH 13721
|
||||
exp = DatetimeIndex(['1970-01-01 00:00:00.00000000',
|
||||
'1970-01-01 00:00:00.00000001',
|
||||
'1970-01-01 00:00:00.00000002'])
|
||||
|
||||
arr = np.array([0, 10, 20], dtype=dtype)
|
||||
tm.assert_index_equal(DatetimeIndex(arr), exp)
|
||||
|
||||
def test_ctor_str_intraday(self):
|
||||
rng = DatetimeIndex(['1-1-2000 00:00:01'])
|
||||
assert rng[0].second == 1
|
||||
|
||||
def test_is_(self):
|
||||
dti = DatetimeIndex(start='1/1/2005', end='12/1/2005', freq='M')
|
||||
assert dti.is_(dti)
|
||||
assert dti.is_(dti.view())
|
||||
assert not dti.is_(dti.copy())
|
||||
|
||||
def test_index_cast_datetime64_other_units(self):
|
||||
arr = np.arange(0, 100, 10, dtype=np.int64).view('M8[D]')
|
||||
idx = Index(arr)
|
||||
|
||||
assert (idx.values == conversion.ensure_datetime64ns(arr)).all()
|
||||
|
||||
def test_constructor_int64_nocopy(self):
|
||||
# GH#1624
|
||||
arr = np.arange(1000, dtype=np.int64)
|
||||
index = DatetimeIndex(arr)
|
||||
|
||||
arr[50:100] = -1
|
||||
assert (index.asi8[50:100] == -1).all()
|
||||
|
||||
arr = np.arange(1000, dtype=np.int64)
|
||||
index = DatetimeIndex(arr, copy=True)
|
||||
|
||||
arr[50:100] = -1
|
||||
assert (index.asi8[50:100] != -1).all()
|
||||
|
||||
@pytest.mark.parametrize('freq', ['M', 'Q', 'A', 'D', 'B', 'BH',
|
||||
'T', 'S', 'L', 'U', 'H', 'N', 'C'])
|
||||
def test_from_freq_recreate_from_data(self, freq):
|
||||
org = DatetimeIndex(start='2001/02/01 09:00', freq=freq, periods=1)
|
||||
idx = DatetimeIndex(org, freq=freq)
|
||||
tm.assert_index_equal(idx, org)
|
||||
|
||||
org = DatetimeIndex(start='2001/02/01 09:00', freq=freq,
|
||||
tz='US/Pacific', periods=1)
|
||||
idx = DatetimeIndex(org, freq=freq, tz='US/Pacific')
|
||||
tm.assert_index_equal(idx, org)
|
||||
|
||||
def test_datetimeindex_constructor_misc(self):
|
||||
arr = ['1/1/2005', '1/2/2005', 'Jn 3, 2005', '2005-01-04']
|
||||
pytest.raises(Exception, DatetimeIndex, arr)
|
||||
|
||||
arr = ['1/1/2005', '1/2/2005', '1/3/2005', '2005-01-04']
|
||||
idx1 = DatetimeIndex(arr)
|
||||
|
||||
arr = [datetime(2005, 1, 1), '1/2/2005', '1/3/2005', '2005-01-04']
|
||||
idx2 = DatetimeIndex(arr)
|
||||
|
||||
arr = [Timestamp(datetime(2005, 1, 1)), '1/2/2005', '1/3/2005',
|
||||
'2005-01-04']
|
||||
idx3 = DatetimeIndex(arr)
|
||||
|
||||
arr = np.array(['1/1/2005', '1/2/2005', '1/3/2005',
|
||||
'2005-01-04'], dtype='O')
|
||||
idx4 = DatetimeIndex(arr)
|
||||
|
||||
arr = to_datetime(['1/1/2005', '1/2/2005', '1/3/2005', '2005-01-04'])
|
||||
idx5 = DatetimeIndex(arr)
|
||||
|
||||
arr = to_datetime(['1/1/2005', '1/2/2005', 'Jan 3, 2005', '2005-01-04'
|
||||
])
|
||||
idx6 = DatetimeIndex(arr)
|
||||
|
||||
idx7 = DatetimeIndex(['12/05/2007', '25/01/2008'], dayfirst=True)
|
||||
idx8 = DatetimeIndex(['2007/05/12', '2008/01/25'], dayfirst=False,
|
||||
yearfirst=True)
|
||||
tm.assert_index_equal(idx7, idx8)
|
||||
|
||||
for other in [idx2, idx3, idx4, idx5, idx6]:
|
||||
assert (idx1.values == other.values).all()
|
||||
|
||||
sdate = datetime(1999, 12, 25)
|
||||
edate = datetime(2000, 1, 1)
|
||||
idx = DatetimeIndex(start=sdate, freq='1B', periods=20)
|
||||
assert len(idx) == 20
|
||||
assert idx[0] == sdate + 0 * offsets.BDay()
|
||||
assert idx.freq == 'B'
|
||||
|
||||
idx = DatetimeIndex(end=edate, freq=('D', 5), periods=20)
|
||||
assert len(idx) == 20
|
||||
assert idx[-1] == edate
|
||||
assert idx.freq == '5D'
|
||||
|
||||
idx1 = DatetimeIndex(start=sdate, end=edate, freq='W-SUN')
|
||||
idx2 = DatetimeIndex(start=sdate, end=edate,
|
||||
freq=offsets.Week(weekday=6))
|
||||
assert len(idx1) == len(idx2)
|
||||
assert idx1.freq == idx2.freq
|
||||
|
||||
idx1 = DatetimeIndex(start=sdate, end=edate, freq='QS')
|
||||
idx2 = DatetimeIndex(start=sdate, end=edate,
|
||||
freq=offsets.QuarterBegin(startingMonth=1))
|
||||
assert len(idx1) == len(idx2)
|
||||
assert idx1.freq == idx2.freq
|
||||
|
||||
idx1 = DatetimeIndex(start=sdate, end=edate, freq='BQ')
|
||||
idx2 = DatetimeIndex(start=sdate, end=edate,
|
||||
freq=offsets.BQuarterEnd(startingMonth=12))
|
||||
assert len(idx1) == len(idx2)
|
||||
assert idx1.freq == idx2.freq
|
||||
-763
@@ -1,763 +0,0 @@
|
||||
"""
|
||||
test date_range, bdate_range construction from the convenience range functions
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
import pytz
|
||||
from pytz import timezone
|
||||
from datetime import datetime, timedelta, time
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
import pandas.util._test_decorators as td
|
||||
from pandas import compat
|
||||
from pandas import date_range, bdate_range, offsets, DatetimeIndex, Timestamp
|
||||
from pandas.tseries.offsets import (generate_range, CDay, BDay, DateOffset,
|
||||
MonthEnd, prefix_mapping)
|
||||
|
||||
from pandas.tests.series.common import TestData
|
||||
|
||||
START, END = datetime(2009, 1, 1), datetime(2010, 1, 1)
|
||||
|
||||
|
||||
class TestTimestampEquivDateRange(object):
|
||||
# Older tests in TestTimeSeries constructed their `stamp` objects
|
||||
# using `date_range` instead of the `Timestamp` constructor.
|
||||
# TestTimestampEquivDateRange checks that these are equivalent in the
|
||||
# pertinent cases.
|
||||
|
||||
def test_date_range_timestamp_equiv(self):
|
||||
rng = date_range('20090415', '20090519', tz='US/Eastern')
|
||||
stamp = rng[0]
|
||||
|
||||
ts = Timestamp('20090415', tz='US/Eastern', freq='D')
|
||||
assert ts == stamp
|
||||
|
||||
def test_date_range_timestamp_equiv_dateutil(self):
|
||||
rng = date_range('20090415', '20090519', tz='dateutil/US/Eastern')
|
||||
stamp = rng[0]
|
||||
|
||||
ts = Timestamp('20090415', tz='dateutil/US/Eastern', freq='D')
|
||||
assert ts == stamp
|
||||
|
||||
def test_date_range_timestamp_equiv_explicit_pytz(self):
|
||||
rng = date_range('20090415', '20090519',
|
||||
tz=pytz.timezone('US/Eastern'))
|
||||
stamp = rng[0]
|
||||
|
||||
ts = Timestamp('20090415', tz=pytz.timezone('US/Eastern'), freq='D')
|
||||
assert ts == stamp
|
||||
|
||||
@td.skip_if_windows_python_3
|
||||
def test_date_range_timestamp_equiv_explicit_dateutil(self):
|
||||
from pandas._libs.tslibs.timezones import dateutil_gettz as gettz
|
||||
|
||||
rng = date_range('20090415', '20090519', tz=gettz('US/Eastern'))
|
||||
stamp = rng[0]
|
||||
|
||||
ts = Timestamp('20090415', tz=gettz('US/Eastern'), freq='D')
|
||||
assert ts == stamp
|
||||
|
||||
def test_date_range_timestamp_equiv_from_datetime_instance(self):
|
||||
datetime_instance = datetime(2014, 3, 4)
|
||||
# build a timestamp with a frequency, since then it supports
|
||||
# addition/subtraction of integers
|
||||
timestamp_instance = date_range(datetime_instance, periods=1,
|
||||
freq='D')[0]
|
||||
|
||||
ts = Timestamp(datetime_instance, freq='D')
|
||||
assert ts == timestamp_instance
|
||||
|
||||
def test_date_range_timestamp_equiv_preserve_frequency(self):
|
||||
timestamp_instance = date_range('2014-03-05', periods=1, freq='D')[0]
|
||||
ts = Timestamp('2014-03-05', freq='D')
|
||||
|
||||
assert timestamp_instance == ts
|
||||
|
||||
|
||||
class TestDateRanges(TestData):
|
||||
|
||||
def test_date_range_gen_error(self):
|
||||
rng = date_range('1/1/2000 00:00', '1/1/2000 00:18', freq='5min')
|
||||
assert len(rng) == 4
|
||||
|
||||
@pytest.mark.parametrize("freq", ["AS", "YS"])
|
||||
def test_begin_year_alias(self, freq):
|
||||
# see gh-9313
|
||||
rng = date_range("1/1/2013", "7/1/2017", freq=freq)
|
||||
exp = pd.DatetimeIndex(["2013-01-01", "2014-01-01",
|
||||
"2015-01-01", "2016-01-01",
|
||||
"2017-01-01"], freq=freq)
|
||||
tm.assert_index_equal(rng, exp)
|
||||
|
||||
@pytest.mark.parametrize("freq", ["A", "Y"])
|
||||
def test_end_year_alias(self, freq):
|
||||
# see gh-9313
|
||||
rng = date_range("1/1/2013", "7/1/2017", freq=freq)
|
||||
exp = pd.DatetimeIndex(["2013-12-31", "2014-12-31",
|
||||
"2015-12-31", "2016-12-31"], freq=freq)
|
||||
tm.assert_index_equal(rng, exp)
|
||||
|
||||
@pytest.mark.parametrize("freq", ["BA", "BY"])
|
||||
def test_business_end_year_alias(self, freq):
|
||||
# see gh-9313
|
||||
rng = date_range("1/1/2013", "7/1/2017", freq=freq)
|
||||
exp = pd.DatetimeIndex(["2013-12-31", "2014-12-31",
|
||||
"2015-12-31", "2016-12-30"], freq=freq)
|
||||
tm.assert_index_equal(rng, exp)
|
||||
|
||||
def test_date_range_negative_freq(self):
|
||||
# GH 11018
|
||||
rng = date_range('2011-12-31', freq='-2A', periods=3)
|
||||
exp = pd.DatetimeIndex(['2011-12-31', '2009-12-31',
|
||||
'2007-12-31'], freq='-2A')
|
||||
tm.assert_index_equal(rng, exp)
|
||||
assert rng.freq == '-2A'
|
||||
|
||||
rng = date_range('2011-01-31', freq='-2M', periods=3)
|
||||
exp = pd.DatetimeIndex(['2011-01-31', '2010-11-30',
|
||||
'2010-09-30'], freq='-2M')
|
||||
tm.assert_index_equal(rng, exp)
|
||||
assert rng.freq == '-2M'
|
||||
|
||||
def test_date_range_bms_bug(self):
|
||||
# #1645
|
||||
rng = date_range('1/1/2000', periods=10, freq='BMS')
|
||||
|
||||
ex_first = Timestamp('2000-01-03')
|
||||
assert rng[0] == ex_first
|
||||
|
||||
def test_date_range_normalize(self):
|
||||
snap = datetime.today()
|
||||
n = 50
|
||||
|
||||
rng = date_range(snap, periods=n, normalize=False, freq='2D')
|
||||
|
||||
offset = timedelta(2)
|
||||
values = DatetimeIndex([snap + i * offset for i in range(n)])
|
||||
|
||||
tm.assert_index_equal(rng, values)
|
||||
|
||||
rng = date_range('1/1/2000 08:15', periods=n, normalize=False,
|
||||
freq='B')
|
||||
the_time = time(8, 15)
|
||||
for val in rng:
|
||||
assert val.time() == the_time
|
||||
|
||||
def test_date_range_fy5252(self):
|
||||
dr = date_range(start="2013-01-01", periods=2, freq=offsets.FY5253(
|
||||
startingMonth=1, weekday=3, variation="nearest"))
|
||||
assert dr[0] == Timestamp('2013-01-31')
|
||||
assert dr[1] == Timestamp('2014-01-30')
|
||||
|
||||
def test_date_range_ambiguous_arguments(self):
|
||||
# #2538
|
||||
start = datetime(2011, 1, 1, 5, 3, 40)
|
||||
end = datetime(2011, 1, 1, 8, 9, 40)
|
||||
|
||||
msg = ('Of the four parameters: start, end, periods, and '
|
||||
'freq, exactly three must be specified')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
date_range(start, end, periods=10, freq='s')
|
||||
|
||||
def test_date_range_convenience_periods(self):
|
||||
# GH 20808
|
||||
result = date_range('2018-04-24', '2018-04-27', periods=3)
|
||||
expected = DatetimeIndex(['2018-04-24 00:00:00',
|
||||
'2018-04-25 12:00:00',
|
||||
'2018-04-27 00:00:00'], freq=None)
|
||||
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# Test if spacing remains linear if tz changes to dst in range
|
||||
result = date_range('2018-04-01 01:00:00',
|
||||
'2018-04-01 04:00:00',
|
||||
tz='Australia/Sydney',
|
||||
periods=3)
|
||||
expected = DatetimeIndex([Timestamp('2018-04-01 01:00:00+1100',
|
||||
tz='Australia/Sydney'),
|
||||
Timestamp('2018-04-01 02:00:00+1000',
|
||||
tz='Australia/Sydney'),
|
||||
Timestamp('2018-04-01 04:00:00+1000',
|
||||
tz='Australia/Sydney')])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize('start,end,result_tz', [
|
||||
['20180101', '20180103', 'US/Eastern'],
|
||||
[datetime(2018, 1, 1), datetime(2018, 1, 3), 'US/Eastern'],
|
||||
[Timestamp('20180101'), Timestamp('20180103'), 'US/Eastern'],
|
||||
[Timestamp('20180101', tz='US/Eastern'),
|
||||
Timestamp('20180103', tz='US/Eastern'), 'US/Eastern'],
|
||||
[Timestamp('20180101', tz='US/Eastern'),
|
||||
Timestamp('20180103', tz='US/Eastern'), None]])
|
||||
def test_date_range_linspacing_tz(self, start, end, result_tz):
|
||||
# GH 20983
|
||||
result = date_range(start, end, periods=3, tz=result_tz)
|
||||
expected = date_range('20180101', periods=3, freq='D', tz='US/Eastern')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_date_range_businesshour(self):
|
||||
idx = DatetimeIndex(['2014-07-04 09:00', '2014-07-04 10:00',
|
||||
'2014-07-04 11:00',
|
||||
'2014-07-04 12:00', '2014-07-04 13:00',
|
||||
'2014-07-04 14:00',
|
||||
'2014-07-04 15:00', '2014-07-04 16:00'],
|
||||
freq='BH')
|
||||
rng = date_range('2014-07-04 09:00', '2014-07-04 16:00', freq='BH')
|
||||
tm.assert_index_equal(idx, rng)
|
||||
|
||||
idx = DatetimeIndex(
|
||||
['2014-07-04 16:00', '2014-07-07 09:00'], freq='BH')
|
||||
rng = date_range('2014-07-04 16:00', '2014-07-07 09:00', freq='BH')
|
||||
tm.assert_index_equal(idx, rng)
|
||||
|
||||
idx = DatetimeIndex(['2014-07-04 09:00', '2014-07-04 10:00',
|
||||
'2014-07-04 11:00',
|
||||
'2014-07-04 12:00', '2014-07-04 13:00',
|
||||
'2014-07-04 14:00',
|
||||
'2014-07-04 15:00', '2014-07-04 16:00',
|
||||
'2014-07-07 09:00', '2014-07-07 10:00',
|
||||
'2014-07-07 11:00',
|
||||
'2014-07-07 12:00', '2014-07-07 13:00',
|
||||
'2014-07-07 14:00',
|
||||
'2014-07-07 15:00', '2014-07-07 16:00',
|
||||
'2014-07-08 09:00', '2014-07-08 10:00',
|
||||
'2014-07-08 11:00',
|
||||
'2014-07-08 12:00', '2014-07-08 13:00',
|
||||
'2014-07-08 14:00',
|
||||
'2014-07-08 15:00', '2014-07-08 16:00'],
|
||||
freq='BH')
|
||||
rng = date_range('2014-07-04 09:00', '2014-07-08 16:00', freq='BH')
|
||||
tm.assert_index_equal(idx, rng)
|
||||
|
||||
def test_range_misspecified(self):
|
||||
# GH #1095
|
||||
msg = ('Of the four parameters: start, end, periods, and '
|
||||
'freq, exactly three must be specified')
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
date_range(start='1/1/2000')
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
date_range(end='1/1/2000')
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
date_range(periods=10)
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
date_range(start='1/1/2000', freq='H')
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
date_range(end='1/1/2000', freq='H')
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
date_range(periods=10, freq='H')
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
date_range()
|
||||
|
||||
@pytest.mark.parametrize('f', [compat.long, int])
|
||||
def test_compat_replace(self, f):
|
||||
# https://github.com/statsmodels/statsmodels/issues/3349
|
||||
# replace should take ints/longs for compat
|
||||
result = date_range(Timestamp('1960-04-01 00:00:00', freq='QS-JAN'),
|
||||
periods=f(76), freq='QS-JAN')
|
||||
assert len(result) == 76
|
||||
|
||||
def test_catch_infinite_loop(self):
|
||||
offset = offsets.DateOffset(minute=5)
|
||||
# blow up, don't loop forever
|
||||
pytest.raises(Exception, date_range, datetime(2011, 11, 11),
|
||||
datetime(2011, 11, 12), freq=offset)
|
||||
|
||||
@pytest.mark.parametrize('periods', (1, 2))
|
||||
def test_wom_len(self, periods):
|
||||
# https://github.com/pandas-dev/pandas/issues/20517
|
||||
res = date_range(start='20110101', periods=periods, freq='WOM-1MON')
|
||||
assert len(res) == periods
|
||||
|
||||
def test_construct_over_dst(self):
|
||||
# GH 20854
|
||||
pre_dst = Timestamp('2010-11-07 01:00:00').tz_localize('US/Pacific',
|
||||
ambiguous=True)
|
||||
pst_dst = Timestamp('2010-11-07 01:00:00').tz_localize('US/Pacific',
|
||||
ambiguous=False)
|
||||
expect_data = [Timestamp('2010-11-07 00:00:00', tz='US/Pacific'),
|
||||
pre_dst,
|
||||
pst_dst]
|
||||
expected = DatetimeIndex(expect_data)
|
||||
result = date_range(start='2010-11-7', periods=3,
|
||||
freq='H', tz='US/Pacific')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
|
||||
class TestGenRangeGeneration(object):
|
||||
|
||||
def test_generate(self):
|
||||
rng1 = list(generate_range(START, END, offset=BDay()))
|
||||
rng2 = list(generate_range(START, END, time_rule='B'))
|
||||
assert rng1 == rng2
|
||||
|
||||
def test_generate_cday(self):
|
||||
rng1 = list(generate_range(START, END, offset=CDay()))
|
||||
rng2 = list(generate_range(START, END, time_rule='C'))
|
||||
assert rng1 == rng2
|
||||
|
||||
def test_1(self):
|
||||
rng = list(generate_range(start=datetime(2009, 3, 25), periods=2))
|
||||
expected = [datetime(2009, 3, 25), datetime(2009, 3, 26)]
|
||||
assert rng == expected
|
||||
|
||||
def test_2(self):
|
||||
rng = list(generate_range(start=datetime(2008, 1, 1),
|
||||
end=datetime(2008, 1, 3)))
|
||||
expected = [datetime(2008, 1, 1),
|
||||
datetime(2008, 1, 2),
|
||||
datetime(2008, 1, 3)]
|
||||
assert rng == expected
|
||||
|
||||
def test_3(self):
|
||||
rng = list(generate_range(start=datetime(2008, 1, 5),
|
||||
end=datetime(2008, 1, 6)))
|
||||
expected = []
|
||||
assert rng == expected
|
||||
|
||||
def test_precision_finer_than_offset(self):
|
||||
# GH 9907
|
||||
result1 = DatetimeIndex(start='2015-04-15 00:00:03',
|
||||
end='2016-04-22 00:00:00', freq='Q')
|
||||
result2 = DatetimeIndex(start='2015-04-15 00:00:03',
|
||||
end='2015-06-22 00:00:04', freq='W')
|
||||
expected1_list = ['2015-06-30 00:00:03', '2015-09-30 00:00:03',
|
||||
'2015-12-31 00:00:03', '2016-03-31 00:00:03']
|
||||
expected2_list = ['2015-04-19 00:00:03', '2015-04-26 00:00:03',
|
||||
'2015-05-03 00:00:03', '2015-05-10 00:00:03',
|
||||
'2015-05-17 00:00:03', '2015-05-24 00:00:03',
|
||||
'2015-05-31 00:00:03', '2015-06-07 00:00:03',
|
||||
'2015-06-14 00:00:03', '2015-06-21 00:00:03']
|
||||
expected1 = DatetimeIndex(expected1_list, dtype='datetime64[ns]',
|
||||
freq='Q-DEC', tz=None)
|
||||
expected2 = DatetimeIndex(expected2_list, dtype='datetime64[ns]',
|
||||
freq='W-SUN', tz=None)
|
||||
tm.assert_index_equal(result1, expected1)
|
||||
tm.assert_index_equal(result2, expected2)
|
||||
|
||||
dt1, dt2 = '2017-01-01', '2017-01-01'
|
||||
tz1, tz2 = 'US/Eastern', 'Europe/London'
|
||||
|
||||
@pytest.mark.parametrize("start,end", [
|
||||
(pd.Timestamp(dt1, tz=tz1), pd.Timestamp(dt2)),
|
||||
(pd.Timestamp(dt1), pd.Timestamp(dt2, tz=tz2)),
|
||||
(pd.Timestamp(dt1, tz=tz1), pd.Timestamp(dt2, tz=tz2)),
|
||||
(pd.Timestamp(dt1, tz=tz2), pd.Timestamp(dt2, tz=tz1))
|
||||
])
|
||||
def test_mismatching_tz_raises_err(self, start, end):
|
||||
# issue 18488
|
||||
with pytest.raises(TypeError):
|
||||
pd.date_range(start, end)
|
||||
with pytest.raises(TypeError):
|
||||
pd.DatetimeIndex(start, end, freq=BDay())
|
||||
|
||||
|
||||
class TestBusinessDateRange(object):
|
||||
|
||||
def test_constructor(self):
|
||||
bdate_range(START, END, freq=BDay())
|
||||
bdate_range(START, periods=20, freq=BDay())
|
||||
bdate_range(end=START, periods=20, freq=BDay())
|
||||
|
||||
msg = 'periods must be a number, got B'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
date_range('2011-1-1', '2012-1-1', 'B')
|
||||
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
bdate_range('2011-1-1', '2012-1-1', 'B')
|
||||
|
||||
msg = 'freq must be specified for bdate_range; use date_range instead'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
bdate_range(START, END, periods=10, freq=None)
|
||||
|
||||
def test_naive_aware_conflicts(self):
|
||||
naive = bdate_range(START, END, freq=BDay(), tz=None)
|
||||
aware = bdate_range(START, END, freq=BDay(), tz="Asia/Hong_Kong")
|
||||
|
||||
msg = 'tz-naive.*tz-aware'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
naive.join(aware)
|
||||
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
aware.join(naive)
|
||||
|
||||
def test_cached_range(self):
|
||||
DatetimeIndex._cached_range(START, END, freq=BDay())
|
||||
DatetimeIndex._cached_range(START, periods=20, freq=BDay())
|
||||
DatetimeIndex._cached_range(end=START, periods=20, freq=BDay())
|
||||
|
||||
with tm.assert_raises_regex(TypeError, "freq"):
|
||||
DatetimeIndex._cached_range(START, END)
|
||||
|
||||
with tm.assert_raises_regex(TypeError, "specify period"):
|
||||
DatetimeIndex._cached_range(START, freq=BDay())
|
||||
|
||||
with tm.assert_raises_regex(TypeError, "specify period"):
|
||||
DatetimeIndex._cached_range(end=END, freq=BDay())
|
||||
|
||||
with tm.assert_raises_regex(TypeError, "start or end"):
|
||||
DatetimeIndex._cached_range(periods=20, freq=BDay())
|
||||
|
||||
def test_cached_range_bug(self):
|
||||
rng = date_range('2010-09-01 05:00:00', periods=50,
|
||||
freq=DateOffset(hours=6))
|
||||
assert len(rng) == 50
|
||||
assert rng[0] == datetime(2010, 9, 1, 5)
|
||||
|
||||
def test_timezone_comparaison_bug(self):
|
||||
# smoke test
|
||||
start = Timestamp('20130220 10:00', tz='US/Eastern')
|
||||
result = date_range(start, periods=2, tz='US/Eastern')
|
||||
assert len(result) == 2
|
||||
|
||||
def test_timezone_comparaison_assert(self):
|
||||
start = Timestamp('20130220 10:00', tz='US/Eastern')
|
||||
msg = 'Inferred time zone not equal to passed time zone'
|
||||
with tm.assert_raises_regex(AssertionError, msg):
|
||||
date_range(start, periods=2, tz='Europe/Berlin')
|
||||
|
||||
def test_misc(self):
|
||||
end = datetime(2009, 5, 13)
|
||||
dr = bdate_range(end=end, periods=20)
|
||||
firstDate = end - 19 * BDay()
|
||||
|
||||
assert len(dr) == 20
|
||||
assert dr[0] == firstDate
|
||||
assert dr[-1] == end
|
||||
|
||||
def test_date_parse_failure(self):
|
||||
badly_formed_date = '2007/100/1'
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
Timestamp(badly_formed_date)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
bdate_range(start=badly_formed_date, periods=10)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
bdate_range(end=badly_formed_date, periods=10)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
bdate_range(badly_formed_date, badly_formed_date)
|
||||
|
||||
def test_daterange_bug_456(self):
|
||||
# GH #456
|
||||
rng1 = bdate_range('12/5/2011', '12/5/2011')
|
||||
rng2 = bdate_range('12/2/2011', '12/5/2011')
|
||||
rng2.freq = BDay()
|
||||
|
||||
result = rng1.union(rng2)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
|
||||
def test_error_with_zero_monthends(self):
|
||||
msg = r'Offset <0 \* MonthEnds> did not increment date'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
date_range('1/1/2000', '1/1/2001', freq=MonthEnd(0))
|
||||
|
||||
def test_range_bug(self):
|
||||
# GH #770
|
||||
offset = DateOffset(months=3)
|
||||
result = date_range("2011-1-1", "2012-1-31", freq=offset)
|
||||
|
||||
start = datetime(2011, 1, 1)
|
||||
expected = DatetimeIndex([start + i * offset for i in range(5)])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_range_tz_pytz(self):
|
||||
# see gh-2906
|
||||
tz = timezone('US/Eastern')
|
||||
start = tz.localize(datetime(2011, 1, 1))
|
||||
end = tz.localize(datetime(2011, 1, 3))
|
||||
|
||||
dr = date_range(start=start, periods=3)
|
||||
assert dr.tz.zone == tz.zone
|
||||
assert dr[0] == start
|
||||
assert dr[2] == end
|
||||
|
||||
dr = date_range(end=end, periods=3)
|
||||
assert dr.tz.zone == tz.zone
|
||||
assert dr[0] == start
|
||||
assert dr[2] == end
|
||||
|
||||
dr = date_range(start=start, end=end)
|
||||
assert dr.tz.zone == tz.zone
|
||||
assert dr[0] == start
|
||||
assert dr[2] == end
|
||||
|
||||
def test_range_tz_dst_straddle_pytz(self):
|
||||
tz = timezone('US/Eastern')
|
||||
dates = [(tz.localize(datetime(2014, 3, 6)),
|
||||
tz.localize(datetime(2014, 3, 12))),
|
||||
(tz.localize(datetime(2013, 11, 1)),
|
||||
tz.localize(datetime(2013, 11, 6)))]
|
||||
for (start, end) in dates:
|
||||
dr = date_range(start, end, freq='D')
|
||||
assert dr[0] == start
|
||||
assert dr[-1] == end
|
||||
assert np.all(dr.hour == 0)
|
||||
|
||||
dr = date_range(start, end, freq='D', tz='US/Eastern')
|
||||
assert dr[0] == start
|
||||
assert dr[-1] == end
|
||||
assert np.all(dr.hour == 0)
|
||||
|
||||
dr = date_range(start.replace(tzinfo=None), end.replace(
|
||||
tzinfo=None), freq='D', tz='US/Eastern')
|
||||
assert dr[0] == start
|
||||
assert dr[-1] == end
|
||||
assert np.all(dr.hour == 0)
|
||||
|
||||
def test_range_tz_dateutil(self):
|
||||
# see gh-2906
|
||||
|
||||
# Use maybe_get_tz to fix filename in tz under dateutil.
|
||||
from pandas._libs.tslibs.timezones import maybe_get_tz
|
||||
tz = lambda x: maybe_get_tz('dateutil/' + x)
|
||||
|
||||
start = datetime(2011, 1, 1, tzinfo=tz('US/Eastern'))
|
||||
end = datetime(2011, 1, 3, tzinfo=tz('US/Eastern'))
|
||||
|
||||
dr = date_range(start=start, periods=3)
|
||||
assert dr.tz == tz('US/Eastern')
|
||||
assert dr[0] == start
|
||||
assert dr[2] == end
|
||||
|
||||
dr = date_range(end=end, periods=3)
|
||||
assert dr.tz == tz('US/Eastern')
|
||||
assert dr[0] == start
|
||||
assert dr[2] == end
|
||||
|
||||
dr = date_range(start=start, end=end)
|
||||
assert dr.tz == tz('US/Eastern')
|
||||
assert dr[0] == start
|
||||
assert dr[2] == end
|
||||
|
||||
@pytest.mark.parametrize('freq', ["1D", "3D", "2M", "7W", "3H", "A"])
|
||||
def test_range_closed(self, freq):
|
||||
begin = datetime(2011, 1, 1)
|
||||
end = datetime(2014, 1, 1)
|
||||
|
||||
closed = date_range(begin, end, closed=None, freq=freq)
|
||||
left = date_range(begin, end, closed="left", freq=freq)
|
||||
right = date_range(begin, end, closed="right", freq=freq)
|
||||
expected_left = left
|
||||
expected_right = right
|
||||
|
||||
if end == closed[-1]:
|
||||
expected_left = closed[:-1]
|
||||
if begin == closed[0]:
|
||||
expected_right = closed[1:]
|
||||
|
||||
tm.assert_index_equal(expected_left, left)
|
||||
tm.assert_index_equal(expected_right, right)
|
||||
|
||||
def test_range_closed_with_tz_aware_start_end(self):
|
||||
# GH12409, GH12684
|
||||
begin = Timestamp('2011/1/1', tz='US/Eastern')
|
||||
end = Timestamp('2014/1/1', tz='US/Eastern')
|
||||
|
||||
for freq in ["1D", "3D", "2M", "7W", "3H", "A"]:
|
||||
closed = date_range(begin, end, closed=None, freq=freq)
|
||||
left = date_range(begin, end, closed="left", freq=freq)
|
||||
right = date_range(begin, end, closed="right", freq=freq)
|
||||
expected_left = left
|
||||
expected_right = right
|
||||
|
||||
if end == closed[-1]:
|
||||
expected_left = closed[:-1]
|
||||
if begin == closed[0]:
|
||||
expected_right = closed[1:]
|
||||
|
||||
tm.assert_index_equal(expected_left, left)
|
||||
tm.assert_index_equal(expected_right, right)
|
||||
|
||||
begin = Timestamp('2011/1/1')
|
||||
end = Timestamp('2014/1/1')
|
||||
begintz = Timestamp('2011/1/1', tz='US/Eastern')
|
||||
endtz = Timestamp('2014/1/1', tz='US/Eastern')
|
||||
|
||||
for freq in ["1D", "3D", "2M", "7W", "3H", "A"]:
|
||||
closed = date_range(begin, end, closed=None, freq=freq,
|
||||
tz='US/Eastern')
|
||||
left = date_range(begin, end, closed="left", freq=freq,
|
||||
tz='US/Eastern')
|
||||
right = date_range(begin, end, closed="right", freq=freq,
|
||||
tz='US/Eastern')
|
||||
expected_left = left
|
||||
expected_right = right
|
||||
|
||||
if endtz == closed[-1]:
|
||||
expected_left = closed[:-1]
|
||||
if begintz == closed[0]:
|
||||
expected_right = closed[1:]
|
||||
|
||||
tm.assert_index_equal(expected_left, left)
|
||||
tm.assert_index_equal(expected_right, right)
|
||||
|
||||
@pytest.mark.parametrize('closed', ['right', 'left', None])
|
||||
def test_range_closed_boundary(self, closed):
|
||||
# GH#11804
|
||||
right_boundary = date_range('2015-09-12', '2015-12-01',
|
||||
freq='QS-MAR', closed=closed)
|
||||
left_boundary = date_range('2015-09-01', '2015-09-12',
|
||||
freq='QS-MAR', closed=closed)
|
||||
both_boundary = date_range('2015-09-01', '2015-12-01',
|
||||
freq='QS-MAR', closed=closed)
|
||||
expected_right = expected_left = expected_both = both_boundary
|
||||
|
||||
if closed == 'right':
|
||||
expected_left = both_boundary[1:]
|
||||
if closed == 'left':
|
||||
expected_right = both_boundary[:-1]
|
||||
if closed is None:
|
||||
expected_right = both_boundary[1:]
|
||||
expected_left = both_boundary[:-1]
|
||||
|
||||
tm.assert_index_equal(right_boundary, expected_right)
|
||||
tm.assert_index_equal(left_boundary, expected_left)
|
||||
tm.assert_index_equal(both_boundary, expected_both)
|
||||
|
||||
def test_years_only(self):
|
||||
# GH 6961
|
||||
dr = date_range('2014', '2015', freq='M')
|
||||
assert dr[0] == datetime(2014, 1, 31)
|
||||
assert dr[-1] == datetime(2014, 12, 31)
|
||||
|
||||
def test_freq_divides_end_in_nanos(self):
|
||||
# GH 10885
|
||||
result_1 = date_range('2005-01-12 10:00', '2005-01-12 16:00',
|
||||
freq='345min')
|
||||
result_2 = date_range('2005-01-13 10:00', '2005-01-13 16:00',
|
||||
freq='345min')
|
||||
expected_1 = DatetimeIndex(['2005-01-12 10:00:00',
|
||||
'2005-01-12 15:45:00'],
|
||||
dtype='datetime64[ns]', freq='345T',
|
||||
tz=None)
|
||||
expected_2 = DatetimeIndex(['2005-01-13 10:00:00',
|
||||
'2005-01-13 15:45:00'],
|
||||
dtype='datetime64[ns]', freq='345T',
|
||||
tz=None)
|
||||
tm.assert_index_equal(result_1, expected_1)
|
||||
tm.assert_index_equal(result_2, expected_2)
|
||||
|
||||
|
||||
class TestCustomDateRange(object):
|
||||
|
||||
def test_constructor(self):
|
||||
bdate_range(START, END, freq=CDay())
|
||||
bdate_range(START, periods=20, freq=CDay())
|
||||
bdate_range(end=START, periods=20, freq=CDay())
|
||||
|
||||
msg = 'periods must be a number, got C'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
date_range('2011-1-1', '2012-1-1', 'C')
|
||||
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
bdate_range('2011-1-1', '2012-1-1', 'C')
|
||||
|
||||
def test_cached_range(self):
|
||||
DatetimeIndex._cached_range(START, END, freq=CDay())
|
||||
DatetimeIndex._cached_range(START, periods=20,
|
||||
freq=CDay())
|
||||
DatetimeIndex._cached_range(end=START, periods=20,
|
||||
freq=CDay())
|
||||
|
||||
# with pytest.raises(TypeError):
|
||||
with tm.assert_raises_regex(TypeError, "freq"):
|
||||
DatetimeIndex._cached_range(START, END)
|
||||
|
||||
# with pytest.raises(TypeError):
|
||||
with tm.assert_raises_regex(TypeError, "specify period"):
|
||||
DatetimeIndex._cached_range(START, freq=CDay())
|
||||
|
||||
# with pytest.raises(TypeError):
|
||||
with tm.assert_raises_regex(TypeError, "specify period"):
|
||||
DatetimeIndex._cached_range(end=END, freq=CDay())
|
||||
|
||||
# with pytest.raises(TypeError):
|
||||
with tm.assert_raises_regex(TypeError, "start or end"):
|
||||
DatetimeIndex._cached_range(periods=20, freq=CDay())
|
||||
|
||||
def test_misc(self):
|
||||
end = datetime(2009, 5, 13)
|
||||
dr = bdate_range(end=end, periods=20, freq='C')
|
||||
firstDate = end - 19 * CDay()
|
||||
|
||||
assert len(dr) == 20
|
||||
assert dr[0] == firstDate
|
||||
assert dr[-1] == end
|
||||
|
||||
def test_daterange_bug_456(self):
|
||||
# GH #456
|
||||
rng1 = bdate_range('12/5/2011', '12/5/2011', freq='C')
|
||||
rng2 = bdate_range('12/2/2011', '12/5/2011', freq='C')
|
||||
rng2.freq = CDay()
|
||||
|
||||
result = rng1.union(rng2)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
|
||||
def test_cdaterange(self):
|
||||
result = bdate_range('2013-05-01', periods=3, freq='C')
|
||||
expected = DatetimeIndex(['2013-05-01', '2013-05-02', '2013-05-03'])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_cdaterange_weekmask(self):
|
||||
result = bdate_range('2013-05-01', periods=3, freq='C',
|
||||
weekmask='Sun Mon Tue Wed Thu')
|
||||
expected = DatetimeIndex(['2013-05-01', '2013-05-02', '2013-05-05'])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# raise with non-custom freq
|
||||
msg = ('a custom frequency string is required when holidays or '
|
||||
'weekmask are passed, got frequency B')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
bdate_range('2013-05-01', periods=3,
|
||||
weekmask='Sun Mon Tue Wed Thu')
|
||||
|
||||
def test_cdaterange_holidays(self):
|
||||
result = bdate_range('2013-05-01', periods=3, freq='C',
|
||||
holidays=['2013-05-01'])
|
||||
expected = DatetimeIndex(['2013-05-02', '2013-05-03', '2013-05-06'])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# raise with non-custom freq
|
||||
msg = ('a custom frequency string is required when holidays or '
|
||||
'weekmask are passed, got frequency B')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
bdate_range('2013-05-01', periods=3, holidays=['2013-05-01'])
|
||||
|
||||
def test_cdaterange_weekmask_and_holidays(self):
|
||||
result = bdate_range('2013-05-01', periods=3, freq='C',
|
||||
weekmask='Sun Mon Tue Wed Thu',
|
||||
holidays=['2013-05-01'])
|
||||
expected = DatetimeIndex(['2013-05-02', '2013-05-05', '2013-05-06'])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# raise with non-custom freq
|
||||
msg = ('a custom frequency string is required when holidays or '
|
||||
'weekmask are passed, got frequency B')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
bdate_range('2013-05-01', periods=3,
|
||||
weekmask='Sun Mon Tue Wed Thu',
|
||||
holidays=['2013-05-01'])
|
||||
|
||||
@pytest.mark.parametrize('freq', [freq for freq in prefix_mapping
|
||||
if freq.startswith('C')])
|
||||
def test_all_custom_freq(self, freq):
|
||||
# should not raise
|
||||
bdate_range(START, END, freq=freq, weekmask='Mon Wed Fri',
|
||||
holidays=['2009-03-14'])
|
||||
|
||||
bad_freq = freq + 'FOO'
|
||||
msg = 'invalid custom frequency string: {freq}'
|
||||
with tm.assert_raises_regex(ValueError, msg.format(freq=bad_freq)):
|
||||
bdate_range(START, END, freq=bad_freq)
|
||||
-385
@@ -1,385 +0,0 @@
|
||||
import warnings
|
||||
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
from datetime import date
|
||||
|
||||
import dateutil
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
from pandas.compat import lrange
|
||||
from pandas import (DatetimeIndex, Index, date_range, DataFrame,
|
||||
Timestamp, offsets)
|
||||
|
||||
from pandas.util.testing import assert_almost_equal
|
||||
|
||||
randn = np.random.randn
|
||||
|
||||
|
||||
class TestDatetimeIndex(object):
|
||||
|
||||
def test_roundtrip_pickle_with_tz(self):
|
||||
|
||||
# GH 8367
|
||||
# round-trip of timezone
|
||||
index = date_range('20130101', periods=3, tz='US/Eastern', name='foo')
|
||||
unpickled = tm.round_trip_pickle(index)
|
||||
tm.assert_index_equal(index, unpickled)
|
||||
|
||||
def test_reindex_preserves_tz_if_target_is_empty_list_or_array(self):
|
||||
# GH7774
|
||||
index = date_range('20130101', periods=3, tz='US/Eastern')
|
||||
assert str(index.reindex([])[0].tz) == 'US/Eastern'
|
||||
assert str(index.reindex(np.array([]))[0].tz) == 'US/Eastern'
|
||||
|
||||
def test_time_loc(self): # GH8667
|
||||
from datetime import time
|
||||
from pandas._libs.index import _SIZE_CUTOFF
|
||||
|
||||
ns = _SIZE_CUTOFF + np.array([-100, 100], dtype=np.int64)
|
||||
key = time(15, 11, 30)
|
||||
start = key.hour * 3600 + key.minute * 60 + key.second
|
||||
step = 24 * 3600
|
||||
|
||||
for n in ns:
|
||||
idx = pd.date_range('2014-11-26', periods=n, freq='S')
|
||||
ts = pd.Series(np.random.randn(n), index=idx)
|
||||
i = np.arange(start, n, step)
|
||||
|
||||
tm.assert_numpy_array_equal(ts.index.get_loc(key), i,
|
||||
check_dtype=False)
|
||||
tm.assert_series_equal(ts[key], ts.iloc[i])
|
||||
|
||||
left, right = ts.copy(), ts.copy()
|
||||
left[key] *= -10
|
||||
right.iloc[i] *= -10
|
||||
tm.assert_series_equal(left, right)
|
||||
|
||||
def test_time_overflow_for_32bit_machines(self):
|
||||
# GH8943. On some machines NumPy defaults to np.int32 (for example,
|
||||
# 32-bit Linux machines). In the function _generate_regular_range
|
||||
# found in tseries/index.py, `periods` gets multiplied by `strides`
|
||||
# (which has value 1e9) and since the max value for np.int32 is ~2e9,
|
||||
# and since those machines won't promote np.int32 to np.int64, we get
|
||||
# overflow.
|
||||
periods = np.int_(1000)
|
||||
|
||||
idx1 = pd.date_range(start='2000', periods=periods, freq='S')
|
||||
assert len(idx1) == periods
|
||||
|
||||
idx2 = pd.date_range(end='2000', periods=periods, freq='S')
|
||||
assert len(idx2) == periods
|
||||
|
||||
def test_nat(self):
|
||||
assert DatetimeIndex([np.nan])[0] is pd.NaT
|
||||
|
||||
def test_week_of_month_frequency(self):
|
||||
# GH 5348: "ValueError: Could not evaluate WOM-1SUN" shouldn't raise
|
||||
d1 = date(2002, 9, 1)
|
||||
d2 = date(2013, 10, 27)
|
||||
d3 = date(2012, 9, 30)
|
||||
idx1 = DatetimeIndex([d1, d2])
|
||||
idx2 = DatetimeIndex([d3])
|
||||
result_append = idx1.append(idx2)
|
||||
expected = DatetimeIndex([d1, d2, d3])
|
||||
tm.assert_index_equal(result_append, expected)
|
||||
result_union = idx1.union(idx2)
|
||||
expected = DatetimeIndex([d1, d3, d2])
|
||||
tm.assert_index_equal(result_union, expected)
|
||||
|
||||
# GH 5115
|
||||
result = date_range("2013-1-1", periods=4, freq='WOM-1SAT')
|
||||
dates = ['2013-01-05', '2013-02-02', '2013-03-02', '2013-04-06']
|
||||
expected = DatetimeIndex(dates, freq='WOM-1SAT')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_hash_error(self):
|
||||
index = date_range('20010101', periods=10)
|
||||
with tm.assert_raises_regex(TypeError, "unhashable type: %r" %
|
||||
type(index).__name__):
|
||||
hash(index)
|
||||
|
||||
def test_stringified_slice_with_tz(self):
|
||||
# GH2658
|
||||
import datetime
|
||||
start = datetime.datetime.now()
|
||||
idx = DatetimeIndex(start=start, freq="1d", periods=10)
|
||||
df = DataFrame(lrange(10), index=idx)
|
||||
df["2013-01-14 23:44:34.437768-05:00":] # no exception here
|
||||
|
||||
def test_append_join_nondatetimeindex(self):
|
||||
rng = date_range('1/1/2000', periods=10)
|
||||
idx = Index(['a', 'b', 'c', 'd'])
|
||||
|
||||
result = rng.append(idx)
|
||||
assert isinstance(result[0], Timestamp)
|
||||
|
||||
# it works
|
||||
rng.join(idx, how='outer')
|
||||
|
||||
def test_map(self):
|
||||
rng = date_range('1/1/2000', periods=10)
|
||||
|
||||
f = lambda x: x.strftime('%Y%m%d')
|
||||
result = rng.map(f)
|
||||
exp = Index([f(x) for x in rng], dtype='<U8')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
def test_iteration_preserves_tz(self):
|
||||
# see gh-8890
|
||||
index = date_range("2012-01-01", periods=3, freq='H', tz='US/Eastern')
|
||||
|
||||
for i, ts in enumerate(index):
|
||||
result = ts
|
||||
expected = index[i]
|
||||
assert result == expected
|
||||
|
||||
index = date_range("2012-01-01", periods=3, freq='H',
|
||||
tz=dateutil.tz.tzoffset(None, -28800))
|
||||
|
||||
for i, ts in enumerate(index):
|
||||
result = ts
|
||||
expected = index[i]
|
||||
assert result._repr_base == expected._repr_base
|
||||
assert result == expected
|
||||
|
||||
# 9100
|
||||
index = pd.DatetimeIndex(['2014-12-01 03:32:39.987000-08:00',
|
||||
'2014-12-01 04:12:34.987000-08:00'])
|
||||
for i, ts in enumerate(index):
|
||||
result = ts
|
||||
expected = index[i]
|
||||
assert result._repr_base == expected._repr_base
|
||||
assert result == expected
|
||||
|
||||
@pytest.mark.parametrize('periods', [0, 9999, 10000, 10001])
|
||||
def test_iteration_over_chunksize(self, periods):
|
||||
# GH21012
|
||||
|
||||
index = date_range('2000-01-01 00:00:00', periods=periods, freq='min')
|
||||
num = 0
|
||||
for stamp in index:
|
||||
assert index[num] == stamp
|
||||
num += 1
|
||||
assert num == len(index)
|
||||
|
||||
def test_misc_coverage(self):
|
||||
rng = date_range('1/1/2000', periods=5)
|
||||
result = rng.groupby(rng.day)
|
||||
assert isinstance(list(result.values())[0][0], Timestamp)
|
||||
|
||||
idx = DatetimeIndex(['2000-01-03', '2000-01-01', '2000-01-02'])
|
||||
assert not idx.equals(list(idx))
|
||||
|
||||
non_datetime = Index(list('abc'))
|
||||
assert not idx.equals(list(non_datetime))
|
||||
|
||||
def test_string_index_series_name_converted(self):
|
||||
# #1644
|
||||
df = DataFrame(np.random.randn(10, 4),
|
||||
index=date_range('1/1/2000', periods=10))
|
||||
|
||||
result = df.loc['1/3/2000']
|
||||
assert result.name == df.index[2]
|
||||
|
||||
result = df.T['1/3/2000']
|
||||
assert result.name == df.index[2]
|
||||
|
||||
def test_get_duplicates(self):
|
||||
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-02',
|
||||
'2000-01-03', '2000-01-03', '2000-01-04'])
|
||||
|
||||
with warnings.catch_warnings(record=True):
|
||||
# Deprecated - see GH20239
|
||||
result = idx.get_duplicates()
|
||||
|
||||
ex = DatetimeIndex(['2000-01-02', '2000-01-03'])
|
||||
tm.assert_index_equal(result, ex)
|
||||
|
||||
def test_argmin_argmax(self):
|
||||
idx = DatetimeIndex(['2000-01-04', '2000-01-01', '2000-01-02'])
|
||||
assert idx.argmin() == 1
|
||||
assert idx.argmax() == 0
|
||||
|
||||
def test_sort_values(self):
|
||||
idx = DatetimeIndex(['2000-01-04', '2000-01-01', '2000-01-02'])
|
||||
|
||||
ordered = idx.sort_values()
|
||||
assert ordered.is_monotonic
|
||||
|
||||
ordered = idx.sort_values(ascending=False)
|
||||
assert ordered[::-1].is_monotonic
|
||||
|
||||
ordered, dexer = idx.sort_values(return_indexer=True)
|
||||
assert ordered.is_monotonic
|
||||
tm.assert_numpy_array_equal(dexer, np.array([1, 2, 0], dtype=np.intp))
|
||||
|
||||
ordered, dexer = idx.sort_values(return_indexer=True, ascending=False)
|
||||
assert ordered[::-1].is_monotonic
|
||||
tm.assert_numpy_array_equal(dexer, np.array([0, 2, 1], dtype=np.intp))
|
||||
|
||||
def test_map_bug_1677(self):
|
||||
index = DatetimeIndex(['2012-04-25 09:30:00.393000'])
|
||||
f = index.asof
|
||||
|
||||
result = index.map(f)
|
||||
expected = Index([f(index[0])])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_groupby_function_tuple_1677(self):
|
||||
df = DataFrame(np.random.rand(100),
|
||||
index=date_range("1/1/2000", periods=100))
|
||||
monthly_group = df.groupby(lambda x: (x.year, x.month))
|
||||
|
||||
result = monthly_group.mean()
|
||||
assert isinstance(result.index[0], tuple)
|
||||
|
||||
def test_append_numpy_bug_1681(self):
|
||||
# another datetime64 bug
|
||||
dr = date_range('2011/1/1', '2012/1/1', freq='W-FRI')
|
||||
a = DataFrame()
|
||||
c = DataFrame({'A': 'foo', 'B': dr}, index=dr)
|
||||
|
||||
result = a.append(c)
|
||||
assert (result['B'] == dr).all()
|
||||
|
||||
def test_isin(self):
|
||||
index = tm.makeDateIndex(4)
|
||||
result = index.isin(index)
|
||||
assert result.all()
|
||||
|
||||
result = index.isin(list(index))
|
||||
assert result.all()
|
||||
|
||||
assert_almost_equal(index.isin([index[2], 5]),
|
||||
np.array([False, False, True, False]))
|
||||
|
||||
def test_does_not_convert_mixed_integer(self):
|
||||
df = tm.makeCustomDataframe(10, 10,
|
||||
data_gen_f=lambda *args, **kwargs: randn(),
|
||||
r_idx_type='i', c_idx_type='dt')
|
||||
cols = df.columns.join(df.index, how='outer')
|
||||
joined = cols.join(df.columns)
|
||||
assert cols.dtype == np.dtype('O')
|
||||
assert cols.dtype == joined.dtype
|
||||
tm.assert_numpy_array_equal(cols.values, joined.values)
|
||||
|
||||
def test_join_self(self, join_type):
|
||||
index = date_range('1/1/2000', periods=10)
|
||||
joined = index.join(index, how=join_type)
|
||||
assert index is joined
|
||||
|
||||
def assert_index_parameters(self, index):
|
||||
assert index.freq == '40960N'
|
||||
assert index.inferred_freq == '40960N'
|
||||
|
||||
def test_ns_index(self):
|
||||
nsamples = 400
|
||||
ns = int(1e9 / 24414)
|
||||
dtstart = np.datetime64('2012-09-20T00:00:00')
|
||||
|
||||
dt = dtstart + np.arange(nsamples) * np.timedelta64(ns, 'ns')
|
||||
freq = ns * offsets.Nano()
|
||||
index = pd.DatetimeIndex(dt, freq=freq, name='time')
|
||||
self.assert_index_parameters(index)
|
||||
|
||||
new_index = pd.DatetimeIndex(start=index[0], end=index[-1],
|
||||
freq=index.freq)
|
||||
self.assert_index_parameters(new_index)
|
||||
|
||||
def test_join_with_period_index(self, join_type):
|
||||
df = tm.makeCustomDataframe(
|
||||
10, 10, data_gen_f=lambda *args: np.random.randint(2),
|
||||
c_idx_type='p', r_idx_type='dt')
|
||||
s = df.iloc[:5, 0]
|
||||
|
||||
with tm.assert_raises_regex(ValueError,
|
||||
'can only call with other '
|
||||
'PeriodIndex-ed objects'):
|
||||
df.columns.join(s.index, how=join_type)
|
||||
|
||||
def test_factorize(self):
|
||||
idx1 = DatetimeIndex(['2014-01', '2014-01', '2014-02', '2014-02',
|
||||
'2014-03', '2014-03'])
|
||||
|
||||
exp_arr = np.array([0, 0, 1, 1, 2, 2], dtype=np.intp)
|
||||
exp_idx = DatetimeIndex(['2014-01', '2014-02', '2014-03'])
|
||||
|
||||
arr, idx = idx1.factorize()
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, exp_idx)
|
||||
|
||||
arr, idx = idx1.factorize(sort=True)
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, exp_idx)
|
||||
|
||||
# tz must be preserved
|
||||
idx1 = idx1.tz_localize('Asia/Tokyo')
|
||||
exp_idx = exp_idx.tz_localize('Asia/Tokyo')
|
||||
|
||||
arr, idx = idx1.factorize()
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, exp_idx)
|
||||
|
||||
idx2 = pd.DatetimeIndex(['2014-03', '2014-03', '2014-02', '2014-01',
|
||||
'2014-03', '2014-01'])
|
||||
|
||||
exp_arr = np.array([2, 2, 1, 0, 2, 0], dtype=np.intp)
|
||||
exp_idx = DatetimeIndex(['2014-01', '2014-02', '2014-03'])
|
||||
arr, idx = idx2.factorize(sort=True)
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, exp_idx)
|
||||
|
||||
exp_arr = np.array([0, 0, 1, 2, 0, 2], dtype=np.intp)
|
||||
exp_idx = DatetimeIndex(['2014-03', '2014-02', '2014-01'])
|
||||
arr, idx = idx2.factorize()
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, exp_idx)
|
||||
|
||||
# freq must be preserved
|
||||
idx3 = date_range('2000-01', periods=4, freq='M', tz='Asia/Tokyo')
|
||||
exp_arr = np.array([0, 1, 2, 3], dtype=np.intp)
|
||||
arr, idx = idx3.factorize()
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, idx3)
|
||||
|
||||
def test_factorize_tz(self, tz_naive_fixture):
|
||||
tz = tz_naive_fixture
|
||||
# GH#13750
|
||||
base = pd.date_range('2016-11-05', freq='H', periods=100, tz=tz)
|
||||
idx = base.repeat(5)
|
||||
|
||||
exp_arr = np.arange(100, dtype=np.intp).repeat(5)
|
||||
|
||||
for obj in [idx, pd.Series(idx)]:
|
||||
arr, res = obj.factorize()
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(res, base)
|
||||
|
||||
def test_factorize_dst(self):
|
||||
# GH 13750
|
||||
idx = pd.date_range('2016-11-06', freq='H', periods=12,
|
||||
tz='US/Eastern')
|
||||
|
||||
for obj in [idx, pd.Series(idx)]:
|
||||
arr, res = obj.factorize()
|
||||
tm.assert_numpy_array_equal(arr, np.arange(12, dtype=np.intp))
|
||||
tm.assert_index_equal(res, idx)
|
||||
|
||||
idx = pd.date_range('2016-06-13', freq='H', periods=12,
|
||||
tz='US/Eastern')
|
||||
|
||||
for obj in [idx, pd.Series(idx)]:
|
||||
arr, res = obj.factorize()
|
||||
tm.assert_numpy_array_equal(arr, np.arange(12, dtype=np.intp))
|
||||
tm.assert_index_equal(res, idx)
|
||||
|
||||
@pytest.mark.parametrize('arr, expected', [
|
||||
(pd.DatetimeIndex(['2017', '2017']), pd.DatetimeIndex(['2017'])),
|
||||
(pd.DatetimeIndex(['2017', '2017'], tz='US/Eastern'),
|
||||
pd.DatetimeIndex(['2017'], tz='US/Eastern')),
|
||||
])
|
||||
def test_unique(self, arr, expected):
|
||||
result = arr.unique()
|
||||
tm.assert_index_equal(result, expected)
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
""" generic tests from the Datetimelike class """
|
||||
|
||||
from pandas.util import testing as tm
|
||||
from pandas import DatetimeIndex, date_range
|
||||
|
||||
from ..datetimelike import DatetimeLike
|
||||
|
||||
|
||||
class TestDatetimeIndex(DatetimeLike):
|
||||
_holder = DatetimeIndex
|
||||
|
||||
def setup_method(self, method):
|
||||
self.indices = dict(index=tm.makeDateIndex(10),
|
||||
index_dec=date_range('20130110', periods=10,
|
||||
freq='-1D'))
|
||||
self.setup_indices()
|
||||
|
||||
def create_index(self):
|
||||
return date_range('20130101', periods=5)
|
||||
|
||||
def test_shift(self):
|
||||
pass # handled in test_ops
|
||||
|
||||
def test_pickle_compat_construction(self):
|
||||
pass
|
||||
|
||||
def test_intersection(self):
|
||||
pass # handled in test_setops
|
||||
|
||||
def test_union(self):
|
||||
pass # handled in test_setops
|
||||
-221
@@ -1,221 +0,0 @@
|
||||
from datetime import datetime
|
||||
from pandas import DatetimeIndex, Series
|
||||
|
||||
import numpy as np
|
||||
import dateutil.tz
|
||||
import pytz
|
||||
import pytest
|
||||
|
||||
import pandas.util.testing as tm
|
||||
import pandas as pd
|
||||
|
||||
|
||||
def test_to_native_types():
|
||||
index = DatetimeIndex(freq='1D', periods=3, start='2017-01-01')
|
||||
|
||||
# First, with no arguments.
|
||||
expected = np.array(['2017-01-01', '2017-01-02',
|
||||
'2017-01-03'], dtype=object)
|
||||
|
||||
result = index.to_native_types()
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
# No NaN values, so na_rep has no effect
|
||||
result = index.to_native_types(na_rep='pandas')
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
# Make sure slicing works
|
||||
expected = np.array(['2017-01-01', '2017-01-03'], dtype=object)
|
||||
|
||||
result = index.to_native_types([0, 2])
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
# Make sure date formatting works
|
||||
expected = np.array(['01-2017-01', '01-2017-02',
|
||||
'01-2017-03'], dtype=object)
|
||||
|
||||
result = index.to_native_types(date_format='%m-%Y-%d')
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
# NULL object handling should work
|
||||
index = DatetimeIndex(['2017-01-01', pd.NaT, '2017-01-03'])
|
||||
expected = np.array(['2017-01-01', 'NaT', '2017-01-03'], dtype=object)
|
||||
|
||||
result = index.to_native_types()
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
expected = np.array(['2017-01-01', 'pandas',
|
||||
'2017-01-03'], dtype=object)
|
||||
|
||||
result = index.to_native_types(na_rep='pandas')
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
|
||||
class TestDatetimeIndexRendering(object):
|
||||
def test_dti_repr_short(self):
|
||||
dr = pd.date_range(start='1/1/2012', periods=1)
|
||||
repr(dr)
|
||||
|
||||
dr = pd.date_range(start='1/1/2012', periods=2)
|
||||
repr(dr)
|
||||
|
||||
dr = pd.date_range(start='1/1/2012', periods=3)
|
||||
repr(dr)
|
||||
|
||||
@pytest.mark.parametrize('method', ['__repr__', '__unicode__', '__str__'])
|
||||
def test_dti_representation(self, method):
|
||||
idxs = []
|
||||
idxs.append(DatetimeIndex([], freq='D'))
|
||||
idxs.append(DatetimeIndex(['2011-01-01'], freq='D'))
|
||||
idxs.append(DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D'))
|
||||
idxs.append(DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'],
|
||||
freq='D'))
|
||||
idxs.append(DatetimeIndex(
|
||||
['2011-01-01 09:00', '2011-01-01 10:00', '2011-01-01 11:00'
|
||||
], freq='H', tz='Asia/Tokyo'))
|
||||
idxs.append(DatetimeIndex(
|
||||
['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT], tz='US/Eastern'))
|
||||
idxs.append(DatetimeIndex(
|
||||
['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT], tz='UTC'))
|
||||
|
||||
exp = []
|
||||
exp.append("""DatetimeIndex([], dtype='datetime64[ns]', freq='D')""")
|
||||
exp.append("DatetimeIndex(['2011-01-01'], dtype='datetime64[ns]', "
|
||||
"freq='D')")
|
||||
exp.append("DatetimeIndex(['2011-01-01', '2011-01-02'], "
|
||||
"dtype='datetime64[ns]', freq='D')")
|
||||
exp.append("DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'], "
|
||||
"dtype='datetime64[ns]', freq='D')")
|
||||
exp.append("DatetimeIndex(['2011-01-01 09:00:00+09:00', "
|
||||
"'2011-01-01 10:00:00+09:00', '2011-01-01 11:00:00+09:00']"
|
||||
", dtype='datetime64[ns, Asia/Tokyo]', freq='H')")
|
||||
exp.append("DatetimeIndex(['2011-01-01 09:00:00-05:00', "
|
||||
"'2011-01-01 10:00:00-05:00', 'NaT'], "
|
||||
"dtype='datetime64[ns, US/Eastern]', freq=None)")
|
||||
exp.append("DatetimeIndex(['2011-01-01 09:00:00+00:00', "
|
||||
"'2011-01-01 10:00:00+00:00', 'NaT'], "
|
||||
"dtype='datetime64[ns, UTC]', freq=None)""")
|
||||
|
||||
with pd.option_context('display.width', 300):
|
||||
for indx, expected in zip(idxs, exp):
|
||||
result = getattr(indx, method)()
|
||||
assert result == expected
|
||||
|
||||
def test_dti_representation_to_series(self):
|
||||
idx1 = DatetimeIndex([], freq='D')
|
||||
idx2 = DatetimeIndex(['2011-01-01'], freq='D')
|
||||
idx3 = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')
|
||||
idx4 = DatetimeIndex(
|
||||
['2011-01-01', '2011-01-02', '2011-01-03'], freq='D')
|
||||
idx5 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00',
|
||||
'2011-01-01 11:00'], freq='H', tz='Asia/Tokyo')
|
||||
idx6 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT],
|
||||
tz='US/Eastern')
|
||||
idx7 = DatetimeIndex(['2011-01-01 09:00', '2011-01-02 10:15'])
|
||||
|
||||
exp1 = """Series([], dtype: datetime64[ns])"""
|
||||
|
||||
exp2 = ("0 2011-01-01\n"
|
||||
"dtype: datetime64[ns]")
|
||||
|
||||
exp3 = ("0 2011-01-01\n"
|
||||
"1 2011-01-02\n"
|
||||
"dtype: datetime64[ns]")
|
||||
|
||||
exp4 = ("0 2011-01-01\n"
|
||||
"1 2011-01-02\n"
|
||||
"2 2011-01-03\n"
|
||||
"dtype: datetime64[ns]")
|
||||
|
||||
exp5 = ("0 2011-01-01 09:00:00+09:00\n"
|
||||
"1 2011-01-01 10:00:00+09:00\n"
|
||||
"2 2011-01-01 11:00:00+09:00\n"
|
||||
"dtype: datetime64[ns, Asia/Tokyo]")
|
||||
|
||||
exp6 = ("0 2011-01-01 09:00:00-05:00\n"
|
||||
"1 2011-01-01 10:00:00-05:00\n"
|
||||
"2 NaT\n"
|
||||
"dtype: datetime64[ns, US/Eastern]")
|
||||
|
||||
exp7 = ("0 2011-01-01 09:00:00\n"
|
||||
"1 2011-01-02 10:15:00\n"
|
||||
"dtype: datetime64[ns]")
|
||||
|
||||
with pd.option_context('display.width', 300):
|
||||
for idx, expected in zip([idx1, idx2, idx3, idx4,
|
||||
idx5, idx6, idx7],
|
||||
[exp1, exp2, exp3, exp4,
|
||||
exp5, exp6, exp7]):
|
||||
result = repr(Series(idx))
|
||||
assert result == expected
|
||||
|
||||
def test_dti_summary(self):
|
||||
# GH#9116
|
||||
idx1 = DatetimeIndex([], freq='D')
|
||||
idx2 = DatetimeIndex(['2011-01-01'], freq='D')
|
||||
idx3 = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')
|
||||
idx4 = DatetimeIndex(
|
||||
['2011-01-01', '2011-01-02', '2011-01-03'], freq='D')
|
||||
idx5 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00',
|
||||
'2011-01-01 11:00'],
|
||||
freq='H', tz='Asia/Tokyo')
|
||||
idx6 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT],
|
||||
tz='US/Eastern')
|
||||
|
||||
exp1 = ("DatetimeIndex: 0 entries\n"
|
||||
"Freq: D")
|
||||
|
||||
exp2 = ("DatetimeIndex: 1 entries, 2011-01-01 to 2011-01-01\n"
|
||||
"Freq: D")
|
||||
|
||||
exp3 = ("DatetimeIndex: 2 entries, 2011-01-01 to 2011-01-02\n"
|
||||
"Freq: D")
|
||||
|
||||
exp4 = ("DatetimeIndex: 3 entries, 2011-01-01 to 2011-01-03\n"
|
||||
"Freq: D")
|
||||
|
||||
exp5 = ("DatetimeIndex: 3 entries, 2011-01-01 09:00:00+09:00 "
|
||||
"to 2011-01-01 11:00:00+09:00\n"
|
||||
"Freq: H")
|
||||
|
||||
exp6 = """DatetimeIndex: 3 entries, 2011-01-01 09:00:00-05:00 to NaT"""
|
||||
|
||||
for idx, expected in zip([idx1, idx2, idx3, idx4, idx5, idx6],
|
||||
[exp1, exp2, exp3, exp4, exp5, exp6]):
|
||||
result = idx._summary()
|
||||
assert result == expected
|
||||
|
||||
def test_dti_business_repr(self):
|
||||
# only really care that it works
|
||||
repr(pd.bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1)))
|
||||
|
||||
def test_dti_business_summary(self):
|
||||
rng = pd.bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1))
|
||||
rng._summary()
|
||||
rng[2:2]._summary()
|
||||
|
||||
def test_dti_business_summary_pytz(self):
|
||||
pd.bdate_range('1/1/2005', '1/1/2009', tz=pytz.utc)._summary()
|
||||
|
||||
def test_dti_business_summary_dateutil(self):
|
||||
pd.bdate_range('1/1/2005', '1/1/2009',
|
||||
tz=dateutil.tz.tzutc())._summary()
|
||||
|
||||
def test_dti_custom_business_repr(self):
|
||||
# only really care that it works
|
||||
repr(pd.bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1),
|
||||
freq='C'))
|
||||
|
||||
def test_dti_custom_business_summary(self):
|
||||
rng = pd.bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1),
|
||||
freq='C')
|
||||
rng._summary()
|
||||
rng[2:2]._summary()
|
||||
|
||||
def test_dti_custom_business_summary_pytz(self):
|
||||
pd.bdate_range('1/1/2005', '1/1/2009', freq='C',
|
||||
tz=pytz.utc)._summary()
|
||||
|
||||
def test_dti_custom_business_summary_dateutil(self):
|
||||
pd.bdate_range('1/1/2005', '1/1/2009', freq='C',
|
||||
tz=dateutil.tz.tzutc())._summary()
|
||||
-589
@@ -1,589 +0,0 @@
|
||||
from datetime import datetime, timedelta, time
|
||||
import pytest
|
||||
|
||||
import pytz
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
import pandas.compat as compat
|
||||
from pandas import notna, Index, DatetimeIndex, date_range, Timestamp
|
||||
from pandas.tseries.offsets import CDay, BDay
|
||||
|
||||
START, END = datetime(2009, 1, 1), datetime(2010, 1, 1)
|
||||
|
||||
|
||||
class TestGetItem(object):
|
||||
def test_getitem(self):
|
||||
idx1 = pd.date_range('2011-01-01', '2011-01-31', freq='D', name='idx')
|
||||
idx2 = pd.date_range('2011-01-01', '2011-01-31', freq='D',
|
||||
tz='Asia/Tokyo', name='idx')
|
||||
|
||||
for idx in [idx1, idx2]:
|
||||
result = idx[0]
|
||||
assert result == Timestamp('2011-01-01', tz=idx.tz)
|
||||
|
||||
result = idx[0:5]
|
||||
expected = pd.date_range('2011-01-01', '2011-01-05', freq='D',
|
||||
tz=idx.tz, name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
|
||||
result = idx[0:10:2]
|
||||
expected = pd.date_range('2011-01-01', '2011-01-09', freq='2D',
|
||||
tz=idx.tz, name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
|
||||
result = idx[-20:-5:3]
|
||||
expected = pd.date_range('2011-01-12', '2011-01-24', freq='3D',
|
||||
tz=idx.tz, name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
|
||||
result = idx[4::-1]
|
||||
expected = DatetimeIndex(['2011-01-05', '2011-01-04', '2011-01-03',
|
||||
'2011-01-02', '2011-01-01'],
|
||||
freq='-1D', tz=idx.tz, name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
|
||||
def test_dti_business_getitem(self):
|
||||
rng = pd.bdate_range(START, END)
|
||||
smaller = rng[:5]
|
||||
exp = DatetimeIndex(rng.view(np.ndarray)[:5])
|
||||
tm.assert_index_equal(smaller, exp)
|
||||
|
||||
assert smaller.freq == rng.freq
|
||||
|
||||
sliced = rng[::5]
|
||||
assert sliced.freq == BDay() * 5
|
||||
|
||||
fancy_indexed = rng[[4, 3, 2, 1, 0]]
|
||||
assert len(fancy_indexed) == 5
|
||||
assert isinstance(fancy_indexed, DatetimeIndex)
|
||||
assert fancy_indexed.freq is None
|
||||
|
||||
# 32-bit vs. 64-bit platforms
|
||||
assert rng[4] == rng[np.int_(4)]
|
||||
|
||||
def test_dti_business_getitem_matplotlib_hackaround(self):
|
||||
rng = pd.bdate_range(START, END)
|
||||
values = rng[:, None]
|
||||
expected = rng.values[:, None]
|
||||
tm.assert_numpy_array_equal(values, expected)
|
||||
|
||||
def test_dti_custom_getitem(self):
|
||||
rng = pd.bdate_range(START, END, freq='C')
|
||||
smaller = rng[:5]
|
||||
exp = DatetimeIndex(rng.view(np.ndarray)[:5])
|
||||
tm.assert_index_equal(smaller, exp)
|
||||
assert smaller.freq == rng.freq
|
||||
|
||||
sliced = rng[::5]
|
||||
assert sliced.freq == CDay() * 5
|
||||
|
||||
fancy_indexed = rng[[4, 3, 2, 1, 0]]
|
||||
assert len(fancy_indexed) == 5
|
||||
assert isinstance(fancy_indexed, DatetimeIndex)
|
||||
assert fancy_indexed.freq is None
|
||||
|
||||
# 32-bit vs. 64-bit platforms
|
||||
assert rng[4] == rng[np.int_(4)]
|
||||
|
||||
def test_dti_custom_getitem_matplotlib_hackaround(self):
|
||||
rng = pd.bdate_range(START, END, freq='C')
|
||||
values = rng[:, None]
|
||||
expected = rng.values[:, None]
|
||||
tm.assert_numpy_array_equal(values, expected)
|
||||
|
||||
|
||||
class TestWhere(object):
|
||||
def test_where_other(self):
|
||||
# other is ndarray or Index
|
||||
i = pd.date_range('20130101', periods=3, tz='US/Eastern')
|
||||
|
||||
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 = Index([pd.NaT, pd.NaT] + i[2:].tolist())
|
||||
result = i.where(notna(i2), i2)
|
||||
tm.assert_index_equal(result, i2)
|
||||
|
||||
i2 = i.copy()
|
||||
i2 = Index([pd.NaT, pd.NaT] + i[2:].tolist())
|
||||
result = i.where(notna(i2), i2.values)
|
||||
tm.assert_index_equal(result, i2)
|
||||
|
||||
def test_where_tz(self):
|
||||
i = pd.date_range('20130101', periods=3, tz='US/Eastern')
|
||||
result = i.where(notna(i))
|
||||
expected = i
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
i2 = i.copy()
|
||||
i2 = Index([pd.NaT, pd.NaT] + i[2:].tolist())
|
||||
result = i.where(notna(i2))
|
||||
expected = i2
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
|
||||
class TestTake(object):
|
||||
def test_take(self):
|
||||
# GH#10295
|
||||
idx1 = pd.date_range('2011-01-01', '2011-01-31', freq='D', name='idx')
|
||||
idx2 = pd.date_range('2011-01-01', '2011-01-31', freq='D',
|
||||
tz='Asia/Tokyo', name='idx')
|
||||
|
||||
for idx in [idx1, idx2]:
|
||||
result = idx.take([0])
|
||||
assert result == Timestamp('2011-01-01', tz=idx.tz)
|
||||
|
||||
result = idx.take([0, 1, 2])
|
||||
expected = pd.date_range('2011-01-01', '2011-01-03', freq='D',
|
||||
tz=idx.tz, name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
|
||||
result = idx.take([0, 2, 4])
|
||||
expected = pd.date_range('2011-01-01', '2011-01-05', freq='2D',
|
||||
tz=idx.tz, name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
|
||||
result = idx.take([7, 4, 1])
|
||||
expected = pd.date_range('2011-01-08', '2011-01-02', freq='-3D',
|
||||
tz=idx.tz, name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
|
||||
result = idx.take([3, 2, 5])
|
||||
expected = DatetimeIndex(['2011-01-04', '2011-01-03',
|
||||
'2011-01-06'],
|
||||
freq=None, tz=idx.tz, name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq is None
|
||||
|
||||
result = idx.take([-3, 2, 5])
|
||||
expected = DatetimeIndex(['2011-01-29', '2011-01-03',
|
||||
'2011-01-06'],
|
||||
freq=None, tz=idx.tz, name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq is None
|
||||
|
||||
def test_take_invalid_kwargs(self):
|
||||
idx = pd.date_range('2011-01-01', '2011-01-31', freq='D', name='idx')
|
||||
indices = [1, 6, 5, 9, 10, 13, 15, 3]
|
||||
|
||||
msg = r"take\(\) got an unexpected keyword argument 'foo'"
|
||||
tm.assert_raises_regex(TypeError, msg, idx.take,
|
||||
indices, foo=2)
|
||||
|
||||
msg = "the 'out' parameter is not supported"
|
||||
tm.assert_raises_regex(ValueError, msg, idx.take,
|
||||
indices, out=indices)
|
||||
|
||||
msg = "the 'mode' parameter is not supported"
|
||||
tm.assert_raises_regex(ValueError, msg, idx.take,
|
||||
indices, mode='clip')
|
||||
|
||||
# TODO: This method came from test_datetime; de-dup with version above
|
||||
@pytest.mark.parametrize('tz', [None, 'US/Eastern', 'Asia/Tokyo'])
|
||||
def test_take2(self, tz):
|
||||
dates = [datetime(2010, 1, 1, 14), datetime(2010, 1, 1, 15),
|
||||
datetime(2010, 1, 1, 17), datetime(2010, 1, 1, 21)]
|
||||
|
||||
idx = DatetimeIndex(start='2010-01-01 09:00',
|
||||
end='2010-02-01 09:00', freq='H', tz=tz,
|
||||
name='idx')
|
||||
expected = DatetimeIndex(dates, freq=None, name='idx', tz=tz)
|
||||
|
||||
taken1 = idx.take([5, 6, 8, 12])
|
||||
taken2 = idx[[5, 6, 8, 12]]
|
||||
|
||||
for taken in [taken1, taken2]:
|
||||
tm.assert_index_equal(taken, expected)
|
||||
assert isinstance(taken, DatetimeIndex)
|
||||
assert taken.freq is None
|
||||
assert taken.tz == expected.tz
|
||||
assert taken.name == expected.name
|
||||
|
||||
def test_take_fill_value(self):
|
||||
# GH#12631
|
||||
idx = pd.DatetimeIndex(['2011-01-01', '2011-02-01', '2011-03-01'],
|
||||
name='xxx')
|
||||
result = idx.take(np.array([1, 0, -1]))
|
||||
expected = pd.DatetimeIndex(['2011-02-01', '2011-01-01', '2011-03-01'],
|
||||
name='xxx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# fill_value
|
||||
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
||||
expected = pd.DatetimeIndex(['2011-02-01', '2011-01-01', 'NaT'],
|
||||
name='xxx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# allow_fill=False
|
||||
result = idx.take(np.array([1, 0, -1]), allow_fill=False,
|
||||
fill_value=True)
|
||||
expected = pd.DatetimeIndex(['2011-02-01', '2011-01-01', '2011-03-01'],
|
||||
name='xxx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
msg = ('When allow_fill=True and fill_value is not None, '
|
||||
'all indices must be >= -1')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
idx.take(np.array([1, 0, -2]), fill_value=True)
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
idx.take(np.array([1, 0, -5]), fill_value=True)
|
||||
|
||||
with pytest.raises(IndexError):
|
||||
idx.take(np.array([1, -5]))
|
||||
|
||||
def test_take_fill_value_with_timezone(self):
|
||||
idx = pd.DatetimeIndex(['2011-01-01', '2011-02-01', '2011-03-01'],
|
||||
name='xxx', tz='US/Eastern')
|
||||
result = idx.take(np.array([1, 0, -1]))
|
||||
expected = pd.DatetimeIndex(['2011-02-01', '2011-01-01', '2011-03-01'],
|
||||
name='xxx', tz='US/Eastern')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# fill_value
|
||||
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
||||
expected = pd.DatetimeIndex(['2011-02-01', '2011-01-01', 'NaT'],
|
||||
name='xxx', tz='US/Eastern')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# allow_fill=False
|
||||
result = idx.take(np.array([1, 0, -1]), allow_fill=False,
|
||||
fill_value=True)
|
||||
expected = pd.DatetimeIndex(['2011-02-01', '2011-01-01', '2011-03-01'],
|
||||
name='xxx', tz='US/Eastern')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
msg = ('When allow_fill=True and fill_value is not None, '
|
||||
'all indices must be >= -1')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
idx.take(np.array([1, 0, -2]), fill_value=True)
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
idx.take(np.array([1, 0, -5]), fill_value=True)
|
||||
|
||||
with pytest.raises(IndexError):
|
||||
idx.take(np.array([1, -5]))
|
||||
|
||||
|
||||
class TestDatetimeIndex(object):
|
||||
@pytest.mark.parametrize('null', [None, np.nan, pd.NaT])
|
||||
@pytest.mark.parametrize('tz', [None, 'UTC', 'US/Eastern'])
|
||||
def test_insert_nat(self, tz, null):
|
||||
# GH#16537, GH#18295 (test missing)
|
||||
idx = pd.DatetimeIndex(['2017-01-01'], tz=tz)
|
||||
expected = pd.DatetimeIndex(['NaT', '2017-01-01'], tz=tz)
|
||||
res = idx.insert(0, null)
|
||||
tm.assert_index_equal(res, expected)
|
||||
|
||||
def test_insert(self):
|
||||
idx = DatetimeIndex(
|
||||
['2000-01-04', '2000-01-01', '2000-01-02'], name='idx')
|
||||
|
||||
result = idx.insert(2, datetime(2000, 1, 5))
|
||||
exp = DatetimeIndex(['2000-01-04', '2000-01-01', '2000-01-05',
|
||||
'2000-01-02'], name='idx')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
# insertion of non-datetime should coerce to object index
|
||||
result = idx.insert(1, 'inserted')
|
||||
expected = Index([datetime(2000, 1, 4), 'inserted',
|
||||
datetime(2000, 1, 1),
|
||||
datetime(2000, 1, 2)], name='idx')
|
||||
assert not isinstance(result, DatetimeIndex)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
|
||||
idx = date_range('1/1/2000', periods=3, freq='M', name='idx')
|
||||
|
||||
# preserve freq
|
||||
expected_0 = DatetimeIndex(['1999-12-31', '2000-01-31', '2000-02-29',
|
||||
'2000-03-31'], name='idx', freq='M')
|
||||
expected_3 = DatetimeIndex(['2000-01-31', '2000-02-29', '2000-03-31',
|
||||
'2000-04-30'], name='idx', freq='M')
|
||||
|
||||
# reset freq to None
|
||||
expected_1_nofreq = DatetimeIndex(['2000-01-31', '2000-01-31',
|
||||
'2000-02-29',
|
||||
'2000-03-31'], name='idx',
|
||||
freq=None)
|
||||
expected_3_nofreq = DatetimeIndex(['2000-01-31', '2000-02-29',
|
||||
'2000-03-31',
|
||||
'2000-01-02'], name='idx',
|
||||
freq=None)
|
||||
|
||||
cases = [(0, datetime(1999, 12, 31), expected_0),
|
||||
(-3, datetime(1999, 12, 31), expected_0),
|
||||
(3, datetime(2000, 4, 30), expected_3),
|
||||
(1, datetime(2000, 1, 31), expected_1_nofreq),
|
||||
(3, datetime(2000, 1, 2), expected_3_nofreq)]
|
||||
|
||||
for n, d, expected in cases:
|
||||
result = idx.insert(n, d)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq == expected.freq
|
||||
|
||||
# reset freq to None
|
||||
result = idx.insert(3, datetime(2000, 1, 2))
|
||||
expected = DatetimeIndex(['2000-01-31', '2000-02-29', '2000-03-31',
|
||||
'2000-01-02'], name='idx', freq=None)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq is None
|
||||
|
||||
# see gh-7299
|
||||
idx = date_range('1/1/2000', periods=3, freq='D', tz='Asia/Tokyo',
|
||||
name='idx')
|
||||
with pytest.raises(ValueError):
|
||||
idx.insert(3, pd.Timestamp('2000-01-04'))
|
||||
with pytest.raises(ValueError):
|
||||
idx.insert(3, datetime(2000, 1, 4))
|
||||
with pytest.raises(ValueError):
|
||||
idx.insert(3, pd.Timestamp('2000-01-04', tz='US/Eastern'))
|
||||
with pytest.raises(ValueError):
|
||||
idx.insert(3, datetime(2000, 1, 4,
|
||||
tzinfo=pytz.timezone('US/Eastern')))
|
||||
|
||||
for tz in ['US/Pacific', 'Asia/Singapore']:
|
||||
idx = date_range('1/1/2000 09:00', periods=6, freq='H', tz=tz,
|
||||
name='idx')
|
||||
# preserve freq
|
||||
expected = date_range('1/1/2000 09:00', periods=7, freq='H', tz=tz,
|
||||
name='idx')
|
||||
for d in [pd.Timestamp('2000-01-01 15:00', tz=tz),
|
||||
pytz.timezone(tz).localize(datetime(2000, 1, 1, 15))]:
|
||||
|
||||
result = idx.insert(6, d)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq == expected.freq
|
||||
assert result.tz == expected.tz
|
||||
|
||||
expected = DatetimeIndex(['2000-01-01 09:00', '2000-01-01 10:00',
|
||||
'2000-01-01 11:00',
|
||||
'2000-01-01 12:00', '2000-01-01 13:00',
|
||||
'2000-01-01 14:00',
|
||||
'2000-01-01 10:00'], name='idx',
|
||||
tz=tz, freq=None)
|
||||
# reset freq to None
|
||||
for d in [pd.Timestamp('2000-01-01 10:00', tz=tz),
|
||||
pytz.timezone(tz).localize(datetime(2000, 1, 1, 10))]:
|
||||
result = idx.insert(6, d)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.tz == expected.tz
|
||||
assert result.freq is None
|
||||
|
||||
def test_delete(self):
|
||||
idx = date_range(start='2000-01-01', periods=5, freq='M', name='idx')
|
||||
|
||||
# prserve freq
|
||||
expected_0 = date_range(start='2000-02-01', periods=4, freq='M',
|
||||
name='idx')
|
||||
expected_4 = date_range(start='2000-01-01', periods=4, freq='M',
|
||||
name='idx')
|
||||
|
||||
# reset freq to None
|
||||
expected_1 = DatetimeIndex(['2000-01-31', '2000-03-31', '2000-04-30',
|
||||
'2000-05-31'], freq=None, name='idx')
|
||||
|
||||
cases = {0: expected_0,
|
||||
-5: expected_0,
|
||||
-1: expected_4,
|
||||
4: expected_4,
|
||||
1: expected_1}
|
||||
for n, expected in compat.iteritems(cases):
|
||||
result = idx.delete(n)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq == expected.freq
|
||||
|
||||
with pytest.raises((IndexError, ValueError)):
|
||||
# either depeidnig on numpy version
|
||||
result = idx.delete(5)
|
||||
|
||||
for tz in [None, 'Asia/Tokyo', 'US/Pacific']:
|
||||
idx = date_range(start='2000-01-01 09:00', periods=10, freq='H',
|
||||
name='idx', tz=tz)
|
||||
|
||||
expected = date_range(start='2000-01-01 10:00', periods=9,
|
||||
freq='H', name='idx', tz=tz)
|
||||
result = idx.delete(0)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freqstr == 'H'
|
||||
assert result.tz == expected.tz
|
||||
|
||||
expected = date_range(start='2000-01-01 09:00', periods=9,
|
||||
freq='H', name='idx', tz=tz)
|
||||
result = idx.delete(-1)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freqstr == 'H'
|
||||
assert result.tz == expected.tz
|
||||
|
||||
def test_delete_slice(self):
|
||||
idx = date_range(start='2000-01-01', periods=10, freq='D', name='idx')
|
||||
|
||||
# prserve freq
|
||||
expected_0_2 = date_range(start='2000-01-04', periods=7, freq='D',
|
||||
name='idx')
|
||||
expected_7_9 = date_range(start='2000-01-01', periods=7, freq='D',
|
||||
name='idx')
|
||||
|
||||
# reset freq to None
|
||||
expected_3_5 = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03',
|
||||
'2000-01-07', '2000-01-08', '2000-01-09',
|
||||
'2000-01-10'], freq=None, name='idx')
|
||||
|
||||
cases = {(0, 1, 2): expected_0_2,
|
||||
(7, 8, 9): expected_7_9,
|
||||
(3, 4, 5): expected_3_5}
|
||||
for n, expected in compat.iteritems(cases):
|
||||
result = idx.delete(n)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq == expected.freq
|
||||
|
||||
result = idx.delete(slice(n[0], n[-1] + 1))
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq == expected.freq
|
||||
|
||||
for tz in [None, 'Asia/Tokyo', 'US/Pacific']:
|
||||
ts = pd.Series(1, index=pd.date_range(
|
||||
'2000-01-01 09:00', periods=10, freq='H', name='idx', tz=tz))
|
||||
# preserve freq
|
||||
result = ts.drop(ts.index[:5]).index
|
||||
expected = pd.date_range('2000-01-01 14:00', periods=5, freq='H',
|
||||
name='idx', tz=tz)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq == expected.freq
|
||||
assert result.tz == expected.tz
|
||||
|
||||
# reset freq to None
|
||||
result = ts.drop(ts.index[[1, 3, 5, 7, 9]]).index
|
||||
expected = DatetimeIndex(['2000-01-01 09:00', '2000-01-01 11:00',
|
||||
'2000-01-01 13:00',
|
||||
'2000-01-01 15:00', '2000-01-01 17:00'],
|
||||
freq=None, name='idx', tz=tz)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq == expected.freq
|
||||
assert result.tz == expected.tz
|
||||
|
||||
def test_get_loc(self):
|
||||
idx = pd.date_range('2000-01-01', periods=3)
|
||||
|
||||
for method in [None, 'pad', 'backfill', 'nearest']:
|
||||
assert idx.get_loc(idx[1], method) == 1
|
||||
assert idx.get_loc(idx[1].to_pydatetime(), method) == 1
|
||||
assert idx.get_loc(str(idx[1]), method) == 1
|
||||
|
||||
if method is not None:
|
||||
assert idx.get_loc(idx[1], method,
|
||||
tolerance=pd.Timedelta('0 days')) == 1
|
||||
|
||||
assert idx.get_loc('2000-01-01', method='nearest') == 0
|
||||
assert idx.get_loc('2000-01-01T12', method='nearest') == 1
|
||||
|
||||
assert idx.get_loc('2000-01-01T12', method='nearest',
|
||||
tolerance='1 day') == 1
|
||||
assert idx.get_loc('2000-01-01T12', method='nearest',
|
||||
tolerance=pd.Timedelta('1D')) == 1
|
||||
assert idx.get_loc('2000-01-01T12', method='nearest',
|
||||
tolerance=np.timedelta64(1, 'D')) == 1
|
||||
assert idx.get_loc('2000-01-01T12', method='nearest',
|
||||
tolerance=timedelta(1)) == 1
|
||||
with tm.assert_raises_regex(ValueError,
|
||||
'unit abbreviation w/o a number'):
|
||||
idx.get_loc('2000-01-01T12', method='nearest', tolerance='foo')
|
||||
with pytest.raises(KeyError):
|
||||
idx.get_loc('2000-01-01T03', method='nearest', tolerance='2 hours')
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match='tolerance size must match target index size'):
|
||||
idx.get_loc('2000-01-01', method='nearest',
|
||||
tolerance=[pd.Timedelta('1day').to_timedelta64(),
|
||||
pd.Timedelta('1day').to_timedelta64()])
|
||||
|
||||
assert idx.get_loc('2000', method='nearest') == slice(0, 3)
|
||||
assert idx.get_loc('2000-01', method='nearest') == slice(0, 3)
|
||||
|
||||
assert idx.get_loc('1999', method='nearest') == 0
|
||||
assert idx.get_loc('2001', method='nearest') == 2
|
||||
|
||||
with pytest.raises(KeyError):
|
||||
idx.get_loc('1999', method='pad')
|
||||
with pytest.raises(KeyError):
|
||||
idx.get_loc('2001', method='backfill')
|
||||
|
||||
with pytest.raises(KeyError):
|
||||
idx.get_loc('foobar')
|
||||
with pytest.raises(TypeError):
|
||||
idx.get_loc(slice(2))
|
||||
|
||||
idx = pd.to_datetime(['2000-01-01', '2000-01-04'])
|
||||
assert idx.get_loc('2000-01-02', method='nearest') == 0
|
||||
assert idx.get_loc('2000-01-03', method='nearest') == 1
|
||||
assert idx.get_loc('2000-01', method='nearest') == slice(0, 2)
|
||||
|
||||
# time indexing
|
||||
idx = pd.date_range('2000-01-01', periods=24, freq='H')
|
||||
tm.assert_numpy_array_equal(idx.get_loc(time(12)),
|
||||
np.array([12]), check_dtype=False)
|
||||
tm.assert_numpy_array_equal(idx.get_loc(time(12, 30)),
|
||||
np.array([]), check_dtype=False)
|
||||
with pytest.raises(NotImplementedError):
|
||||
idx.get_loc(time(12, 30), method='pad')
|
||||
|
||||
def test_get_indexer(self):
|
||||
idx = pd.date_range('2000-01-01', periods=3)
|
||||
exp = np.array([0, 1, 2], dtype=np.intp)
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(idx), exp)
|
||||
|
||||
target = idx[0] + pd.to_timedelta(['-1 hour', '12 hours',
|
||||
'1 day 1 hour'])
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(target, 'pad'),
|
||||
np.array([-1, 0, 1], dtype=np.intp))
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(target, 'backfill'),
|
||||
np.array([0, 1, 2], dtype=np.intp))
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest'),
|
||||
np.array([0, 1, 1], dtype=np.intp))
|
||||
tm.assert_numpy_array_equal(
|
||||
idx.get_indexer(target, 'nearest',
|
||||
tolerance=pd.Timedelta('1 hour')),
|
||||
np.array([0, -1, 1], dtype=np.intp))
|
||||
tol_raw = [pd.Timedelta('1 hour'),
|
||||
pd.Timedelta('1 hour'),
|
||||
pd.Timedelta('1 hour').to_timedelta64(), ]
|
||||
tm.assert_numpy_array_equal(
|
||||
idx.get_indexer(target, 'nearest',
|
||||
tolerance=[np.timedelta64(x) for x in tol_raw]),
|
||||
np.array([0, -1, 1], dtype=np.intp))
|
||||
tol_bad = [pd.Timedelta('2 hour').to_timedelta64(),
|
||||
pd.Timedelta('1 hour').to_timedelta64(),
|
||||
'foo', ]
|
||||
with pytest.raises(
|
||||
ValueError, match='abbreviation w/o a number'):
|
||||
idx.get_indexer(target, 'nearest', tolerance=tol_bad)
|
||||
with pytest.raises(ValueError):
|
||||
idx.get_indexer(idx[[0]], method='nearest', tolerance='foo')
|
||||
|
||||
def test_reasonable_keyerror(self):
|
||||
# GH#1062
|
||||
index = DatetimeIndex(['1/3/2000'])
|
||||
try:
|
||||
index.get_loc('1/1/2000')
|
||||
except KeyError as e:
|
||||
assert '2000' in str(e)
|
||||
@@ -1,298 +0,0 @@
|
||||
import locale
|
||||
import calendar
|
||||
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
from pandas import (Index, DatetimeIndex, datetime, offsets,
|
||||
date_range, Timestamp)
|
||||
|
||||
|
||||
class TestTimeSeries(object):
|
||||
|
||||
def test_pass_datetimeindex_to_index(self):
|
||||
# Bugs in #1396
|
||||
rng = date_range('1/1/2000', '3/1/2000')
|
||||
idx = Index(rng, dtype=object)
|
||||
|
||||
expected = Index(rng.to_pydatetime(), dtype=object)
|
||||
|
||||
tm.assert_numpy_array_equal(idx.values, expected.values)
|
||||
|
||||
def test_range_edges(self):
|
||||
# GH 13672
|
||||
idx = DatetimeIndex(start=Timestamp('1970-01-01 00:00:00.000000001'),
|
||||
end=Timestamp('1970-01-01 00:00:00.000000004'),
|
||||
freq='N')
|
||||
exp = DatetimeIndex(['1970-01-01 00:00:00.000000001',
|
||||
'1970-01-01 00:00:00.000000002',
|
||||
'1970-01-01 00:00:00.000000003',
|
||||
'1970-01-01 00:00:00.000000004'])
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
idx = DatetimeIndex(start=Timestamp('1970-01-01 00:00:00.000000004'),
|
||||
end=Timestamp('1970-01-01 00:00:00.000000001'),
|
||||
freq='N')
|
||||
exp = DatetimeIndex([])
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
idx = DatetimeIndex(start=Timestamp('1970-01-01 00:00:00.000000001'),
|
||||
end=Timestamp('1970-01-01 00:00:00.000000001'),
|
||||
freq='N')
|
||||
exp = DatetimeIndex(['1970-01-01 00:00:00.000000001'])
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
idx = DatetimeIndex(start=Timestamp('1970-01-01 00:00:00.000001'),
|
||||
end=Timestamp('1970-01-01 00:00:00.000004'),
|
||||
freq='U')
|
||||
exp = DatetimeIndex(['1970-01-01 00:00:00.000001',
|
||||
'1970-01-01 00:00:00.000002',
|
||||
'1970-01-01 00:00:00.000003',
|
||||
'1970-01-01 00:00:00.000004'])
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
idx = DatetimeIndex(start=Timestamp('1970-01-01 00:00:00.001'),
|
||||
end=Timestamp('1970-01-01 00:00:00.004'),
|
||||
freq='L')
|
||||
exp = DatetimeIndex(['1970-01-01 00:00:00.001',
|
||||
'1970-01-01 00:00:00.002',
|
||||
'1970-01-01 00:00:00.003',
|
||||
'1970-01-01 00:00:00.004'])
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
idx = DatetimeIndex(start=Timestamp('1970-01-01 00:00:01'),
|
||||
end=Timestamp('1970-01-01 00:00:04'), freq='S')
|
||||
exp = DatetimeIndex(['1970-01-01 00:00:01', '1970-01-01 00:00:02',
|
||||
'1970-01-01 00:00:03', '1970-01-01 00:00:04'])
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
idx = DatetimeIndex(start=Timestamp('1970-01-01 00:01'),
|
||||
end=Timestamp('1970-01-01 00:04'), freq='T')
|
||||
exp = DatetimeIndex(['1970-01-01 00:01', '1970-01-01 00:02',
|
||||
'1970-01-01 00:03', '1970-01-01 00:04'])
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
idx = DatetimeIndex(start=Timestamp('1970-01-01 01:00'),
|
||||
end=Timestamp('1970-01-01 04:00'), freq='H')
|
||||
exp = DatetimeIndex(['1970-01-01 01:00', '1970-01-01 02:00',
|
||||
'1970-01-01 03:00', '1970-01-01 04:00'])
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
idx = DatetimeIndex(start=Timestamp('1970-01-01'),
|
||||
end=Timestamp('1970-01-04'), freq='D')
|
||||
exp = DatetimeIndex(['1970-01-01', '1970-01-02',
|
||||
'1970-01-03', '1970-01-04'])
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
|
||||
class TestDatetime64(object):
|
||||
|
||||
def test_datetimeindex_accessors(self):
|
||||
dti_naive = DatetimeIndex(freq='D', start=datetime(1998, 1, 1),
|
||||
periods=365)
|
||||
# GH 13303
|
||||
dti_tz = DatetimeIndex(freq='D', start=datetime(1998, 1, 1),
|
||||
periods=365, tz='US/Eastern')
|
||||
for dti in [dti_naive, dti_tz]:
|
||||
|
||||
assert dti.year[0] == 1998
|
||||
assert dti.month[0] == 1
|
||||
assert dti.day[0] == 1
|
||||
assert dti.hour[0] == 0
|
||||
assert dti.minute[0] == 0
|
||||
assert dti.second[0] == 0
|
||||
assert dti.microsecond[0] == 0
|
||||
assert dti.dayofweek[0] == 3
|
||||
|
||||
assert dti.dayofyear[0] == 1
|
||||
assert dti.dayofyear[120] == 121
|
||||
|
||||
assert dti.weekofyear[0] == 1
|
||||
assert dti.weekofyear[120] == 18
|
||||
|
||||
assert dti.quarter[0] == 1
|
||||
assert dti.quarter[120] == 2
|
||||
|
||||
assert dti.days_in_month[0] == 31
|
||||
assert dti.days_in_month[90] == 30
|
||||
|
||||
assert dti.is_month_start[0]
|
||||
assert not dti.is_month_start[1]
|
||||
assert dti.is_month_start[31]
|
||||
assert dti.is_quarter_start[0]
|
||||
assert dti.is_quarter_start[90]
|
||||
assert dti.is_year_start[0]
|
||||
assert not dti.is_year_start[364]
|
||||
assert not dti.is_month_end[0]
|
||||
assert dti.is_month_end[30]
|
||||
assert not dti.is_month_end[31]
|
||||
assert dti.is_month_end[364]
|
||||
assert not dti.is_quarter_end[0]
|
||||
assert not dti.is_quarter_end[30]
|
||||
assert dti.is_quarter_end[89]
|
||||
assert dti.is_quarter_end[364]
|
||||
assert not dti.is_year_end[0]
|
||||
assert dti.is_year_end[364]
|
||||
|
||||
assert len(dti.year) == 365
|
||||
assert len(dti.month) == 365
|
||||
assert len(dti.day) == 365
|
||||
assert len(dti.hour) == 365
|
||||
assert len(dti.minute) == 365
|
||||
assert len(dti.second) == 365
|
||||
assert len(dti.microsecond) == 365
|
||||
assert len(dti.dayofweek) == 365
|
||||
assert len(dti.dayofyear) == 365
|
||||
assert len(dti.weekofyear) == 365
|
||||
assert len(dti.quarter) == 365
|
||||
assert len(dti.is_month_start) == 365
|
||||
assert len(dti.is_month_end) == 365
|
||||
assert len(dti.is_quarter_start) == 365
|
||||
assert len(dti.is_quarter_end) == 365
|
||||
assert len(dti.is_year_start) == 365
|
||||
assert len(dti.is_year_end) == 365
|
||||
assert len(dti.weekday_name) == 365
|
||||
|
||||
dti.name = 'name'
|
||||
|
||||
# non boolean accessors -> return Index
|
||||
for accessor in DatetimeIndex._field_ops:
|
||||
res = getattr(dti, accessor)
|
||||
assert len(res) == 365
|
||||
assert isinstance(res, Index)
|
||||
assert res.name == 'name'
|
||||
|
||||
# boolean accessors -> return array
|
||||
for accessor in DatetimeIndex._bool_ops:
|
||||
res = getattr(dti, accessor)
|
||||
assert len(res) == 365
|
||||
assert isinstance(res, np.ndarray)
|
||||
|
||||
# test boolean indexing
|
||||
res = dti[dti.is_quarter_start]
|
||||
exp = dti[[0, 90, 181, 273]]
|
||||
tm.assert_index_equal(res, exp)
|
||||
res = dti[dti.is_leap_year]
|
||||
exp = DatetimeIndex([], freq='D', tz=dti.tz, name='name')
|
||||
tm.assert_index_equal(res, exp)
|
||||
|
||||
dti = DatetimeIndex(freq='BQ-FEB', start=datetime(1998, 1, 1),
|
||||
periods=4)
|
||||
|
||||
assert sum(dti.is_quarter_start) == 0
|
||||
assert sum(dti.is_quarter_end) == 4
|
||||
assert sum(dti.is_year_start) == 0
|
||||
assert sum(dti.is_year_end) == 1
|
||||
|
||||
# Ensure is_start/end accessors throw ValueError for CustomBusinessDay,
|
||||
# CBD requires np >= 1.7
|
||||
bday_egypt = offsets.CustomBusinessDay(weekmask='Sun Mon Tue Wed Thu')
|
||||
dti = date_range(datetime(2013, 4, 30), periods=5, freq=bday_egypt)
|
||||
pytest.raises(ValueError, lambda: dti.is_month_start)
|
||||
|
||||
dti = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03'])
|
||||
|
||||
assert dti.is_month_start[0] == 1
|
||||
|
||||
tests = [
|
||||
(Timestamp('2013-06-01', freq='M').is_month_start, 1),
|
||||
(Timestamp('2013-06-01', freq='BM').is_month_start, 0),
|
||||
(Timestamp('2013-06-03', freq='M').is_month_start, 0),
|
||||
(Timestamp('2013-06-03', freq='BM').is_month_start, 1),
|
||||
(Timestamp('2013-02-28', freq='Q-FEB').is_month_end, 1),
|
||||
(Timestamp('2013-02-28', freq='Q-FEB').is_quarter_end, 1),
|
||||
(Timestamp('2013-02-28', freq='Q-FEB').is_year_end, 1),
|
||||
(Timestamp('2013-03-01', freq='Q-FEB').is_month_start, 1),
|
||||
(Timestamp('2013-03-01', freq='Q-FEB').is_quarter_start, 1),
|
||||
(Timestamp('2013-03-01', freq='Q-FEB').is_year_start, 1),
|
||||
(Timestamp('2013-03-31', freq='QS-FEB').is_month_end, 1),
|
||||
(Timestamp('2013-03-31', freq='QS-FEB').is_quarter_end, 0),
|
||||
(Timestamp('2013-03-31', freq='QS-FEB').is_year_end, 0),
|
||||
(Timestamp('2013-02-01', freq='QS-FEB').is_month_start, 1),
|
||||
(Timestamp('2013-02-01', freq='QS-FEB').is_quarter_start, 1),
|
||||
(Timestamp('2013-02-01', freq='QS-FEB').is_year_start, 1),
|
||||
(Timestamp('2013-06-30', freq='BQ').is_month_end, 0),
|
||||
(Timestamp('2013-06-30', freq='BQ').is_quarter_end, 0),
|
||||
(Timestamp('2013-06-30', freq='BQ').is_year_end, 0),
|
||||
(Timestamp('2013-06-28', freq='BQ').is_month_end, 1),
|
||||
(Timestamp('2013-06-28', freq='BQ').is_quarter_end, 1),
|
||||
(Timestamp('2013-06-28', freq='BQ').is_year_end, 0),
|
||||
(Timestamp('2013-06-30', freq='BQS-APR').is_month_end, 0),
|
||||
(Timestamp('2013-06-30', freq='BQS-APR').is_quarter_end, 0),
|
||||
(Timestamp('2013-06-30', freq='BQS-APR').is_year_end, 0),
|
||||
(Timestamp('2013-06-28', freq='BQS-APR').is_month_end, 1),
|
||||
(Timestamp('2013-06-28', freq='BQS-APR').is_quarter_end, 1),
|
||||
(Timestamp('2013-03-29', freq='BQS-APR').is_year_end, 1),
|
||||
(Timestamp('2013-11-01', freq='AS-NOV').is_year_start, 1),
|
||||
(Timestamp('2013-10-31', freq='AS-NOV').is_year_end, 1),
|
||||
(Timestamp('2012-02-01').days_in_month, 29),
|
||||
(Timestamp('2013-02-01').days_in_month, 28)]
|
||||
|
||||
for ts, value in tests:
|
||||
assert ts == value
|
||||
|
||||
# GH 6538: Check that DatetimeIndex and its TimeStamp elements
|
||||
# return the same weekofyear accessor close to new year w/ tz
|
||||
dates = ["2013/12/29", "2013/12/30", "2013/12/31"]
|
||||
dates = DatetimeIndex(dates, tz="Europe/Brussels")
|
||||
expected = [52, 1, 1]
|
||||
assert dates.weekofyear.tolist() == expected
|
||||
assert [d.weekofyear for d in dates] == expected
|
||||
|
||||
# GH 12806
|
||||
@pytest.mark.parametrize('time_locale', [
|
||||
None] if tm.get_locales() is None else [None] + tm.get_locales())
|
||||
def test_datetime_name_accessors(self, time_locale):
|
||||
# Test Monday -> Sunday and January -> December, in that sequence
|
||||
if time_locale is None:
|
||||
# If the time_locale is None, day-name and month_name should
|
||||
# return the english attributes
|
||||
expected_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
|
||||
'Friday', 'Saturday', 'Sunday']
|
||||
expected_months = ['January', 'February', 'March', 'April', 'May',
|
||||
'June', 'July', 'August', 'September',
|
||||
'October', 'November', 'December']
|
||||
else:
|
||||
with tm.set_locale(time_locale, locale.LC_TIME):
|
||||
expected_days = calendar.day_name[:]
|
||||
expected_months = calendar.month_name[1:]
|
||||
|
||||
# GH 11128
|
||||
dti = DatetimeIndex(freq='D', start=datetime(1998, 1, 1),
|
||||
periods=365)
|
||||
english_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
|
||||
'Friday', 'Saturday', 'Sunday']
|
||||
for day, name, eng_name in zip(range(4, 11),
|
||||
expected_days,
|
||||
english_days):
|
||||
name = name.capitalize()
|
||||
assert dti.weekday_name[day] == eng_name
|
||||
assert dti.day_name(locale=time_locale)[day] == name
|
||||
ts = Timestamp(datetime(2016, 4, day))
|
||||
with tm.assert_produces_warning(FutureWarning,
|
||||
check_stacklevel=False):
|
||||
assert ts.weekday_name == eng_name
|
||||
assert ts.day_name(locale=time_locale) == name
|
||||
dti = dti.append(DatetimeIndex([pd.NaT]))
|
||||
assert np.isnan(dti.day_name(locale=time_locale)[-1])
|
||||
ts = Timestamp(pd.NaT)
|
||||
assert np.isnan(ts.day_name(locale=time_locale))
|
||||
|
||||
# GH 12805
|
||||
dti = DatetimeIndex(freq='M', start='2012', end='2013')
|
||||
result = dti.month_name(locale=time_locale)
|
||||
expected = Index([month.capitalize() for month in expected_months])
|
||||
tm.assert_index_equal(result, expected)
|
||||
for date, expected in zip(dti, expected_months):
|
||||
result = date.month_name(locale=time_locale)
|
||||
assert result == expected.capitalize()
|
||||
dti = dti.append(DatetimeIndex([pd.NaT]))
|
||||
assert np.isnan(dti.month_name(locale=time_locale)[-1])
|
||||
|
||||
def test_nanosecond_field(self):
|
||||
dti = DatetimeIndex(np.arange(10))
|
||||
|
||||
tm.assert_index_equal(dti.nanosecond,
|
||||
pd.Index(np.arange(10, dtype=np.int64)))
|
||||
-52
@@ -1,52 +0,0 @@
|
||||
import pytest
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
|
||||
|
||||
class TestDatetimeIndex(object):
|
||||
|
||||
@pytest.mark.parametrize('tz', ['US/Eastern', 'Asia/Tokyo'])
|
||||
def test_fillna_datetime64(self, tz):
|
||||
# GH 11343
|
||||
idx = pd.DatetimeIndex(['2011-01-01 09:00', pd.NaT,
|
||||
'2011-01-01 11:00'])
|
||||
|
||||
exp = pd.DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00',
|
||||
'2011-01-01 11:00'])
|
||||
tm.assert_index_equal(
|
||||
idx.fillna(pd.Timestamp('2011-01-01 10:00')), exp)
|
||||
|
||||
# tz mismatch
|
||||
exp = pd.Index([pd.Timestamp('2011-01-01 09:00'),
|
||||
pd.Timestamp('2011-01-01 10:00', tz=tz),
|
||||
pd.Timestamp('2011-01-01 11:00')], dtype=object)
|
||||
tm.assert_index_equal(
|
||||
idx.fillna(pd.Timestamp('2011-01-01 10:00', tz=tz)), exp)
|
||||
|
||||
# object
|
||||
exp = pd.Index([pd.Timestamp('2011-01-01 09:00'), 'x',
|
||||
pd.Timestamp('2011-01-01 11:00')], dtype=object)
|
||||
tm.assert_index_equal(idx.fillna('x'), exp)
|
||||
|
||||
idx = pd.DatetimeIndex(['2011-01-01 09:00', pd.NaT,
|
||||
'2011-01-01 11:00'], tz=tz)
|
||||
|
||||
exp = pd.DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00',
|
||||
'2011-01-01 11:00'], tz=tz)
|
||||
tm.assert_index_equal(
|
||||
idx.fillna(pd.Timestamp('2011-01-01 10:00', tz=tz)), exp)
|
||||
|
||||
exp = pd.Index([pd.Timestamp('2011-01-01 09:00', tz=tz),
|
||||
pd.Timestamp('2011-01-01 10:00'),
|
||||
pd.Timestamp('2011-01-01 11:00', tz=tz)],
|
||||
dtype=object)
|
||||
tm.assert_index_equal(
|
||||
idx.fillna(pd.Timestamp('2011-01-01 10:00')), exp)
|
||||
|
||||
# object
|
||||
exp = pd.Index([pd.Timestamp('2011-01-01 09:00', tz=tz),
|
||||
'x',
|
||||
pd.Timestamp('2011-01-01 11:00', tz=tz)],
|
||||
dtype=object)
|
||||
tm.assert_index_equal(idx.fillna('x'), exp)
|
||||
@@ -1,554 +0,0 @@
|
||||
import pytest
|
||||
import warnings
|
||||
import numpy as np
|
||||
from datetime import datetime
|
||||
|
||||
import pandas as pd
|
||||
import pandas._libs.tslib as tslib
|
||||
import pandas.util.testing as tm
|
||||
from pandas import (DatetimeIndex, PeriodIndex, Series, Timestamp,
|
||||
date_range, _np_version_under1p10, Index,
|
||||
bdate_range)
|
||||
from pandas.tseries.offsets import BMonthEnd, CDay, BDay, Day, Hour
|
||||
from pandas.tests.test_base import Ops
|
||||
from pandas.core.dtypes.generic import ABCDateOffset
|
||||
|
||||
|
||||
@pytest.fixture(params=[None, 'UTC', 'Asia/Tokyo', 'US/Eastern',
|
||||
'dateutil/Asia/Singapore',
|
||||
'dateutil/US/Pacific'])
|
||||
def tz_fixture(request):
|
||||
return request.param
|
||||
|
||||
|
||||
START, END = datetime(2009, 1, 1), datetime(2010, 1, 1)
|
||||
|
||||
|
||||
class TestDatetimeIndexOps(Ops):
|
||||
|
||||
def setup_method(self, method):
|
||||
super(TestDatetimeIndexOps, self).setup_method(method)
|
||||
mask = lambda x: (isinstance(x, DatetimeIndex) or
|
||||
isinstance(x, PeriodIndex))
|
||||
self.is_valid_objs = [o for o in self.objs if mask(o)]
|
||||
self.not_valid_objs = [o for o in self.objs if not mask(o)]
|
||||
|
||||
def test_ops_properties(self):
|
||||
f = lambda x: isinstance(x, DatetimeIndex)
|
||||
self.check_ops_properties(DatetimeIndex._field_ops, f)
|
||||
self.check_ops_properties(DatetimeIndex._object_ops, f)
|
||||
self.check_ops_properties(DatetimeIndex._bool_ops, f)
|
||||
|
||||
def test_ops_properties_basic(self):
|
||||
|
||||
# sanity check that the behavior didn't change
|
||||
# GH7206
|
||||
for op in ['year', 'day', 'second', 'weekday']:
|
||||
pytest.raises(TypeError, lambda x: getattr(self.dt_series, op))
|
||||
|
||||
# attribute access should still work!
|
||||
s = Series(dict(year=2000, month=1, day=10))
|
||||
assert s.year == 2000
|
||||
assert s.month == 1
|
||||
assert s.day == 10
|
||||
pytest.raises(AttributeError, lambda: s.weekday)
|
||||
|
||||
def test_minmax_tz(self, tz_fixture):
|
||||
tz = tz_fixture
|
||||
# monotonic
|
||||
idx1 = pd.DatetimeIndex(['2011-01-01', '2011-01-02',
|
||||
'2011-01-03'], tz=tz)
|
||||
assert idx1.is_monotonic
|
||||
|
||||
# non-monotonic
|
||||
idx2 = pd.DatetimeIndex(['2011-01-01', pd.NaT, '2011-01-03',
|
||||
'2011-01-02', pd.NaT], tz=tz)
|
||||
assert not idx2.is_monotonic
|
||||
|
||||
for idx in [idx1, idx2]:
|
||||
assert idx.min() == Timestamp('2011-01-01', tz=tz)
|
||||
assert idx.max() == Timestamp('2011-01-03', tz=tz)
|
||||
assert idx.argmin() == 0
|
||||
assert idx.argmax() == 2
|
||||
|
||||
@pytest.mark.parametrize('op', ['min', 'max'])
|
||||
def test_minmax_nat(self, op):
|
||||
# Return NaT
|
||||
obj = DatetimeIndex([])
|
||||
assert pd.isna(getattr(obj, op)())
|
||||
|
||||
obj = DatetimeIndex([pd.NaT])
|
||||
assert pd.isna(getattr(obj, op)())
|
||||
|
||||
obj = DatetimeIndex([pd.NaT, pd.NaT, pd.NaT])
|
||||
assert pd.isna(getattr(obj, op)())
|
||||
|
||||
def test_numpy_minmax(self):
|
||||
dr = pd.date_range(start='2016-01-15', end='2016-01-20')
|
||||
|
||||
assert np.min(dr) == Timestamp('2016-01-15 00:00:00', freq='D')
|
||||
assert np.max(dr) == Timestamp('2016-01-20 00:00:00', freq='D')
|
||||
|
||||
errmsg = "the 'out' parameter is not supported"
|
||||
tm.assert_raises_regex(ValueError, errmsg, np.min, dr, out=0)
|
||||
tm.assert_raises_regex(ValueError, errmsg, np.max, dr, out=0)
|
||||
|
||||
assert np.argmin(dr) == 0
|
||||
assert np.argmax(dr) == 5
|
||||
|
||||
if not _np_version_under1p10:
|
||||
errmsg = "the 'out' parameter is not supported"
|
||||
tm.assert_raises_regex(
|
||||
ValueError, errmsg, np.argmin, dr, out=0)
|
||||
tm.assert_raises_regex(
|
||||
ValueError, errmsg, np.argmax, dr, out=0)
|
||||
|
||||
def test_repeat_range(self, tz_fixture):
|
||||
tz = tz_fixture
|
||||
rng = date_range('1/1/2000', '1/1/2001')
|
||||
|
||||
result = rng.repeat(5)
|
||||
assert result.freq is None
|
||||
assert len(result) == 5 * len(rng)
|
||||
|
||||
index = pd.date_range('2001-01-01', periods=2, freq='D', tz=tz)
|
||||
exp = pd.DatetimeIndex(['2001-01-01', '2001-01-01',
|
||||
'2001-01-02', '2001-01-02'], tz=tz)
|
||||
for res in [index.repeat(2), np.repeat(index, 2)]:
|
||||
tm.assert_index_equal(res, exp)
|
||||
assert res.freq is None
|
||||
|
||||
index = pd.date_range('2001-01-01', periods=2, freq='2D', tz=tz)
|
||||
exp = pd.DatetimeIndex(['2001-01-01', '2001-01-01',
|
||||
'2001-01-03', '2001-01-03'], tz=tz)
|
||||
for res in [index.repeat(2), np.repeat(index, 2)]:
|
||||
tm.assert_index_equal(res, exp)
|
||||
assert res.freq is None
|
||||
|
||||
index = pd.DatetimeIndex(['2001-01-01', 'NaT', '2003-01-01'],
|
||||
tz=tz)
|
||||
exp = pd.DatetimeIndex(['2001-01-01', '2001-01-01', '2001-01-01',
|
||||
'NaT', 'NaT', 'NaT',
|
||||
'2003-01-01', '2003-01-01', '2003-01-01'],
|
||||
tz=tz)
|
||||
for res in [index.repeat(3), np.repeat(index, 3)]:
|
||||
tm.assert_index_equal(res, exp)
|
||||
assert res.freq is None
|
||||
|
||||
def test_repeat(self, tz_fixture):
|
||||
tz = tz_fixture
|
||||
reps = 2
|
||||
msg = "the 'axis' parameter is not supported"
|
||||
|
||||
rng = pd.date_range(start='2016-01-01', periods=2,
|
||||
freq='30Min', tz=tz)
|
||||
|
||||
expected_rng = DatetimeIndex([
|
||||
Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
|
||||
Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
|
||||
Timestamp('2016-01-01 00:30:00', tz=tz, freq='30T'),
|
||||
Timestamp('2016-01-01 00:30:00', tz=tz, freq='30T'),
|
||||
])
|
||||
|
||||
res = rng.repeat(reps)
|
||||
tm.assert_index_equal(res, expected_rng)
|
||||
assert res.freq is None
|
||||
|
||||
tm.assert_index_equal(np.repeat(rng, reps), expected_rng)
|
||||
tm.assert_raises_regex(ValueError, msg, np.repeat,
|
||||
rng, reps, axis=1)
|
||||
|
||||
def test_resolution(self, tz_fixture):
|
||||
tz = tz_fixture
|
||||
for freq, expected in zip(['A', 'Q', 'M', 'D', 'H', 'T',
|
||||
'S', 'L', 'U'],
|
||||
['day', 'day', 'day', 'day', 'hour',
|
||||
'minute', 'second', 'millisecond',
|
||||
'microsecond']):
|
||||
idx = pd.date_range(start='2013-04-01', periods=30, freq=freq,
|
||||
tz=tz)
|
||||
assert idx.resolution == expected
|
||||
|
||||
def test_value_counts_unique(self, tz_fixture):
|
||||
tz = tz_fixture
|
||||
# GH 7735
|
||||
idx = pd.date_range('2011-01-01 09:00', freq='H', periods=10)
|
||||
# create repeated values, 'n'th element is repeated by n+1 times
|
||||
idx = DatetimeIndex(np.repeat(idx.values, range(1, len(idx) + 1)),
|
||||
tz=tz)
|
||||
|
||||
exp_idx = pd.date_range('2011-01-01 18:00', freq='-1H', periods=10,
|
||||
tz=tz)
|
||||
expected = Series(range(10, 0, -1), index=exp_idx, dtype='int64')
|
||||
|
||||
for obj in [idx, Series(idx)]:
|
||||
tm.assert_series_equal(obj.value_counts(), expected)
|
||||
|
||||
expected = pd.date_range('2011-01-01 09:00', freq='H', periods=10,
|
||||
tz=tz)
|
||||
tm.assert_index_equal(idx.unique(), expected)
|
||||
|
||||
idx = DatetimeIndex(['2013-01-01 09:00', '2013-01-01 09:00',
|
||||
'2013-01-01 09:00', '2013-01-01 08:00',
|
||||
'2013-01-01 08:00', pd.NaT], tz=tz)
|
||||
|
||||
exp_idx = DatetimeIndex(['2013-01-01 09:00', '2013-01-01 08:00'],
|
||||
tz=tz)
|
||||
expected = Series([3, 2], index=exp_idx)
|
||||
|
||||
for obj in [idx, Series(idx)]:
|
||||
tm.assert_series_equal(obj.value_counts(), expected)
|
||||
|
||||
exp_idx = DatetimeIndex(['2013-01-01 09:00', '2013-01-01 08:00',
|
||||
pd.NaT], tz=tz)
|
||||
expected = Series([3, 2, 1], index=exp_idx)
|
||||
|
||||
for obj in [idx, Series(idx)]:
|
||||
tm.assert_series_equal(obj.value_counts(dropna=False),
|
||||
expected)
|
||||
|
||||
tm.assert_index_equal(idx.unique(), exp_idx)
|
||||
|
||||
def test_nonunique_contains(self):
|
||||
# GH 9512
|
||||
for idx in map(DatetimeIndex,
|
||||
([0, 1, 0], [0, 0, -1], [0, -1, -1],
|
||||
['2015', '2015', '2016'], ['2015', '2015', '2014'])):
|
||||
assert idx[0] in idx
|
||||
|
||||
@pytest.mark.parametrize('idx',
|
||||
[
|
||||
DatetimeIndex(
|
||||
['2011-01-01',
|
||||
'2011-01-02',
|
||||
'2011-01-03'],
|
||||
freq='D', name='idx'),
|
||||
DatetimeIndex(
|
||||
['2011-01-01 09:00',
|
||||
'2011-01-01 10:00',
|
||||
'2011-01-01 11:00'],
|
||||
freq='H', name='tzidx', tz='Asia/Tokyo')
|
||||
])
|
||||
def test_order_with_freq(self, idx):
|
||||
ordered = idx.sort_values()
|
||||
tm.assert_index_equal(ordered, idx)
|
||||
assert ordered.freq == idx.freq
|
||||
|
||||
ordered = idx.sort_values(ascending=False)
|
||||
expected = idx[::-1]
|
||||
tm.assert_index_equal(ordered, expected)
|
||||
assert ordered.freq == expected.freq
|
||||
assert ordered.freq.n == -1
|
||||
|
||||
ordered, indexer = idx.sort_values(return_indexer=True)
|
||||
tm.assert_index_equal(ordered, idx)
|
||||
tm.assert_numpy_array_equal(indexer, np.array([0, 1, 2]),
|
||||
check_dtype=False)
|
||||
assert ordered.freq == idx.freq
|
||||
|
||||
ordered, indexer = idx.sort_values(return_indexer=True,
|
||||
ascending=False)
|
||||
expected = idx[::-1]
|
||||
tm.assert_index_equal(ordered, expected)
|
||||
tm.assert_numpy_array_equal(indexer,
|
||||
np.array([2, 1, 0]),
|
||||
check_dtype=False)
|
||||
assert ordered.freq == expected.freq
|
||||
assert ordered.freq.n == -1
|
||||
|
||||
@pytest.mark.parametrize('index_dates,expected_dates', [
|
||||
(['2011-01-01', '2011-01-03', '2011-01-05',
|
||||
'2011-01-02', '2011-01-01'],
|
||||
['2011-01-01', '2011-01-01', '2011-01-02',
|
||||
'2011-01-03', '2011-01-05']),
|
||||
(['2011-01-01', '2011-01-03', '2011-01-05',
|
||||
'2011-01-02', '2011-01-01'],
|
||||
['2011-01-01', '2011-01-01', '2011-01-02',
|
||||
'2011-01-03', '2011-01-05']),
|
||||
([pd.NaT, '2011-01-03', '2011-01-05',
|
||||
'2011-01-02', pd.NaT],
|
||||
[pd.NaT, pd.NaT, '2011-01-02', '2011-01-03',
|
||||
'2011-01-05'])
|
||||
])
|
||||
def test_order_without_freq(self, index_dates, expected_dates, tz_fixture):
|
||||
tz = tz_fixture
|
||||
|
||||
# without freq
|
||||
index = DatetimeIndex(index_dates, tz=tz, name='idx')
|
||||
expected = DatetimeIndex(expected_dates, tz=tz, name='idx')
|
||||
|
||||
ordered = index.sort_values()
|
||||
tm.assert_index_equal(ordered, expected)
|
||||
assert ordered.freq is None
|
||||
|
||||
ordered = index.sort_values(ascending=False)
|
||||
tm.assert_index_equal(ordered, expected[::-1])
|
||||
assert ordered.freq is None
|
||||
|
||||
ordered, indexer = index.sort_values(return_indexer=True)
|
||||
tm.assert_index_equal(ordered, expected)
|
||||
|
||||
exp = np.array([0, 4, 3, 1, 2])
|
||||
tm.assert_numpy_array_equal(indexer, exp, check_dtype=False)
|
||||
assert ordered.freq is None
|
||||
|
||||
ordered, indexer = index.sort_values(return_indexer=True,
|
||||
ascending=False)
|
||||
tm.assert_index_equal(ordered, expected[::-1])
|
||||
|
||||
exp = np.array([2, 1, 3, 4, 0])
|
||||
tm.assert_numpy_array_equal(indexer, exp, check_dtype=False)
|
||||
assert ordered.freq is None
|
||||
|
||||
def test_drop_duplicates_metadata(self):
|
||||
# GH 10115
|
||||
idx = pd.date_range('2011-01-01', '2011-01-31', freq='D', name='idx')
|
||||
result = idx.drop_duplicates()
|
||||
tm.assert_index_equal(idx, result)
|
||||
assert idx.freq == result.freq
|
||||
|
||||
idx_dup = idx.append(idx)
|
||||
assert idx_dup.freq is None # freq is reset
|
||||
result = idx_dup.drop_duplicates()
|
||||
tm.assert_index_equal(idx, result)
|
||||
assert result.freq is None
|
||||
|
||||
def test_drop_duplicates(self):
|
||||
# to check Index/Series compat
|
||||
base = pd.date_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)))
|
||||
|
||||
@pytest.mark.parametrize('freq', [
|
||||
'A', '2A', '-2A', 'Q', '-1Q', 'M', '-1M', 'D', '3D',
|
||||
'-3D', 'W', '-1W', 'H', '2H', '-2H', 'T', '2T', 'S',
|
||||
'-3S'])
|
||||
def test_infer_freq(self, freq):
|
||||
# GH 11018
|
||||
idx = pd.date_range('2011-01-01 09:00:00', freq=freq, periods=10)
|
||||
result = pd.DatetimeIndex(idx.asi8, freq='infer')
|
||||
tm.assert_index_equal(idx, result)
|
||||
assert result.freq == freq
|
||||
|
||||
def test_nat_new(self):
|
||||
idx = pd.date_range('2011-01-01', freq='D', periods=5, name='x')
|
||||
result = idx._nat_new()
|
||||
exp = pd.DatetimeIndex([pd.NaT] * 5, name='x')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
result = idx._nat_new(box=False)
|
||||
exp = np.array([tslib.iNaT] * 5, dtype=np.int64)
|
||||
tm.assert_numpy_array_equal(result, exp)
|
||||
|
||||
def test_nat(self, tz_naive_fixture):
|
||||
timezone = tz_naive_fixture
|
||||
assert pd.DatetimeIndex._na_value is pd.NaT
|
||||
assert pd.DatetimeIndex([])._na_value is pd.NaT
|
||||
|
||||
idx = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], tz=timezone)
|
||||
assert idx._can_hold_na
|
||||
|
||||
tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
|
||||
assert not idx.hasnans
|
||||
tm.assert_numpy_array_equal(idx._nan_idxs,
|
||||
np.array([], dtype=np.intp))
|
||||
|
||||
idx = pd.DatetimeIndex(['2011-01-01', 'NaT'], tz=timezone)
|
||||
assert idx._can_hold_na
|
||||
|
||||
tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
|
||||
assert idx.hasnans
|
||||
tm.assert_numpy_array_equal(idx._nan_idxs,
|
||||
np.array([1], dtype=np.intp))
|
||||
|
||||
def test_equals(self):
|
||||
# GH 13107
|
||||
idx = pd.DatetimeIndex(['2011-01-01', '2011-01-02', 'NaT'])
|
||||
assert idx.equals(idx)
|
||||
assert idx.equals(idx.copy())
|
||||
assert idx.equals(idx.astype(object))
|
||||
assert idx.astype(object).equals(idx)
|
||||
assert idx.astype(object).equals(idx.astype(object))
|
||||
assert not idx.equals(list(idx))
|
||||
assert not idx.equals(pd.Series(idx))
|
||||
|
||||
idx2 = pd.DatetimeIndex(['2011-01-01', '2011-01-02', 'NaT'],
|
||||
tz='US/Pacific')
|
||||
assert not idx.equals(idx2)
|
||||
assert not idx.equals(idx2.copy())
|
||||
assert not idx.equals(idx2.astype(object))
|
||||
assert not idx.astype(object).equals(idx2)
|
||||
assert not idx.equals(list(idx2))
|
||||
assert not idx.equals(pd.Series(idx2))
|
||||
|
||||
# same internal, different tz
|
||||
idx3 = pd.DatetimeIndex._simple_new(idx.asi8, tz='US/Pacific')
|
||||
tm.assert_numpy_array_equal(idx.asi8, idx3.asi8)
|
||||
assert not idx.equals(idx3)
|
||||
assert not idx.equals(idx3.copy())
|
||||
assert not idx.equals(idx3.astype(object))
|
||||
assert not idx.astype(object).equals(idx3)
|
||||
assert not idx.equals(list(idx3))
|
||||
assert not idx.equals(pd.Series(idx3))
|
||||
|
||||
@pytest.mark.parametrize('values', [
|
||||
['20180101', '20180103', '20180105'], []])
|
||||
@pytest.mark.parametrize('freq', [
|
||||
'2D', Day(2), '2B', BDay(2), '48H', Hour(48)])
|
||||
@pytest.mark.parametrize('tz', [None, 'US/Eastern'])
|
||||
def test_freq_setter(self, values, freq, tz):
|
||||
# GH 20678
|
||||
idx = DatetimeIndex(values, tz=tz)
|
||||
|
||||
# can set to an offset, converting from string if necessary
|
||||
idx.freq = freq
|
||||
assert idx.freq == freq
|
||||
assert isinstance(idx.freq, ABCDateOffset)
|
||||
|
||||
# can reset to None
|
||||
idx.freq = None
|
||||
assert idx.freq is None
|
||||
|
||||
def test_freq_setter_errors(self):
|
||||
# GH 20678
|
||||
idx = DatetimeIndex(['20180101', '20180103', '20180105'])
|
||||
|
||||
# setting with an incompatible freq
|
||||
msg = ('Inferred frequency 2D from passed values does not conform to '
|
||||
'passed frequency 5D')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
idx.freq = '5D'
|
||||
|
||||
# setting with non-freq string
|
||||
with tm.assert_raises_regex(ValueError, 'Invalid frequency'):
|
||||
idx.freq = 'foo'
|
||||
|
||||
def test_offset_deprecated(self):
|
||||
# GH 20716
|
||||
idx = pd.DatetimeIndex(['20180101', '20180102'])
|
||||
|
||||
# getter deprecated
|
||||
with tm.assert_produces_warning(FutureWarning):
|
||||
idx.offset
|
||||
|
||||
# setter deprecated
|
||||
with tm.assert_produces_warning(FutureWarning):
|
||||
idx.offset = BDay()
|
||||
|
||||
|
||||
class TestBusinessDatetimeIndex(object):
|
||||
|
||||
def setup_method(self, method):
|
||||
self.rng = bdate_range(START, END)
|
||||
|
||||
def test_comparison(self):
|
||||
d = self.rng[10]
|
||||
|
||||
comp = self.rng > d
|
||||
assert comp[11]
|
||||
assert not comp[9]
|
||||
|
||||
def test_pickle_unpickle(self):
|
||||
unpickled = tm.round_trip_pickle(self.rng)
|
||||
assert unpickled.freq is not None
|
||||
|
||||
def test_copy(self):
|
||||
cp = self.rng.copy()
|
||||
repr(cp)
|
||||
tm.assert_index_equal(cp, self.rng)
|
||||
|
||||
def test_shift(self):
|
||||
shifted = self.rng.shift(5)
|
||||
assert shifted[0] == self.rng[5]
|
||||
assert shifted.freq == self.rng.freq
|
||||
|
||||
shifted = self.rng.shift(-5)
|
||||
assert shifted[5] == self.rng[0]
|
||||
assert shifted.freq == self.rng.freq
|
||||
|
||||
shifted = self.rng.shift(0)
|
||||
assert shifted[0] == self.rng[0]
|
||||
assert shifted.freq == self.rng.freq
|
||||
|
||||
rng = date_range(START, END, freq=BMonthEnd())
|
||||
shifted = rng.shift(1, freq=BDay())
|
||||
assert shifted[0] == rng[0] + BDay()
|
||||
|
||||
def test_equals(self):
|
||||
assert not self.rng.equals(list(self.rng))
|
||||
|
||||
def test_identical(self):
|
||||
t1 = self.rng.copy()
|
||||
t2 = self.rng.copy()
|
||||
assert t1.identical(t2)
|
||||
|
||||
# name
|
||||
t1 = t1.rename('foo')
|
||||
assert t1.equals(t2)
|
||||
assert not t1.identical(t2)
|
||||
t2 = t2.rename('foo')
|
||||
assert t1.identical(t2)
|
||||
|
||||
# freq
|
||||
t2v = Index(t2.values)
|
||||
assert t1.equals(t2v)
|
||||
assert not t1.identical(t2v)
|
||||
|
||||
|
||||
class TestCustomDatetimeIndex(object):
|
||||
def setup_method(self, method):
|
||||
self.rng = bdate_range(START, END, freq='C')
|
||||
|
||||
def test_comparison(self):
|
||||
d = self.rng[10]
|
||||
|
||||
comp = self.rng > d
|
||||
assert comp[11]
|
||||
assert not comp[9]
|
||||
|
||||
def test_copy(self):
|
||||
cp = self.rng.copy()
|
||||
repr(cp)
|
||||
tm.assert_index_equal(cp, self.rng)
|
||||
|
||||
def test_shift(self):
|
||||
|
||||
shifted = self.rng.shift(5)
|
||||
assert shifted[0] == self.rng[5]
|
||||
assert shifted.freq == self.rng.freq
|
||||
|
||||
shifted = self.rng.shift(-5)
|
||||
assert shifted[5] == self.rng[0]
|
||||
assert shifted.freq == self.rng.freq
|
||||
|
||||
shifted = self.rng.shift(0)
|
||||
assert shifted[0] == self.rng[0]
|
||||
assert shifted.freq == self.rng.freq
|
||||
|
||||
# PerformanceWarning
|
||||
with warnings.catch_warnings(record=True):
|
||||
rng = date_range(START, END, freq=BMonthEnd())
|
||||
shifted = rng.shift(1, freq=CDay())
|
||||
assert shifted[0] == rng[0] + CDay()
|
||||
|
||||
def test_pickle_unpickle(self):
|
||||
unpickled = tm.round_trip_pickle(self.rng)
|
||||
assert unpickled.freq is not None
|
||||
|
||||
def test_equals(self):
|
||||
assert not self.rng.equals(list(self.rng))
|
||||
-387
@@ -1,387 +0,0 @@
|
||||
""" test partial slicing on Series/Frame """
|
||||
|
||||
import pytest
|
||||
|
||||
from datetime import datetime
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import operator as op
|
||||
|
||||
from pandas import (DatetimeIndex, Series, DataFrame,
|
||||
date_range, Index, Timedelta, Timestamp)
|
||||
from pandas.util import testing as tm
|
||||
|
||||
|
||||
class TestSlicing(object):
|
||||
def test_dti_slicing(self):
|
||||
dti = DatetimeIndex(start='1/1/2005', end='12/1/2005', freq='M')
|
||||
dti2 = dti[[1, 3, 5]]
|
||||
|
||||
v1 = dti2[0]
|
||||
v2 = dti2[1]
|
||||
v3 = dti2[2]
|
||||
|
||||
assert v1 == Timestamp('2/28/2005')
|
||||
assert v2 == Timestamp('4/30/2005')
|
||||
assert v3 == Timestamp('6/30/2005')
|
||||
|
||||
# don't carry freq through irregular slicing
|
||||
assert dti2.freq is None
|
||||
|
||||
def test_slice_keeps_name(self):
|
||||
# GH4226
|
||||
st = pd.Timestamp('2013-07-01 00:00:00', tz='America/Los_Angeles')
|
||||
et = pd.Timestamp('2013-07-02 00:00:00', tz='America/Los_Angeles')
|
||||
dr = pd.date_range(st, et, freq='H', name='timebucket')
|
||||
assert dr[1:].name == dr.name
|
||||
|
||||
def test_slice_with_negative_step(self):
|
||||
ts = Series(np.arange(20),
|
||||
date_range('2014-01-01', periods=20, freq='MS'))
|
||||
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[Timestamp('2014-10-01')::-1], SLC[9::-1])
|
||||
assert_slices_equivalent(SLC['2014-10-01'::-1], SLC[9::-1])
|
||||
|
||||
assert_slices_equivalent(SLC[:Timestamp('2014-10-01'):-1], SLC[:8:-1])
|
||||
assert_slices_equivalent(SLC[:'2014-10-01':-1], SLC[:8:-1])
|
||||
|
||||
assert_slices_equivalent(SLC['2015-02-01':'2014-10-01':-1],
|
||||
SLC[13:8:-1])
|
||||
assert_slices_equivalent(SLC[Timestamp('2015-02-01'):Timestamp(
|
||||
'2014-10-01'):-1], SLC[13:8:-1])
|
||||
assert_slices_equivalent(SLC['2015-02-01':Timestamp('2014-10-01'):-1],
|
||||
SLC[13:8:-1])
|
||||
assert_slices_equivalent(SLC[Timestamp('2015-02-01'):'2014-10-01':-1],
|
||||
SLC[13:8:-1])
|
||||
|
||||
assert_slices_equivalent(SLC['2014-10-01':'2015-02-01':-1], SLC[:0])
|
||||
|
||||
def test_slice_with_zero_step_raises(self):
|
||||
ts = Series(np.arange(20),
|
||||
date_range('2014-01-01', periods=20, freq='MS'))
|
||||
tm.assert_raises_regex(ValueError, 'slice step cannot be zero',
|
||||
lambda: ts[::0])
|
||||
tm.assert_raises_regex(ValueError, 'slice step cannot be zero',
|
||||
lambda: ts.loc[::0])
|
||||
tm.assert_raises_regex(ValueError, 'slice step cannot be zero',
|
||||
lambda: ts.loc[::0])
|
||||
|
||||
def test_slice_bounds_empty(self):
|
||||
# GH 14354
|
||||
empty_idx = DatetimeIndex(freq='1H', periods=0, end='2015')
|
||||
|
||||
right = empty_idx._maybe_cast_slice_bound('2015-01-02', 'right', 'loc')
|
||||
exp = Timestamp('2015-01-02 23:59:59.999999999')
|
||||
assert right == exp
|
||||
|
||||
left = empty_idx._maybe_cast_slice_bound('2015-01-02', 'left', 'loc')
|
||||
exp = Timestamp('2015-01-02 00:00:00')
|
||||
assert left == exp
|
||||
|
||||
def test_slice_duplicate_monotonic(self):
|
||||
# https://github.com/pandas-dev/pandas/issues/16515
|
||||
idx = pd.DatetimeIndex(['2017', '2017'])
|
||||
result = idx._maybe_cast_slice_bound('2017-01-01', 'left', 'loc')
|
||||
expected = Timestamp('2017-01-01')
|
||||
assert result == expected
|
||||
|
||||
def test_monotone_DTI_indexing_bug(self):
|
||||
# GH 19362
|
||||
# Testing accessing the first element in a montononic descending
|
||||
# partial string indexing.
|
||||
|
||||
df = pd.DataFrame(list(range(5)))
|
||||
date_list = ['2018-01-02', '2017-02-10', '2016-03-10',
|
||||
'2015-03-15', '2014-03-16']
|
||||
date_index = pd.to_datetime(date_list)
|
||||
df['date'] = date_index
|
||||
expected = pd.DataFrame({0: list(range(5)), 'date': date_index})
|
||||
tm.assert_frame_equal(df, expected)
|
||||
|
||||
df = pd.DataFrame({'A': [1, 2, 3]},
|
||||
index=pd.date_range('20170101',
|
||||
periods=3)[::-1])
|
||||
expected = pd.DataFrame({'A': 1},
|
||||
index=pd.date_range('20170103',
|
||||
periods=1))
|
||||
tm.assert_frame_equal(df.loc['2017-01-03'], expected)
|
||||
|
||||
def test_slice_year(self):
|
||||
dti = DatetimeIndex(freq='B', start=datetime(2005, 1, 1), periods=500)
|
||||
|
||||
s = Series(np.arange(len(dti)), index=dti)
|
||||
result = s['2005']
|
||||
expected = s[s.index.year == 2005]
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
df = DataFrame(np.random.rand(len(dti), 5), index=dti)
|
||||
result = df.loc['2005']
|
||||
expected = df[df.index.year == 2005]
|
||||
tm.assert_frame_equal(result, expected)
|
||||
|
||||
rng = date_range('1/1/2000', '1/1/2010')
|
||||
|
||||
result = rng.get_loc('2009')
|
||||
expected = slice(3288, 3653)
|
||||
assert result == expected
|
||||
|
||||
def test_slice_quarter(self):
|
||||
dti = DatetimeIndex(freq='D', start=datetime(2000, 6, 1), periods=500)
|
||||
|
||||
s = Series(np.arange(len(dti)), index=dti)
|
||||
assert len(s['2001Q1']) == 90
|
||||
|
||||
df = DataFrame(np.random.rand(len(dti), 5), index=dti)
|
||||
assert len(df.loc['1Q01']) == 90
|
||||
|
||||
def test_slice_month(self):
|
||||
dti = DatetimeIndex(freq='D', start=datetime(2005, 1, 1), periods=500)
|
||||
s = Series(np.arange(len(dti)), index=dti)
|
||||
assert len(s['2005-11']) == 30
|
||||
|
||||
df = DataFrame(np.random.rand(len(dti), 5), index=dti)
|
||||
assert len(df.loc['2005-11']) == 30
|
||||
|
||||
tm.assert_series_equal(s['2005-11'], s['11-2005'])
|
||||
|
||||
def test_partial_slice(self):
|
||||
rng = DatetimeIndex(freq='D', start=datetime(2005, 1, 1), periods=500)
|
||||
s = Series(np.arange(len(rng)), index=rng)
|
||||
|
||||
result = s['2005-05':'2006-02']
|
||||
expected = s['20050501':'20060228']
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
result = s['2005-05':]
|
||||
expected = s['20050501':]
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
result = s[:'2006-02']
|
||||
expected = s[:'20060228']
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
result = s['2005-1-1']
|
||||
assert result == s.iloc[0]
|
||||
|
||||
pytest.raises(Exception, s.__getitem__, '2004-12-31')
|
||||
|
||||
def test_partial_slice_daily(self):
|
||||
rng = DatetimeIndex(freq='H', start=datetime(2005, 1, 31), periods=500)
|
||||
s = Series(np.arange(len(rng)), index=rng)
|
||||
|
||||
result = s['2005-1-31']
|
||||
tm.assert_series_equal(result, s.iloc[:24])
|
||||
|
||||
pytest.raises(Exception, s.__getitem__, '2004-12-31 00')
|
||||
|
||||
def test_partial_slice_hourly(self):
|
||||
rng = DatetimeIndex(freq='T', start=datetime(2005, 1, 1, 20, 0, 0),
|
||||
periods=500)
|
||||
s = Series(np.arange(len(rng)), index=rng)
|
||||
|
||||
result = s['2005-1-1']
|
||||
tm.assert_series_equal(result, s.iloc[:60 * 4])
|
||||
|
||||
result = s['2005-1-1 20']
|
||||
tm.assert_series_equal(result, s.iloc[:60])
|
||||
|
||||
assert s['2005-1-1 20:00'] == s.iloc[0]
|
||||
pytest.raises(Exception, s.__getitem__, '2004-12-31 00:15')
|
||||
|
||||
def test_partial_slice_minutely(self):
|
||||
rng = DatetimeIndex(freq='S', start=datetime(2005, 1, 1, 23, 59, 0),
|
||||
periods=500)
|
||||
s = Series(np.arange(len(rng)), index=rng)
|
||||
|
||||
result = s['2005-1-1 23:59']
|
||||
tm.assert_series_equal(result, s.iloc[:60])
|
||||
|
||||
result = s['2005-1-1']
|
||||
tm.assert_series_equal(result, s.iloc[:60])
|
||||
|
||||
assert s[Timestamp('2005-1-1 23:59:00')] == s.iloc[0]
|
||||
pytest.raises(Exception, s.__getitem__, '2004-12-31 00:00:00')
|
||||
|
||||
def test_partial_slice_second_precision(self):
|
||||
rng = DatetimeIndex(start=datetime(2005, 1, 1, 0, 0, 59,
|
||||
microsecond=999990),
|
||||
periods=20, freq='US')
|
||||
s = Series(np.arange(20), rng)
|
||||
|
||||
tm.assert_series_equal(s['2005-1-1 00:00'], s.iloc[:10])
|
||||
tm.assert_series_equal(s['2005-1-1 00:00:59'], s.iloc[:10])
|
||||
|
||||
tm.assert_series_equal(s['2005-1-1 00:01'], s.iloc[10:])
|
||||
tm.assert_series_equal(s['2005-1-1 00:01:00'], s.iloc[10:])
|
||||
|
||||
assert s[Timestamp('2005-1-1 00:00:59.999990')] == s.iloc[0]
|
||||
tm.assert_raises_regex(KeyError, '2005-1-1 00:00:00',
|
||||
lambda: s['2005-1-1 00:00:00'])
|
||||
|
||||
def test_partial_slicing_dataframe(self):
|
||||
# GH14856
|
||||
# Test various combinations of string slicing resolution vs.
|
||||
# index resolution
|
||||
# - If string resolution is less precise than index resolution,
|
||||
# string is considered a slice
|
||||
# - If string resolution is equal to or more precise than index
|
||||
# resolution, string is considered an exact match
|
||||
formats = ['%Y', '%Y-%m', '%Y-%m-%d', '%Y-%m-%d %H',
|
||||
'%Y-%m-%d %H:%M', '%Y-%m-%d %H:%M:%S']
|
||||
resolutions = ['year', 'month', 'day', 'hour', 'minute', 'second']
|
||||
for rnum, resolution in enumerate(resolutions[2:], 2):
|
||||
# we check only 'day', 'hour', 'minute' and 'second'
|
||||
unit = Timedelta("1 " + resolution)
|
||||
middate = datetime(2012, 1, 1, 0, 0, 0)
|
||||
index = DatetimeIndex([middate - unit,
|
||||
middate, middate + unit])
|
||||
values = [1, 2, 3]
|
||||
df = DataFrame({'a': values}, index, dtype=np.int64)
|
||||
assert df.index.resolution == resolution
|
||||
|
||||
# Timestamp with the same resolution as index
|
||||
# Should be exact match for Series (return scalar)
|
||||
# and raise KeyError for Frame
|
||||
for timestamp, expected in zip(index, values):
|
||||
ts_string = timestamp.strftime(formats[rnum])
|
||||
# make ts_string as precise as index
|
||||
result = df['a'][ts_string]
|
||||
assert isinstance(result, np.int64)
|
||||
assert result == expected
|
||||
pytest.raises(KeyError, df.__getitem__, ts_string)
|
||||
|
||||
# Timestamp with resolution less precise than index
|
||||
for fmt in formats[:rnum]:
|
||||
for element, theslice in [[0, slice(None, 1)],
|
||||
[1, slice(1, None)]]:
|
||||
ts_string = index[element].strftime(fmt)
|
||||
|
||||
# Series should return slice
|
||||
result = df['a'][ts_string]
|
||||
expected = df['a'][theslice]
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
# Frame should return slice as well
|
||||
result = df[ts_string]
|
||||
expected = df[theslice]
|
||||
tm.assert_frame_equal(result, expected)
|
||||
|
||||
# Timestamp with resolution more precise than index
|
||||
# Compatible with existing key
|
||||
# Should return scalar for Series
|
||||
# and raise KeyError for Frame
|
||||
for fmt in formats[rnum + 1:]:
|
||||
ts_string = index[1].strftime(fmt)
|
||||
result = df['a'][ts_string]
|
||||
assert isinstance(result, np.int64)
|
||||
assert result == 2
|
||||
pytest.raises(KeyError, df.__getitem__, ts_string)
|
||||
|
||||
# Not compatible with existing key
|
||||
# Should raise KeyError
|
||||
for fmt, res in list(zip(formats, resolutions))[rnum + 1:]:
|
||||
ts = index[1] + Timedelta("1 " + res)
|
||||
ts_string = ts.strftime(fmt)
|
||||
pytest.raises(KeyError, df['a'].__getitem__, ts_string)
|
||||
pytest.raises(KeyError, df.__getitem__, ts_string)
|
||||
|
||||
def test_partial_slicing_with_multiindex(self):
|
||||
|
||||
# GH 4758
|
||||
# partial string indexing with a multi-index buggy
|
||||
df = DataFrame({'ACCOUNT': ["ACCT1", "ACCT1", "ACCT1", "ACCT2"],
|
||||
'TICKER': ["ABC", "MNP", "XYZ", "XYZ"],
|
||||
'val': [1, 2, 3, 4]},
|
||||
index=date_range("2013-06-19 09:30:00",
|
||||
periods=4, freq='5T'))
|
||||
df_multi = df.set_index(['ACCOUNT', 'TICKER'], append=True)
|
||||
|
||||
expected = DataFrame([
|
||||
[1]
|
||||
], index=Index(['ABC'], name='TICKER'), columns=['val'])
|
||||
result = df_multi.loc[('2013-06-19 09:30:00', 'ACCT1')]
|
||||
tm.assert_frame_equal(result, expected)
|
||||
|
||||
expected = df_multi.loc[
|
||||
(pd.Timestamp('2013-06-19 09:30:00', tz=None), 'ACCT1', 'ABC')]
|
||||
result = df_multi.loc[('2013-06-19 09:30:00', 'ACCT1', 'ABC')]
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
# this is a KeyError as we don't do partial string selection on
|
||||
# multi-levels
|
||||
def f():
|
||||
df_multi.loc[('2013-06-19', 'ACCT1', 'ABC')]
|
||||
|
||||
pytest.raises(KeyError, f)
|
||||
|
||||
# GH 4294
|
||||
# partial slice on a series mi
|
||||
s = pd.DataFrame(np.random.rand(1000, 1000), index=pd.date_range(
|
||||
'2000-1-1', periods=1000)).stack()
|
||||
|
||||
s2 = s[:-1].copy()
|
||||
expected = s2['2000-1-4']
|
||||
result = s2[pd.Timestamp('2000-1-4')]
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
result = s[pd.Timestamp('2000-1-4')]
|
||||
expected = s['2000-1-4']
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
df2 = pd.DataFrame(s)
|
||||
expected = df2.xs('2000-1-4')
|
||||
result = df2.loc[pd.Timestamp('2000-1-4')]
|
||||
tm.assert_frame_equal(result, expected)
|
||||
|
||||
def test_partial_slice_doesnt_require_monotonicity(self):
|
||||
# For historical reasons.
|
||||
s = pd.Series(np.arange(10), pd.date_range('2014-01-01', periods=10))
|
||||
|
||||
nonmonotonic = s[[3, 5, 4]]
|
||||
expected = nonmonotonic.iloc[:0]
|
||||
timestamp = pd.Timestamp('2014-01-10')
|
||||
|
||||
tm.assert_series_equal(nonmonotonic['2014-01-10':], expected)
|
||||
tm.assert_raises_regex(KeyError,
|
||||
r"Timestamp\('2014-01-10 00:00:00'\)",
|
||||
lambda: nonmonotonic[timestamp:])
|
||||
|
||||
tm.assert_series_equal(nonmonotonic.loc['2014-01-10':], expected)
|
||||
tm.assert_raises_regex(KeyError,
|
||||
r"Timestamp\('2014-01-10 00:00:00'\)",
|
||||
lambda: nonmonotonic.loc[timestamp:])
|
||||
|
||||
def test_loc_datetime_length_one(self):
|
||||
# GH16071
|
||||
df = pd.DataFrame(columns=['1'],
|
||||
index=pd.date_range('2016-10-01T00:00:00',
|
||||
'2016-10-01T23:59:59'))
|
||||
result = df.loc[datetime(2016, 10, 1):]
|
||||
tm.assert_frame_equal(result, df)
|
||||
|
||||
result = df.loc['2016-10-01T00:00:00':]
|
||||
tm.assert_frame_equal(result, df)
|
||||
|
||||
@pytest.mark.parametrize('datetimelike', [
|
||||
Timestamp('20130101'), datetime(2013, 1, 1),
|
||||
np.datetime64('2013-01-01T00:00', 'ns')])
|
||||
@pytest.mark.parametrize('op,expected', [
|
||||
(op.lt, [True, False, False, False]),
|
||||
(op.le, [True, True, False, False]),
|
||||
(op.eq, [False, True, False, False]),
|
||||
(op.gt, [False, False, False, True])])
|
||||
def test_selection_by_datetimelike(self, datetimelike, op, expected):
|
||||
# GH issue #17965, test for ability to compare datetime64[ns] columns
|
||||
# to datetimelike
|
||||
df = DataFrame({'A': [pd.Timestamp('20120101'),
|
||||
pd.Timestamp('20130101'),
|
||||
np.nan, pd.Timestamp('20130103')]})
|
||||
result = op(df.A, datetimelike)
|
||||
expected = Series(expected, name='A')
|
||||
tm.assert_series_equal(result, expected)
|
||||
-236
@@ -1,236 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Tests for DatetimeIndex methods behaving like their Timestamp counterparts
|
||||
"""
|
||||
from datetime import datetime
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import pandas.util.testing as tm
|
||||
import pandas as pd
|
||||
|
||||
from pandas import date_range, Timestamp, DatetimeIndex
|
||||
|
||||
|
||||
@pytest.fixture(params=[None, 'UTC', 'Asia/Tokyo',
|
||||
'US/Eastern', 'dateutil/Asia/Singapore',
|
||||
'dateutil/US/Pacific'])
|
||||
def tz(request):
|
||||
return request.param
|
||||
|
||||
|
||||
class TestDatetimeIndexOps(object):
|
||||
def test_dti_time(self):
|
||||
rng = date_range('1/1/2000', freq='12min', periods=10)
|
||||
result = pd.Index(rng).time
|
||||
expected = [t.time() for t in rng]
|
||||
assert (result == expected).all()
|
||||
|
||||
def test_dti_date(self):
|
||||
rng = date_range('1/1/2000', freq='12H', periods=10)
|
||||
result = pd.Index(rng).date
|
||||
expected = [t.date() for t in rng]
|
||||
assert (result == expected).all()
|
||||
|
||||
def test_dti_date_out_of_range(self):
|
||||
# GH#1475
|
||||
pytest.raises(ValueError, DatetimeIndex, ['1400-01-01'])
|
||||
pytest.raises(ValueError, DatetimeIndex, [datetime(1400, 1, 1)])
|
||||
|
||||
@pytest.mark.parametrize('field', [
|
||||
'dayofweek', 'dayofyear', 'week', 'weekofyear', 'quarter',
|
||||
'days_in_month', 'is_month_start', 'is_month_end',
|
||||
'is_quarter_start', 'is_quarter_end', 'is_year_start',
|
||||
'is_year_end', 'weekday_name'])
|
||||
def test_dti_timestamp_fields(self, field):
|
||||
# extra fields from DatetimeIndex like quarter and week
|
||||
idx = tm.makeDateIndex(100)
|
||||
expected = getattr(idx, field)[-1]
|
||||
if field == 'weekday_name':
|
||||
with tm.assert_produces_warning(FutureWarning,
|
||||
check_stacklevel=False):
|
||||
result = getattr(Timestamp(idx[-1]), field)
|
||||
else:
|
||||
result = getattr(Timestamp(idx[-1]), field)
|
||||
assert result == expected
|
||||
|
||||
def test_dti_timestamp_freq_fields(self):
|
||||
# extra fields from DatetimeIndex like quarter and week
|
||||
idx = tm.makeDateIndex(100)
|
||||
|
||||
assert idx.freq == Timestamp(idx[-1], idx.freq).freq
|
||||
assert idx.freqstr == Timestamp(idx[-1], idx.freq).freqstr
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# DatetimeIndex.round
|
||||
|
||||
def test_round_daily(self):
|
||||
dti = date_range('20130101 09:10:11', periods=5)
|
||||
result = dti.round('D')
|
||||
expected = date_range('20130101', periods=5)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
dti = dti.tz_localize('UTC').tz_convert('US/Eastern')
|
||||
result = dti.round('D')
|
||||
expected = date_range('20130101',
|
||||
periods=5).tz_localize('US/Eastern')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = dti.round('s')
|
||||
tm.assert_index_equal(result, dti)
|
||||
|
||||
# invalid
|
||||
for freq in ['Y', 'M', 'foobar']:
|
||||
pytest.raises(ValueError, lambda: dti.round(freq))
|
||||
|
||||
def test_round(self, tz):
|
||||
rng = date_range(start='2016-01-01', periods=5,
|
||||
freq='30Min', tz=tz)
|
||||
elt = rng[1]
|
||||
|
||||
expected_rng = DatetimeIndex([
|
||||
Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
|
||||
Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
|
||||
Timestamp('2016-01-01 01:00:00', tz=tz, freq='30T'),
|
||||
Timestamp('2016-01-01 02:00:00', tz=tz, freq='30T'),
|
||||
Timestamp('2016-01-01 02:00:00', tz=tz, freq='30T'),
|
||||
])
|
||||
expected_elt = expected_rng[1]
|
||||
|
||||
tm.assert_index_equal(rng.round(freq='H'), expected_rng)
|
||||
assert elt.round(freq='H') == expected_elt
|
||||
|
||||
msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
rng.round(freq='foo')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
elt.round(freq='foo')
|
||||
|
||||
msg = "<MonthEnd> is a non-fixed frequency"
|
||||
tm.assert_raises_regex(ValueError, msg, rng.round, freq='M')
|
||||
tm.assert_raises_regex(ValueError, msg, elt.round, freq='M')
|
||||
|
||||
# GH#14440 & GH#15578
|
||||
index = DatetimeIndex(['2016-10-17 12:00:00.0015'], tz=tz)
|
||||
result = index.round('ms')
|
||||
expected = DatetimeIndex(['2016-10-17 12:00:00.002000'], tz=tz)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
for freq in ['us', 'ns']:
|
||||
tm.assert_index_equal(index, index.round(freq))
|
||||
|
||||
index = DatetimeIndex(['2016-10-17 12:00:00.00149'], tz=tz)
|
||||
result = index.round('ms')
|
||||
expected = DatetimeIndex(['2016-10-17 12:00:00.001000'], tz=tz)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
index = DatetimeIndex(['2016-10-17 12:00:00.001501031'])
|
||||
result = index.round('10ns')
|
||||
expected = DatetimeIndex(['2016-10-17 12:00:00.001501030'])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
with tm.assert_produces_warning():
|
||||
ts = '2016-10-17 12:00:00.001501031'
|
||||
DatetimeIndex([ts]).round('1010ns')
|
||||
|
||||
def test_no_rounding_occurs(self, tz):
|
||||
# GH 21262
|
||||
rng = date_range(start='2016-01-01', periods=5,
|
||||
freq='2Min', tz=tz)
|
||||
|
||||
expected_rng = DatetimeIndex([
|
||||
Timestamp('2016-01-01 00:00:00', tz=tz, freq='2T'),
|
||||
Timestamp('2016-01-01 00:02:00', tz=tz, freq='2T'),
|
||||
Timestamp('2016-01-01 00:04:00', tz=tz, freq='2T'),
|
||||
Timestamp('2016-01-01 00:06:00', tz=tz, freq='2T'),
|
||||
Timestamp('2016-01-01 00:08:00', tz=tz, freq='2T'),
|
||||
])
|
||||
|
||||
tm.assert_index_equal(rng.round(freq='2T'), expected_rng)
|
||||
|
||||
@pytest.mark.parametrize('test_input, rounder, freq, expected', [
|
||||
(['2117-01-01 00:00:45'], 'floor', '15s', ['2117-01-01 00:00:45']),
|
||||
(['2117-01-01 00:00:45'], 'ceil', '15s', ['2117-01-01 00:00:45']),
|
||||
(['2117-01-01 00:00:45.000000012'], 'floor', '10ns',
|
||||
['2117-01-01 00:00:45.000000010']),
|
||||
(['1823-01-01 00:00:01.000000012'], 'ceil', '10ns',
|
||||
['1823-01-01 00:00:01.000000020']),
|
||||
(['1823-01-01 00:00:01'], 'floor', '1s', ['1823-01-01 00:00:01']),
|
||||
(['1823-01-01 00:00:01'], 'ceil', '1s', ['1823-01-01 00:00:01']),
|
||||
(['2018-01-01 00:15:00'], 'ceil', '15T', ['2018-01-01 00:15:00']),
|
||||
(['2018-01-01 00:15:00'], 'floor', '15T', ['2018-01-01 00:15:00']),
|
||||
(['1823-01-01 03:00:00'], 'ceil', '3H', ['1823-01-01 03:00:00']),
|
||||
(['1823-01-01 03:00:00'], 'floor', '3H', ['1823-01-01 03:00:00']),
|
||||
(('NaT', '1823-01-01 00:00:01'), 'floor', '1s',
|
||||
('NaT', '1823-01-01 00:00:01')),
|
||||
(('NaT', '1823-01-01 00:00:01'), 'ceil', '1s',
|
||||
('NaT', '1823-01-01 00:00:01'))
|
||||
])
|
||||
def test_ceil_floor_edge(self, tz, test_input, rounder, freq, expected):
|
||||
dt = DatetimeIndex(list(test_input))
|
||||
func = getattr(dt, rounder)
|
||||
result = func(freq)
|
||||
expected = DatetimeIndex(list(expected))
|
||||
assert expected.equals(result)
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# DatetimeIndex.normalize
|
||||
|
||||
def test_normalize(self):
|
||||
rng = date_range('1/1/2000 9:30', periods=10, freq='D')
|
||||
|
||||
result = rng.normalize()
|
||||
expected = date_range('1/1/2000', periods=10, freq='D')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
arr_ns = np.array([1380585623454345752,
|
||||
1380585612343234312]).astype("datetime64[ns]")
|
||||
rng_ns = DatetimeIndex(arr_ns)
|
||||
rng_ns_normalized = rng_ns.normalize()
|
||||
|
||||
arr_ns = np.array([1380585600000000000,
|
||||
1380585600000000000]).astype("datetime64[ns]")
|
||||
expected = DatetimeIndex(arr_ns)
|
||||
tm.assert_index_equal(rng_ns_normalized, expected)
|
||||
|
||||
assert result.is_normalized
|
||||
assert not rng.is_normalized
|
||||
|
||||
|
||||
class TestDateTimeIndexToJulianDate(object):
|
||||
|
||||
def test_1700(self):
|
||||
dr = date_range(start=Timestamp('1710-10-01'), periods=5, freq='D')
|
||||
r1 = pd.Index([x.to_julian_date() for x in dr])
|
||||
r2 = dr.to_julian_date()
|
||||
assert isinstance(r2, pd.Float64Index)
|
||||
tm.assert_index_equal(r1, r2)
|
||||
|
||||
def test_2000(self):
|
||||
dr = date_range(start=Timestamp('2000-02-27'), periods=5, freq='D')
|
||||
r1 = pd.Index([x.to_julian_date() for x in dr])
|
||||
r2 = dr.to_julian_date()
|
||||
assert isinstance(r2, pd.Float64Index)
|
||||
tm.assert_index_equal(r1, r2)
|
||||
|
||||
def test_hour(self):
|
||||
dr = date_range(start=Timestamp('2000-02-27'), periods=5, freq='H')
|
||||
r1 = pd.Index([x.to_julian_date() for x in dr])
|
||||
r2 = dr.to_julian_date()
|
||||
assert isinstance(r2, pd.Float64Index)
|
||||
tm.assert_index_equal(r1, r2)
|
||||
|
||||
def test_minute(self):
|
||||
dr = date_range(start=Timestamp('2000-02-27'), periods=5, freq='T')
|
||||
r1 = pd.Index([x.to_julian_date() for x in dr])
|
||||
r2 = dr.to_julian_date()
|
||||
assert isinstance(r2, pd.Float64Index)
|
||||
tm.assert_index_equal(r1, r2)
|
||||
|
||||
def test_second(self):
|
||||
dr = date_range(start=Timestamp('2000-02-27'), periods=5, freq='S')
|
||||
r1 = pd.Index([x.to_julian_date() for x in dr])
|
||||
r2 = dr.to_julian_date()
|
||||
assert isinstance(r2, pd.Float64Index)
|
||||
tm.assert_index_equal(r1, r2)
|
||||
-486
@@ -1,486 +0,0 @@
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
import numpy as np
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
import pandas.util._test_decorators as td
|
||||
from pandas import (DatetimeIndex, date_range, Series, bdate_range, DataFrame,
|
||||
Int64Index, Index, to_datetime)
|
||||
from pandas.tseries.offsets import Minute, BMonthEnd, MonthEnd
|
||||
|
||||
START, END = datetime(2009, 1, 1), datetime(2010, 1, 1)
|
||||
|
||||
|
||||
class TestDatetimeIndexSetOps(object):
|
||||
tz = [None, 'UTC', 'Asia/Tokyo', 'US/Eastern', 'dateutil/Asia/Singapore',
|
||||
'dateutil/US/Pacific']
|
||||
|
||||
# TODO: moved from test_datetimelike; dedup with version below
|
||||
def test_union2(self):
|
||||
everything = tm.makeDateIndex(10)
|
||||
first = everything[:5]
|
||||
second = everything[5:]
|
||||
union = first.union(second)
|
||||
assert tm.equalContents(union, everything)
|
||||
|
||||
# GH 10149
|
||||
cases = [klass(second.values) for klass in [np.array, Series, list]]
|
||||
for case in cases:
|
||||
result = first.union(case)
|
||||
assert tm.equalContents(result, everything)
|
||||
|
||||
@pytest.mark.parametrize("tz", tz)
|
||||
def test_union(self, tz):
|
||||
rng1 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
|
||||
other1 = pd.date_range('1/6/2000', freq='D', periods=5, tz=tz)
|
||||
expected1 = pd.date_range('1/1/2000', freq='D', periods=10, tz=tz)
|
||||
|
||||
rng2 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
|
||||
other2 = pd.date_range('1/4/2000', freq='D', periods=5, tz=tz)
|
||||
expected2 = pd.date_range('1/1/2000', freq='D', periods=8, tz=tz)
|
||||
|
||||
rng3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
|
||||
other3 = pd.DatetimeIndex([], tz=tz)
|
||||
expected3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
|
||||
|
||||
for rng, other, expected in [(rng1, other1, expected1),
|
||||
(rng2, other2, expected2),
|
||||
(rng3, other3, expected3)]:
|
||||
|
||||
result_union = rng.union(other)
|
||||
tm.assert_index_equal(result_union, expected)
|
||||
|
||||
def test_union_coverage(self):
|
||||
idx = DatetimeIndex(['2000-01-03', '2000-01-01', '2000-01-02'])
|
||||
ordered = DatetimeIndex(idx.sort_values(), freq='infer')
|
||||
result = ordered.union(idx)
|
||||
tm.assert_index_equal(result, ordered)
|
||||
|
||||
result = ordered[:0].union(ordered)
|
||||
tm.assert_index_equal(result, ordered)
|
||||
assert result.freq == ordered.freq
|
||||
|
||||
def test_union_bug_1730(self):
|
||||
rng_a = date_range('1/1/2012', periods=4, freq='3H')
|
||||
rng_b = date_range('1/1/2012', periods=4, freq='4H')
|
||||
|
||||
result = rng_a.union(rng_b)
|
||||
exp = DatetimeIndex(sorted(set(list(rng_a)) | set(list(rng_b))))
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
def test_union_bug_1745(self):
|
||||
left = DatetimeIndex(['2012-05-11 15:19:49.695000'])
|
||||
right = DatetimeIndex(['2012-05-29 13:04:21.322000',
|
||||
'2012-05-11 15:27:24.873000',
|
||||
'2012-05-11 15:31:05.350000'])
|
||||
|
||||
result = left.union(right)
|
||||
exp = DatetimeIndex(sorted(set(list(left)) | set(list(right))))
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
def test_union_bug_4564(self):
|
||||
from pandas import DateOffset
|
||||
left = date_range("2013-01-01", "2013-02-01")
|
||||
right = left + DateOffset(minutes=15)
|
||||
|
||||
result = left.union(right)
|
||||
exp = DatetimeIndex(sorted(set(list(left)) | set(list(right))))
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
def test_union_freq_both_none(self):
|
||||
# GH11086
|
||||
expected = bdate_range('20150101', periods=10)
|
||||
expected.freq = None
|
||||
|
||||
result = expected.union(expected)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq is None
|
||||
|
||||
def test_union_dataframe_index(self):
|
||||
rng1 = date_range('1/1/1999', '1/1/2012', freq='MS')
|
||||
s1 = Series(np.random.randn(len(rng1)), rng1)
|
||||
|
||||
rng2 = date_range('1/1/1980', '12/1/2001', freq='MS')
|
||||
s2 = Series(np.random.randn(len(rng2)), rng2)
|
||||
df = DataFrame({'s1': s1, 's2': s2})
|
||||
|
||||
exp = pd.date_range('1/1/1980', '1/1/2012', freq='MS')
|
||||
tm.assert_index_equal(df.index, exp)
|
||||
|
||||
def test_union_with_DatetimeIndex(self):
|
||||
i1 = Int64Index(np.arange(0, 20, 2))
|
||||
i2 = DatetimeIndex(start='2012-01-03 00:00:00', periods=10, freq='D')
|
||||
i1.union(i2) # Works
|
||||
i2.union(i1) # Fails with "AttributeError: can't set attribute"
|
||||
|
||||
# TODO: moved from test_datetimelike; de-duplicate with version below
|
||||
def test_intersection2(self):
|
||||
first = tm.makeDateIndex(10)
|
||||
second = first[5:]
|
||||
intersect = first.intersection(second)
|
||||
assert tm.equalContents(intersect, second)
|
||||
|
||||
# GH 10149
|
||||
cases = [klass(second.values) for klass in [np.array, Series, list]]
|
||||
for case in cases:
|
||||
result = first.intersection(case)
|
||||
assert tm.equalContents(result, second)
|
||||
|
||||
third = Index(['a', 'b', 'c'])
|
||||
result = first.intersection(third)
|
||||
expected = pd.Index([], dtype=object)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize("tz", [None, 'Asia/Tokyo', 'US/Eastern',
|
||||
'dateutil/US/Pacific'])
|
||||
def test_intersection(self, tz):
|
||||
# GH 4690 (with tz)
|
||||
base = date_range('6/1/2000', '6/30/2000', freq='D', name='idx')
|
||||
|
||||
# if target has the same name, it is preserved
|
||||
rng2 = date_range('5/15/2000', '6/20/2000', freq='D', name='idx')
|
||||
expected2 = date_range('6/1/2000', '6/20/2000', freq='D', name='idx')
|
||||
|
||||
# if target name is different, it will be reset
|
||||
rng3 = date_range('5/15/2000', '6/20/2000', freq='D', name='other')
|
||||
expected3 = date_range('6/1/2000', '6/20/2000', freq='D', name=None)
|
||||
|
||||
rng4 = date_range('7/1/2000', '7/31/2000', freq='D', name='idx')
|
||||
expected4 = DatetimeIndex([], name='idx')
|
||||
|
||||
for (rng, expected) in [(rng2, expected2), (rng3, expected3),
|
||||
(rng4, expected4)]:
|
||||
result = base.intersection(rng)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq == expected.freq
|
||||
assert result.tz == expected.tz
|
||||
|
||||
# non-monotonic
|
||||
base = DatetimeIndex(['2011-01-05', '2011-01-04',
|
||||
'2011-01-02', '2011-01-03'],
|
||||
tz=tz, name='idx')
|
||||
|
||||
rng2 = DatetimeIndex(['2011-01-04', '2011-01-02',
|
||||
'2011-02-02', '2011-02-03'],
|
||||
tz=tz, name='idx')
|
||||
expected2 = DatetimeIndex(['2011-01-04', '2011-01-02'],
|
||||
tz=tz, name='idx')
|
||||
|
||||
rng3 = DatetimeIndex(['2011-01-04', '2011-01-02',
|
||||
'2011-02-02', '2011-02-03'],
|
||||
tz=tz, name='other')
|
||||
expected3 = DatetimeIndex(['2011-01-04', '2011-01-02'],
|
||||
tz=tz, name=None)
|
||||
|
||||
# GH 7880
|
||||
rng4 = date_range('7/1/2000', '7/31/2000', freq='D', tz=tz,
|
||||
name='idx')
|
||||
expected4 = DatetimeIndex([], tz=tz, name='idx')
|
||||
|
||||
for (rng, expected) in [(rng2, expected2), (rng3, expected3),
|
||||
(rng4, expected4)]:
|
||||
result = base.intersection(rng)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq is None
|
||||
assert result.tz == expected.tz
|
||||
|
||||
def test_intersection_empty(self):
|
||||
# empty same freq GH2129
|
||||
rng = date_range('6/1/2000', '6/15/2000', freq='T')
|
||||
result = rng[0:0].intersection(rng)
|
||||
assert len(result) == 0
|
||||
|
||||
result = rng.intersection(rng[0:0])
|
||||
assert len(result) == 0
|
||||
|
||||
def test_intersection_bug_1708(self):
|
||||
from pandas import DateOffset
|
||||
index_1 = date_range('1/1/2012', periods=4, freq='12H')
|
||||
index_2 = index_1 + DateOffset(hours=1)
|
||||
|
||||
result = index_1 & index_2
|
||||
assert len(result) == 0
|
||||
|
||||
@pytest.mark.parametrize("tz", tz)
|
||||
def test_difference(self, tz):
|
||||
rng1 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
|
||||
other1 = pd.date_range('1/6/2000', freq='D', periods=5, tz=tz)
|
||||
expected1 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
|
||||
|
||||
rng2 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
|
||||
other2 = pd.date_range('1/4/2000', freq='D', periods=5, tz=tz)
|
||||
expected2 = pd.date_range('1/1/2000', freq='D', periods=3, tz=tz)
|
||||
|
||||
rng3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
|
||||
other3 = pd.DatetimeIndex([], tz=tz)
|
||||
expected3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
|
||||
|
||||
for rng, other, expected in [(rng1, other1, expected1),
|
||||
(rng2, other2, expected2),
|
||||
(rng3, other3, expected3)]:
|
||||
result_diff = rng.difference(other)
|
||||
tm.assert_index_equal(result_diff, expected)
|
||||
|
||||
def test_difference_freq(self):
|
||||
# GH14323: difference of DatetimeIndex should not preserve frequency
|
||||
|
||||
index = date_range("20160920", "20160925", freq="D")
|
||||
other = date_range("20160921", "20160924", freq="D")
|
||||
expected = DatetimeIndex(["20160920", "20160925"], freq=None)
|
||||
idx_diff = index.difference(other)
|
||||
tm.assert_index_equal(idx_diff, expected)
|
||||
tm.assert_attr_equal('freq', idx_diff, expected)
|
||||
|
||||
other = date_range("20160922", "20160925", freq="D")
|
||||
idx_diff = index.difference(other)
|
||||
expected = DatetimeIndex(["20160920", "20160921"], freq=None)
|
||||
tm.assert_index_equal(idx_diff, expected)
|
||||
tm.assert_attr_equal('freq', idx_diff, expected)
|
||||
|
||||
def test_datetimeindex_diff(self):
|
||||
dti1 = DatetimeIndex(freq='Q-JAN', start=datetime(1997, 12, 31),
|
||||
periods=100)
|
||||
dti2 = DatetimeIndex(freq='Q-JAN', start=datetime(1997, 12, 31),
|
||||
periods=98)
|
||||
assert len(dti1.difference(dti2)) == 2
|
||||
|
||||
def test_datetimeindex_union_join_empty(self):
|
||||
dti = DatetimeIndex(start='1/1/2001', end='2/1/2001', freq='D')
|
||||
empty = Index([])
|
||||
|
||||
result = dti.union(empty)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
assert result is result
|
||||
|
||||
result = dti.join(empty)
|
||||
assert isinstance(result, DatetimeIndex)
|
||||
|
||||
def test_join_nonunique(self):
|
||||
idx1 = to_datetime(['2012-11-06 16:00:11.477563',
|
||||
'2012-11-06 16:00:11.477563'])
|
||||
idx2 = to_datetime(['2012-11-06 15:11:09.006507',
|
||||
'2012-11-06 15:11:09.006507'])
|
||||
rs = idx1.join(idx2, how='outer')
|
||||
assert rs.is_monotonic
|
||||
|
||||
|
||||
class TestBusinessDatetimeIndex(object):
|
||||
|
||||
def setup_method(self, method):
|
||||
self.rng = bdate_range(START, END)
|
||||
|
||||
def test_union(self):
|
||||
# overlapping
|
||||
left = self.rng[:10]
|
||||
right = self.rng[5:10]
|
||||
|
||||
the_union = left.union(right)
|
||||
assert isinstance(the_union, DatetimeIndex)
|
||||
|
||||
# non-overlapping, gap in middle
|
||||
left = self.rng[:5]
|
||||
right = self.rng[10:]
|
||||
|
||||
the_union = left.union(right)
|
||||
assert isinstance(the_union, Index)
|
||||
|
||||
# non-overlapping, no gap
|
||||
left = self.rng[:5]
|
||||
right = self.rng[5:10]
|
||||
|
||||
the_union = left.union(right)
|
||||
assert isinstance(the_union, DatetimeIndex)
|
||||
|
||||
# order does not matter
|
||||
tm.assert_index_equal(right.union(left), the_union)
|
||||
|
||||
# overlapping, but different offset
|
||||
rng = date_range(START, END, freq=BMonthEnd())
|
||||
|
||||
the_union = self.rng.union(rng)
|
||||
assert isinstance(the_union, DatetimeIndex)
|
||||
|
||||
def test_outer_join(self):
|
||||
# should just behave as union
|
||||
|
||||
# overlapping
|
||||
left = self.rng[:10]
|
||||
right = self.rng[5:10]
|
||||
|
||||
the_join = left.join(right, how='outer')
|
||||
assert isinstance(the_join, DatetimeIndex)
|
||||
|
||||
# non-overlapping, gap in middle
|
||||
left = self.rng[:5]
|
||||
right = self.rng[10:]
|
||||
|
||||
the_join = left.join(right, how='outer')
|
||||
assert isinstance(the_join, DatetimeIndex)
|
||||
assert the_join.freq is None
|
||||
|
||||
# non-overlapping, no gap
|
||||
left = self.rng[:5]
|
||||
right = self.rng[5:10]
|
||||
|
||||
the_join = left.join(right, how='outer')
|
||||
assert isinstance(the_join, DatetimeIndex)
|
||||
|
||||
# overlapping, but different offset
|
||||
rng = date_range(START, END, freq=BMonthEnd())
|
||||
|
||||
the_join = self.rng.join(rng, how='outer')
|
||||
assert isinstance(the_join, DatetimeIndex)
|
||||
assert the_join.freq is None
|
||||
|
||||
def test_union_not_cacheable(self):
|
||||
rng = date_range('1/1/2000', periods=50, freq=Minute())
|
||||
rng1 = rng[10:]
|
||||
rng2 = rng[:25]
|
||||
the_union = rng1.union(rng2)
|
||||
tm.assert_index_equal(the_union, rng)
|
||||
|
||||
rng1 = rng[10:]
|
||||
rng2 = rng[15:35]
|
||||
the_union = rng1.union(rng2)
|
||||
expected = rng[10:]
|
||||
tm.assert_index_equal(the_union, expected)
|
||||
|
||||
def test_intersection(self):
|
||||
rng = date_range('1/1/2000', periods=50, freq=Minute())
|
||||
rng1 = rng[10:]
|
||||
rng2 = rng[:25]
|
||||
the_int = rng1.intersection(rng2)
|
||||
expected = rng[10:25]
|
||||
tm.assert_index_equal(the_int, expected)
|
||||
assert isinstance(the_int, DatetimeIndex)
|
||||
assert the_int.freq == rng.freq
|
||||
|
||||
the_int = rng1.intersection(rng2.view(DatetimeIndex))
|
||||
tm.assert_index_equal(the_int, expected)
|
||||
|
||||
# non-overlapping
|
||||
the_int = rng[:10].intersection(rng[10:])
|
||||
expected = DatetimeIndex([])
|
||||
tm.assert_index_equal(the_int, expected)
|
||||
|
||||
def test_intersection_bug(self):
|
||||
# GH #771
|
||||
a = bdate_range('11/30/2011', '12/31/2011')
|
||||
b = bdate_range('12/10/2011', '12/20/2011')
|
||||
result = a.intersection(b)
|
||||
tm.assert_index_equal(result, b)
|
||||
|
||||
def test_month_range_union_tz_pytz(self):
|
||||
from pytz import timezone
|
||||
tz = timezone('US/Eastern')
|
||||
|
||||
early_start = datetime(2011, 1, 1)
|
||||
early_end = datetime(2011, 3, 1)
|
||||
|
||||
late_start = datetime(2011, 3, 1)
|
||||
late_end = datetime(2011, 5, 1)
|
||||
|
||||
early_dr = date_range(start=early_start, end=early_end, tz=tz,
|
||||
freq=MonthEnd())
|
||||
late_dr = date_range(start=late_start, end=late_end, tz=tz,
|
||||
freq=MonthEnd())
|
||||
|
||||
early_dr.union(late_dr)
|
||||
|
||||
@td.skip_if_windows_python_3
|
||||
def test_month_range_union_tz_dateutil(self):
|
||||
from pandas._libs.tslibs.timezones import dateutil_gettz
|
||||
tz = dateutil_gettz('US/Eastern')
|
||||
|
||||
early_start = datetime(2011, 1, 1)
|
||||
early_end = datetime(2011, 3, 1)
|
||||
|
||||
late_start = datetime(2011, 3, 1)
|
||||
late_end = datetime(2011, 5, 1)
|
||||
|
||||
early_dr = date_range(start=early_start, end=early_end, tz=tz,
|
||||
freq=MonthEnd())
|
||||
late_dr = date_range(start=late_start, end=late_end, tz=tz,
|
||||
freq=MonthEnd())
|
||||
|
||||
early_dr.union(late_dr)
|
||||
|
||||
|
||||
class TestCustomDatetimeIndex(object):
|
||||
|
||||
def setup_method(self, method):
|
||||
self.rng = bdate_range(START, END, freq='C')
|
||||
|
||||
def test_union(self):
|
||||
# overlapping
|
||||
left = self.rng[:10]
|
||||
right = self.rng[5:10]
|
||||
|
||||
the_union = left.union(right)
|
||||
assert isinstance(the_union, DatetimeIndex)
|
||||
|
||||
# non-overlapping, gap in middle
|
||||
left = self.rng[:5]
|
||||
right = self.rng[10:]
|
||||
|
||||
the_union = left.union(right)
|
||||
assert isinstance(the_union, Index)
|
||||
|
||||
# non-overlapping, no gap
|
||||
left = self.rng[:5]
|
||||
right = self.rng[5:10]
|
||||
|
||||
the_union = left.union(right)
|
||||
assert isinstance(the_union, DatetimeIndex)
|
||||
|
||||
# order does not matter
|
||||
tm.assert_index_equal(right.union(left), the_union)
|
||||
|
||||
# overlapping, but different offset
|
||||
rng = date_range(START, END, freq=BMonthEnd())
|
||||
|
||||
the_union = self.rng.union(rng)
|
||||
assert isinstance(the_union, DatetimeIndex)
|
||||
|
||||
def test_outer_join(self):
|
||||
# should just behave as union
|
||||
|
||||
# overlapping
|
||||
left = self.rng[:10]
|
||||
right = self.rng[5:10]
|
||||
|
||||
the_join = left.join(right, how='outer')
|
||||
assert isinstance(the_join, DatetimeIndex)
|
||||
|
||||
# non-overlapping, gap in middle
|
||||
left = self.rng[:5]
|
||||
right = self.rng[10:]
|
||||
|
||||
the_join = left.join(right, how='outer')
|
||||
assert isinstance(the_join, DatetimeIndex)
|
||||
assert the_join.freq is None
|
||||
|
||||
# non-overlapping, no gap
|
||||
left = self.rng[:5]
|
||||
right = self.rng[5:10]
|
||||
|
||||
the_join = left.join(right, how='outer')
|
||||
assert isinstance(the_join, DatetimeIndex)
|
||||
|
||||
# overlapping, but different offset
|
||||
rng = date_range(START, END, freq=BMonthEnd())
|
||||
|
||||
the_join = self.rng.join(rng, how='outer')
|
||||
assert isinstance(the_join, DatetimeIndex)
|
||||
assert the_join.freq is None
|
||||
|
||||
def test_intersection_bug(self):
|
||||
# GH #771
|
||||
a = bdate_range('11/30/2011', '12/31/2011', freq='C')
|
||||
b = bdate_range('12/10/2011', '12/20/2011', freq='C')
|
||||
result = a.intersection(b)
|
||||
tm.assert_index_equal(result, b)
|
||||
-1046
File diff suppressed because it is too large
Load Diff
-1599
File diff suppressed because it is too large
Load Diff
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
-209
@@ -1,209 +0,0 @@
|
||||
from __future__ import division
|
||||
|
||||
import pytest
|
||||
import numpy as np
|
||||
from pandas import (
|
||||
Index,
|
||||
IntervalIndex,
|
||||
interval_range,
|
||||
CategoricalIndex,
|
||||
Timestamp,
|
||||
Timedelta,
|
||||
NaT)
|
||||
from pandas.core.dtypes.dtypes import CategoricalDtype, IntervalDtype
|
||||
import pandas.util.testing as tm
|
||||
|
||||
|
||||
class Base(object):
|
||||
"""Tests common to IntervalIndex with any subtype"""
|
||||
|
||||
def test_astype_idempotent(self, index):
|
||||
result = index.astype('interval')
|
||||
tm.assert_index_equal(result, index)
|
||||
|
||||
result = index.astype(index.dtype)
|
||||
tm.assert_index_equal(result, index)
|
||||
|
||||
def test_astype_object(self, index):
|
||||
result = index.astype(object)
|
||||
expected = Index(index.values, dtype='object')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert not result.equals(index)
|
||||
|
||||
def test_astype_category(self, index):
|
||||
result = index.astype('category')
|
||||
expected = CategoricalIndex(index.values)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = index.astype(CategoricalDtype())
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# non-default params
|
||||
categories = index.dropna().unique().values[:-1]
|
||||
dtype = CategoricalDtype(categories=categories, ordered=True)
|
||||
result = index.astype(dtype)
|
||||
expected = CategoricalIndex(
|
||||
index.values, categories=categories, ordered=True)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize('dtype', [
|
||||
'int64', 'uint64', 'float64', 'complex128', 'period[M]',
|
||||
'timedelta64', 'timedelta64[ns]', 'datetime64', 'datetime64[ns]',
|
||||
'datetime64[ns, US/Eastern]'])
|
||||
def test_astype_cannot_cast(self, index, dtype):
|
||||
msg = 'Cannot cast IntervalIndex to dtype'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
index.astype(dtype)
|
||||
|
||||
def test_astype_invalid_dtype(self, index):
|
||||
msg = 'data type "fake_dtype" not understood'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
index.astype('fake_dtype')
|
||||
|
||||
|
||||
class TestIntSubtype(Base):
|
||||
"""Tests specific to IntervalIndex with integer-like subtype"""
|
||||
|
||||
indexes = [
|
||||
IntervalIndex.from_breaks(np.arange(-10, 11, dtype='int64')),
|
||||
IntervalIndex.from_breaks(
|
||||
np.arange(100, dtype='uint64'), closed='left'),
|
||||
]
|
||||
|
||||
@pytest.fixture(params=indexes)
|
||||
def index(self, request):
|
||||
return request.param
|
||||
|
||||
@pytest.mark.parametrize('subtype', [
|
||||
'float64', 'datetime64[ns]', 'timedelta64[ns]'])
|
||||
def test_subtype_conversion(self, index, subtype):
|
||||
dtype = IntervalDtype(subtype)
|
||||
result = index.astype(dtype)
|
||||
expected = IntervalIndex.from_arrays(index.left.astype(subtype),
|
||||
index.right.astype(subtype),
|
||||
closed=index.closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize('subtype_start, subtype_end', [
|
||||
('int64', 'uint64'), ('uint64', 'int64')])
|
||||
def test_subtype_integer(self, subtype_start, subtype_end):
|
||||
index = IntervalIndex.from_breaks(np.arange(100, dtype=subtype_start))
|
||||
dtype = IntervalDtype(subtype_end)
|
||||
result = index.astype(dtype)
|
||||
expected = IntervalIndex.from_arrays(index.left.astype(subtype_end),
|
||||
index.right.astype(subtype_end),
|
||||
closed=index.closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.xfail(reason='GH 15832')
|
||||
def test_subtype_integer_errors(self):
|
||||
# int64 -> uint64 fails with negative values
|
||||
index = interval_range(-10, 10)
|
||||
dtype = IntervalDtype('uint64')
|
||||
with pytest.raises(ValueError):
|
||||
index.astype(dtype)
|
||||
|
||||
|
||||
class TestFloatSubtype(Base):
|
||||
"""Tests specific to IntervalIndex with float subtype"""
|
||||
|
||||
indexes = [
|
||||
interval_range(-10.0, 10.0, closed='neither'),
|
||||
IntervalIndex.from_arrays([-1.5, np.nan, 0., 0., 1.5],
|
||||
[-0.5, np.nan, 1., 1., 3.],
|
||||
closed='both'),
|
||||
]
|
||||
|
||||
@pytest.fixture(params=indexes)
|
||||
def index(self, request):
|
||||
return request.param
|
||||
|
||||
@pytest.mark.parametrize('subtype', ['int64', 'uint64'])
|
||||
def test_subtype_integer(self, subtype):
|
||||
index = interval_range(0.0, 10.0)
|
||||
dtype = IntervalDtype(subtype)
|
||||
result = index.astype(dtype)
|
||||
expected = IntervalIndex.from_arrays(index.left.astype(subtype),
|
||||
index.right.astype(subtype),
|
||||
closed=index.closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# raises with NA
|
||||
msg = 'Cannot convert NA to integer'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
index.insert(0, np.nan).astype(dtype)
|
||||
|
||||
@pytest.mark.xfail(reason='GH 15832')
|
||||
def test_subtype_integer_errors(self):
|
||||
# float64 -> uint64 fails with negative values
|
||||
index = interval_range(-10.0, 10.0)
|
||||
dtype = IntervalDtype('uint64')
|
||||
with pytest.raises(ValueError):
|
||||
index.astype(dtype)
|
||||
|
||||
# float64 -> integer-like fails with non-integer valued floats
|
||||
index = interval_range(0.0, 10.0, freq=0.25)
|
||||
dtype = IntervalDtype('int64')
|
||||
with pytest.raises(ValueError):
|
||||
index.astype(dtype)
|
||||
|
||||
dtype = IntervalDtype('uint64')
|
||||
with pytest.raises(ValueError):
|
||||
index.astype(dtype)
|
||||
|
||||
@pytest.mark.parametrize('subtype', ['datetime64[ns]', 'timedelta64[ns]'])
|
||||
def test_subtype_datetimelike(self, index, subtype):
|
||||
dtype = IntervalDtype(subtype)
|
||||
msg = 'Cannot convert .* to .*; subtypes are incompatible'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
index.astype(dtype)
|
||||
|
||||
|
||||
class TestDatetimelikeSubtype(Base):
|
||||
"""Tests specific to IntervalIndex with datetime-like subtype"""
|
||||
|
||||
indexes = [
|
||||
interval_range(Timestamp('2018-01-01'), periods=10, closed='neither'),
|
||||
interval_range(Timestamp('2018-01-01'), periods=10).insert(2, NaT),
|
||||
interval_range(Timestamp('2018-01-01', tz='US/Eastern'), periods=10),
|
||||
interval_range(Timedelta('0 days'), periods=10, closed='both'),
|
||||
interval_range(Timedelta('0 days'), periods=10).insert(2, NaT),
|
||||
]
|
||||
|
||||
@pytest.fixture(params=indexes)
|
||||
def index(self, request):
|
||||
return request.param
|
||||
|
||||
@pytest.mark.parametrize('subtype', ['int64', 'uint64'])
|
||||
def test_subtype_integer(self, index, subtype):
|
||||
dtype = IntervalDtype(subtype)
|
||||
result = index.astype(dtype)
|
||||
expected = IntervalIndex.from_arrays(index.left.astype(subtype),
|
||||
index.right.astype(subtype),
|
||||
closed=index.closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_subtype_float(self, index):
|
||||
dtype = IntervalDtype('float64')
|
||||
msg = 'Cannot convert .* to .*; subtypes are incompatible'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
index.astype(dtype)
|
||||
|
||||
def test_subtype_datetimelike(self):
|
||||
# datetime -> timedelta raises
|
||||
dtype = IntervalDtype('timedelta64[ns]')
|
||||
msg = 'Cannot convert .* to .*; subtypes are incompatible'
|
||||
|
||||
index = interval_range(Timestamp('2018-01-01'), periods=10)
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
index.astype(dtype)
|
||||
|
||||
index = interval_range(Timestamp('2018-01-01', tz='CET'), periods=10)
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
index.astype(dtype)
|
||||
|
||||
# timedelta -> datetime raises
|
||||
dtype = IntervalDtype('datetime64[ns]')
|
||||
index = interval_range(Timedelta('0 days'), periods=10)
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
index.astype(dtype)
|
||||
-363
@@ -1,363 +0,0 @@
|
||||
from __future__ import division
|
||||
|
||||
import pytest
|
||||
import numpy as np
|
||||
from functools import partial
|
||||
|
||||
from pandas import (
|
||||
Interval, IntervalIndex, Index, Int64Index, Float64Index, Categorical,
|
||||
CategoricalIndex, date_range, timedelta_range, period_range, notna)
|
||||
from pandas.compat import lzip
|
||||
from pandas.core.dtypes.common import is_categorical_dtype
|
||||
from pandas.core.dtypes.dtypes import IntervalDtype
|
||||
import pandas.core.common as com
|
||||
import pandas.util.testing as tm
|
||||
|
||||
|
||||
@pytest.fixture(params=['left', 'right', 'both', 'neither'])
|
||||
def closed(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(params=[None, 'foo'])
|
||||
def name(request):
|
||||
return request.param
|
||||
|
||||
|
||||
class Base(object):
|
||||
"""
|
||||
Common tests for all variations of IntervalIndex construction. Input data
|
||||
to be supplied in breaks format, then converted by the subclass method
|
||||
get_kwargs_from_breaks to the expected format.
|
||||
"""
|
||||
|
||||
@pytest.mark.parametrize('breaks', [
|
||||
[3, 14, 15, 92, 653],
|
||||
np.arange(10, dtype='int64'),
|
||||
Int64Index(range(-10, 11)),
|
||||
Float64Index(np.arange(20, 30, 0.5)),
|
||||
date_range('20180101', periods=10),
|
||||
date_range('20180101', periods=10, tz='US/Eastern'),
|
||||
timedelta_range('1 day', periods=10)])
|
||||
def test_constructor(self, constructor, breaks, closed, name):
|
||||
result_kwargs = self.get_kwargs_from_breaks(breaks, closed)
|
||||
result = constructor(closed=closed, name=name, **result_kwargs)
|
||||
|
||||
assert result.closed == closed
|
||||
assert result.name == name
|
||||
assert result.dtype.subtype == getattr(breaks, 'dtype', 'int64')
|
||||
tm.assert_index_equal(result.left, Index(breaks[:-1]))
|
||||
tm.assert_index_equal(result.right, Index(breaks[1:]))
|
||||
|
||||
@pytest.mark.parametrize('breaks, subtype', [
|
||||
(Int64Index([0, 1, 2, 3, 4]), 'float64'),
|
||||
(Int64Index([0, 1, 2, 3, 4]), 'datetime64[ns]'),
|
||||
(Int64Index([0, 1, 2, 3, 4]), 'timedelta64[ns]'),
|
||||
(Float64Index([0, 1, 2, 3, 4]), 'int64'),
|
||||
(date_range('2017-01-01', periods=5), 'int64'),
|
||||
(timedelta_range('1 day', periods=5), 'int64')])
|
||||
def test_constructor_dtype(self, constructor, breaks, subtype):
|
||||
# GH 19262: conversion via dtype parameter
|
||||
expected_kwargs = self.get_kwargs_from_breaks(breaks.astype(subtype))
|
||||
expected = constructor(**expected_kwargs)
|
||||
|
||||
result_kwargs = self.get_kwargs_from_breaks(breaks)
|
||||
iv_dtype = IntervalDtype(subtype)
|
||||
for dtype in (iv_dtype, str(iv_dtype)):
|
||||
result = constructor(dtype=dtype, **result_kwargs)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize('breaks', [
|
||||
[np.nan] * 2, [np.nan] * 4, [np.nan] * 50])
|
||||
def test_constructor_nan(self, constructor, breaks, closed):
|
||||
# GH 18421
|
||||
result_kwargs = self.get_kwargs_from_breaks(breaks)
|
||||
result = constructor(closed=closed, **result_kwargs)
|
||||
|
||||
expected_subtype = np.float64
|
||||
expected_values = np.array(breaks[:-1], dtype=object)
|
||||
|
||||
assert result.closed == closed
|
||||
assert result.dtype.subtype == expected_subtype
|
||||
tm.assert_numpy_array_equal(result.values, expected_values)
|
||||
|
||||
@pytest.mark.parametrize('breaks', [
|
||||
[],
|
||||
np.array([], dtype='int64'),
|
||||
np.array([], dtype='float64'),
|
||||
np.array([], dtype='datetime64[ns]'),
|
||||
np.array([], dtype='timedelta64[ns]')])
|
||||
def test_constructor_empty(self, constructor, breaks, closed):
|
||||
# GH 18421
|
||||
result_kwargs = self.get_kwargs_from_breaks(breaks)
|
||||
result = constructor(closed=closed, **result_kwargs)
|
||||
|
||||
expected_values = np.array([], dtype=object)
|
||||
expected_subtype = getattr(breaks, 'dtype', np.int64)
|
||||
|
||||
assert result.empty
|
||||
assert result.closed == closed
|
||||
assert result.dtype.subtype == expected_subtype
|
||||
tm.assert_numpy_array_equal(result.values, expected_values)
|
||||
|
||||
@pytest.mark.parametrize('breaks', [
|
||||
tuple('0123456789'),
|
||||
list('abcdefghij'),
|
||||
np.array(list('abcdefghij'), dtype=object),
|
||||
np.array(list('abcdefghij'), dtype='<U1')])
|
||||
def test_constructor_string(self, constructor, breaks):
|
||||
# GH 19016
|
||||
msg = ('category, object, and string subtypes are not supported '
|
||||
'for IntervalIndex')
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
constructor(**self.get_kwargs_from_breaks(breaks))
|
||||
|
||||
@pytest.mark.parametrize('cat_constructor', [
|
||||
Categorical, CategoricalIndex])
|
||||
def test_constructor_categorical_valid(self, constructor, cat_constructor):
|
||||
# GH 21243/21253
|
||||
if isinstance(constructor, partial) and constructor.func is Index:
|
||||
# Index is defined to create CategoricalIndex from categorical data
|
||||
pytest.skip()
|
||||
|
||||
breaks = np.arange(10, dtype='int64')
|
||||
expected = IntervalIndex.from_breaks(breaks)
|
||||
|
||||
cat_breaks = cat_constructor(breaks)
|
||||
result_kwargs = self.get_kwargs_from_breaks(cat_breaks)
|
||||
result = constructor(**result_kwargs)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_generic_errors(self, constructor):
|
||||
# filler input data to be used when supplying invalid kwargs
|
||||
filler = self.get_kwargs_from_breaks(range(10))
|
||||
|
||||
# invalid closed
|
||||
msg = "invalid option for 'closed': invalid"
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
constructor(closed='invalid', **filler)
|
||||
|
||||
# unsupported dtype
|
||||
msg = 'dtype must be an IntervalDtype, got int64'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
constructor(dtype='int64', **filler)
|
||||
|
||||
# invalid dtype
|
||||
msg = 'data type "invalid" not understood'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
constructor(dtype='invalid', **filler)
|
||||
|
||||
# no point in nesting periods in an IntervalIndex
|
||||
periods = period_range('2000-01-01', periods=10)
|
||||
periods_kwargs = self.get_kwargs_from_breaks(periods)
|
||||
msg = 'Period dtypes are not supported, use a PeriodIndex instead'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
constructor(**periods_kwargs)
|
||||
|
||||
# decreasing values
|
||||
decreasing_kwargs = self.get_kwargs_from_breaks(range(10, -1, -1))
|
||||
msg = 'left side of interval must be <= right side'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
constructor(**decreasing_kwargs)
|
||||
|
||||
|
||||
class TestFromArrays(Base):
|
||||
"""Tests specific to IntervalIndex.from_arrays"""
|
||||
|
||||
@pytest.fixture
|
||||
def constructor(self):
|
||||
return IntervalIndex.from_arrays
|
||||
|
||||
def get_kwargs_from_breaks(self, breaks, closed='right'):
|
||||
"""
|
||||
converts intervals in breaks format to a dictionary of kwargs to
|
||||
specific to the format expected by IntervalIndex.from_arrays
|
||||
"""
|
||||
return {'left': breaks[:-1], 'right': breaks[1:]}
|
||||
|
||||
def test_constructor_errors(self):
|
||||
# GH 19016: categorical data
|
||||
data = Categorical(list('01234abcde'), ordered=True)
|
||||
msg = ('category, object, and string subtypes are not supported '
|
||||
'for IntervalIndex')
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
IntervalIndex.from_arrays(data[:-1], data[1:])
|
||||
|
||||
# unequal length
|
||||
left = [0, 1, 2]
|
||||
right = [2, 3]
|
||||
msg = 'left and right must have the same length'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
IntervalIndex.from_arrays(left, right)
|
||||
|
||||
@pytest.mark.parametrize('left_subtype, right_subtype', [
|
||||
(np.int64, np.float64), (np.float64, np.int64)])
|
||||
def test_mixed_float_int(self, left_subtype, right_subtype):
|
||||
"""mixed int/float left/right results in float for both sides"""
|
||||
left = np.arange(9, dtype=left_subtype)
|
||||
right = np.arange(1, 10, dtype=right_subtype)
|
||||
result = IntervalIndex.from_arrays(left, right)
|
||||
|
||||
expected_left = Float64Index(left)
|
||||
expected_right = Float64Index(right)
|
||||
expected_subtype = np.float64
|
||||
|
||||
tm.assert_index_equal(result.left, expected_left)
|
||||
tm.assert_index_equal(result.right, expected_right)
|
||||
assert result.dtype.subtype == expected_subtype
|
||||
|
||||
|
||||
class TestFromBreaks(Base):
|
||||
"""Tests specific to IntervalIndex.from_breaks"""
|
||||
|
||||
@pytest.fixture
|
||||
def constructor(self):
|
||||
return IntervalIndex.from_breaks
|
||||
|
||||
def get_kwargs_from_breaks(self, breaks, closed='right'):
|
||||
"""
|
||||
converts intervals in breaks format to a dictionary of kwargs to
|
||||
specific to the format expected by IntervalIndex.from_breaks
|
||||
"""
|
||||
return {'breaks': breaks}
|
||||
|
||||
def test_constructor_errors(self):
|
||||
# GH 19016: categorical data
|
||||
data = Categorical(list('01234abcde'), ordered=True)
|
||||
msg = ('category, object, and string subtypes are not supported '
|
||||
'for IntervalIndex')
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
IntervalIndex.from_breaks(data)
|
||||
|
||||
def test_length_one(self):
|
||||
"""breaks of length one produce an empty IntervalIndex"""
|
||||
breaks = [0]
|
||||
result = IntervalIndex.from_breaks(breaks)
|
||||
expected = IntervalIndex.from_breaks([])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
|
||||
class TestFromTuples(Base):
|
||||
"""Tests specific to IntervalIndex.from_tuples"""
|
||||
|
||||
@pytest.fixture
|
||||
def constructor(self):
|
||||
return IntervalIndex.from_tuples
|
||||
|
||||
def get_kwargs_from_breaks(self, breaks, closed='right'):
|
||||
"""
|
||||
converts intervals in breaks format to a dictionary of kwargs to
|
||||
specific to the format expected by IntervalIndex.from_tuples
|
||||
"""
|
||||
if len(breaks) == 0:
|
||||
return {'data': breaks}
|
||||
|
||||
tuples = lzip(breaks[:-1], breaks[1:])
|
||||
if isinstance(breaks, (list, tuple)):
|
||||
return {'data': tuples}
|
||||
elif is_categorical_dtype(breaks):
|
||||
return {'data': breaks._constructor(tuples)}
|
||||
return {'data': com._asarray_tuplesafe(tuples)}
|
||||
|
||||
def test_constructor_errors(self):
|
||||
# non-tuple
|
||||
tuples = [(0, 1), 2, (3, 4)]
|
||||
msg = 'IntervalIndex.from_tuples received an invalid item, 2'
|
||||
with tm.assert_raises_regex(TypeError, msg.format(t=tuples)):
|
||||
IntervalIndex.from_tuples(tuples)
|
||||
|
||||
# too few/many items
|
||||
tuples = [(0, 1), (2,), (3, 4)]
|
||||
msg = 'IntervalIndex.from_tuples requires tuples of length 2, got {t}'
|
||||
with tm.assert_raises_regex(ValueError, msg.format(t=tuples)):
|
||||
IntervalIndex.from_tuples(tuples)
|
||||
|
||||
tuples = [(0, 1), (2, 3, 4), (5, 6)]
|
||||
with tm.assert_raises_regex(ValueError, msg.format(t=tuples)):
|
||||
IntervalIndex.from_tuples(tuples)
|
||||
|
||||
def test_na_tuples(self):
|
||||
# tuple (NA, NA) evaluates the same as NA as an elemenent
|
||||
na_tuple = [(0, 1), (np.nan, np.nan), (2, 3)]
|
||||
idx_na_tuple = IntervalIndex.from_tuples(na_tuple)
|
||||
idx_na_element = IntervalIndex.from_tuples([(0, 1), np.nan, (2, 3)])
|
||||
tm.assert_index_equal(idx_na_tuple, idx_na_element)
|
||||
|
||||
|
||||
class TestClassConstructors(Base):
|
||||
"""Tests specific to the IntervalIndex/Index constructors"""
|
||||
|
||||
@pytest.fixture(params=[IntervalIndex, partial(Index, dtype='interval')],
|
||||
ids=['IntervalIndex', 'Index'])
|
||||
def constructor(self, request):
|
||||
return request.param
|
||||
|
||||
def get_kwargs_from_breaks(self, breaks, closed='right'):
|
||||
"""
|
||||
converts intervals in breaks format to a dictionary of kwargs to
|
||||
specific to the format expected by the IntervalIndex/Index constructors
|
||||
"""
|
||||
if len(breaks) == 0:
|
||||
return {'data': breaks}
|
||||
|
||||
ivs = [Interval(l, r, closed) if notna(l) else l
|
||||
for l, r in zip(breaks[:-1], breaks[1:])]
|
||||
|
||||
if isinstance(breaks, list):
|
||||
return {'data': ivs}
|
||||
elif is_categorical_dtype(breaks):
|
||||
return {'data': breaks._constructor(ivs)}
|
||||
return {'data': np.array(ivs, dtype=object)}
|
||||
|
||||
def test_generic_errors(self, constructor):
|
||||
"""
|
||||
override the base class implementation since errors are handled
|
||||
differently; checks unnecessary since caught at the Interval level
|
||||
"""
|
||||
pass
|
||||
|
||||
def test_constructor_errors(self, constructor):
|
||||
# mismatched closed inferred from intervals vs constructor.
|
||||
ivs = [Interval(0, 1, closed='both'), Interval(1, 2, closed='both')]
|
||||
msg = 'conflicting values for closed'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
constructor(ivs, closed='neither')
|
||||
|
||||
# mismatched closed within intervals
|
||||
ivs = [Interval(0, 1, closed='right'), Interval(2, 3, closed='left')]
|
||||
msg = 'intervals must all be closed on the same side'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
constructor(ivs)
|
||||
|
||||
# scalar
|
||||
msg = (r'IntervalIndex\(...\) must be called with a collection of '
|
||||
'some kind, 5 was passed')
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
constructor(5)
|
||||
|
||||
# not an interval
|
||||
msg = ("type <(class|type) 'numpy.int64'> with value 0 "
|
||||
"is not an interval")
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
constructor([0, 1])
|
||||
|
||||
|
||||
class TestFromIntervals(TestClassConstructors):
|
||||
"""
|
||||
Tests for IntervalIndex.from_intervals, which is deprecated in favor of the
|
||||
IntervalIndex constructor. Same tests as the IntervalIndex constructor,
|
||||
plus deprecation test. Should only need to delete this class when removed.
|
||||
"""
|
||||
|
||||
@pytest.fixture
|
||||
def constructor(self):
|
||||
def from_intervals_ignore_warnings(*args, **kwargs):
|
||||
with tm.assert_produces_warning(FutureWarning,
|
||||
check_stacklevel=False):
|
||||
return IntervalIndex.from_intervals(*args, **kwargs)
|
||||
return from_intervals_ignore_warnings
|
||||
|
||||
def test_deprecated(self):
|
||||
ivs = [Interval(0, 1), Interval(1, 2)]
|
||||
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
|
||||
IntervalIndex.from_intervals(ivs)
|
||||
-984
@@ -1,984 +0,0 @@
|
||||
from __future__ import division
|
||||
|
||||
import pytest
|
||||
import numpy as np
|
||||
from pandas import (
|
||||
Interval, IntervalIndex, Index, isna, notna, interval_range, Timestamp,
|
||||
Timedelta, date_range, timedelta_range)
|
||||
from pandas.compat import lzip
|
||||
import pandas.core.common as com
|
||||
from pandas.tests.indexes.common import Base
|
||||
import pandas.util.testing as tm
|
||||
import pandas as pd
|
||||
|
||||
|
||||
@pytest.fixture(scope='class', params=['left', 'right', 'both', 'neither'])
|
||||
def closed(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(scope='class', params=[None, 'foo'])
|
||||
def name(request):
|
||||
return request.param
|
||||
|
||||
|
||||
class TestIntervalIndex(Base):
|
||||
_holder = IntervalIndex
|
||||
|
||||
def setup_method(self, method):
|
||||
self.index = IntervalIndex.from_arrays([0, 1], [1, 2])
|
||||
self.index_with_nan = IntervalIndex.from_tuples(
|
||||
[(0, 1), np.nan, (1, 2)])
|
||||
self.indices = dict(intervalIndex=tm.makeIntervalIndex(10))
|
||||
|
||||
def create_index(self, closed='right'):
|
||||
return IntervalIndex.from_breaks(range(11), closed=closed)
|
||||
|
||||
def create_index_with_nan(self, closed='right'):
|
||||
mask = [True, False] + [True] * 8
|
||||
return IntervalIndex.from_arrays(
|
||||
np.where(mask, np.arange(10), np.nan),
|
||||
np.where(mask, np.arange(1, 11), np.nan), closed=closed)
|
||||
|
||||
def test_properties(self, closed):
|
||||
index = self.create_index(closed=closed)
|
||||
assert len(index) == 10
|
||||
assert index.size == 10
|
||||
assert index.shape == (10, )
|
||||
|
||||
tm.assert_index_equal(index.left, Index(np.arange(10)))
|
||||
tm.assert_index_equal(index.right, Index(np.arange(1, 11)))
|
||||
tm.assert_index_equal(index.mid, Index(np.arange(0.5, 10.5)))
|
||||
|
||||
assert index.closed == closed
|
||||
|
||||
ivs = [Interval(l, r, closed) for l, r in zip(range(10), range(1, 11))]
|
||||
expected = np.array(ivs, dtype=object)
|
||||
tm.assert_numpy_array_equal(np.asarray(index), expected)
|
||||
tm.assert_numpy_array_equal(index.values, expected)
|
||||
|
||||
# with nans
|
||||
index = self.create_index_with_nan(closed=closed)
|
||||
assert len(index) == 10
|
||||
assert index.size == 10
|
||||
assert index.shape == (10, )
|
||||
|
||||
expected_left = Index([0, np.nan, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
expected_right = expected_left + 1
|
||||
expected_mid = expected_left + 0.5
|
||||
tm.assert_index_equal(index.left, expected_left)
|
||||
tm.assert_index_equal(index.right, expected_right)
|
||||
tm.assert_index_equal(index.mid, expected_mid)
|
||||
|
||||
assert index.closed == closed
|
||||
|
||||
ivs = [Interval(l, r, closed) if notna(l) else np.nan
|
||||
for l, r in zip(expected_left, expected_right)]
|
||||
expected = np.array(ivs, dtype=object)
|
||||
tm.assert_numpy_array_equal(np.asarray(index), expected)
|
||||
tm.assert_numpy_array_equal(index.values, expected)
|
||||
|
||||
@pytest.mark.parametrize('breaks', [
|
||||
[1, 1, 2, 5, 15, 53, 217, 1014, 5335, 31240, 201608],
|
||||
[-np.inf, -100, -10, 0.5, 1, 1.5, 3.8, 101, 202, np.inf],
|
||||
pd.to_datetime(['20170101', '20170202', '20170303', '20170404']),
|
||||
pd.to_timedelta(['1ns', '2ms', '3s', '4M', '5H', '6D'])])
|
||||
def test_length(self, closed, breaks):
|
||||
# GH 18789
|
||||
index = IntervalIndex.from_breaks(breaks, closed=closed)
|
||||
result = index.length
|
||||
expected = Index(iv.length for iv in index)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# with NA
|
||||
index = index.insert(1, np.nan)
|
||||
result = index.length
|
||||
expected = Index(iv.length if notna(iv) else iv for iv in index)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_with_nans(self, closed):
|
||||
index = self.create_index(closed=closed)
|
||||
assert not index.hasnans
|
||||
|
||||
result = index.isna()
|
||||
expected = np.repeat(False, len(index))
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
result = index.notna()
|
||||
expected = np.repeat(True, len(index))
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
index = self.create_index_with_nan(closed=closed)
|
||||
assert index.hasnans
|
||||
|
||||
result = index.isna()
|
||||
expected = np.array([False, True] + [False] * (len(index) - 2))
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
result = index.notna()
|
||||
expected = np.array([True, False] + [True] * (len(index) - 2))
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
def test_copy(self, closed):
|
||||
expected = self.create_index(closed=closed)
|
||||
|
||||
result = expected.copy()
|
||||
assert result.equals(expected)
|
||||
|
||||
result = expected.copy(deep=True)
|
||||
assert result.equals(expected)
|
||||
assert result.left is not expected.left
|
||||
|
||||
def test_ensure_copied_data(self, closed):
|
||||
# exercise the copy flag in the constructor
|
||||
|
||||
# not copying
|
||||
index = self.create_index(closed=closed)
|
||||
result = IntervalIndex(index, copy=False)
|
||||
tm.assert_numpy_array_equal(index.left.values, result.left.values,
|
||||
check_same='same')
|
||||
tm.assert_numpy_array_equal(index.right.values, result.right.values,
|
||||
check_same='same')
|
||||
|
||||
# by-definition make a copy
|
||||
result = IntervalIndex(index.values, copy=False)
|
||||
tm.assert_numpy_array_equal(index.left.values, result.left.values,
|
||||
check_same='copy')
|
||||
tm.assert_numpy_array_equal(index.right.values, result.right.values,
|
||||
check_same='copy')
|
||||
|
||||
def test_equals(self, closed):
|
||||
expected = IntervalIndex.from_breaks(np.arange(5), closed=closed)
|
||||
assert expected.equals(expected)
|
||||
assert expected.equals(expected.copy())
|
||||
|
||||
assert not expected.equals(expected.astype(object))
|
||||
assert not expected.equals(np.array(expected))
|
||||
assert not expected.equals(list(expected))
|
||||
|
||||
assert not expected.equals([1, 2])
|
||||
assert not expected.equals(np.array([1, 2]))
|
||||
assert not expected.equals(pd.date_range('20130101', periods=2))
|
||||
|
||||
expected_name1 = IntervalIndex.from_breaks(
|
||||
np.arange(5), closed=closed, name='foo')
|
||||
expected_name2 = IntervalIndex.from_breaks(
|
||||
np.arange(5), closed=closed, name='bar')
|
||||
assert expected.equals(expected_name1)
|
||||
assert expected_name1.equals(expected_name2)
|
||||
|
||||
for other_closed in {'left', 'right', 'both', 'neither'} - {closed}:
|
||||
expected_other_closed = IntervalIndex.from_breaks(
|
||||
np.arange(5), closed=other_closed)
|
||||
assert not expected.equals(expected_other_closed)
|
||||
|
||||
@pytest.mark.parametrize('klass', [list, tuple, np.array, pd.Series])
|
||||
def test_where(self, closed, klass):
|
||||
idx = self.create_index(closed=closed)
|
||||
cond = [True] * len(idx)
|
||||
expected = idx
|
||||
result = expected.where(klass(cond))
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
cond = [False] + [True] * len(idx[1:])
|
||||
expected = IntervalIndex([np.nan] + idx[1:].tolist())
|
||||
result = idx.where(klass(cond))
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_delete(self, closed):
|
||||
expected = IntervalIndex.from_breaks(np.arange(1, 11), closed=closed)
|
||||
result = self.create_index(closed=closed).delete(0)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize('data', [
|
||||
interval_range(0, periods=10, closed='neither'),
|
||||
interval_range(1.7, periods=8, freq=2.5, closed='both'),
|
||||
interval_range(Timestamp('20170101'), periods=12, closed='left'),
|
||||
interval_range(Timedelta('1 day'), periods=6, closed='right')])
|
||||
def test_insert(self, data):
|
||||
item = data[0]
|
||||
idx_item = IntervalIndex([item])
|
||||
|
||||
# start
|
||||
expected = idx_item.append(data)
|
||||
result = data.insert(0, item)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# end
|
||||
expected = data.append(idx_item)
|
||||
result = data.insert(len(data), item)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# mid
|
||||
expected = data[:3].append(idx_item).append(data[3:])
|
||||
result = data.insert(3, item)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# invalid type
|
||||
msg = 'can only insert Interval objects and NA into an IntervalIndex'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
data.insert(1, 'foo')
|
||||
|
||||
# invalid closed
|
||||
msg = 'inserted item must be closed on the same side as the index'
|
||||
for closed in {'left', 'right', 'both', 'neither'} - {item.closed}:
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
bad_item = Interval(item.left, item.right, closed=closed)
|
||||
data.insert(1, bad_item)
|
||||
|
||||
# GH 18295 (test missing)
|
||||
na_idx = IntervalIndex([np.nan], closed=data.closed)
|
||||
for na in (np.nan, pd.NaT, None):
|
||||
expected = data[:1].append(na_idx).append(data[1:])
|
||||
result = data.insert(1, na)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_take(self, closed):
|
||||
index = self.create_index(closed=closed)
|
||||
|
||||
result = index.take(range(10))
|
||||
tm.assert_index_equal(result, index)
|
||||
|
||||
result = index.take([0, 0, 1])
|
||||
expected = IntervalIndex.from_arrays(
|
||||
[0, 0, 1], [1, 1, 2], closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_unique(self, closed):
|
||||
# unique non-overlapping
|
||||
idx = IntervalIndex.from_tuples(
|
||||
[(0, 1), (2, 3), (4, 5)], closed=closed)
|
||||
assert idx.is_unique
|
||||
|
||||
# unique overlapping - distinct endpoints
|
||||
idx = IntervalIndex.from_tuples([(0, 1), (0.5, 1.5)], closed=closed)
|
||||
assert idx.is_unique
|
||||
|
||||
# unique overlapping - shared endpoints
|
||||
idx = pd.IntervalIndex.from_tuples(
|
||||
[(1, 2), (1, 3), (2, 3)], closed=closed)
|
||||
assert idx.is_unique
|
||||
|
||||
# unique nested
|
||||
idx = IntervalIndex.from_tuples([(-1, 1), (-2, 2)], closed=closed)
|
||||
assert idx.is_unique
|
||||
|
||||
# duplicate
|
||||
idx = IntervalIndex.from_tuples(
|
||||
[(0, 1), (0, 1), (2, 3)], closed=closed)
|
||||
assert not idx.is_unique
|
||||
|
||||
# empty
|
||||
idx = IntervalIndex([], closed=closed)
|
||||
assert idx.is_unique
|
||||
|
||||
def test_monotonic(self, closed):
|
||||
# increasing non-overlapping
|
||||
idx = IntervalIndex.from_tuples(
|
||||
[(0, 1), (2, 3), (4, 5)], closed=closed)
|
||||
assert idx.is_monotonic
|
||||
assert idx._is_strictly_monotonic_increasing
|
||||
assert not idx.is_monotonic_decreasing
|
||||
assert not idx._is_strictly_monotonic_decreasing
|
||||
|
||||
# decreasing non-overlapping
|
||||
idx = IntervalIndex.from_tuples(
|
||||
[(4, 5), (2, 3), (1, 2)], closed=closed)
|
||||
assert not idx.is_monotonic
|
||||
assert not idx._is_strictly_monotonic_increasing
|
||||
assert idx.is_monotonic_decreasing
|
||||
assert idx._is_strictly_monotonic_decreasing
|
||||
|
||||
# unordered non-overlapping
|
||||
idx = IntervalIndex.from_tuples(
|
||||
[(0, 1), (4, 5), (2, 3)], closed=closed)
|
||||
assert not idx.is_monotonic
|
||||
assert not idx._is_strictly_monotonic_increasing
|
||||
assert not idx.is_monotonic_decreasing
|
||||
assert not idx._is_strictly_monotonic_decreasing
|
||||
|
||||
# increasing overlapping
|
||||
idx = IntervalIndex.from_tuples(
|
||||
[(0, 2), (0.5, 2.5), (1, 3)], closed=closed)
|
||||
assert idx.is_monotonic
|
||||
assert idx._is_strictly_monotonic_increasing
|
||||
assert not idx.is_monotonic_decreasing
|
||||
assert not idx._is_strictly_monotonic_decreasing
|
||||
|
||||
# decreasing overlapping
|
||||
idx = IntervalIndex.from_tuples(
|
||||
[(1, 3), (0.5, 2.5), (0, 2)], closed=closed)
|
||||
assert not idx.is_monotonic
|
||||
assert not idx._is_strictly_monotonic_increasing
|
||||
assert idx.is_monotonic_decreasing
|
||||
assert idx._is_strictly_monotonic_decreasing
|
||||
|
||||
# unordered overlapping
|
||||
idx = IntervalIndex.from_tuples(
|
||||
[(0.5, 2.5), (0, 2), (1, 3)], closed=closed)
|
||||
assert not idx.is_monotonic
|
||||
assert not idx._is_strictly_monotonic_increasing
|
||||
assert not idx.is_monotonic_decreasing
|
||||
assert not idx._is_strictly_monotonic_decreasing
|
||||
|
||||
# increasing overlapping shared endpoints
|
||||
idx = pd.IntervalIndex.from_tuples(
|
||||
[(1, 2), (1, 3), (2, 3)], closed=closed)
|
||||
assert idx.is_monotonic
|
||||
assert idx._is_strictly_monotonic_increasing
|
||||
assert not idx.is_monotonic_decreasing
|
||||
assert not idx._is_strictly_monotonic_decreasing
|
||||
|
||||
# decreasing overlapping shared endpoints
|
||||
idx = pd.IntervalIndex.from_tuples(
|
||||
[(2, 3), (1, 3), (1, 2)], closed=closed)
|
||||
assert not idx.is_monotonic
|
||||
assert not idx._is_strictly_monotonic_increasing
|
||||
assert idx.is_monotonic_decreasing
|
||||
assert idx._is_strictly_monotonic_decreasing
|
||||
|
||||
# stationary
|
||||
idx = IntervalIndex.from_tuples([(0, 1), (0, 1)], closed=closed)
|
||||
assert idx.is_monotonic
|
||||
assert not idx._is_strictly_monotonic_increasing
|
||||
assert idx.is_monotonic_decreasing
|
||||
assert not idx._is_strictly_monotonic_decreasing
|
||||
|
||||
# empty
|
||||
idx = IntervalIndex([], closed=closed)
|
||||
assert idx.is_monotonic
|
||||
assert idx._is_strictly_monotonic_increasing
|
||||
assert idx.is_monotonic_decreasing
|
||||
assert idx._is_strictly_monotonic_decreasing
|
||||
|
||||
@pytest.mark.skip(reason='not a valid repr as we use interval notation')
|
||||
def test_repr(self):
|
||||
i = IntervalIndex.from_tuples([(0, 1), (1, 2)], closed='right')
|
||||
expected = ("IntervalIndex(left=[0, 1],"
|
||||
"\n right=[1, 2],"
|
||||
"\n closed='right',"
|
||||
"\n dtype='interval[int64]')")
|
||||
assert repr(i) == expected
|
||||
|
||||
i = IntervalIndex.from_tuples((Timestamp('20130101'),
|
||||
Timestamp('20130102')),
|
||||
(Timestamp('20130102'),
|
||||
Timestamp('20130103')),
|
||||
closed='right')
|
||||
expected = ("IntervalIndex(left=['2013-01-01', '2013-01-02'],"
|
||||
"\n right=['2013-01-02', '2013-01-03'],"
|
||||
"\n closed='right',"
|
||||
"\n dtype='interval[datetime64[ns]]')")
|
||||
assert repr(i) == expected
|
||||
|
||||
@pytest.mark.skip(reason='not a valid repr as we use interval notation')
|
||||
def test_repr_max_seq_item_setting(self):
|
||||
super(TestIntervalIndex, self).test_repr_max_seq_item_setting()
|
||||
|
||||
@pytest.mark.skip(reason='not a valid repr as we use interval notation')
|
||||
def test_repr_roundtrip(self):
|
||||
super(TestIntervalIndex, self).test_repr_roundtrip()
|
||||
|
||||
# TODO: check this behavior is consistent with test_interval_new.py
|
||||
def test_get_item(self, closed):
|
||||
i = IntervalIndex.from_arrays((0, 1, np.nan), (1, 2, np.nan),
|
||||
closed=closed)
|
||||
assert i[0] == Interval(0.0, 1.0, closed=closed)
|
||||
assert i[1] == Interval(1.0, 2.0, closed=closed)
|
||||
assert isna(i[2])
|
||||
|
||||
result = i[0:1]
|
||||
expected = IntervalIndex.from_arrays((0.,), (1.,), closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = i[0:2]
|
||||
expected = IntervalIndex.from_arrays((0., 1), (1., 2.), closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = i[1:3]
|
||||
expected = IntervalIndex.from_arrays((1., np.nan), (2., np.nan),
|
||||
closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
|
||||
def test_get_loc_value(self):
|
||||
pytest.raises(KeyError, self.index.get_loc, 0)
|
||||
assert self.index.get_loc(0.5) == 0
|
||||
assert self.index.get_loc(1) == 0
|
||||
assert self.index.get_loc(1.5) == 1
|
||||
assert self.index.get_loc(2) == 1
|
||||
pytest.raises(KeyError, self.index.get_loc, -1)
|
||||
pytest.raises(KeyError, self.index.get_loc, 3)
|
||||
|
||||
idx = IntervalIndex.from_tuples([(0, 2), (1, 3)])
|
||||
assert idx.get_loc(0.5) == 0
|
||||
assert idx.get_loc(1) == 0
|
||||
tm.assert_numpy_array_equal(idx.get_loc(1.5),
|
||||
np.array([0, 1], dtype='int64'))
|
||||
tm.assert_numpy_array_equal(np.sort(idx.get_loc(2)),
|
||||
np.array([0, 1], dtype='int64'))
|
||||
assert idx.get_loc(3) == 1
|
||||
pytest.raises(KeyError, idx.get_loc, 3.5)
|
||||
|
||||
idx = IntervalIndex.from_arrays([0, 2], [1, 3])
|
||||
pytest.raises(KeyError, idx.get_loc, 1.5)
|
||||
|
||||
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
|
||||
def slice_locs_cases(self, breaks):
|
||||
# TODO: same tests for more index types
|
||||
index = IntervalIndex.from_breaks([0, 1, 2], closed='right')
|
||||
assert index.slice_locs() == (0, 2)
|
||||
assert index.slice_locs(0, 1) == (0, 1)
|
||||
assert index.slice_locs(1, 1) == (0, 1)
|
||||
assert index.slice_locs(0, 2) == (0, 2)
|
||||
assert index.slice_locs(0.5, 1.5) == (0, 2)
|
||||
assert index.slice_locs(0, 0.5) == (0, 1)
|
||||
assert index.slice_locs(start=1) == (0, 2)
|
||||
assert index.slice_locs(start=1.2) == (1, 2)
|
||||
assert index.slice_locs(end=1) == (0, 1)
|
||||
assert index.slice_locs(end=1.1) == (0, 2)
|
||||
assert index.slice_locs(end=1.0) == (0, 1)
|
||||
assert index.slice_locs(-1, -1) == (0, 0)
|
||||
|
||||
index = IntervalIndex.from_breaks([0, 1, 2], closed='neither')
|
||||
assert index.slice_locs(0, 1) == (0, 1)
|
||||
assert index.slice_locs(0, 2) == (0, 2)
|
||||
assert index.slice_locs(0.5, 1.5) == (0, 2)
|
||||
assert index.slice_locs(1, 1) == (1, 1)
|
||||
assert index.slice_locs(1, 2) == (1, 2)
|
||||
|
||||
index = IntervalIndex.from_tuples([(0, 1), (2, 3), (4, 5)],
|
||||
closed='both')
|
||||
assert index.slice_locs(1, 1) == (0, 1)
|
||||
assert index.slice_locs(1, 2) == (0, 2)
|
||||
|
||||
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
|
||||
def test_slice_locs_int64(self):
|
||||
self.slice_locs_cases([0, 1, 2])
|
||||
|
||||
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
|
||||
def test_slice_locs_float64(self):
|
||||
self.slice_locs_cases([0.0, 1.0, 2.0])
|
||||
|
||||
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
|
||||
def slice_locs_decreasing_cases(self, tuples):
|
||||
index = IntervalIndex.from_tuples(tuples)
|
||||
assert index.slice_locs(1.5, 0.5) == (1, 3)
|
||||
assert index.slice_locs(2, 0) == (1, 3)
|
||||
assert index.slice_locs(2, 1) == (1, 3)
|
||||
assert index.slice_locs(3, 1.1) == (0, 3)
|
||||
assert index.slice_locs(3, 3) == (0, 2)
|
||||
assert index.slice_locs(3.5, 3.3) == (0, 1)
|
||||
assert index.slice_locs(1, -3) == (2, 3)
|
||||
|
||||
slice_locs = index.slice_locs(-1, -1)
|
||||
assert slice_locs[0] == slice_locs[1]
|
||||
|
||||
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
|
||||
def test_slice_locs_decreasing_int64(self):
|
||||
self.slice_locs_cases([(2, 4), (1, 3), (0, 2)])
|
||||
|
||||
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
|
||||
def test_slice_locs_decreasing_float64(self):
|
||||
self.slice_locs_cases([(2., 4.), (1., 3.), (0., 2.)])
|
||||
|
||||
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
|
||||
def test_slice_locs_fails(self):
|
||||
index = IntervalIndex.from_tuples([(1, 2), (0, 1), (2, 3)])
|
||||
with pytest.raises(KeyError):
|
||||
index.slice_locs(1, 2)
|
||||
|
||||
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
|
||||
def test_get_loc_interval(self):
|
||||
assert self.index.get_loc(Interval(0, 1)) == 0
|
||||
assert self.index.get_loc(Interval(0, 0.5)) == 0
|
||||
assert self.index.get_loc(Interval(0, 1, 'left')) == 0
|
||||
pytest.raises(KeyError, self.index.get_loc, Interval(2, 3))
|
||||
pytest.raises(KeyError, self.index.get_loc,
|
||||
Interval(-1, 0, 'left'))
|
||||
|
||||
# Make consistent with test_interval_new.py (see #16316, #16386)
|
||||
@pytest.mark.parametrize('item', [3, Interval(1, 4)])
|
||||
def test_get_loc_length_one(self, item, closed):
|
||||
# GH 20921
|
||||
index = IntervalIndex.from_tuples([(0, 5)], closed=closed)
|
||||
result = index.get_loc(item)
|
||||
assert result == 0
|
||||
|
||||
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
|
||||
def test_get_indexer(self):
|
||||
actual = self.index.get_indexer([-1, 0, 0.5, 1, 1.5, 2, 3])
|
||||
expected = np.array([-1, -1, 0, 0, 1, 1, -1], dtype='intp')
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
actual = self.index.get_indexer(self.index)
|
||||
expected = np.array([0, 1], dtype='intp')
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
index = IntervalIndex.from_breaks([0, 1, 2], closed='left')
|
||||
actual = index.get_indexer([-1, 0, 0.5, 1, 1.5, 2, 3])
|
||||
expected = np.array([-1, 0, 0, 1, 1, -1, -1], dtype='intp')
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
actual = self.index.get_indexer(index[:1])
|
||||
expected = np.array([0], dtype='intp')
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
actual = self.index.get_indexer(index)
|
||||
expected = np.array([-1, 1], dtype='intp')
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
|
||||
def test_get_indexer_subintervals(self):
|
||||
|
||||
# TODO: is this right?
|
||||
# return indexers for wholly contained subintervals
|
||||
target = IntervalIndex.from_breaks(np.linspace(0, 2, 5))
|
||||
actual = self.index.get_indexer(target)
|
||||
expected = np.array([0, 0, 1, 1], dtype='p')
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
target = IntervalIndex.from_breaks([0, 0.67, 1.33, 2])
|
||||
actual = self.index.get_indexer(target)
|
||||
expected = np.array([0, 0, 1, 1], dtype='intp')
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
actual = self.index.get_indexer(target[[0, -1]])
|
||||
expected = np.array([0, 1], dtype='intp')
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
target = IntervalIndex.from_breaks([0, 0.33, 0.67, 1], closed='left')
|
||||
actual = self.index.get_indexer(target)
|
||||
expected = np.array([0, 0, 0], dtype='intp')
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
# Make consistent with test_interval_new.py (see #16316, #16386)
|
||||
@pytest.mark.parametrize('item', [
|
||||
[3], np.arange(1, 5), [Interval(1, 4)], interval_range(1, 4)])
|
||||
def test_get_indexer_length_one(self, item, closed):
|
||||
# GH 17284
|
||||
index = IntervalIndex.from_tuples([(0, 5)], closed=closed)
|
||||
result = index.get_indexer(item)
|
||||
expected = np.array([0] * len(item), dtype='intp')
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
|
||||
def test_contains(self):
|
||||
# Only endpoints are valid.
|
||||
i = IntervalIndex.from_arrays([0, 1], [1, 2])
|
||||
|
||||
# Invalid
|
||||
assert 0 not in i
|
||||
assert 1 not in i
|
||||
assert 2 not in i
|
||||
|
||||
# Valid
|
||||
assert Interval(0, 1) in i
|
||||
assert Interval(0, 2) in i
|
||||
assert Interval(0, 0.5) in i
|
||||
assert Interval(3, 5) not in i
|
||||
assert Interval(-1, 0, closed='left') not in i
|
||||
|
||||
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
|
||||
def testcontains(self):
|
||||
# can select values that are IN the range of a value
|
||||
i = IntervalIndex.from_arrays([0, 1], [1, 2])
|
||||
|
||||
assert i.contains(0.1)
|
||||
assert i.contains(0.5)
|
||||
assert i.contains(1)
|
||||
assert i.contains(Interval(0, 1))
|
||||
assert i.contains(Interval(0, 2))
|
||||
|
||||
# these overlaps completely
|
||||
assert i.contains(Interval(0, 3))
|
||||
assert i.contains(Interval(1, 3))
|
||||
|
||||
assert not i.contains(20)
|
||||
assert not i.contains(-20)
|
||||
|
||||
def test_dropna(self, closed):
|
||||
|
||||
expected = IntervalIndex.from_tuples(
|
||||
[(0.0, 1.0), (1.0, 2.0)], closed=closed)
|
||||
|
||||
ii = IntervalIndex.from_tuples([(0, 1), (1, 2), np.nan], closed=closed)
|
||||
result = ii.dropna()
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
ii = IntervalIndex.from_arrays(
|
||||
[0, 1, np.nan], [1, 2, np.nan], closed=closed)
|
||||
result = ii.dropna()
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# TODO: check this behavior is consistent with test_interval_new.py
|
||||
def test_non_contiguous(self, closed):
|
||||
index = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=closed)
|
||||
target = [0.5, 1.5, 2.5]
|
||||
actual = index.get_indexer(target)
|
||||
expected = np.array([0, -1, 1], dtype='intp')
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
assert 1.5 not in index
|
||||
|
||||
def test_union(self, closed):
|
||||
index = self.create_index(closed=closed)
|
||||
other = IntervalIndex.from_breaks(range(5, 13), closed=closed)
|
||||
|
||||
expected = IntervalIndex.from_breaks(range(13), closed=closed)
|
||||
result = index.union(other)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = other.union(index)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
tm.assert_index_equal(index.union(index), index)
|
||||
tm.assert_index_equal(index.union(index[:1]), index)
|
||||
|
||||
# GH 19101: empty result, same dtype
|
||||
index = IntervalIndex(np.array([], dtype='int64'), closed=closed)
|
||||
result = index.union(index)
|
||||
tm.assert_index_equal(result, index)
|
||||
|
||||
# GH 19101: empty result, different dtypes
|
||||
other = IntervalIndex(np.array([], dtype='float64'), closed=closed)
|
||||
result = index.union(other)
|
||||
tm.assert_index_equal(result, index)
|
||||
|
||||
def test_intersection(self, closed):
|
||||
index = self.create_index(closed=closed)
|
||||
other = IntervalIndex.from_breaks(range(5, 13), closed=closed)
|
||||
|
||||
expected = IntervalIndex.from_breaks(range(5, 11), closed=closed)
|
||||
result = index.intersection(other)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = other.intersection(index)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
tm.assert_index_equal(index.intersection(index), index)
|
||||
|
||||
# GH 19101: empty result, same dtype
|
||||
other = IntervalIndex.from_breaks(range(300, 314), closed=closed)
|
||||
expected = IntervalIndex(np.array([], dtype='int64'), closed=closed)
|
||||
result = index.intersection(other)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# GH 19101: empty result, different dtypes
|
||||
breaks = np.arange(300, 314, dtype='float64')
|
||||
other = IntervalIndex.from_breaks(breaks, closed=closed)
|
||||
result = index.intersection(other)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_difference(self, closed):
|
||||
index = self.create_index(closed=closed)
|
||||
tm.assert_index_equal(index.difference(index[:1]), index[1:])
|
||||
|
||||
# GH 19101: empty result, same dtype
|
||||
result = index.difference(index)
|
||||
expected = IntervalIndex(np.array([], dtype='int64'), closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# GH 19101: empty result, different dtypes
|
||||
other = IntervalIndex.from_arrays(index.left.astype('float64'),
|
||||
index.right, closed=closed)
|
||||
result = index.difference(other)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_symmetric_difference(self, closed):
|
||||
index = self.create_index(closed=closed)
|
||||
result = index[1:].symmetric_difference(index[:-1])
|
||||
expected = IntervalIndex([index[0], index[-1]])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# GH 19101: empty result, same dtype
|
||||
result = index.symmetric_difference(index)
|
||||
expected = IntervalIndex(np.array([], dtype='int64'), closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# GH 19101: empty result, different dtypes
|
||||
other = IntervalIndex.from_arrays(index.left.astype('float64'),
|
||||
index.right, closed=closed)
|
||||
result = index.symmetric_difference(other)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize('op_name', [
|
||||
'union', 'intersection', 'difference', 'symmetric_difference'])
|
||||
def test_set_operation_errors(self, closed, op_name):
|
||||
index = self.create_index(closed=closed)
|
||||
set_op = getattr(index, op_name)
|
||||
|
||||
# non-IntervalIndex
|
||||
msg = ('the other index needs to be an IntervalIndex too, but '
|
||||
'was type Int64Index')
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
set_op(Index([1, 2, 3]))
|
||||
|
||||
# mixed closed
|
||||
msg = ('can only do set operations between two IntervalIndex objects '
|
||||
'that are closed on the same side')
|
||||
for other_closed in {'right', 'left', 'both', 'neither'} - {closed}:
|
||||
other = self.create_index(closed=other_closed)
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
set_op(other)
|
||||
|
||||
# GH 19016: incompatible dtypes
|
||||
other = interval_range(Timestamp('20180101'), periods=9, closed=closed)
|
||||
msg = ('can only do {op} between two IntervalIndex objects that have '
|
||||
'compatible dtypes').format(op=op_name)
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
set_op(other)
|
||||
|
||||
def test_isin(self, closed):
|
||||
index = self.create_index(closed=closed)
|
||||
|
||||
expected = np.array([True] + [False] * (len(index) - 1))
|
||||
result = index.isin(index[:1])
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
result = index.isin([index[0]])
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
other = IntervalIndex.from_breaks(np.arange(-2, 10), closed=closed)
|
||||
expected = np.array([True] * (len(index) - 1) + [False])
|
||||
result = index.isin(other)
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
result = index.isin(other.tolist())
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
for other_closed in {'right', 'left', 'both', 'neither'}:
|
||||
other = self.create_index(closed=other_closed)
|
||||
expected = np.repeat(closed == other_closed, len(index))
|
||||
result = index.isin(other)
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
result = index.isin(other.tolist())
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
def test_comparison(self):
|
||||
actual = Interval(0, 1) < self.index
|
||||
expected = np.array([False, True])
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
actual = Interval(0.5, 1.5) < self.index
|
||||
expected = np.array([False, True])
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
actual = self.index > Interval(0.5, 1.5)
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
actual = self.index == self.index
|
||||
expected = np.array([True, True])
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
actual = self.index <= self.index
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
actual = self.index >= self.index
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
actual = self.index < self.index
|
||||
expected = np.array([False, False])
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
actual = self.index > self.index
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
actual = self.index == IntervalIndex.from_breaks([0, 1, 2], 'left')
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
actual = self.index == self.index.values
|
||||
tm.assert_numpy_array_equal(actual, np.array([True, True]))
|
||||
actual = self.index.values == self.index
|
||||
tm.assert_numpy_array_equal(actual, np.array([True, True]))
|
||||
actual = self.index <= self.index.values
|
||||
tm.assert_numpy_array_equal(actual, np.array([True, True]))
|
||||
actual = self.index != self.index.values
|
||||
tm.assert_numpy_array_equal(actual, np.array([False, False]))
|
||||
actual = self.index > self.index.values
|
||||
tm.assert_numpy_array_equal(actual, np.array([False, False]))
|
||||
actual = self.index.values > self.index
|
||||
tm.assert_numpy_array_equal(actual, np.array([False, False]))
|
||||
|
||||
# invalid comparisons
|
||||
actual = self.index == 0
|
||||
tm.assert_numpy_array_equal(actual, np.array([False, False]))
|
||||
actual = self.index == self.index.left
|
||||
tm.assert_numpy_array_equal(actual, np.array([False, False]))
|
||||
|
||||
with tm.assert_raises_regex(TypeError, 'unorderable types'):
|
||||
self.index > 0
|
||||
with tm.assert_raises_regex(TypeError, 'unorderable types'):
|
||||
self.index <= 0
|
||||
with pytest.raises(TypeError):
|
||||
self.index > np.arange(2)
|
||||
with pytest.raises(ValueError):
|
||||
self.index > np.arange(3)
|
||||
|
||||
def test_missing_values(self, closed):
|
||||
idx = Index([np.nan, Interval(0, 1, closed=closed),
|
||||
Interval(1, 2, closed=closed)])
|
||||
idx2 = IntervalIndex.from_arrays(
|
||||
[np.nan, 0, 1], [np.nan, 1, 2], closed=closed)
|
||||
assert idx.equals(idx2)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
IntervalIndex.from_arrays(
|
||||
[np.nan, 0, 1], np.array([0, 1, 2]), closed=closed)
|
||||
|
||||
tm.assert_numpy_array_equal(isna(idx),
|
||||
np.array([True, False, False]))
|
||||
|
||||
def test_sort_values(self, closed):
|
||||
index = self.create_index(closed=closed)
|
||||
|
||||
result = index.sort_values()
|
||||
tm.assert_index_equal(result, index)
|
||||
|
||||
result = index.sort_values(ascending=False)
|
||||
tm.assert_index_equal(result, index[::-1])
|
||||
|
||||
# with nan
|
||||
index = IntervalIndex([Interval(1, 2), np.nan, Interval(0, 1)])
|
||||
|
||||
result = index.sort_values()
|
||||
expected = IntervalIndex([Interval(0, 1), Interval(1, 2), np.nan])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = index.sort_values(ascending=False)
|
||||
expected = IntervalIndex([np.nan, Interval(1, 2), Interval(0, 1)])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize('tz', [None, 'US/Eastern'])
|
||||
def test_datetime(self, tz):
|
||||
start = Timestamp('2000-01-01', tz=tz)
|
||||
dates = date_range(start=start, periods=10)
|
||||
index = IntervalIndex.from_breaks(dates)
|
||||
|
||||
# test mid
|
||||
start = Timestamp('2000-01-01T12:00', tz=tz)
|
||||
expected = date_range(start=start, periods=9)
|
||||
tm.assert_index_equal(index.mid, expected)
|
||||
|
||||
# __contains__ doesn't check individual points
|
||||
assert Timestamp('2000-01-01', tz=tz) not in index
|
||||
assert Timestamp('2000-01-01T12', tz=tz) not in index
|
||||
assert Timestamp('2000-01-02', tz=tz) not in index
|
||||
iv_true = Interval(Timestamp('2000-01-01T08', tz=tz),
|
||||
Timestamp('2000-01-01T18', tz=tz))
|
||||
iv_false = Interval(Timestamp('1999-12-31', tz=tz),
|
||||
Timestamp('2000-01-01', tz=tz))
|
||||
assert iv_true in index
|
||||
assert iv_false not in index
|
||||
|
||||
# .contains does check individual points
|
||||
assert not index.contains(Timestamp('2000-01-01', tz=tz))
|
||||
assert index.contains(Timestamp('2000-01-01T12', tz=tz))
|
||||
assert index.contains(Timestamp('2000-01-02', tz=tz))
|
||||
assert index.contains(iv_true)
|
||||
assert not index.contains(iv_false)
|
||||
|
||||
# test get_indexer
|
||||
start = Timestamp('1999-12-31T12:00', tz=tz)
|
||||
target = date_range(start=start, periods=7, freq='12H')
|
||||
actual = index.get_indexer(target)
|
||||
expected = np.array([-1, -1, 0, 0, 1, 1, 2], dtype='intp')
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
start = Timestamp('2000-01-08T18:00', tz=tz)
|
||||
target = date_range(start=start, periods=7, freq='6H')
|
||||
actual = index.get_indexer(target)
|
||||
expected = np.array([7, 7, 8, 8, 8, 8, -1], dtype='intp')
|
||||
tm.assert_numpy_array_equal(actual, expected)
|
||||
|
||||
def test_append(self, closed):
|
||||
|
||||
index1 = IntervalIndex.from_arrays([0, 1], [1, 2], closed=closed)
|
||||
index2 = IntervalIndex.from_arrays([1, 2], [2, 3], closed=closed)
|
||||
|
||||
result = index1.append(index2)
|
||||
expected = IntervalIndex.from_arrays(
|
||||
[0, 1, 1, 2], [1, 2, 2, 3], closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = index1.append([index1, index2])
|
||||
expected = IntervalIndex.from_arrays(
|
||||
[0, 1, 0, 1, 1, 2], [1, 2, 1, 2, 2, 3], closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
msg = ('can only append two IntervalIndex objects that are closed '
|
||||
'on the same side')
|
||||
for other_closed in {'left', 'right', 'both', 'neither'} - {closed}:
|
||||
index_other_closed = IntervalIndex.from_arrays(
|
||||
[0, 1], [1, 2], closed=other_closed)
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
index1.append(index_other_closed)
|
||||
|
||||
def test_is_non_overlapping_monotonic(self, closed):
|
||||
# Should be True in all cases
|
||||
tpls = [(0, 1), (2, 3), (4, 5), (6, 7)]
|
||||
idx = IntervalIndex.from_tuples(tpls, closed=closed)
|
||||
assert idx.is_non_overlapping_monotonic is True
|
||||
|
||||
idx = IntervalIndex.from_tuples(tpls[::-1], closed=closed)
|
||||
assert idx.is_non_overlapping_monotonic is True
|
||||
|
||||
# Should be False in all cases (overlapping)
|
||||
tpls = [(0, 2), (1, 3), (4, 5), (6, 7)]
|
||||
idx = IntervalIndex.from_tuples(tpls, closed=closed)
|
||||
assert idx.is_non_overlapping_monotonic is False
|
||||
|
||||
idx = IntervalIndex.from_tuples(tpls[::-1], closed=closed)
|
||||
assert idx.is_non_overlapping_monotonic is False
|
||||
|
||||
# Should be False in all cases (non-monotonic)
|
||||
tpls = [(0, 1), (2, 3), (6, 7), (4, 5)]
|
||||
idx = IntervalIndex.from_tuples(tpls, closed=closed)
|
||||
assert idx.is_non_overlapping_monotonic is False
|
||||
|
||||
idx = IntervalIndex.from_tuples(tpls[::-1], closed=closed)
|
||||
assert idx.is_non_overlapping_monotonic is False
|
||||
|
||||
# Should be False for closed='both', otherwise True (GH16560)
|
||||
if closed == 'both':
|
||||
idx = IntervalIndex.from_breaks(range(4), closed=closed)
|
||||
assert idx.is_non_overlapping_monotonic is False
|
||||
else:
|
||||
idx = IntervalIndex.from_breaks(range(4), closed=closed)
|
||||
assert idx.is_non_overlapping_monotonic is True
|
||||
|
||||
@pytest.mark.parametrize('tuples', [
|
||||
lzip(range(10), range(1, 11)),
|
||||
lzip(date_range('20170101', periods=10),
|
||||
date_range('20170101', periods=10)),
|
||||
lzip(timedelta_range('0 days', periods=10),
|
||||
timedelta_range('1 day', periods=10))])
|
||||
def test_to_tuples(self, tuples):
|
||||
# GH 18756
|
||||
idx = IntervalIndex.from_tuples(tuples)
|
||||
result = idx.to_tuples()
|
||||
expected = Index(com._asarray_tuplesafe(tuples))
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize('tuples', [
|
||||
lzip(range(10), range(1, 11)) + [np.nan],
|
||||
lzip(date_range('20170101', periods=10),
|
||||
date_range('20170101', periods=10)) + [np.nan],
|
||||
lzip(timedelta_range('0 days', periods=10),
|
||||
timedelta_range('1 day', periods=10)) + [np.nan]])
|
||||
@pytest.mark.parametrize('na_tuple', [True, False])
|
||||
def test_to_tuples_na(self, tuples, na_tuple):
|
||||
# GH 18756
|
||||
idx = IntervalIndex.from_tuples(tuples)
|
||||
result = idx.to_tuples(na_tuple=na_tuple)
|
||||
|
||||
# check the non-NA portion
|
||||
expected_notna = Index(com._asarray_tuplesafe(tuples[:-1]))
|
||||
result_notna = result[:-1]
|
||||
tm.assert_index_equal(result_notna, expected_notna)
|
||||
|
||||
# check the NA portion
|
||||
result_na = result[-1]
|
||||
if na_tuple:
|
||||
assert isinstance(result_na, tuple)
|
||||
assert len(result_na) == 2
|
||||
assert all(isna(x) for x in result_na)
|
||||
else:
|
||||
assert isna(result_na)
|
||||
-315
@@ -1,315 +0,0 @@
|
||||
from __future__ import division
|
||||
|
||||
import pytest
|
||||
import numpy as np
|
||||
|
||||
from pandas import Interval, IntervalIndex, Int64Index
|
||||
import pandas.util.testing as tm
|
||||
|
||||
|
||||
pytestmark = pytest.mark.skip(reason="new indexing tests for issue 16316")
|
||||
|
||||
|
||||
class TestIntervalIndex(object):
|
||||
|
||||
def _compare_tuple_of_numpy_array(self, result, expected):
|
||||
lidx, ridx = result
|
||||
lidx_expected, ridx_expected = expected
|
||||
|
||||
tm.assert_numpy_array_equal(lidx, lidx_expected)
|
||||
tm.assert_numpy_array_equal(ridx, ridx_expected)
|
||||
|
||||
@pytest.mark.parametrize("idx_side", ['right', 'left', 'both', 'neither'])
|
||||
@pytest.mark.parametrize("side", ['right', 'left', 'both', 'neither'])
|
||||
def test_get_loc_interval(self, idx_side, side):
|
||||
|
||||
idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side)
|
||||
|
||||
for bound in [[0, 1], [1, 2], [2, 3], [3, 4],
|
||||
[0, 2], [2.5, 3], [-1, 4]]:
|
||||
# if get_loc is supplied an interval, it should only search
|
||||
# for exact matches, not overlaps or covers, else KeyError.
|
||||
if idx_side == side:
|
||||
if bound == [0, 1]:
|
||||
assert idx.get_loc(Interval(0, 1, closed=side)) == 0
|
||||
elif bound == [2, 3]:
|
||||
assert idx.get_loc(Interval(2, 3, closed=side)) == 1
|
||||
else:
|
||||
with pytest.raises(KeyError):
|
||||
idx.get_loc(Interval(*bound, closed=side))
|
||||
else:
|
||||
with pytest.raises(KeyError):
|
||||
idx.get_loc(Interval(*bound, closed=side))
|
||||
|
||||
@pytest.mark.parametrize("idx_side", ['right', 'left', 'both', 'neither'])
|
||||
@pytest.mark.parametrize("scalar", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5])
|
||||
def test_get_loc_scalar(self, idx_side, scalar):
|
||||
|
||||
# correct = {side: {query: answer}}.
|
||||
# If query is not in the dict, that query should raise a KeyError
|
||||
correct = {'right': {0.5: 0, 1: 0, 2.5: 1, 3: 1},
|
||||
'left': {0: 0, 0.5: 0, 2: 1, 2.5: 1},
|
||||
'both': {0: 0, 0.5: 0, 1: 0, 2: 1, 2.5: 1, 3: 1},
|
||||
'neither': {0.5: 0, 2.5: 1}}
|
||||
|
||||
idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side)
|
||||
|
||||
# if get_loc is supplied a scalar, it should return the index of
|
||||
# the interval which contains the scalar, or KeyError.
|
||||
if scalar in correct[idx_side].keys():
|
||||
assert idx.get_loc(scalar) == correct[idx_side][scalar]
|
||||
else:
|
||||
pytest.raises(KeyError, idx.get_loc, scalar)
|
||||
|
||||
def test_slice_locs_with_interval(self):
|
||||
|
||||
# increasing monotonically
|
||||
index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)])
|
||||
|
||||
assert index.slice_locs(
|
||||
start=Interval(0, 2), end=Interval(2, 4)) == (0, 3)
|
||||
assert index.slice_locs(start=Interval(0, 2)) == (0, 3)
|
||||
assert index.slice_locs(end=Interval(2, 4)) == (0, 3)
|
||||
assert index.slice_locs(end=Interval(0, 2)) == (0, 1)
|
||||
assert index.slice_locs(
|
||||
start=Interval(2, 4), end=Interval(0, 2)) == (2, 1)
|
||||
|
||||
# decreasing monotonically
|
||||
index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)])
|
||||
|
||||
assert index.slice_locs(
|
||||
start=Interval(0, 2), end=Interval(2, 4)) == (2, 1)
|
||||
assert index.slice_locs(start=Interval(0, 2)) == (2, 3)
|
||||
assert index.slice_locs(end=Interval(2, 4)) == (0, 1)
|
||||
assert index.slice_locs(end=Interval(0, 2)) == (0, 3)
|
||||
assert index.slice_locs(
|
||||
start=Interval(2, 4), end=Interval(0, 2)) == (0, 3)
|
||||
|
||||
# sorted duplicates
|
||||
index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)])
|
||||
|
||||
assert index.slice_locs(
|
||||
start=Interval(0, 2), end=Interval(2, 4)) == (0, 3)
|
||||
assert index.slice_locs(start=Interval(0, 2)) == (0, 3)
|
||||
assert index.slice_locs(end=Interval(2, 4)) == (0, 3)
|
||||
assert index.slice_locs(end=Interval(0, 2)) == (0, 2)
|
||||
assert index.slice_locs(
|
||||
start=Interval(2, 4), end=Interval(0, 2)) == (2, 2)
|
||||
|
||||
# unsorted duplicates
|
||||
index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)])
|
||||
|
||||
pytest.raises(KeyError, index.slice_locs(
|
||||
start=Interval(0, 2), end=Interval(2, 4)))
|
||||
pytest.raises(KeyError, index.slice_locs(start=Interval(0, 2)))
|
||||
assert index.slice_locs(end=Interval(2, 4)) == (0, 2)
|
||||
pytest.raises(KeyError, index.slice_locs(end=Interval(0, 2)))
|
||||
pytest.raises(KeyError, index.slice_locs(
|
||||
start=Interval(2, 4), end=Interval(0, 2)))
|
||||
|
||||
# another unsorted duplicates
|
||||
index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)])
|
||||
|
||||
assert index.slice_locs(
|
||||
start=Interval(0, 2), end=Interval(2, 4)) == (0, 3)
|
||||
assert index.slice_locs(start=Interval(0, 2)) == (0, 4)
|
||||
assert index.slice_locs(end=Interval(2, 4)) == (0, 3)
|
||||
assert index.slice_locs(end=Interval(0, 2)) == (0, 2)
|
||||
assert index.slice_locs(
|
||||
start=Interval(2, 4), end=Interval(0, 2)) == (2, 2)
|
||||
|
||||
def test_slice_locs_with_ints_and_floats_succeeds(self):
|
||||
|
||||
# increasing non-overlapping
|
||||
index = IntervalIndex.from_tuples([(0, 1), (1, 2), (3, 4)])
|
||||
|
||||
assert index.slice_locs(0, 1) == (0, 1)
|
||||
assert index.slice_locs(0, 2) == (0, 2)
|
||||
assert index.slice_locs(0, 3) == (0, 2)
|
||||
assert index.slice_locs(3, 1) == (2, 1)
|
||||
assert index.slice_locs(3, 4) == (2, 3)
|
||||
assert index.slice_locs(0, 4) == (0, 3)
|
||||
|
||||
# decreasing non-overlapping
|
||||
index = IntervalIndex.from_tuples([(3, 4), (1, 2), (0, 1)])
|
||||
assert index.slice_locs(0, 1) == (3, 2)
|
||||
assert index.slice_locs(0, 2) == (3, 1)
|
||||
assert index.slice_locs(0, 3) == (3, 1)
|
||||
assert index.slice_locs(3, 1) == (1, 2)
|
||||
assert index.slice_locs(3, 4) == (1, 0)
|
||||
assert index.slice_locs(0, 4) == (3, 0)
|
||||
|
||||
@pytest.mark.parametrize("query", [[0, 1], [0, 2], [0, 3],
|
||||
[3, 1], [3, 4], [0, 4]])
|
||||
def test_slice_locs_with_ints_and_floats_fails(self, query):
|
||||
|
||||
# increasing overlapping
|
||||
index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)])
|
||||
pytest.raises(KeyError, index.slice_locs, query)
|
||||
|
||||
# decreasing overlapping
|
||||
index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)])
|
||||
pytest.raises(KeyError, index.slice_locs, query)
|
||||
|
||||
# sorted duplicates
|
||||
index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)])
|
||||
pytest.raises(KeyError, index.slice_locs, query)
|
||||
|
||||
# unsorted duplicates
|
||||
index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)])
|
||||
pytest.raises(KeyError, index.slice_locs, query)
|
||||
|
||||
# another unsorted duplicates
|
||||
index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)])
|
||||
pytest.raises(KeyError, index.slice_locs, query)
|
||||
|
||||
@pytest.mark.parametrize("query", [
|
||||
Interval(1, 3, closed='right'),
|
||||
Interval(1, 3, closed='left'),
|
||||
Interval(1, 3, closed='both'),
|
||||
Interval(1, 3, closed='neither'),
|
||||
Interval(1, 4, closed='right'),
|
||||
Interval(0, 4, closed='right'),
|
||||
Interval(1, 2, closed='right')])
|
||||
@pytest.mark.parametrize("expected_result", [1, -1, -1, -1, -1, -1, -1])
|
||||
def test_get_indexer_with_interval_single_queries(
|
||||
self, query, expected_result):
|
||||
|
||||
index = IntervalIndex.from_tuples(
|
||||
[(0, 2.5), (1, 3), (2, 4)], closed='right')
|
||||
|
||||
result = index.get_indexer([query])
|
||||
expect = np.array([expected_result], dtype='intp')
|
||||
tm.assert_numpy_array_equal(result, expect)
|
||||
|
||||
@pytest.mark.parametrize("query", [
|
||||
[Interval(2, 4, closed='right'), Interval(1, 3, closed='right')],
|
||||
[Interval(1, 3, closed='right'), Interval(0, 2, closed='right')],
|
||||
[Interval(1, 3, closed='right'), Interval(1, 3, closed='left')]])
|
||||
@pytest.mark.parametrize("expected_result", [[2, 1], [1, -1], [1, -1]])
|
||||
def test_get_indexer_with_interval_multiple_queries(
|
||||
self, query, expected_result):
|
||||
|
||||
index = IntervalIndex.from_tuples(
|
||||
[(0, 2.5), (1, 3), (2, 4)], closed='right')
|
||||
|
||||
result = index.get_indexer(query)
|
||||
expect = np.array(expected_result, dtype='intp')
|
||||
tm.assert_numpy_array_equal(result, expect)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"query",
|
||||
[-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5])
|
||||
@pytest.mark.parametrize(
|
||||
"expected_result",
|
||||
[-1, -1, 0, 0, 1, 1, -1, -1, 2, 2, -1])
|
||||
def test_get_indexer_with_ints_and_floats_single_queries(
|
||||
self, query, expected_result):
|
||||
|
||||
index = IntervalIndex.from_tuples(
|
||||
[(0, 1), (1, 2), (3, 4)], closed='right')
|
||||
|
||||
result = index.get_indexer([query])
|
||||
expect = np.array([expected_result], dtype='intp')
|
||||
tm.assert_numpy_array_equal(result, expect)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"query",
|
||||
[[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]])
|
||||
@pytest.mark.parametrize(
|
||||
"expected_result",
|
||||
[[0, 1], [0, 1, -1], [0, 1, -1, 2], [0, 1, -1, 2, 1]])
|
||||
def test_get_indexer_with_ints_and_floats_multiple_queries(
|
||||
self, query, expected_result):
|
||||
|
||||
index = IntervalIndex.from_tuples(
|
||||
[(0, 1), (1, 2), (3, 4)], closed='right')
|
||||
|
||||
result = index.get_indexer(query)
|
||||
expect = np.array(expected_result, dtype='intp')
|
||||
tm.assert_numpy_array_equal(result, expect)
|
||||
|
||||
index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)])
|
||||
# TODO: @shoyer believes this should raise, master branch doesn't
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"query",
|
||||
[-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5])
|
||||
@pytest.mark.parametrize("expected_result", [
|
||||
(Int64Index([], dtype='int64'), np.array([0])),
|
||||
(Int64Index([0], dtype='int64'), np.array([])),
|
||||
(Int64Index([0], dtype='int64'), np.array([])),
|
||||
(Int64Index([0, 1], dtype='int64'), np.array([])),
|
||||
(Int64Index([0, 1], dtype='int64'), np.array([])),
|
||||
(Int64Index([0, 1, 2], dtype='int64'), np.array([])),
|
||||
(Int64Index([1, 2], dtype='int64'), np.array([])),
|
||||
(Int64Index([2], dtype='int64'), np.array([])),
|
||||
(Int64Index([2], dtype='int64'), np.array([])),
|
||||
(Int64Index([], dtype='int64'), np.array([0])),
|
||||
(Int64Index([], dtype='int64'), np.array([0]))])
|
||||
def test_get_indexer_non_unique_with_ints_and_floats_single_queries(
|
||||
self, query, expected_result):
|
||||
|
||||
index = IntervalIndex.from_tuples(
|
||||
[(0, 2.5), (1, 3), (2, 4)], closed='left')
|
||||
|
||||
result = index.get_indexer_non_unique([query])
|
||||
tm.assert_numpy_array_equal(result, expected_result)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"query",
|
||||
[[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]])
|
||||
@pytest.mark.parametrize("expected_result", [
|
||||
(Int64Index([0, 1, 0, 1, 2], dtype='int64'), np.array([])),
|
||||
(Int64Index([0, 1, 0, 1, 2, 2], dtype='int64'), np.array([])),
|
||||
(Int64Index([0, 1, 0, 1, 2, 2, -1], dtype='int64'), np.array([3])),
|
||||
(Int64Index([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], dtype='int64'),
|
||||
np.array([3]))])
|
||||
def test_get_indexer_non_unique_with_ints_and_floats_multiple_queries(
|
||||
self, query, expected_result):
|
||||
|
||||
index = IntervalIndex.from_tuples(
|
||||
[(0, 2.5), (1, 3), (2, 4)], closed='left')
|
||||
|
||||
result = index.get_indexer_non_unique(query)
|
||||
tm.assert_numpy_array_equal(result, expected_result)
|
||||
|
||||
# TODO we may also want to test get_indexer for the case when
|
||||
# the intervals are duplicated, decreasing, non-monotonic, etc..
|
||||
|
||||
def test_contains(self):
|
||||
|
||||
index = IntervalIndex.from_arrays([0, 1], [1, 2], closed='right')
|
||||
|
||||
# __contains__ requires perfect matches to intervals.
|
||||
assert 0 not in index
|
||||
assert 1 not in index
|
||||
assert 2 not in index
|
||||
|
||||
assert Interval(0, 1, closed='right') in index
|
||||
assert Interval(0, 2, closed='right') not in index
|
||||
assert Interval(0, 0.5, closed='right') not in index
|
||||
assert Interval(3, 5, closed='right') not in index
|
||||
assert Interval(-1, 0, closed='left') not in index
|
||||
assert Interval(0, 1, closed='left') not in index
|
||||
assert Interval(0, 1, closed='both') not in index
|
||||
|
||||
def test_contains_method(self):
|
||||
|
||||
index = IntervalIndex.from_arrays([0, 1], [1, 2], closed='right')
|
||||
|
||||
assert not index.contains(0)
|
||||
assert index.contains(0.1)
|
||||
assert index.contains(0.5)
|
||||
assert index.contains(1)
|
||||
|
||||
assert index.contains(Interval(0, 1), closed='right')
|
||||
assert not index.contains(Interval(0, 1), closed='left')
|
||||
assert not index.contains(Interval(0, 1), closed='both')
|
||||
assert not index.contains(Interval(0, 2), closed='right')
|
||||
|
||||
assert not index.contains(Interval(0, 3), closed='right')
|
||||
assert not index.contains(Interval(1, 3), closed='right')
|
||||
|
||||
assert not index.contains(20)
|
||||
assert not index.contains(-20)
|
||||
-317
@@ -1,317 +0,0 @@
|
||||
from __future__ import division
|
||||
|
||||
import pytest
|
||||
import numpy as np
|
||||
from datetime import timedelta
|
||||
from pandas import (
|
||||
Interval, IntervalIndex, Timestamp, Timedelta, DateOffset,
|
||||
interval_range, date_range, timedelta_range)
|
||||
from pandas.core.dtypes.common import is_integer
|
||||
from pandas.tseries.offsets import Day
|
||||
import pandas.util.testing as tm
|
||||
|
||||
|
||||
@pytest.fixture(scope='class', params=['left', 'right', 'both', 'neither'])
|
||||
def closed(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(scope='class', params=[None, 'foo'])
|
||||
def name(request):
|
||||
return request.param
|
||||
|
||||
|
||||
class TestIntervalRange(object):
|
||||
|
||||
@pytest.mark.parametrize('freq, periods', [
|
||||
(1, 100), (2.5, 40), (5, 20), (25, 4)])
|
||||
def test_constructor_numeric(self, closed, name, freq, periods):
|
||||
start, end = 0, 100
|
||||
breaks = np.arange(101, step=freq)
|
||||
expected = IntervalIndex.from_breaks(breaks, name=name, closed=closed)
|
||||
|
||||
# defined from start/end/freq
|
||||
result = interval_range(
|
||||
start=start, end=end, freq=freq, name=name, closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# defined from start/periods/freq
|
||||
result = interval_range(
|
||||
start=start, periods=periods, freq=freq, name=name, closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# defined from end/periods/freq
|
||||
result = interval_range(
|
||||
end=end, periods=periods, freq=freq, name=name, closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# GH 20976: linspace behavior defined from start/end/periods
|
||||
result = interval_range(
|
||||
start=start, end=end, periods=periods, name=name, closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize('tz', [None, 'US/Eastern'])
|
||||
@pytest.mark.parametrize('freq, periods', [
|
||||
('D', 364), ('2D', 182), ('22D18H', 16), ('M', 11)])
|
||||
def test_constructor_timestamp(self, closed, name, freq, periods, tz):
|
||||
start, end = Timestamp('20180101', tz=tz), Timestamp('20181231', tz=tz)
|
||||
breaks = date_range(start=start, end=end, freq=freq)
|
||||
expected = IntervalIndex.from_breaks(breaks, name=name, closed=closed)
|
||||
|
||||
# defined from start/end/freq
|
||||
result = interval_range(
|
||||
start=start, end=end, freq=freq, name=name, closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# defined from start/periods/freq
|
||||
result = interval_range(
|
||||
start=start, periods=periods, freq=freq, name=name, closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# defined from end/periods/freq
|
||||
result = interval_range(
|
||||
end=end, periods=periods, freq=freq, name=name, closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# GH 20976: linspace behavior defined from start/end/periods
|
||||
if not breaks.freq.isAnchored() and tz is None:
|
||||
# matches expected only for non-anchored offsets and tz naive
|
||||
# (anchored/DST transitions cause unequal spacing in expected)
|
||||
result = interval_range(start=start, end=end, periods=periods,
|
||||
name=name, closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize('freq, periods', [
|
||||
('D', 100), ('2D12H', 40), ('5D', 20), ('25D', 4)])
|
||||
def test_constructor_timedelta(self, closed, name, freq, periods):
|
||||
start, end = Timedelta('0 days'), Timedelta('100 days')
|
||||
breaks = timedelta_range(start=start, end=end, freq=freq)
|
||||
expected = IntervalIndex.from_breaks(breaks, name=name, closed=closed)
|
||||
|
||||
# defined from start/end/freq
|
||||
result = interval_range(
|
||||
start=start, end=end, freq=freq, name=name, closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# defined from start/periods/freq
|
||||
result = interval_range(
|
||||
start=start, periods=periods, freq=freq, name=name, closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# defined from end/periods/freq
|
||||
result = interval_range(
|
||||
end=end, periods=periods, freq=freq, name=name, closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# GH 20976: linspace behavior defined from start/end/periods
|
||||
result = interval_range(
|
||||
start=start, end=end, periods=periods, name=name, closed=closed)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize('start, end, freq, expected_endpoint', [
|
||||
(0, 10, 3, 9),
|
||||
(0, 10, 1.5, 9),
|
||||
(0.5, 10, 3, 9.5),
|
||||
(Timedelta('0D'), Timedelta('10D'), '2D4H', Timedelta('8D16H')),
|
||||
(Timestamp('2018-01-01'),
|
||||
Timestamp('2018-02-09'),
|
||||
'MS',
|
||||
Timestamp('2018-02-01')),
|
||||
(Timestamp('2018-01-01', tz='US/Eastern'),
|
||||
Timestamp('2018-01-20', tz='US/Eastern'),
|
||||
'5D12H',
|
||||
Timestamp('2018-01-17 12:00:00', tz='US/Eastern'))])
|
||||
def test_early_truncation(self, start, end, freq, expected_endpoint):
|
||||
# index truncates early if freq causes end to be skipped
|
||||
result = interval_range(start=start, end=end, freq=freq)
|
||||
result_endpoint = result.right[-1]
|
||||
assert result_endpoint == expected_endpoint
|
||||
|
||||
@pytest.mark.parametrize('start, end, freq', [
|
||||
(0.5, None, None),
|
||||
(None, 4.5, None),
|
||||
(0.5, None, 1.5),
|
||||
(None, 6.5, 1.5)])
|
||||
def test_no_invalid_float_truncation(self, start, end, freq):
|
||||
# GH 21161
|
||||
if freq is None:
|
||||
breaks = [0.5, 1.5, 2.5, 3.5, 4.5]
|
||||
else:
|
||||
breaks = [0.5, 2.0, 3.5, 5.0, 6.5]
|
||||
expected = IntervalIndex.from_breaks(breaks)
|
||||
|
||||
result = interval_range(start=start, end=end, periods=4, freq=freq)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize('start, mid, end', [
|
||||
(Timestamp('2018-03-10', tz='US/Eastern'),
|
||||
Timestamp('2018-03-10 23:30:00', tz='US/Eastern'),
|
||||
Timestamp('2018-03-12', tz='US/Eastern')),
|
||||
(Timestamp('2018-11-03', tz='US/Eastern'),
|
||||
Timestamp('2018-11-04 00:30:00', tz='US/Eastern'),
|
||||
Timestamp('2018-11-05', tz='US/Eastern'))])
|
||||
def test_linspace_dst_transition(self, start, mid, end):
|
||||
# GH 20976: linspace behavior defined from start/end/periods
|
||||
# accounts for the hour gained/lost during DST transition
|
||||
result = interval_range(start=start, end=end, periods=2)
|
||||
expected = IntervalIndex.from_breaks([start, mid, end])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize('freq', [2, 2.0])
|
||||
@pytest.mark.parametrize('end', [10, 10.0])
|
||||
@pytest.mark.parametrize('start', [0, 0.0])
|
||||
def test_float_subtype(self, start, end, freq):
|
||||
# Has float subtype if any of start/end/freq are float, even if all
|
||||
# resulting endpoints can safely be upcast to integers
|
||||
|
||||
# defined from start/end/freq
|
||||
index = interval_range(start=start, end=end, freq=freq)
|
||||
result = index.dtype.subtype
|
||||
expected = 'int64' if is_integer(start + end + freq) else 'float64'
|
||||
assert result == expected
|
||||
|
||||
# defined from start/periods/freq
|
||||
index = interval_range(start=start, periods=5, freq=freq)
|
||||
result = index.dtype.subtype
|
||||
expected = 'int64' if is_integer(start + freq) else 'float64'
|
||||
assert result == expected
|
||||
|
||||
# defined from end/periods/freq
|
||||
index = interval_range(end=end, periods=5, freq=freq)
|
||||
result = index.dtype.subtype
|
||||
expected = 'int64' if is_integer(end + freq) else 'float64'
|
||||
assert result == expected
|
||||
|
||||
# GH 20976: linspace behavior defined from start/end/periods
|
||||
index = interval_range(start=start, end=end, periods=5)
|
||||
result = index.dtype.subtype
|
||||
expected = 'int64' if is_integer(start + end) else 'float64'
|
||||
assert result == expected
|
||||
|
||||
def test_constructor_coverage(self):
|
||||
# float value for periods
|
||||
expected = interval_range(start=0, periods=10)
|
||||
result = interval_range(start=0, periods=10.5)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# equivalent timestamp-like start/end
|
||||
start, end = Timestamp('2017-01-01'), Timestamp('2017-01-15')
|
||||
expected = interval_range(start=start, end=end)
|
||||
|
||||
result = interval_range(start=start.to_pydatetime(),
|
||||
end=end.to_pydatetime())
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = interval_range(start=start.asm8, end=end.asm8)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# equivalent freq with timestamp
|
||||
equiv_freq = ['D', Day(), Timedelta(days=1), timedelta(days=1),
|
||||
DateOffset(days=1)]
|
||||
for freq in equiv_freq:
|
||||
result = interval_range(start=start, end=end, freq=freq)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# equivalent timedelta-like start/end
|
||||
start, end = Timedelta(days=1), Timedelta(days=10)
|
||||
expected = interval_range(start=start, end=end)
|
||||
|
||||
result = interval_range(start=start.to_pytimedelta(),
|
||||
end=end.to_pytimedelta())
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = interval_range(start=start.asm8, end=end.asm8)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# equivalent freq with timedelta
|
||||
equiv_freq = ['D', Day(), Timedelta(days=1), timedelta(days=1)]
|
||||
for freq in equiv_freq:
|
||||
result = interval_range(start=start, end=end, freq=freq)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_errors(self):
|
||||
# not enough params
|
||||
msg = ('Of the four parameters: start, end, periods, and freq, '
|
||||
'exactly three must be specified')
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
interval_range(start=0)
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
interval_range(end=5)
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
interval_range(periods=2)
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
interval_range()
|
||||
|
||||
# too many params
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
interval_range(start=0, end=5, periods=6, freq=1.5)
|
||||
|
||||
# mixed units
|
||||
msg = 'start, end, freq need to be type compatible'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
interval_range(start=0, end=Timestamp('20130101'), freq=2)
|
||||
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
interval_range(start=0, end=Timedelta('1 day'), freq=2)
|
||||
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
interval_range(start=0, end=10, freq='D')
|
||||
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
interval_range(start=Timestamp('20130101'), end=10, freq='D')
|
||||
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
interval_range(start=Timestamp('20130101'),
|
||||
end=Timedelta('1 day'), freq='D')
|
||||
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
interval_range(start=Timestamp('20130101'),
|
||||
end=Timestamp('20130110'), freq=2)
|
||||
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
interval_range(start=Timedelta('1 day'), end=10, freq='D')
|
||||
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
interval_range(start=Timedelta('1 day'),
|
||||
end=Timestamp('20130110'), freq='D')
|
||||
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
interval_range(start=Timedelta('1 day'),
|
||||
end=Timedelta('10 days'), freq=2)
|
||||
|
||||
# invalid periods
|
||||
msg = 'periods must be a number, got foo'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
interval_range(start=0, periods='foo')
|
||||
|
||||
# invalid start
|
||||
msg = 'start must be numeric or datetime-like, got foo'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
interval_range(start='foo', periods=10)
|
||||
|
||||
# invalid end
|
||||
msg = r'end must be numeric or datetime-like, got \(0, 1\]'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
interval_range(end=Interval(0, 1), periods=10)
|
||||
|
||||
# invalid freq for datetime-like
|
||||
msg = 'freq must be numeric or convertible to DateOffset, got foo'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
interval_range(start=0, end=10, freq='foo')
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
interval_range(start=Timestamp('20130101'), periods=10, freq='foo')
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
interval_range(end=Timedelta('1 day'), periods=10, freq='foo')
|
||||
|
||||
# mixed tz
|
||||
start = Timestamp('2017-01-01', tz='US/Eastern')
|
||||
end = Timestamp('2017-01-07', tz='US/Pacific')
|
||||
msg = 'Start and end cannot both be tz-aware with different timezones'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
interval_range(start=start, end=end)
|
||||
-95
@@ -1,95 +0,0 @@
|
||||
from __future__ import division
|
||||
|
||||
import pytest
|
||||
import numpy as np
|
||||
from pandas import compat
|
||||
from pandas._libs.interval import IntervalTree
|
||||
import pandas.util.testing as tm
|
||||
|
||||
|
||||
@pytest.fixture(scope='class', params=['left', 'right', 'both', 'neither'])
|
||||
def closed(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(
|
||||
scope='class', params=['int32', 'int64', 'float32', 'float64', 'uint64'])
|
||||
def dtype(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(scope='class')
|
||||
def tree(dtype):
|
||||
left = np.arange(5, dtype=dtype)
|
||||
return IntervalTree(left, left + 2)
|
||||
|
||||
|
||||
class TestIntervalTree(object):
|
||||
|
||||
def test_get_loc(self, tree):
|
||||
tm.assert_numpy_array_equal(tree.get_loc(1),
|
||||
np.array([0], dtype='int64'))
|
||||
tm.assert_numpy_array_equal(np.sort(tree.get_loc(2)),
|
||||
np.array([0, 1], dtype='int64'))
|
||||
with pytest.raises(KeyError):
|
||||
tree.get_loc(-1)
|
||||
|
||||
def test_get_indexer(self, tree):
|
||||
tm.assert_numpy_array_equal(
|
||||
tree.get_indexer(np.array([1.0, 5.5, 6.5])),
|
||||
np.array([0, 4, -1], dtype='int64'))
|
||||
with pytest.raises(KeyError):
|
||||
tree.get_indexer(np.array([3.0]))
|
||||
|
||||
def test_get_indexer_non_unique(self, tree):
|
||||
indexer, missing = tree.get_indexer_non_unique(
|
||||
np.array([1.0, 2.0, 6.5]))
|
||||
tm.assert_numpy_array_equal(indexer[:1],
|
||||
np.array([0], dtype='int64'))
|
||||
tm.assert_numpy_array_equal(np.sort(indexer[1:3]),
|
||||
np.array([0, 1], dtype='int64'))
|
||||
tm.assert_numpy_array_equal(np.sort(indexer[3:]),
|
||||
np.array([-1], dtype='int64'))
|
||||
tm.assert_numpy_array_equal(missing, np.array([2], dtype='int64'))
|
||||
|
||||
def test_duplicates(self, dtype):
|
||||
left = np.array([0, 0, 0], dtype=dtype)
|
||||
tree = IntervalTree(left, left + 1)
|
||||
tm.assert_numpy_array_equal(np.sort(tree.get_loc(0.5)),
|
||||
np.array([0, 1, 2], dtype='int64'))
|
||||
|
||||
with pytest.raises(KeyError):
|
||||
tree.get_indexer(np.array([0.5]))
|
||||
|
||||
indexer, missing = tree.get_indexer_non_unique(np.array([0.5]))
|
||||
tm.assert_numpy_array_equal(np.sort(indexer),
|
||||
np.array([0, 1, 2], dtype='int64'))
|
||||
tm.assert_numpy_array_equal(missing, np.array([], dtype='int64'))
|
||||
|
||||
def test_get_loc_closed(self, closed):
|
||||
tree = IntervalTree([0], [1], closed=closed)
|
||||
for p, errors in [(0, tree.open_left),
|
||||
(1, tree.open_right)]:
|
||||
if errors:
|
||||
with pytest.raises(KeyError):
|
||||
tree.get_loc(p)
|
||||
else:
|
||||
tm.assert_numpy_array_equal(tree.get_loc(p),
|
||||
np.array([0], dtype='int64'))
|
||||
|
||||
@pytest.mark.skipif(compat.is_platform_32bit(),
|
||||
reason="int type mismatch on 32bit")
|
||||
@pytest.mark.parametrize('leaf_size', [1, 10, 100, 10000])
|
||||
def test_get_indexer_closed(self, closed, leaf_size):
|
||||
x = np.arange(1000, dtype='float64')
|
||||
found = x.astype('intp')
|
||||
not_found = (-1 * np.ones(1000)).astype('intp')
|
||||
|
||||
tree = IntervalTree(x, x + 0.5, closed=closed, leaf_size=leaf_size)
|
||||
tm.assert_numpy_array_equal(found, tree.get_indexer(x + 0.25))
|
||||
|
||||
expected = found if tree.closed_left else not_found
|
||||
tm.assert_numpy_array_equal(expected, tree.get_indexer(x + 0.0))
|
||||
|
||||
expected = found if tree.closed_right else not_found
|
||||
tm.assert_numpy_array_equal(expected, tree.get_indexer(x + 0.5))
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
-885
@@ -1,885 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from datetime import timedelta
|
||||
import operator
|
||||
|
||||
import pytest
|
||||
import numpy as np
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
from pandas import (Timedelta,
|
||||
period_range, Period, PeriodIndex,
|
||||
_np_version_under1p10)
|
||||
import pandas.core.indexes.period as period
|
||||
from pandas.core import ops
|
||||
from pandas.errors import PerformanceWarning
|
||||
|
||||
|
||||
_common_mismatch = [pd.offsets.YearBegin(2),
|
||||
pd.offsets.MonthBegin(1),
|
||||
pd.offsets.Minute()]
|
||||
|
||||
|
||||
@pytest.fixture(params=[timedelta(minutes=30),
|
||||
np.timedelta64(30, 's'),
|
||||
Timedelta(seconds=30)] + _common_mismatch)
|
||||
def not_hourly(request):
|
||||
"""
|
||||
Several timedelta-like and DateOffset instances that are _not_
|
||||
compatible with Hourly frequencies.
|
||||
"""
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(params=[np.timedelta64(4, 'h'),
|
||||
timedelta(hours=23),
|
||||
Timedelta('23:00:00')] + _common_mismatch)
|
||||
def not_daily(request):
|
||||
"""
|
||||
Several timedelta-like and DateOffset instances that are _not_
|
||||
compatible with Daily frequencies.
|
||||
"""
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(params=[np.timedelta64(365, 'D'),
|
||||
timedelta(365),
|
||||
Timedelta(days=365)] + _common_mismatch)
|
||||
def mismatched(request):
|
||||
"""
|
||||
Several timedelta-like and DateOffset instances that are _not_
|
||||
compatible with Monthly or Annual frequencies.
|
||||
"""
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(params=[pd.offsets.Day(3),
|
||||
timedelta(days=3),
|
||||
np.timedelta64(3, 'D'),
|
||||
pd.offsets.Hour(72),
|
||||
timedelta(minutes=60 * 24 * 3),
|
||||
np.timedelta64(72, 'h'),
|
||||
Timedelta('72:00:00')])
|
||||
def three_days(request):
|
||||
"""
|
||||
Several timedelta-like and DateOffset objects that each represent
|
||||
a 3-day timedelta
|
||||
"""
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(params=[pd.offsets.Hour(2),
|
||||
timedelta(hours=2),
|
||||
np.timedelta64(2, 'h'),
|
||||
pd.offsets.Minute(120),
|
||||
timedelta(minutes=120),
|
||||
np.timedelta64(120, 'm')])
|
||||
def two_hours(request):
|
||||
"""
|
||||
Several timedelta-like and DateOffset objects that each represent
|
||||
a 2-hour timedelta
|
||||
"""
|
||||
return request.param
|
||||
|
||||
|
||||
class TestPeriodIndexComparisons(object):
|
||||
def test_pi_cmp_period(self):
|
||||
idx = period_range('2007-01', periods=20, freq='M')
|
||||
|
||||
result = idx < idx[10]
|
||||
exp = idx.values < idx.values[10]
|
||||
tm.assert_numpy_array_equal(result, exp)
|
||||
|
||||
@pytest.mark.parametrize('freq', ['M', '2M', '3M'])
|
||||
def test_pi_cmp_pi(self, freq):
|
||||
base = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'],
|
||||
freq=freq)
|
||||
per = Period('2011-02', freq=freq)
|
||||
|
||||
exp = np.array([False, True, False, False])
|
||||
tm.assert_numpy_array_equal(base == per, exp)
|
||||
tm.assert_numpy_array_equal(per == base, exp)
|
||||
|
||||
exp = np.array([True, False, True, True])
|
||||
tm.assert_numpy_array_equal(base != per, exp)
|
||||
tm.assert_numpy_array_equal(per != base, exp)
|
||||
|
||||
exp = np.array([False, False, True, True])
|
||||
tm.assert_numpy_array_equal(base > per, exp)
|
||||
tm.assert_numpy_array_equal(per < base, exp)
|
||||
|
||||
exp = np.array([True, False, False, False])
|
||||
tm.assert_numpy_array_equal(base < per, exp)
|
||||
tm.assert_numpy_array_equal(per > base, exp)
|
||||
|
||||
exp = np.array([False, True, True, True])
|
||||
tm.assert_numpy_array_equal(base >= per, exp)
|
||||
tm.assert_numpy_array_equal(per <= base, exp)
|
||||
|
||||
exp = np.array([True, True, False, False])
|
||||
tm.assert_numpy_array_equal(base <= per, exp)
|
||||
tm.assert_numpy_array_equal(per >= base, exp)
|
||||
|
||||
idx = PeriodIndex(['2011-02', '2011-01', '2011-03', '2011-05'],
|
||||
freq=freq)
|
||||
|
||||
exp = np.array([False, False, True, False])
|
||||
tm.assert_numpy_array_equal(base == idx, exp)
|
||||
|
||||
exp = np.array([True, True, False, True])
|
||||
tm.assert_numpy_array_equal(base != idx, exp)
|
||||
|
||||
exp = np.array([False, True, False, False])
|
||||
tm.assert_numpy_array_equal(base > idx, exp)
|
||||
|
||||
exp = np.array([True, False, False, True])
|
||||
tm.assert_numpy_array_equal(base < idx, exp)
|
||||
|
||||
exp = np.array([False, True, True, False])
|
||||
tm.assert_numpy_array_equal(base >= idx, exp)
|
||||
|
||||
exp = np.array([True, False, True, True])
|
||||
tm.assert_numpy_array_equal(base <= idx, exp)
|
||||
|
||||
@pytest.mark.parametrize('freq', ['M', '2M', '3M'])
|
||||
def test_pi_cmp_pi_mismatched_freq_raises(self, freq):
|
||||
# different base freq
|
||||
base = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'],
|
||||
freq=freq)
|
||||
|
||||
msg = "Input has different freq=A-DEC from PeriodIndex"
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
base <= Period('2011', freq='A')
|
||||
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
Period('2011', freq='A') >= base
|
||||
|
||||
idx = PeriodIndex(['2011', '2012', '2013', '2014'], freq='A')
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
base <= idx
|
||||
|
||||
# Different frequency
|
||||
msg = "Input has different freq=4M from PeriodIndex"
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
base <= Period('2011', freq='4M')
|
||||
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
Period('2011', freq='4M') >= base
|
||||
|
||||
idx = PeriodIndex(['2011', '2012', '2013', '2014'], freq='4M')
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
base <= idx
|
||||
|
||||
@pytest.mark.parametrize('freq', ['M', '2M', '3M'])
|
||||
def test_pi_cmp_nat(self, freq):
|
||||
idx1 = PeriodIndex(['2011-01', '2011-02', 'NaT', '2011-05'], freq=freq)
|
||||
|
||||
result = idx1 > Period('2011-02', freq=freq)
|
||||
exp = np.array([False, False, False, True])
|
||||
tm.assert_numpy_array_equal(result, exp)
|
||||
result = Period('2011-02', freq=freq) < idx1
|
||||
tm.assert_numpy_array_equal(result, exp)
|
||||
|
||||
result = idx1 == Period('NaT', freq=freq)
|
||||
exp = np.array([False, False, False, False])
|
||||
tm.assert_numpy_array_equal(result, exp)
|
||||
result = Period('NaT', freq=freq) == idx1
|
||||
tm.assert_numpy_array_equal(result, exp)
|
||||
|
||||
result = idx1 != Period('NaT', freq=freq)
|
||||
exp = np.array([True, True, True, True])
|
||||
tm.assert_numpy_array_equal(result, exp)
|
||||
result = Period('NaT', freq=freq) != idx1
|
||||
tm.assert_numpy_array_equal(result, exp)
|
||||
|
||||
idx2 = PeriodIndex(['2011-02', '2011-01', '2011-04', 'NaT'], freq=freq)
|
||||
result = idx1 < idx2
|
||||
exp = np.array([True, False, False, False])
|
||||
tm.assert_numpy_array_equal(result, exp)
|
||||
|
||||
result = idx1 == idx2
|
||||
exp = np.array([False, False, False, False])
|
||||
tm.assert_numpy_array_equal(result, exp)
|
||||
|
||||
result = idx1 != idx2
|
||||
exp = np.array([True, True, True, True])
|
||||
tm.assert_numpy_array_equal(result, exp)
|
||||
|
||||
result = idx1 == idx1
|
||||
exp = np.array([True, True, False, True])
|
||||
tm.assert_numpy_array_equal(result, exp)
|
||||
|
||||
result = idx1 != idx1
|
||||
exp = np.array([False, False, True, False])
|
||||
tm.assert_numpy_array_equal(result, exp)
|
||||
|
||||
@pytest.mark.parametrize('freq', ['M', '2M', '3M'])
|
||||
def test_pi_cmp_nat_mismatched_freq_raises(self, freq):
|
||||
idx1 = PeriodIndex(['2011-01', '2011-02', 'NaT', '2011-05'], freq=freq)
|
||||
|
||||
diff = PeriodIndex(['2011-02', '2011-01', '2011-04', 'NaT'], freq='4M')
|
||||
msg = "Input has different freq=4M from PeriodIndex"
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
idx1 > diff
|
||||
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
idx1 == diff
|
||||
|
||||
# TODO: De-duplicate with test_pi_cmp_nat
|
||||
@pytest.mark.parametrize('dtype', [object, None])
|
||||
def test_comp_nat(self, dtype):
|
||||
left = pd.PeriodIndex([pd.Period('2011-01-01'), pd.NaT,
|
||||
pd.Period('2011-01-03')])
|
||||
right = pd.PeriodIndex([pd.NaT, pd.NaT, pd.Period('2011-01-03')])
|
||||
|
||||
if dtype is not None:
|
||||
left = left.astype(dtype)
|
||||
right = right.astype(dtype)
|
||||
|
||||
result = left == right
|
||||
expected = np.array([False, False, True])
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
result = left != right
|
||||
expected = np.array([True, True, False])
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
expected = np.array([False, False, False])
|
||||
tm.assert_numpy_array_equal(left == pd.NaT, expected)
|
||||
tm.assert_numpy_array_equal(pd.NaT == right, expected)
|
||||
|
||||
expected = np.array([True, True, True])
|
||||
tm.assert_numpy_array_equal(left != pd.NaT, expected)
|
||||
tm.assert_numpy_array_equal(pd.NaT != left, expected)
|
||||
|
||||
expected = np.array([False, False, False])
|
||||
tm.assert_numpy_array_equal(left < pd.NaT, expected)
|
||||
tm.assert_numpy_array_equal(pd.NaT > left, expected)
|
||||
|
||||
|
||||
class TestPeriodIndexArithmetic(object):
|
||||
|
||||
# -------------------------------------------------------------
|
||||
# Invalid Operations
|
||||
|
||||
@pytest.mark.parametrize('other', [3.14, np.array([2.0, 3.0])])
|
||||
@pytest.mark.parametrize('op', [operator.add, ops.radd,
|
||||
operator.sub, ops.rsub])
|
||||
def test_pi_add_sub_float(self, op, other):
|
||||
dti = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')
|
||||
pi = dti.to_period('D')
|
||||
with pytest.raises(TypeError):
|
||||
op(pi, other)
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# __add__/__sub__ with ndarray[datetime64] and ndarray[timedelta64]
|
||||
|
||||
def test_pi_add_sub_dt64_array_raises(self):
|
||||
rng = pd.period_range('1/1/2000', freq='D', periods=3)
|
||||
dti = pd.date_range('2016-01-01', periods=3)
|
||||
dtarr = dti.values
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
rng + dtarr
|
||||
with pytest.raises(TypeError):
|
||||
dtarr + rng
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
rng - dtarr
|
||||
with pytest.raises(TypeError):
|
||||
dtarr - rng
|
||||
|
||||
def test_pi_add_sub_td64_array_non_tick_raises(self):
|
||||
rng = pd.period_range('1/1/2000', freq='Q', periods=3)
|
||||
dti = pd.date_range('2016-01-01', periods=3)
|
||||
tdi = dti - dti.shift(1)
|
||||
tdarr = tdi.values
|
||||
|
||||
with pytest.raises(period.IncompatibleFrequency):
|
||||
rng + tdarr
|
||||
with pytest.raises(period.IncompatibleFrequency):
|
||||
tdarr + rng
|
||||
|
||||
with pytest.raises(period.IncompatibleFrequency):
|
||||
rng - tdarr
|
||||
with pytest.raises(period.IncompatibleFrequency):
|
||||
tdarr - rng
|
||||
|
||||
@pytest.mark.xfail(reason='op with TimedeltaIndex raises, with ndarray OK')
|
||||
def test_pi_add_sub_td64_array_tick(self):
|
||||
rng = pd.period_range('1/1/2000', freq='Q', periods=3)
|
||||
dti = pd.date_range('2016-01-01', periods=3)
|
||||
tdi = dti - dti.shift(1)
|
||||
tdarr = tdi.values
|
||||
|
||||
expected = rng + tdi
|
||||
result = rng + tdarr
|
||||
tm.assert_index_equal(result, expected)
|
||||
result = tdarr + rng
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
expected = rng - tdi
|
||||
result = rng - tdarr
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
tdarr - rng
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# operations with array/Index of DateOffset objects
|
||||
|
||||
@pytest.mark.parametrize('box', [np.array, pd.Index])
|
||||
def test_pi_add_offset_array(self, box):
|
||||
# GH#18849
|
||||
pi = pd.PeriodIndex([pd.Period('2015Q1'), pd.Period('2016Q2')])
|
||||
offs = box([pd.offsets.QuarterEnd(n=1, startingMonth=12),
|
||||
pd.offsets.QuarterEnd(n=-2, startingMonth=12)])
|
||||
expected = pd.PeriodIndex([pd.Period('2015Q2'), pd.Period('2015Q4')])
|
||||
|
||||
with tm.assert_produces_warning(PerformanceWarning):
|
||||
res = pi + offs
|
||||
tm.assert_index_equal(res, expected)
|
||||
|
||||
with tm.assert_produces_warning(PerformanceWarning):
|
||||
res2 = offs + pi
|
||||
tm.assert_index_equal(res2, expected)
|
||||
|
||||
unanchored = np.array([pd.offsets.Hour(n=1),
|
||||
pd.offsets.Minute(n=-2)])
|
||||
# addition/subtraction ops with incompatible offsets should issue
|
||||
# a PerformanceWarning and _then_ raise a TypeError.
|
||||
with pytest.raises(period.IncompatibleFrequency):
|
||||
with tm.assert_produces_warning(PerformanceWarning):
|
||||
pi + unanchored
|
||||
with pytest.raises(period.IncompatibleFrequency):
|
||||
with tm.assert_produces_warning(PerformanceWarning):
|
||||
unanchored + pi
|
||||
|
||||
@pytest.mark.parametrize('box', [np.array, pd.Index])
|
||||
def test_pi_sub_offset_array(self, box):
|
||||
# GH#18824
|
||||
pi = pd.PeriodIndex([pd.Period('2015Q1'), pd.Period('2016Q2')])
|
||||
other = box([pd.offsets.QuarterEnd(n=1, startingMonth=12),
|
||||
pd.offsets.QuarterEnd(n=-2, startingMonth=12)])
|
||||
|
||||
expected = PeriodIndex([pi[n] - other[n] for n in range(len(pi))])
|
||||
|
||||
with tm.assert_produces_warning(PerformanceWarning):
|
||||
res = pi - other
|
||||
tm.assert_index_equal(res, expected)
|
||||
|
||||
anchored = box([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)])
|
||||
|
||||
# addition/subtraction ops with anchored offsets should issue
|
||||
# a PerformanceWarning and _then_ raise a TypeError.
|
||||
with pytest.raises(period.IncompatibleFrequency):
|
||||
with tm.assert_produces_warning(PerformanceWarning):
|
||||
pi - anchored
|
||||
with pytest.raises(period.IncompatibleFrequency):
|
||||
with tm.assert_produces_warning(PerformanceWarning):
|
||||
anchored - pi
|
||||
|
||||
def test_pi_add_iadd_pi_raises(self):
|
||||
rng = pd.period_range('1/1/2000', freq='D', periods=5)
|
||||
other = pd.period_range('1/6/2000', freq='D', periods=5)
|
||||
|
||||
# previously performed setop union, now raises TypeError (GH14164)
|
||||
with pytest.raises(TypeError):
|
||||
rng + other
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
rng += other
|
||||
|
||||
def test_pi_add_iadd_int(self, one):
|
||||
# Variants of `one` for #19012
|
||||
rng = pd.period_range('2000-01-01 09:00', freq='H', periods=10)
|
||||
result = rng + one
|
||||
expected = pd.period_range('2000-01-01 10:00', freq='H', periods=10)
|
||||
tm.assert_index_equal(result, expected)
|
||||
rng += one
|
||||
tm.assert_index_equal(rng, expected)
|
||||
|
||||
def test_pi_sub_isub_int(self, one):
|
||||
"""
|
||||
PeriodIndex.__sub__ and __isub__ with several representations of
|
||||
the integer 1, e.g. int, long, np.int64, np.uint8, ...
|
||||
"""
|
||||
rng = pd.period_range('2000-01-01 09:00', freq='H', periods=10)
|
||||
result = rng - one
|
||||
expected = pd.period_range('2000-01-01 08:00', freq='H', periods=10)
|
||||
tm.assert_index_equal(result, expected)
|
||||
rng -= one
|
||||
tm.assert_index_equal(rng, expected)
|
||||
|
||||
@pytest.mark.parametrize('five', [5, np.array(5, dtype=np.int64)])
|
||||
def test_pi_sub_intlike(self, five):
|
||||
rng = period_range('2007-01', periods=50)
|
||||
|
||||
result = rng - five
|
||||
exp = rng + (-five)
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
def test_pi_sub_isub_pi_raises(self):
|
||||
# previously performed setop, now raises TypeError (GH14164)
|
||||
# TODO needs to wait on #13077 for decision on result type
|
||||
rng = pd.period_range('1/1/2000', freq='D', periods=5)
|
||||
other = pd.period_range('1/6/2000', freq='D', periods=5)
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
rng - other
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
rng -= other
|
||||
|
||||
def test_pi_sub_isub_offset(self):
|
||||
# offset
|
||||
# DateOffset
|
||||
rng = pd.period_range('2014', '2024', freq='A')
|
||||
result = rng - pd.offsets.YearEnd(5)
|
||||
expected = pd.period_range('2009', '2019', freq='A')
|
||||
tm.assert_index_equal(result, expected)
|
||||
rng -= pd.offsets.YearEnd(5)
|
||||
tm.assert_index_equal(rng, expected)
|
||||
|
||||
rng = pd.period_range('2014-01', '2016-12', freq='M')
|
||||
result = rng - pd.offsets.MonthEnd(5)
|
||||
expected = pd.period_range('2013-08', '2016-07', freq='M')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
rng -= pd.offsets.MonthEnd(5)
|
||||
tm.assert_index_equal(rng, expected)
|
||||
|
||||
# ---------------------------------------------------------------
|
||||
# Timedelta-like (timedelta, timedelta64, Timedelta, Tick)
|
||||
# TODO: Some of these are misnomers because of non-Tick DateOffsets
|
||||
|
||||
def test_pi_add_iadd_timedeltalike_daily(self, three_days):
|
||||
# Tick
|
||||
other = three_days
|
||||
rng = pd.period_range('2014-05-01', '2014-05-15', freq='D')
|
||||
expected = pd.period_range('2014-05-04', '2014-05-18', freq='D')
|
||||
|
||||
result = rng + other
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
rng += other
|
||||
tm.assert_index_equal(rng, expected)
|
||||
|
||||
def test_pi_sub_isub_timedeltalike_daily(self, three_days):
|
||||
# Tick-like 3 Days
|
||||
other = three_days
|
||||
rng = pd.period_range('2014-05-01', '2014-05-15', freq='D')
|
||||
expected = pd.period_range('2014-04-28', '2014-05-12', freq='D')
|
||||
|
||||
result = rng - other
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
rng -= other
|
||||
tm.assert_index_equal(rng, expected)
|
||||
|
||||
def test_pi_add_iadd_timedeltalike_freq_mismatch_daily(self, not_daily):
|
||||
other = not_daily
|
||||
rng = pd.period_range('2014-05-01', '2014-05-15', freq='D')
|
||||
msg = 'Input has different freq(=.+)? from PeriodIndex\\(freq=D\\)'
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
rng + other
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
rng += other
|
||||
|
||||
def test_pi_sub_timedeltalike_freq_mismatch_daily(self, not_daily):
|
||||
other = not_daily
|
||||
rng = pd.period_range('2014-05-01', '2014-05-15', freq='D')
|
||||
msg = 'Input has different freq(=.+)? from PeriodIndex\\(freq=D\\)'
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
rng - other
|
||||
|
||||
def test_pi_add_iadd_timedeltalike_hourly(self, two_hours):
|
||||
other = two_hours
|
||||
rng = pd.period_range('2014-01-01 10:00', '2014-01-05 10:00', freq='H')
|
||||
expected = pd.period_range('2014-01-01 12:00', '2014-01-05 12:00',
|
||||
freq='H')
|
||||
|
||||
result = rng + other
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
rng += other
|
||||
tm.assert_index_equal(rng, expected)
|
||||
|
||||
def test_pi_add_timedeltalike_mismatched_freq_hourly(self, not_hourly):
|
||||
other = not_hourly
|
||||
rng = pd.period_range('2014-01-01 10:00', '2014-01-05 10:00', freq='H')
|
||||
msg = 'Input has different freq(=.+)? from PeriodIndex\\(freq=H\\)'
|
||||
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
rng + other
|
||||
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
rng += other
|
||||
|
||||
def test_pi_sub_isub_timedeltalike_hourly(self, two_hours):
|
||||
other = two_hours
|
||||
rng = pd.period_range('2014-01-01 10:00', '2014-01-05 10:00', freq='H')
|
||||
expected = pd.period_range('2014-01-01 08:00', '2014-01-05 08:00',
|
||||
freq='H')
|
||||
|
||||
result = rng - other
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
rng -= other
|
||||
tm.assert_index_equal(rng, expected)
|
||||
|
||||
def test_add_iadd_timedeltalike_annual(self):
|
||||
# offset
|
||||
# DateOffset
|
||||
rng = pd.period_range('2014', '2024', freq='A')
|
||||
result = rng + pd.offsets.YearEnd(5)
|
||||
expected = pd.period_range('2019', '2029', freq='A')
|
||||
tm.assert_index_equal(result, expected)
|
||||
rng += pd.offsets.YearEnd(5)
|
||||
tm.assert_index_equal(rng, expected)
|
||||
|
||||
def test_pi_add_iadd_timedeltalike_freq_mismatch_annual(self, mismatched):
|
||||
other = mismatched
|
||||
rng = pd.period_range('2014', '2024', freq='A')
|
||||
msg = ('Input has different freq(=.+)? '
|
||||
'from PeriodIndex\\(freq=A-DEC\\)')
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
rng + other
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
rng += other
|
||||
|
||||
def test_pi_sub_isub_timedeltalike_freq_mismatch_annual(self, mismatched):
|
||||
other = mismatched
|
||||
rng = pd.period_range('2014', '2024', freq='A')
|
||||
msg = ('Input has different freq(=.+)? '
|
||||
'from PeriodIndex\\(freq=A-DEC\\)')
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
rng - other
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
rng -= other
|
||||
|
||||
def test_pi_add_iadd_timedeltalike_M(self):
|
||||
rng = pd.period_range('2014-01', '2016-12', freq='M')
|
||||
expected = pd.period_range('2014-06', '2017-05', freq='M')
|
||||
|
||||
result = rng + pd.offsets.MonthEnd(5)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
rng += pd.offsets.MonthEnd(5)
|
||||
tm.assert_index_equal(rng, expected)
|
||||
|
||||
def test_pi_add_iadd_timedeltalike_freq_mismatch_monthly(self, mismatched):
|
||||
other = mismatched
|
||||
rng = pd.period_range('2014-01', '2016-12', freq='M')
|
||||
msg = 'Input has different freq(=.+)? from PeriodIndex\\(freq=M\\)'
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
rng + other
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
rng += other
|
||||
|
||||
def test_pi_sub_isub_timedeltalike_freq_mismatch_monthly(self, mismatched):
|
||||
other = mismatched
|
||||
rng = pd.period_range('2014-01', '2016-12', freq='M')
|
||||
msg = 'Input has different freq(=.+)? from PeriodIndex\\(freq=M\\)'
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
rng - other
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
rng -= other
|
||||
|
||||
# ---------------------------------------------------------------
|
||||
# PeriodIndex.shift is used by __add__ and __sub__
|
||||
|
||||
def test_pi_shift_ndarray(self):
|
||||
idx = PeriodIndex(['2011-01', '2011-02', 'NaT', '2011-04'],
|
||||
freq='M', name='idx')
|
||||
result = idx.shift(np.array([1, 2, 3, 4]))
|
||||
expected = PeriodIndex(['2011-02', '2011-04', 'NaT', '2011-08'],
|
||||
freq='M', name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = idx.shift(np.array([1, -2, 3, -4]))
|
||||
expected = PeriodIndex(['2011-02', '2010-12', 'NaT', '2010-12'],
|
||||
freq='M', name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_shift(self):
|
||||
pi1 = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009')
|
||||
pi2 = PeriodIndex(freq='A', start='1/1/2002', end='12/1/2010')
|
||||
|
||||
tm.assert_index_equal(pi1.shift(0), pi1)
|
||||
|
||||
assert len(pi1) == len(pi2)
|
||||
tm.assert_index_equal(pi1.shift(1), pi2)
|
||||
|
||||
pi1 = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009')
|
||||
pi2 = PeriodIndex(freq='A', start='1/1/2000', end='12/1/2008')
|
||||
assert len(pi1) == len(pi2)
|
||||
tm.assert_index_equal(pi1.shift(-1), pi2)
|
||||
|
||||
pi1 = PeriodIndex(freq='M', start='1/1/2001', end='12/1/2009')
|
||||
pi2 = PeriodIndex(freq='M', start='2/1/2001', end='1/1/2010')
|
||||
assert len(pi1) == len(pi2)
|
||||
tm.assert_index_equal(pi1.shift(1), pi2)
|
||||
|
||||
pi1 = PeriodIndex(freq='M', start='1/1/2001', end='12/1/2009')
|
||||
pi2 = PeriodIndex(freq='M', start='12/1/2000', end='11/1/2009')
|
||||
assert len(pi1) == len(pi2)
|
||||
tm.assert_index_equal(pi1.shift(-1), pi2)
|
||||
|
||||
pi1 = PeriodIndex(freq='D', start='1/1/2001', end='12/1/2009')
|
||||
pi2 = PeriodIndex(freq='D', start='1/2/2001', end='12/2/2009')
|
||||
assert len(pi1) == len(pi2)
|
||||
tm.assert_index_equal(pi1.shift(1), pi2)
|
||||
|
||||
pi1 = PeriodIndex(freq='D', start='1/1/2001', end='12/1/2009')
|
||||
pi2 = PeriodIndex(freq='D', start='12/31/2000', end='11/30/2009')
|
||||
assert len(pi1) == len(pi2)
|
||||
tm.assert_index_equal(pi1.shift(-1), pi2)
|
||||
|
||||
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)
|
||||
|
||||
def test_shift_nat(self):
|
||||
idx = PeriodIndex(['2011-01', '2011-02', 'NaT', '2011-04'],
|
||||
freq='M', name='idx')
|
||||
result = idx.shift(1)
|
||||
expected = PeriodIndex(['2011-02', '2011-03', 'NaT', '2011-05'],
|
||||
freq='M', name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
|
||||
def test_shift_gh8083(self):
|
||||
# test shift for PeriodIndex
|
||||
# GH#8083
|
||||
drange = pd.period_range('20130101', periods=5, freq='D')
|
||||
result = drange.shift(1)
|
||||
expected = PeriodIndex(['2013-01-02', '2013-01-03', '2013-01-04',
|
||||
'2013-01-05', '2013-01-06'], freq='D')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
|
||||
class TestPeriodIndexSeriesMethods(object):
|
||||
""" Test PeriodIndex and Period Series Ops consistency """
|
||||
|
||||
def _check(self, values, func, expected):
|
||||
idx = pd.PeriodIndex(values)
|
||||
result = func(idx)
|
||||
if isinstance(expected, pd.Index):
|
||||
tm.assert_index_equal(result, expected)
|
||||
else:
|
||||
# comp op results in bool
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
ser = pd.Series(values)
|
||||
result = func(ser)
|
||||
|
||||
exp = pd.Series(expected, name=values.name)
|
||||
tm.assert_series_equal(result, exp)
|
||||
|
||||
def test_pi_ops(self):
|
||||
idx = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'],
|
||||
freq='M', name='idx')
|
||||
|
||||
expected = PeriodIndex(['2011-03', '2011-04', '2011-05', '2011-06'],
|
||||
freq='M', name='idx')
|
||||
self._check(idx, lambda x: x + 2, expected)
|
||||
self._check(idx, lambda x: 2 + x, expected)
|
||||
|
||||
self._check(idx + 2, lambda x: x - 2, idx)
|
||||
result = idx - Period('2011-01', freq='M')
|
||||
exp = pd.Index([0, 1, 2, 3], name='idx')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
result = Period('2011-01', freq='M') - idx
|
||||
exp = pd.Index([0, -1, -2, -3], name='idx')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
@pytest.mark.parametrize('ng', ["str", 1.5])
|
||||
def test_pi_ops_errors(self, ng):
|
||||
idx = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'],
|
||||
freq='M', name='idx')
|
||||
ser = pd.Series(idx)
|
||||
|
||||
msg = r"unsupported operand type\(s\)"
|
||||
|
||||
for obj in [idx, ser]:
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
obj + ng
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
# error message differs between PY2 and 3
|
||||
ng + obj
|
||||
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
obj - ng
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
np.add(obj, ng)
|
||||
|
||||
if _np_version_under1p10:
|
||||
assert np.add(ng, obj) is NotImplemented
|
||||
else:
|
||||
with pytest.raises(TypeError):
|
||||
np.add(ng, obj)
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
np.subtract(obj, ng)
|
||||
|
||||
if _np_version_under1p10:
|
||||
assert np.subtract(ng, obj) is NotImplemented
|
||||
else:
|
||||
with pytest.raises(TypeError):
|
||||
np.subtract(ng, obj)
|
||||
|
||||
def test_pi_ops_nat(self):
|
||||
idx = PeriodIndex(['2011-01', '2011-02', 'NaT', '2011-04'],
|
||||
freq='M', name='idx')
|
||||
expected = PeriodIndex(['2011-03', '2011-04', 'NaT', '2011-06'],
|
||||
freq='M', name='idx')
|
||||
self._check(idx, lambda x: x + 2, expected)
|
||||
self._check(idx, lambda x: 2 + x, expected)
|
||||
self._check(idx, lambda x: np.add(x, 2), expected)
|
||||
|
||||
self._check(idx + 2, lambda x: x - 2, idx)
|
||||
self._check(idx + 2, lambda x: np.subtract(x, 2), idx)
|
||||
|
||||
# freq with mult
|
||||
idx = PeriodIndex(['2011-01', '2011-02', 'NaT', '2011-04'],
|
||||
freq='2M', name='idx')
|
||||
expected = PeriodIndex(['2011-07', '2011-08', 'NaT', '2011-10'],
|
||||
freq='2M', name='idx')
|
||||
self._check(idx, lambda x: x + 3, expected)
|
||||
self._check(idx, lambda x: 3 + x, expected)
|
||||
self._check(idx, lambda x: np.add(x, 3), expected)
|
||||
|
||||
self._check(idx + 3, lambda x: x - 3, idx)
|
||||
self._check(idx + 3, lambda x: np.subtract(x, 3), idx)
|
||||
|
||||
def test_pi_ops_array_int(self):
|
||||
idx = PeriodIndex(['2011-01', '2011-02', 'NaT', '2011-04'],
|
||||
freq='M', name='idx')
|
||||
f = lambda x: x + np.array([1, 2, 3, 4])
|
||||
exp = PeriodIndex(['2011-02', '2011-04', 'NaT', '2011-08'],
|
||||
freq='M', name='idx')
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: np.add(x, np.array([4, -1, 1, 2]))
|
||||
exp = PeriodIndex(['2011-05', '2011-01', 'NaT', '2011-06'],
|
||||
freq='M', name='idx')
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: x - np.array([1, 2, 3, 4])
|
||||
exp = PeriodIndex(['2010-12', '2010-12', 'NaT', '2010-12'],
|
||||
freq='M', name='idx')
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: np.subtract(x, np.array([3, 2, 3, -2]))
|
||||
exp = PeriodIndex(['2010-10', '2010-12', 'NaT', '2011-06'],
|
||||
freq='M', name='idx')
|
||||
self._check(idx, f, exp)
|
||||
|
||||
def test_pi_ops_offset(self):
|
||||
idx = PeriodIndex(['2011-01-01', '2011-02-01', '2011-03-01',
|
||||
'2011-04-01'], freq='D', name='idx')
|
||||
f = lambda x: x + pd.offsets.Day()
|
||||
exp = PeriodIndex(['2011-01-02', '2011-02-02', '2011-03-02',
|
||||
'2011-04-02'], freq='D', name='idx')
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: x + pd.offsets.Day(2)
|
||||
exp = PeriodIndex(['2011-01-03', '2011-02-03', '2011-03-03',
|
||||
'2011-04-03'], freq='D', name='idx')
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: x - pd.offsets.Day(2)
|
||||
exp = PeriodIndex(['2010-12-30', '2011-01-30', '2011-02-27',
|
||||
'2011-03-30'], freq='D', name='idx')
|
||||
self._check(idx, f, exp)
|
||||
|
||||
def test_pi_offset_errors(self):
|
||||
idx = PeriodIndex(['2011-01-01', '2011-02-01', '2011-03-01',
|
||||
'2011-04-01'], freq='D', name='idx')
|
||||
ser = pd.Series(idx)
|
||||
|
||||
# Series op is applied per Period instance, thus error is raised
|
||||
# from Period
|
||||
msg_idx = r"Input has different freq from PeriodIndex\(freq=D\)"
|
||||
msg_s = r"Input cannot be converted to Period\(freq=D\)"
|
||||
for obj, msg in [(idx, msg_idx), (ser, msg_s)]:
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
obj + pd.offsets.Hour(2)
|
||||
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
pd.offsets.Hour(2) + obj
|
||||
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
obj - pd.offsets.Hour(2)
|
||||
|
||||
def test_pi_sub_period(self):
|
||||
# GH 13071
|
||||
idx = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'],
|
||||
freq='M', name='idx')
|
||||
|
||||
result = idx - pd.Period('2012-01', freq='M')
|
||||
exp = pd.Index([-12, -11, -10, -9], name='idx')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
result = np.subtract(idx, pd.Period('2012-01', freq='M'))
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
result = pd.Period('2012-01', freq='M') - idx
|
||||
exp = pd.Index([12, 11, 10, 9], name='idx')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
result = np.subtract(pd.Period('2012-01', freq='M'), idx)
|
||||
if _np_version_under1p10:
|
||||
assert result is NotImplemented
|
||||
else:
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
exp = pd.TimedeltaIndex([np.nan, np.nan, np.nan, np.nan], name='idx')
|
||||
tm.assert_index_equal(idx - pd.Period('NaT', freq='M'), exp)
|
||||
tm.assert_index_equal(pd.Period('NaT', freq='M') - idx, exp)
|
||||
|
||||
def test_pi_sub_pdnat(self):
|
||||
# GH 13071
|
||||
idx = PeriodIndex(['2011-01', '2011-02', 'NaT', '2011-04'],
|
||||
freq='M', name='idx')
|
||||
exp = pd.TimedeltaIndex([pd.NaT] * 4, name='idx')
|
||||
tm.assert_index_equal(pd.NaT - idx, exp)
|
||||
tm.assert_index_equal(idx - pd.NaT, exp)
|
||||
|
||||
def test_pi_sub_period_nat(self):
|
||||
# GH 13071
|
||||
idx = PeriodIndex(['2011-01', 'NaT', '2011-03', '2011-04'],
|
||||
freq='M', name='idx')
|
||||
|
||||
result = idx - pd.Period('2012-01', freq='M')
|
||||
exp = pd.Index([-12, np.nan, -10, -9], name='idx')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
result = pd.Period('2012-01', freq='M') - idx
|
||||
exp = pd.Index([12, np.nan, 10, 9], name='idx')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
exp = pd.TimedeltaIndex([np.nan, np.nan, np.nan, np.nan], name='idx')
|
||||
tm.assert_index_equal(idx - pd.Period('NaT', freq='M'), exp)
|
||||
tm.assert_index_equal(pd.Period('NaT', freq='M') - idx, exp)
|
||||
@@ -1,152 +0,0 @@
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from pandas.util import testing as tm
|
||||
from pandas import PeriodIndex, Series, DataFrame
|
||||
|
||||
|
||||
class TestPeriodIndex(object):
|
||||
|
||||
def test_asfreq(self):
|
||||
pi1 = PeriodIndex(freq='A', start='1/1/2001', end='1/1/2001')
|
||||
pi2 = PeriodIndex(freq='Q', start='1/1/2001', end='1/1/2001')
|
||||
pi3 = PeriodIndex(freq='M', start='1/1/2001', end='1/1/2001')
|
||||
pi4 = PeriodIndex(freq='D', start='1/1/2001', end='1/1/2001')
|
||||
pi5 = PeriodIndex(freq='H', start='1/1/2001', end='1/1/2001 00:00')
|
||||
pi6 = PeriodIndex(freq='Min', start='1/1/2001', end='1/1/2001 00:00')
|
||||
pi7 = PeriodIndex(freq='S', start='1/1/2001', end='1/1/2001 00:00:00')
|
||||
|
||||
assert pi1.asfreq('Q', 'S') == pi2
|
||||
assert pi1.asfreq('Q', 's') == pi2
|
||||
assert pi1.asfreq('M', 'start') == pi3
|
||||
assert pi1.asfreq('D', 'StarT') == pi4
|
||||
assert pi1.asfreq('H', 'beGIN') == pi5
|
||||
assert pi1.asfreq('Min', 'S') == pi6
|
||||
assert pi1.asfreq('S', 'S') == pi7
|
||||
|
||||
assert pi2.asfreq('A', 'S') == pi1
|
||||
assert pi2.asfreq('M', 'S') == pi3
|
||||
assert pi2.asfreq('D', 'S') == pi4
|
||||
assert pi2.asfreq('H', 'S') == pi5
|
||||
assert pi2.asfreq('Min', 'S') == pi6
|
||||
assert pi2.asfreq('S', 'S') == pi7
|
||||
|
||||
assert pi3.asfreq('A', 'S') == pi1
|
||||
assert pi3.asfreq('Q', 'S') == pi2
|
||||
assert pi3.asfreq('D', 'S') == pi4
|
||||
assert pi3.asfreq('H', 'S') == pi5
|
||||
assert pi3.asfreq('Min', 'S') == pi6
|
||||
assert pi3.asfreq('S', 'S') == pi7
|
||||
|
||||
assert pi4.asfreq('A', 'S') == pi1
|
||||
assert pi4.asfreq('Q', 'S') == pi2
|
||||
assert pi4.asfreq('M', 'S') == pi3
|
||||
assert pi4.asfreq('H', 'S') == pi5
|
||||
assert pi4.asfreq('Min', 'S') == pi6
|
||||
assert pi4.asfreq('S', 'S') == pi7
|
||||
|
||||
assert pi5.asfreq('A', 'S') == pi1
|
||||
assert pi5.asfreq('Q', 'S') == pi2
|
||||
assert pi5.asfreq('M', 'S') == pi3
|
||||
assert pi5.asfreq('D', 'S') == pi4
|
||||
assert pi5.asfreq('Min', 'S') == pi6
|
||||
assert pi5.asfreq('S', 'S') == pi7
|
||||
|
||||
assert pi6.asfreq('A', 'S') == pi1
|
||||
assert pi6.asfreq('Q', 'S') == pi2
|
||||
assert pi6.asfreq('M', 'S') == pi3
|
||||
assert pi6.asfreq('D', 'S') == pi4
|
||||
assert pi6.asfreq('H', 'S') == pi5
|
||||
assert pi6.asfreq('S', 'S') == pi7
|
||||
|
||||
assert pi7.asfreq('A', 'S') == pi1
|
||||
assert pi7.asfreq('Q', 'S') == pi2
|
||||
assert pi7.asfreq('M', 'S') == pi3
|
||||
assert pi7.asfreq('D', 'S') == pi4
|
||||
assert pi7.asfreq('H', 'S') == pi5
|
||||
assert pi7.asfreq('Min', 'S') == pi6
|
||||
|
||||
pytest.raises(ValueError, pi7.asfreq, 'T', 'foo')
|
||||
result1 = pi1.asfreq('3M')
|
||||
result2 = pi1.asfreq('M')
|
||||
expected = PeriodIndex(freq='M', start='2001-12', end='2001-12')
|
||||
tm.assert_numpy_array_equal(result1.asi8, expected.asi8)
|
||||
assert result1.freqstr == '3M'
|
||||
tm.assert_numpy_array_equal(result2.asi8, expected.asi8)
|
||||
assert result2.freqstr == 'M'
|
||||
|
||||
def test_asfreq_nat(self):
|
||||
idx = PeriodIndex(['2011-01', '2011-02', 'NaT', '2011-04'], freq='M')
|
||||
result = idx.asfreq(freq='Q')
|
||||
expected = PeriodIndex(['2011Q1', '2011Q1', 'NaT', '2011Q2'], freq='Q')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize('freq', ['D', '3D'])
|
||||
def test_asfreq_mult_pi(self, freq):
|
||||
pi = PeriodIndex(['2001-01', '2001-02', 'NaT', '2001-03'], freq='2M')
|
||||
|
||||
result = pi.asfreq(freq)
|
||||
exp = PeriodIndex(['2001-02-28', '2001-03-31', 'NaT',
|
||||
'2001-04-30'], freq=freq)
|
||||
tm.assert_index_equal(result, exp)
|
||||
assert result.freq == exp.freq
|
||||
|
||||
result = pi.asfreq(freq, how='S')
|
||||
exp = PeriodIndex(['2001-01-01', '2001-02-01', 'NaT',
|
||||
'2001-03-01'], freq=freq)
|
||||
tm.assert_index_equal(result, exp)
|
||||
assert result.freq == exp.freq
|
||||
|
||||
def test_asfreq_combined_pi(self):
|
||||
pi = pd.PeriodIndex(['2001-01-01 00:00', '2001-01-02 02:00', 'NaT'],
|
||||
freq='H')
|
||||
exp = PeriodIndex(['2001-01-01 00:00', '2001-01-02 02:00', 'NaT'],
|
||||
freq='25H')
|
||||
for freq, how in zip(['1D1H', '1H1D'], ['S', 'E']):
|
||||
result = pi.asfreq(freq, how=how)
|
||||
tm.assert_index_equal(result, exp)
|
||||
assert result.freq == exp.freq
|
||||
|
||||
for freq in ['1D1H', '1H1D']:
|
||||
pi = pd.PeriodIndex(['2001-01-01 00:00', '2001-01-02 02:00',
|
||||
'NaT'], freq=freq)
|
||||
result = pi.asfreq('H')
|
||||
exp = PeriodIndex(['2001-01-02 00:00', '2001-01-03 02:00', 'NaT'],
|
||||
freq='H')
|
||||
tm.assert_index_equal(result, exp)
|
||||
assert result.freq == exp.freq
|
||||
|
||||
pi = pd.PeriodIndex(['2001-01-01 00:00', '2001-01-02 02:00',
|
||||
'NaT'], freq=freq)
|
||||
result = pi.asfreq('H', how='S')
|
||||
exp = PeriodIndex(['2001-01-01 00:00', '2001-01-02 02:00', 'NaT'],
|
||||
freq='H')
|
||||
tm.assert_index_equal(result, exp)
|
||||
assert result.freq == exp.freq
|
||||
|
||||
def test_asfreq_ts(self):
|
||||
index = PeriodIndex(freq='A', start='1/1/2001', end='12/31/2010')
|
||||
ts = Series(np.random.randn(len(index)), index=index)
|
||||
df = DataFrame(np.random.randn(len(index), 3), index=index)
|
||||
|
||||
result = ts.asfreq('D', how='end')
|
||||
df_result = df.asfreq('D', how='end')
|
||||
exp_index = index.asfreq('D', how='end')
|
||||
assert len(result) == len(ts)
|
||||
tm.assert_index_equal(result.index, exp_index)
|
||||
tm.assert_index_equal(df_result.index, exp_index)
|
||||
|
||||
result = ts.asfreq('D', how='start')
|
||||
assert len(result) == len(ts)
|
||||
tm.assert_index_equal(result.index, index.asfreq('D', how='start'))
|
||||
|
||||
def test_astype_asfreq(self):
|
||||
pi1 = PeriodIndex(['2011-01-01', '2011-02-01', '2011-03-01'], freq='D')
|
||||
exp = PeriodIndex(['2011-01', '2011-02', '2011-03'], freq='M')
|
||||
tm.assert_index_equal(pi1.asfreq('M'), exp)
|
||||
tm.assert_index_equal(pi1.astype('period[M]'), exp)
|
||||
|
||||
exp = PeriodIndex(['2011-01', '2011-02', '2011-03'], freq='3M')
|
||||
tm.assert_index_equal(pi1.asfreq('3M'), exp)
|
||||
tm.assert_index_equal(pi1.astype('period[3M]'), exp)
|
||||
@@ -1,99 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
from pandas import NaT, Period, PeriodIndex, Int64Index, Index, period_range
|
||||
|
||||
|
||||
class TestPeriodIndexAsType(object):
|
||||
@pytest.mark.parametrize('dtype', [
|
||||
float, 'timedelta64', 'timedelta64[ns]'])
|
||||
def test_astype_raises(self, dtype):
|
||||
# GH#13149, GH#13209
|
||||
idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D')
|
||||
msg = 'Cannot cast PeriodIndex to dtype'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
idx.astype(dtype)
|
||||
|
||||
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(int)
|
||||
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)
|
||||
|
||||
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)
|
||||
def test_astype_object2(self):
|
||||
idx = pd.period_range(start='2013-01-01', periods=4, freq='M',
|
||||
name='idx')
|
||||
expected_list = [pd.Period('2013-01-31', freq='M'),
|
||||
pd.Period('2013-02-28', freq='M'),
|
||||
pd.Period('2013-03-31', freq='M'),
|
||||
pd.Period('2013-04-30', freq='M')]
|
||||
expected = pd.Index(expected_list, dtype=object, name='idx')
|
||||
result = idx.astype(object)
|
||||
assert isinstance(result, Index)
|
||||
assert result.dtype == object
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert idx.tolist() == expected_list
|
||||
|
||||
idx = PeriodIndex(['2013-01-01', '2013-01-02', 'NaT',
|
||||
'2013-01-04'], freq='D', name='idx')
|
||||
expected_list = [pd.Period('2013-01-01', freq='D'),
|
||||
pd.Period('2013-01-02', freq='D'),
|
||||
pd.Period('NaT', freq='D'),
|
||||
pd.Period('2013-01-04', freq='D')]
|
||||
expected = pd.Index(expected_list, dtype=object, name='idx')
|
||||
result = idx.astype(object)
|
||||
assert isinstance(result, Index)
|
||||
assert result.dtype == object
|
||||
tm.assert_index_equal(result, expected)
|
||||
for i in [0, 1, 3]:
|
||||
assert result[i] == expected[i]
|
||||
assert result[2] is pd.NaT
|
||||
assert result.name == expected.name
|
||||
|
||||
result_list = idx.tolist()
|
||||
for i in [0, 1, 3]:
|
||||
assert result_list[i] == expected_list[i]
|
||||
assert result_list[2] is pd.NaT
|
||||
-489
@@ -1,489 +0,0 @@
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
import pandas.core.indexes.period as period
|
||||
from pandas.compat import lrange, PY3, text_type, lmap
|
||||
from pandas import (Period, PeriodIndex, period_range, offsets, date_range,
|
||||
Series, Index)
|
||||
|
||||
|
||||
class TestPeriodIndex(object):
|
||||
|
||||
def setup_method(self, method):
|
||||
pass
|
||||
|
||||
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))
|
||||
|
||||
def test_constructor_use_start_freq(self):
|
||||
# GH #1118
|
||||
p = Period('4/2/2012', freq='B')
|
||||
index = PeriodIndex(start=p, periods=10)
|
||||
expected = PeriodIndex(start='4/2/2012', periods=10, freq='B')
|
||||
tm.assert_index_equal(index, expected)
|
||||
|
||||
def test_constructor_field_arrays(self):
|
||||
# GH #1264
|
||||
|
||||
years = np.arange(1990, 2010).repeat(4)[2:-2]
|
||||
quarters = np.tile(np.arange(1, 5), 20)[2:-2]
|
||||
|
||||
index = PeriodIndex(year=years, quarter=quarters, freq='Q-DEC')
|
||||
expected = period_range('1990Q3', '2009Q2', freq='Q-DEC')
|
||||
tm.assert_index_equal(index, expected)
|
||||
|
||||
index2 = PeriodIndex(year=years, quarter=quarters, freq='2Q-DEC')
|
||||
tm.assert_numpy_array_equal(index.asi8, index2.asi8)
|
||||
|
||||
index = PeriodIndex(year=years, quarter=quarters)
|
||||
tm.assert_index_equal(index, expected)
|
||||
|
||||
years = [2007, 2007, 2007]
|
||||
months = [1, 2]
|
||||
pytest.raises(ValueError, PeriodIndex, year=years, month=months,
|
||||
freq='M')
|
||||
pytest.raises(ValueError, PeriodIndex, year=years, month=months,
|
||||
freq='2M')
|
||||
pytest.raises(ValueError, PeriodIndex, year=years, month=months,
|
||||
freq='M', start=Period('2007-01', freq='M'))
|
||||
|
||||
years = [2007, 2007, 2007]
|
||||
months = [1, 2, 3]
|
||||
idx = PeriodIndex(year=years, month=months, freq='M')
|
||||
exp = period_range('2007-01', periods=3, freq='M')
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
def test_constructor_U(self):
|
||||
# U was used as undefined period
|
||||
pytest.raises(ValueError, period_range, '2007-1-1', periods=500,
|
||||
freq='X')
|
||||
|
||||
def test_constructor_nano(self):
|
||||
idx = period_range(start=Period(ordinal=1, freq='N'),
|
||||
end=Period(ordinal=4, freq='N'), freq='N')
|
||||
exp = PeriodIndex([Period(ordinal=1, freq='N'),
|
||||
Period(ordinal=2, freq='N'),
|
||||
Period(ordinal=3, freq='N'),
|
||||
Period(ordinal=4, freq='N')], freq='N')
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
def test_constructor_arrays_negative_year(self):
|
||||
years = np.arange(1960, 2000, dtype=np.int64).repeat(4)
|
||||
quarters = np.tile(np.array([1, 2, 3, 4], dtype=np.int64), 40)
|
||||
|
||||
pindex = PeriodIndex(year=years, quarter=quarters)
|
||||
|
||||
tm.assert_index_equal(pindex.year, pd.Index(years))
|
||||
tm.assert_index_equal(pindex.quarter, pd.Index(quarters))
|
||||
|
||||
def test_constructor_invalid_quarters(self):
|
||||
pytest.raises(ValueError, PeriodIndex, year=lrange(2000, 2004),
|
||||
quarter=lrange(4), freq='Q-DEC')
|
||||
|
||||
def test_constructor_corner(self):
|
||||
pytest.raises(ValueError, PeriodIndex, periods=10, freq='A')
|
||||
|
||||
start = Period('2007', freq='A-JUN')
|
||||
end = Period('2010', freq='A-DEC')
|
||||
pytest.raises(ValueError, PeriodIndex, start=start, end=end)
|
||||
pytest.raises(ValueError, PeriodIndex, start=start)
|
||||
pytest.raises(ValueError, PeriodIndex, end=end)
|
||||
|
||||
result = period_range('2007-01', periods=10.5, freq='M')
|
||||
exp = period_range('2007-01', periods=10, freq='M')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
def test_constructor_fromarraylike(self):
|
||||
idx = period_range('2007-01', periods=20, freq='M')
|
||||
|
||||
# values is an array of Period, thus can retrieve freq
|
||||
tm.assert_index_equal(PeriodIndex(idx.values), idx)
|
||||
tm.assert_index_equal(PeriodIndex(list(idx.values)), idx)
|
||||
|
||||
pytest.raises(ValueError, PeriodIndex, idx._ndarray_values)
|
||||
pytest.raises(ValueError, PeriodIndex, list(idx._ndarray_values))
|
||||
pytest.raises(TypeError, PeriodIndex,
|
||||
data=Period('2007', freq='A'))
|
||||
|
||||
result = PeriodIndex(iter(idx))
|
||||
tm.assert_index_equal(result, idx)
|
||||
|
||||
result = PeriodIndex(idx)
|
||||
tm.assert_index_equal(result, idx)
|
||||
|
||||
result = PeriodIndex(idx, freq='M')
|
||||
tm.assert_index_equal(result, idx)
|
||||
|
||||
result = PeriodIndex(idx, freq=offsets.MonthEnd())
|
||||
tm.assert_index_equal(result, idx)
|
||||
assert result.freq == 'M'
|
||||
|
||||
result = PeriodIndex(idx, freq='2M')
|
||||
tm.assert_index_equal(result, idx.asfreq('2M'))
|
||||
assert result.freq == '2M'
|
||||
|
||||
result = PeriodIndex(idx, freq=offsets.MonthEnd(2))
|
||||
tm.assert_index_equal(result, idx.asfreq('2M'))
|
||||
assert result.freq == '2M'
|
||||
|
||||
result = PeriodIndex(idx, freq='D')
|
||||
exp = idx.asfreq('D', 'e')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
def test_constructor_datetime64arr(self):
|
||||
vals = np.arange(100000, 100000 + 10000, 100, dtype=np.int64)
|
||||
vals = vals.view(np.dtype('M8[us]'))
|
||||
|
||||
pytest.raises(ValueError, PeriodIndex, vals, freq='D')
|
||||
|
||||
def test_constructor_dtype(self):
|
||||
# passing a dtype with a tz should localize
|
||||
idx = PeriodIndex(['2013-01', '2013-03'], dtype='period[M]')
|
||||
exp = PeriodIndex(['2013-01', '2013-03'], freq='M')
|
||||
tm.assert_index_equal(idx, exp)
|
||||
assert idx.dtype == 'period[M]'
|
||||
|
||||
idx = PeriodIndex(['2013-01-05', '2013-03-05'], dtype='period[3D]')
|
||||
exp = PeriodIndex(['2013-01-05', '2013-03-05'], freq='3D')
|
||||
tm.assert_index_equal(idx, exp)
|
||||
assert idx.dtype == 'period[3D]'
|
||||
|
||||
# if we already have a freq and its not the same, then asfreq
|
||||
# (not changed)
|
||||
idx = PeriodIndex(['2013-01-01', '2013-01-02'], freq='D')
|
||||
|
||||
res = PeriodIndex(idx, dtype='period[M]')
|
||||
exp = PeriodIndex(['2013-01', '2013-01'], freq='M')
|
||||
tm.assert_index_equal(res, exp)
|
||||
assert res.dtype == 'period[M]'
|
||||
|
||||
res = PeriodIndex(idx, freq='M')
|
||||
tm.assert_index_equal(res, exp)
|
||||
assert res.dtype == 'period[M]'
|
||||
|
||||
msg = 'specified freq and dtype are different'
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
PeriodIndex(['2011-01'], freq='M', dtype='period[D]')
|
||||
|
||||
def test_constructor_empty(self):
|
||||
idx = pd.PeriodIndex([], freq='M')
|
||||
assert isinstance(idx, PeriodIndex)
|
||||
assert len(idx) == 0
|
||||
assert idx.freq == 'M'
|
||||
|
||||
with tm.assert_raises_regex(ValueError, 'freq not specified'):
|
||||
pd.PeriodIndex([])
|
||||
|
||||
def test_constructor_pi_nat(self):
|
||||
idx = PeriodIndex([Period('2011-01', freq='M'), pd.NaT,
|
||||
Period('2011-01', freq='M')])
|
||||
exp = PeriodIndex(['2011-01', 'NaT', '2011-01'], freq='M')
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
idx = PeriodIndex(np.array([Period('2011-01', freq='M'), pd.NaT,
|
||||
Period('2011-01', freq='M')]))
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
idx = PeriodIndex([pd.NaT, pd.NaT, Period('2011-01', freq='M'),
|
||||
Period('2011-01', freq='M')])
|
||||
exp = PeriodIndex(['NaT', 'NaT', '2011-01', '2011-01'], freq='M')
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
idx = PeriodIndex(np.array([pd.NaT, pd.NaT,
|
||||
Period('2011-01', freq='M'),
|
||||
Period('2011-01', freq='M')]))
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
idx = PeriodIndex([pd.NaT, pd.NaT, '2011-01', '2011-01'], freq='M')
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
with tm.assert_raises_regex(ValueError, 'freq not specified'):
|
||||
PeriodIndex([pd.NaT, pd.NaT])
|
||||
|
||||
with tm.assert_raises_regex(ValueError, 'freq not specified'):
|
||||
PeriodIndex(np.array([pd.NaT, pd.NaT]))
|
||||
|
||||
with tm.assert_raises_regex(ValueError, 'freq not specified'):
|
||||
PeriodIndex(['NaT', 'NaT'])
|
||||
|
||||
with tm.assert_raises_regex(ValueError, 'freq not specified'):
|
||||
PeriodIndex(np.array(['NaT', 'NaT']))
|
||||
|
||||
def test_constructor_incompat_freq(self):
|
||||
msg = "Input has different freq=D from PeriodIndex\\(freq=M\\)"
|
||||
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
PeriodIndex([Period('2011-01', freq='M'), pd.NaT,
|
||||
Period('2011-01', freq='D')])
|
||||
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
PeriodIndex(np.array([Period('2011-01', freq='M'), pd.NaT,
|
||||
Period('2011-01', freq='D')]))
|
||||
|
||||
# first element is pd.NaT
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
PeriodIndex([pd.NaT, Period('2011-01', freq='M'),
|
||||
Period('2011-01', freq='D')])
|
||||
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
PeriodIndex(np.array([pd.NaT, Period('2011-01', freq='M'),
|
||||
Period('2011-01', freq='D')]))
|
||||
|
||||
def test_constructor_mixed(self):
|
||||
idx = PeriodIndex(['2011-01', pd.NaT, Period('2011-01', freq='M')])
|
||||
exp = PeriodIndex(['2011-01', 'NaT', '2011-01'], freq='M')
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
idx = PeriodIndex(['NaT', pd.NaT, Period('2011-01', freq='M')])
|
||||
exp = PeriodIndex(['NaT', 'NaT', '2011-01'], freq='M')
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
idx = PeriodIndex([Period('2011-01-01', freq='D'), pd.NaT,
|
||||
'2012-01-01'])
|
||||
exp = PeriodIndex(['2011-01-01', 'NaT', '2012-01-01'], freq='D')
|
||||
tm.assert_index_equal(idx, exp)
|
||||
|
||||
def test_constructor_simple_new(self):
|
||||
idx = period_range('2007-01', name='p', periods=2, freq='M')
|
||||
result = idx._simple_new(idx, 'p', freq=idx.freq)
|
||||
tm.assert_index_equal(result, idx)
|
||||
|
||||
result = idx._simple_new(idx.astype('i8'), 'p', freq=idx.freq)
|
||||
tm.assert_index_equal(result, idx)
|
||||
|
||||
result = idx._simple_new([pd.Period('2007-01', freq='M'),
|
||||
pd.Period('2007-02', freq='M')],
|
||||
'p', freq=idx.freq)
|
||||
tm.assert_index_equal(result, idx)
|
||||
|
||||
result = idx._simple_new(np.array([pd.Period('2007-01', freq='M'),
|
||||
pd.Period('2007-02', freq='M')]),
|
||||
'p', freq=idx.freq)
|
||||
tm.assert_index_equal(result, idx)
|
||||
|
||||
def test_constructor_simple_new_empty(self):
|
||||
# GH13079
|
||||
idx = PeriodIndex([], freq='M', name='p')
|
||||
result = idx._simple_new(idx, name='p', freq='M')
|
||||
tm.assert_index_equal(result, idx)
|
||||
|
||||
@pytest.mark.parametrize('floats', [[1.1, 2.1], np.array([1.1, 2.1])])
|
||||
def test_constructor_floats(self, floats):
|
||||
# GH#13079
|
||||
with pytest.raises(TypeError):
|
||||
pd.PeriodIndex._simple_new(floats, freq='M')
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
pd.PeriodIndex(floats, freq='M')
|
||||
|
||||
def test_constructor_nat(self):
|
||||
pytest.raises(ValueError, period_range, start='NaT',
|
||||
end='2011-01-01', freq='M')
|
||||
pytest.raises(ValueError, period_range, start='2011-01-01',
|
||||
end='NaT', freq='M')
|
||||
|
||||
def test_constructor_year_and_quarter(self):
|
||||
year = pd.Series([2001, 2002, 2003])
|
||||
quarter = year - 2000
|
||||
idx = PeriodIndex(year=year, quarter=quarter)
|
||||
strs = ['%dQ%d' % t for t in zip(quarter, year)]
|
||||
lops = list(map(Period, strs))
|
||||
p = PeriodIndex(lops)
|
||||
tm.assert_index_equal(p, idx)
|
||||
|
||||
def test_constructor_freq_mult(self):
|
||||
# GH #7811
|
||||
for func in [PeriodIndex, period_range]:
|
||||
# must be the same, but for sure...
|
||||
pidx = func(start='2014-01', freq='2M', periods=4)
|
||||
expected = PeriodIndex(['2014-01', '2014-03',
|
||||
'2014-05', '2014-07'], freq='2M')
|
||||
tm.assert_index_equal(pidx, expected)
|
||||
|
||||
pidx = func(start='2014-01-02', end='2014-01-15', freq='3D')
|
||||
expected = PeriodIndex(['2014-01-02', '2014-01-05',
|
||||
'2014-01-08', '2014-01-11',
|
||||
'2014-01-14'], freq='3D')
|
||||
tm.assert_index_equal(pidx, expected)
|
||||
|
||||
pidx = func(end='2014-01-01 17:00', freq='4H', periods=3)
|
||||
expected = PeriodIndex(['2014-01-01 09:00', '2014-01-01 13:00',
|
||||
'2014-01-01 17:00'], freq='4H')
|
||||
tm.assert_index_equal(pidx, expected)
|
||||
|
||||
msg = ('Frequency must be positive, because it'
|
||||
' represents span: -1M')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
PeriodIndex(['2011-01'], freq='-1M')
|
||||
|
||||
msg = ('Frequency must be positive, because it' ' represents span: 0M')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
PeriodIndex(['2011-01'], freq='0M')
|
||||
|
||||
msg = ('Frequency must be positive, because it' ' represents span: 0M')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
period_range('2011-01', periods=3, freq='0M')
|
||||
|
||||
@pytest.mark.parametrize('freq', ['A', 'M', 'D', 'T', 'S'])
|
||||
@pytest.mark.parametrize('mult', [1, 2, 3, 4, 5])
|
||||
def test_constructor_freq_mult_dti_compat(self, mult, freq):
|
||||
freqstr = str(mult) + freq
|
||||
pidx = PeriodIndex(start='2014-04-01', freq=freqstr, periods=10)
|
||||
expected = date_range(start='2014-04-01', freq=freqstr,
|
||||
periods=10).to_period(freqstr)
|
||||
tm.assert_index_equal(pidx, expected)
|
||||
|
||||
def test_constructor_freq_combined(self):
|
||||
for freq in ['1D1H', '1H1D']:
|
||||
pidx = PeriodIndex(['2016-01-01', '2016-01-02'], freq=freq)
|
||||
expected = PeriodIndex(['2016-01-01 00:00', '2016-01-02 00:00'],
|
||||
freq='25H')
|
||||
for freq, func in zip(['1D1H', '1H1D'], [PeriodIndex, period_range]):
|
||||
pidx = func(start='2016-01-01', periods=2, freq=freq)
|
||||
expected = PeriodIndex(['2016-01-01 00:00', '2016-01-02 01:00'],
|
||||
freq='25H')
|
||||
tm.assert_index_equal(pidx, expected)
|
||||
|
||||
def test_constructor(self):
|
||||
pi = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009')
|
||||
assert len(pi) == 9
|
||||
|
||||
pi = PeriodIndex(freq='Q', start='1/1/2001', end='12/1/2009')
|
||||
assert len(pi) == 4 * 9
|
||||
|
||||
pi = PeriodIndex(freq='M', start='1/1/2001', end='12/1/2009')
|
||||
assert len(pi) == 12 * 9
|
||||
|
||||
pi = PeriodIndex(freq='D', start='1/1/2001', end='12/31/2009')
|
||||
assert len(pi) == 365 * 9 + 2
|
||||
|
||||
pi = PeriodIndex(freq='B', start='1/1/2001', end='12/31/2009')
|
||||
assert len(pi) == 261 * 9
|
||||
|
||||
pi = PeriodIndex(freq='H', start='1/1/2001', end='12/31/2001 23:00')
|
||||
assert len(pi) == 365 * 24
|
||||
|
||||
pi = PeriodIndex(freq='Min', start='1/1/2001', end='1/1/2001 23:59')
|
||||
assert len(pi) == 24 * 60
|
||||
|
||||
pi = PeriodIndex(freq='S', start='1/1/2001', end='1/1/2001 23:59:59')
|
||||
assert len(pi) == 24 * 60 * 60
|
||||
|
||||
start = Period('02-Apr-2005', 'B')
|
||||
i1 = PeriodIndex(start=start, periods=20)
|
||||
assert len(i1) == 20
|
||||
assert i1.freq == start.freq
|
||||
assert i1[0] == start
|
||||
|
||||
end_intv = Period('2006-12-31', 'W')
|
||||
i1 = PeriodIndex(end=end_intv, periods=10)
|
||||
assert len(i1) == 10
|
||||
assert i1.freq == end_intv.freq
|
||||
assert i1[-1] == end_intv
|
||||
|
||||
end_intv = Period('2006-12-31', '1w')
|
||||
i2 = PeriodIndex(end=end_intv, periods=10)
|
||||
assert len(i1) == len(i2)
|
||||
assert (i1 == i2).all()
|
||||
assert i1.freq == i2.freq
|
||||
|
||||
end_intv = Period('2006-12-31', ('w', 1))
|
||||
i2 = PeriodIndex(end=end_intv, periods=10)
|
||||
assert len(i1) == len(i2)
|
||||
assert (i1 == i2).all()
|
||||
assert i1.freq == i2.freq
|
||||
|
||||
end_intv = Period('2005-05-01', 'B')
|
||||
i1 = PeriodIndex(start=start, end=end_intv)
|
||||
|
||||
# infer freq from first element
|
||||
i2 = PeriodIndex([end_intv, Period('2005-05-05', 'B')])
|
||||
assert len(i2) == 2
|
||||
assert i2[0] == end_intv
|
||||
|
||||
i2 = PeriodIndex(np.array([end_intv, Period('2005-05-05', 'B')]))
|
||||
assert len(i2) == 2
|
||||
assert i2[0] == end_intv
|
||||
|
||||
# Mixed freq should fail
|
||||
vals = [end_intv, Period('2006-12-31', 'w')]
|
||||
pytest.raises(ValueError, PeriodIndex, vals)
|
||||
vals = np.array(vals)
|
||||
pytest.raises(ValueError, PeriodIndex, vals)
|
||||
|
||||
def test_constructor_error(self):
|
||||
start = Period('02-Apr-2005', 'B')
|
||||
end_intv = Period('2006-12-31', ('w', 1))
|
||||
|
||||
msg = 'start and end must have same freq'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
PeriodIndex(start=start, end=end_intv)
|
||||
|
||||
msg = ('Of the three parameters: start, end, and periods, '
|
||||
'exactly two must be specified')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
PeriodIndex(start=start)
|
||||
|
||||
@pytest.mark.parametrize('freq', ['M', 'Q', 'A', 'D', 'B',
|
||||
'T', 'S', 'L', 'U', 'N', 'H'])
|
||||
def test_recreate_from_data(self, freq):
|
||||
org = PeriodIndex(start='2001/04/01', freq=freq, periods=1)
|
||||
idx = PeriodIndex(org.values, freq=freq)
|
||||
tm.assert_index_equal(idx, org)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
class TestSeriesPeriod(object):
|
||||
|
||||
def setup_method(self, method):
|
||||
self.series = Series(period_range('2000-01-01', periods=10, freq='D'))
|
||||
|
||||
def test_constructor_cant_cast_period(self):
|
||||
with pytest.raises(TypeError):
|
||||
Series(period_range('2000-01-01', periods=10, freq='D'),
|
||||
dtype=float)
|
||||
|
||||
def test_constructor_cast_object(self):
|
||||
s = Series(period_range('1/1/2000', periods=10), dtype=object)
|
||||
exp = Series(period_range('1/1/2000', periods=10))
|
||||
tm.assert_series_equal(s, exp)
|
||||
@@ -1,209 +0,0 @@
|
||||
from pandas import PeriodIndex
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import pandas.util.testing as tm
|
||||
import pandas as pd
|
||||
|
||||
|
||||
def test_to_native_types():
|
||||
index = PeriodIndex(['2017-01-01', '2017-01-02',
|
||||
'2017-01-03'], freq='D')
|
||||
|
||||
# First, with no arguments.
|
||||
expected = np.array(['2017-01-01', '2017-01-02',
|
||||
'2017-01-03'], dtype='=U10')
|
||||
|
||||
result = index.to_native_types()
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
# No NaN values, so na_rep has no effect
|
||||
result = index.to_native_types(na_rep='pandas')
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
# Make sure slicing works
|
||||
expected = np.array(['2017-01-01', '2017-01-03'], dtype='=U10')
|
||||
|
||||
result = index.to_native_types([0, 2])
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
# Make sure date formatting works
|
||||
expected = np.array(['01-2017-01', '01-2017-02',
|
||||
'01-2017-03'], dtype='=U10')
|
||||
|
||||
result = index.to_native_types(date_format='%m-%Y-%d')
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
# NULL object handling should work
|
||||
index = PeriodIndex(['2017-01-01', pd.NaT, '2017-01-03'], freq='D')
|
||||
expected = np.array(['2017-01-01', 'NaT', '2017-01-03'], dtype=object)
|
||||
|
||||
result = index.to_native_types()
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
expected = np.array(['2017-01-01', 'pandas',
|
||||
'2017-01-03'], dtype=object)
|
||||
|
||||
result = index.to_native_types(na_rep='pandas')
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
|
||||
class TestPeriodIndexRendering(object):
|
||||
@pytest.mark.parametrize('method', ['__repr__', '__unicode__', '__str__'])
|
||||
def test_representation(self, method):
|
||||
# GH#7601
|
||||
idx1 = PeriodIndex([], freq='D')
|
||||
idx2 = PeriodIndex(['2011-01-01'], freq='D')
|
||||
idx3 = PeriodIndex(['2011-01-01', '2011-01-02'], freq='D')
|
||||
idx4 = PeriodIndex(['2011-01-01', '2011-01-02', '2011-01-03'],
|
||||
freq='D')
|
||||
idx5 = PeriodIndex(['2011', '2012', '2013'], freq='A')
|
||||
idx6 = PeriodIndex(['2011-01-01 09:00', '2012-02-01 10:00', 'NaT'],
|
||||
freq='H')
|
||||
idx7 = pd.period_range('2013Q1', periods=1, freq="Q")
|
||||
idx8 = pd.period_range('2013Q1', periods=2, freq="Q")
|
||||
idx9 = pd.period_range('2013Q1', periods=3, freq="Q")
|
||||
idx10 = PeriodIndex(['2011-01-01', '2011-02-01'], freq='3D')
|
||||
|
||||
exp1 = """PeriodIndex([], dtype='period[D]', freq='D')"""
|
||||
|
||||
exp2 = """PeriodIndex(['2011-01-01'], dtype='period[D]', freq='D')"""
|
||||
|
||||
exp3 = ("PeriodIndex(['2011-01-01', '2011-01-02'], dtype='period[D]', "
|
||||
"freq='D')")
|
||||
|
||||
exp4 = ("PeriodIndex(['2011-01-01', '2011-01-02', '2011-01-03'], "
|
||||
"dtype='period[D]', freq='D')")
|
||||
|
||||
exp5 = ("PeriodIndex(['2011', '2012', '2013'], dtype='period[A-DEC]', "
|
||||
"freq='A-DEC')")
|
||||
|
||||
exp6 = ("PeriodIndex(['2011-01-01 09:00', '2012-02-01 10:00', 'NaT'], "
|
||||
"dtype='period[H]', freq='H')")
|
||||
|
||||
exp7 = ("PeriodIndex(['2013Q1'], dtype='period[Q-DEC]', "
|
||||
"freq='Q-DEC')")
|
||||
|
||||
exp8 = ("PeriodIndex(['2013Q1', '2013Q2'], dtype='period[Q-DEC]', "
|
||||
"freq='Q-DEC')")
|
||||
|
||||
exp9 = ("PeriodIndex(['2013Q1', '2013Q2', '2013Q3'], "
|
||||
"dtype='period[Q-DEC]', freq='Q-DEC')")
|
||||
|
||||
exp10 = ("PeriodIndex(['2011-01-01', '2011-02-01'], "
|
||||
"dtype='period[3D]', freq='3D')")
|
||||
|
||||
for idx, expected in zip([idx1, idx2, idx3, idx4, idx5,
|
||||
idx6, idx7, idx8, idx9, idx10],
|
||||
[exp1, exp2, exp3, exp4, exp5,
|
||||
exp6, exp7, exp8, exp9, exp10]):
|
||||
result = getattr(idx, method)()
|
||||
assert result == expected
|
||||
|
||||
def test_representation_to_series(self):
|
||||
# GH#10971
|
||||
idx1 = PeriodIndex([], freq='D')
|
||||
idx2 = PeriodIndex(['2011-01-01'], freq='D')
|
||||
idx3 = PeriodIndex(['2011-01-01', '2011-01-02'], freq='D')
|
||||
idx4 = PeriodIndex(['2011-01-01', '2011-01-02', '2011-01-03'],
|
||||
freq='D')
|
||||
idx5 = PeriodIndex(['2011', '2012', '2013'], freq='A')
|
||||
idx6 = PeriodIndex(['2011-01-01 09:00', '2012-02-01 10:00', 'NaT'],
|
||||
freq='H')
|
||||
|
||||
idx7 = pd.period_range('2013Q1', periods=1, freq="Q")
|
||||
idx8 = pd.period_range('2013Q1', periods=2, freq="Q")
|
||||
idx9 = pd.period_range('2013Q1', periods=3, freq="Q")
|
||||
|
||||
exp1 = """Series([], dtype: object)"""
|
||||
|
||||
exp2 = """0 2011-01-01
|
||||
dtype: object"""
|
||||
|
||||
exp3 = """0 2011-01-01
|
||||
1 2011-01-02
|
||||
dtype: object"""
|
||||
|
||||
exp4 = """0 2011-01-01
|
||||
1 2011-01-02
|
||||
2 2011-01-03
|
||||
dtype: object"""
|
||||
|
||||
exp5 = """0 2011
|
||||
1 2012
|
||||
2 2013
|
||||
dtype: object"""
|
||||
|
||||
exp6 = """0 2011-01-01 09:00
|
||||
1 2012-02-01 10:00
|
||||
2 NaT
|
||||
dtype: object"""
|
||||
|
||||
exp7 = """0 2013Q1
|
||||
dtype: object"""
|
||||
|
||||
exp8 = """0 2013Q1
|
||||
1 2013Q2
|
||||
dtype: object"""
|
||||
|
||||
exp9 = """0 2013Q1
|
||||
1 2013Q2
|
||||
2 2013Q3
|
||||
dtype: object"""
|
||||
|
||||
for idx, expected in zip([idx1, idx2, idx3, idx4, idx5,
|
||||
idx6, idx7, idx8, idx9],
|
||||
[exp1, exp2, exp3, exp4, exp5,
|
||||
exp6, exp7, exp8, exp9]):
|
||||
result = repr(pd.Series(idx))
|
||||
assert result == expected
|
||||
|
||||
def test_summary(self):
|
||||
# GH#9116
|
||||
idx1 = PeriodIndex([], freq='D')
|
||||
idx2 = PeriodIndex(['2011-01-01'], freq='D')
|
||||
idx3 = PeriodIndex(['2011-01-01', '2011-01-02'], freq='D')
|
||||
idx4 = PeriodIndex(['2011-01-01', '2011-01-02', '2011-01-03'],
|
||||
freq='D')
|
||||
idx5 = PeriodIndex(['2011', '2012', '2013'], freq='A')
|
||||
idx6 = PeriodIndex(['2011-01-01 09:00', '2012-02-01 10:00', 'NaT'],
|
||||
freq='H')
|
||||
|
||||
idx7 = pd.period_range('2013Q1', periods=1, freq="Q")
|
||||
idx8 = pd.period_range('2013Q1', periods=2, freq="Q")
|
||||
idx9 = pd.period_range('2013Q1', periods=3, freq="Q")
|
||||
|
||||
exp1 = """PeriodIndex: 0 entries
|
||||
Freq: D"""
|
||||
|
||||
exp2 = """PeriodIndex: 1 entries, 2011-01-01 to 2011-01-01
|
||||
Freq: D"""
|
||||
|
||||
exp3 = """PeriodIndex: 2 entries, 2011-01-01 to 2011-01-02
|
||||
Freq: D"""
|
||||
|
||||
exp4 = """PeriodIndex: 3 entries, 2011-01-01 to 2011-01-03
|
||||
Freq: D"""
|
||||
|
||||
exp5 = """PeriodIndex: 3 entries, 2011 to 2013
|
||||
Freq: A-DEC"""
|
||||
|
||||
exp6 = """PeriodIndex: 3 entries, 2011-01-01 09:00 to NaT
|
||||
Freq: H"""
|
||||
|
||||
exp7 = """PeriodIndex: 1 entries, 2013Q1 to 2013Q1
|
||||
Freq: Q-DEC"""
|
||||
|
||||
exp8 = """PeriodIndex: 2 entries, 2013Q1 to 2013Q2
|
||||
Freq: Q-DEC"""
|
||||
|
||||
exp9 = """PeriodIndex: 3 entries, 2013Q1 to 2013Q3
|
||||
Freq: Q-DEC"""
|
||||
|
||||
for idx, expected in zip([idx1, idx2, idx3, idx4, idx5,
|
||||
idx6, idx7, idx8, idx9],
|
||||
[exp1, exp2, exp3, exp4, exp5,
|
||||
exp6, exp7, exp8, exp9]):
|
||||
result = idx._summary()
|
||||
assert result == expected
|
||||
-635
@@ -1,635 +0,0 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from pandas.util import testing as tm
|
||||
from pandas.compat import lrange
|
||||
from pandas._libs import tslibs
|
||||
from pandas import (PeriodIndex, Series, DatetimeIndex,
|
||||
period_range, Period, notna)
|
||||
from pandas._libs.tslibs import period as libperiod
|
||||
|
||||
|
||||
class TestGetItem(object):
|
||||
|
||||
def test_getitem(self):
|
||||
idx1 = pd.period_range('2011-01-01', '2011-01-31', freq='D',
|
||||
name='idx')
|
||||
|
||||
for idx in [idx1]:
|
||||
result = idx[0]
|
||||
assert result == pd.Period('2011-01-01', freq='D')
|
||||
|
||||
result = idx[-1]
|
||||
assert result == pd.Period('2011-01-31', freq='D')
|
||||
|
||||
result = idx[0:5]
|
||||
expected = pd.period_range('2011-01-01', '2011-01-05', freq='D',
|
||||
name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
assert result.freq == 'D'
|
||||
|
||||
result = idx[0:10:2]
|
||||
expected = pd.PeriodIndex(['2011-01-01', '2011-01-03',
|
||||
'2011-01-05',
|
||||
'2011-01-07', '2011-01-09'],
|
||||
freq='D', name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
assert result.freq == 'D'
|
||||
|
||||
result = idx[-20:-5:3]
|
||||
expected = pd.PeriodIndex(['2011-01-12', '2011-01-15',
|
||||
'2011-01-18',
|
||||
'2011-01-21', '2011-01-24'],
|
||||
freq='D', name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
assert result.freq == 'D'
|
||||
|
||||
result = idx[4::-1]
|
||||
expected = PeriodIndex(['2011-01-05', '2011-01-04', '2011-01-03',
|
||||
'2011-01-02', '2011-01-01'],
|
||||
freq='D', name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
assert result.freq == 'D'
|
||||
|
||||
def test_getitem_index(self):
|
||||
idx = period_range('2007-01', periods=10, freq='M', name='x')
|
||||
|
||||
result = idx[[1, 3, 5]]
|
||||
exp = pd.PeriodIndex(['2007-02', '2007-04', '2007-06'],
|
||||
freq='M', name='x')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
result = idx[[True, True, False, False, False,
|
||||
True, True, False, False, False]]
|
||||
exp = pd.PeriodIndex(['2007-01', '2007-02', '2007-06', '2007-07'],
|
||||
freq='M', name='x')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
def test_getitem_partial(self):
|
||||
rng = period_range('2007-01', periods=50, freq='M')
|
||||
ts = Series(np.random.randn(len(rng)), rng)
|
||||
|
||||
pytest.raises(KeyError, ts.__getitem__, '2006')
|
||||
|
||||
result = ts['2008']
|
||||
assert (result.index.year == 2008).all()
|
||||
|
||||
result = ts['2008':'2009']
|
||||
assert len(result) == 24
|
||||
|
||||
result = ts['2008-1':'2009-12']
|
||||
assert len(result) == 24
|
||||
|
||||
result = ts['2008Q1':'2009Q4']
|
||||
assert len(result) == 24
|
||||
|
||||
result = ts[:'2009']
|
||||
assert len(result) == 36
|
||||
|
||||
result = ts['2009':]
|
||||
assert len(result) == 50 - 24
|
||||
|
||||
exp = result
|
||||
result = ts[24:]
|
||||
tm.assert_series_equal(exp, result)
|
||||
|
||||
ts = ts[10:].append(ts[10:])
|
||||
tm.assert_raises_regex(KeyError,
|
||||
"left slice bound for non-unique "
|
||||
"label: '2008'",
|
||||
ts.__getitem__, slice('2008', '2009'))
|
||||
|
||||
def test_getitem_datetime(self):
|
||||
rng = period_range(start='2012-01-01', periods=10, freq='W-MON')
|
||||
ts = Series(lrange(len(rng)), index=rng)
|
||||
|
||||
dt1 = datetime(2011, 10, 2)
|
||||
dt4 = datetime(2012, 4, 20)
|
||||
|
||||
rs = ts[dt1:dt4]
|
||||
tm.assert_series_equal(rs, ts)
|
||||
|
||||
def test_getitem_nat(self):
|
||||
idx = pd.PeriodIndex(['2011-01', 'NaT', '2011-02'], freq='M')
|
||||
assert idx[0] == pd.Period('2011-01', freq='M')
|
||||
assert idx[1] is pd.NaT
|
||||
|
||||
s = pd.Series([0, 1, 2], index=idx)
|
||||
assert s[pd.NaT] == 1
|
||||
|
||||
s = pd.Series(idx, index=idx)
|
||||
assert (s[pd.Period('2011-01', freq='M')] ==
|
||||
pd.Period('2011-01', freq='M'))
|
||||
assert s[pd.NaT] is pd.NaT
|
||||
|
||||
def test_getitem_list_periods(self):
|
||||
# GH 7710
|
||||
rng = period_range(start='2012-01-01', periods=10, freq='D')
|
||||
ts = Series(lrange(len(rng)), index=rng)
|
||||
exp = ts.iloc[[1]]
|
||||
tm.assert_series_equal(ts[[Period('2012-01-02', freq='D')]], exp)
|
||||
|
||||
def test_getitem_seconds(self):
|
||||
# GH 6716
|
||||
didx = DatetimeIndex(start='2013/01/01 09:00:00', freq='S',
|
||||
periods=4000)
|
||||
pidx = PeriodIndex(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)
|
||||
|
||||
def test_getitem_day(self):
|
||||
# GH 6716
|
||||
# Confirm DatetimeIndex and PeriodIndex works identically
|
||||
didx = DatetimeIndex(start='2013/01/01', freq='D', periods=400)
|
||||
pidx = PeriodIndex(start='2013/01/01', freq='D', periods=400)
|
||||
|
||||
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'], s[0:31])
|
||||
tm.assert_series_equal(s['2013/02'], s[31:59])
|
||||
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(KeyError):
|
||||
s[v]
|
||||
|
||||
|
||||
class TestWhere(object):
|
||||
@pytest.mark.parametrize('klass', [list, tuple, np.array, Series])
|
||||
def test_where(self, klass):
|
||||
i = period_range('20130101', periods=5, freq='D')
|
||||
cond = [True] * len(i)
|
||||
expected = i
|
||||
result = i.where(klass(cond))
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
cond = [False] + [True] * (len(i) - 1)
|
||||
expected = PeriodIndex([pd.NaT] + i[1:].tolist(), freq='D')
|
||||
result = i.where(klass(cond))
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
class TestTake(object):
|
||||
def test_take(self):
|
||||
# GH#10295
|
||||
idx1 = pd.period_range('2011-01-01', '2011-01-31', freq='D',
|
||||
name='idx')
|
||||
|
||||
for idx in [idx1]:
|
||||
result = idx.take([0])
|
||||
assert result == pd.Period('2011-01-01', freq='D')
|
||||
|
||||
result = idx.take([5])
|
||||
assert result == pd.Period('2011-01-06', freq='D')
|
||||
|
||||
result = idx.take([0, 1, 2])
|
||||
expected = pd.period_range('2011-01-01', '2011-01-03', freq='D',
|
||||
name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == 'D'
|
||||
assert result.freq == expected.freq
|
||||
|
||||
result = idx.take([0, 2, 4])
|
||||
expected = pd.PeriodIndex(['2011-01-01', '2011-01-03',
|
||||
'2011-01-05'], freq='D', name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
assert result.freq == 'D'
|
||||
|
||||
result = idx.take([7, 4, 1])
|
||||
expected = pd.PeriodIndex(['2011-01-08', '2011-01-05',
|
||||
'2011-01-02'],
|
||||
freq='D', name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
assert result.freq == 'D'
|
||||
|
||||
result = idx.take([3, 2, 5])
|
||||
expected = PeriodIndex(['2011-01-04', '2011-01-03', '2011-01-06'],
|
||||
freq='D', name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
assert result.freq == 'D'
|
||||
|
||||
result = idx.take([-3, 2, 5])
|
||||
expected = PeriodIndex(['2011-01-29', '2011-01-03', '2011-01-06'],
|
||||
freq='D', name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
assert result.freq == 'D'
|
||||
|
||||
def test_take_misc(self):
|
||||
index = PeriodIndex(start='1/1/10', end='12/31/12', freq='D',
|
||||
name='idx')
|
||||
expected = PeriodIndex([datetime(2010, 1, 6), datetime(2010, 1, 7),
|
||||
datetime(2010, 1, 9), datetime(2010, 1, 13)],
|
||||
freq='D', name='idx')
|
||||
|
||||
taken1 = index.take([5, 6, 8, 12])
|
||||
taken2 = index[[5, 6, 8, 12]]
|
||||
|
||||
for taken in [taken1, taken2]:
|
||||
tm.assert_index_equal(taken, expected)
|
||||
assert isinstance(taken, PeriodIndex)
|
||||
assert taken.freq == index.freq
|
||||
assert taken.name == expected.name
|
||||
|
||||
def test_take_fill_value(self):
|
||||
# GH#12631
|
||||
idx = pd.PeriodIndex(['2011-01-01', '2011-02-01', '2011-03-01'],
|
||||
name='xxx', freq='D')
|
||||
result = idx.take(np.array([1, 0, -1]))
|
||||
expected = pd.PeriodIndex(['2011-02-01', '2011-01-01', '2011-03-01'],
|
||||
name='xxx', freq='D')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# fill_value
|
||||
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
||||
expected = pd.PeriodIndex(['2011-02-01', '2011-01-01', 'NaT'],
|
||||
name='xxx', freq='D')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# allow_fill=False
|
||||
result = idx.take(np.array([1, 0, -1]), allow_fill=False,
|
||||
fill_value=True)
|
||||
expected = pd.PeriodIndex(['2011-02-01', '2011-01-01', '2011-03-01'],
|
||||
name='xxx', freq='D')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
msg = ('When allow_fill=True and fill_value is not None, '
|
||||
'all indices must be >= -1')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
idx.take(np.array([1, 0, -2]), fill_value=True)
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
idx.take(np.array([1, 0, -5]), fill_value=True)
|
||||
|
||||
with pytest.raises(IndexError):
|
||||
idx.take(np.array([1, -5]))
|
||||
|
||||
|
||||
class TestIndexing(object):
|
||||
|
||||
def test_get_loc_msg(self):
|
||||
idx = period_range('2000-1-1', freq='A', periods=10)
|
||||
bad_period = Period('2012', 'A')
|
||||
pytest.raises(KeyError, idx.get_loc, bad_period)
|
||||
|
||||
try:
|
||||
idx.get_loc(bad_period)
|
||||
except KeyError as inst:
|
||||
assert inst.args[0] == bad_period
|
||||
|
||||
def test_get_loc_nat(self):
|
||||
didx = DatetimeIndex(['2011-01-01', 'NaT', '2011-01-03'])
|
||||
pidx = PeriodIndex(['2011-01-01', 'NaT', '2011-01-03'], freq='M')
|
||||
|
||||
# check DatetimeIndex compat
|
||||
for idx in [didx, pidx]:
|
||||
assert idx.get_loc(pd.NaT) == 1
|
||||
assert idx.get_loc(None) == 1
|
||||
assert idx.get_loc(float('nan')) == 1
|
||||
assert idx.get_loc(np.nan) == 1
|
||||
|
||||
def test_get_loc(self):
|
||||
# GH 17717
|
||||
p0 = pd.Period('2017-09-01')
|
||||
p1 = pd.Period('2017-09-02')
|
||||
p2 = pd.Period('2017-09-03')
|
||||
|
||||
# get the location of p1/p2 from
|
||||
# monotonic increasing PeriodIndex with non-duplicate
|
||||
idx0 = pd.PeriodIndex([p0, p1, p2])
|
||||
expected_idx1_p1 = 1
|
||||
expected_idx1_p2 = 2
|
||||
|
||||
assert idx0.get_loc(p1) == expected_idx1_p1
|
||||
assert idx0.get_loc(str(p1)) == expected_idx1_p1
|
||||
assert idx0.get_loc(p2) == expected_idx1_p2
|
||||
assert idx0.get_loc(str(p2)) == expected_idx1_p2
|
||||
|
||||
pytest.raises(tslibs.parsing.DateParseError, idx0.get_loc, 'foo')
|
||||
pytest.raises(KeyError, idx0.get_loc, 1.1)
|
||||
pytest.raises(TypeError, idx0.get_loc, idx0)
|
||||
|
||||
# get the location of p1/p2 from
|
||||
# monotonic increasing PeriodIndex with duplicate
|
||||
idx1 = pd.PeriodIndex([p1, p1, p2])
|
||||
expected_idx1_p1 = slice(0, 2)
|
||||
expected_idx1_p2 = 2
|
||||
|
||||
assert idx1.get_loc(p1) == expected_idx1_p1
|
||||
assert idx1.get_loc(str(p1)) == expected_idx1_p1
|
||||
assert idx1.get_loc(p2) == expected_idx1_p2
|
||||
assert idx1.get_loc(str(p2)) == expected_idx1_p2
|
||||
|
||||
pytest.raises(tslibs.parsing.DateParseError, idx1.get_loc, 'foo')
|
||||
pytest.raises(KeyError, idx1.get_loc, 1.1)
|
||||
pytest.raises(TypeError, idx1.get_loc, idx1)
|
||||
|
||||
# get the location of p1/p2 from
|
||||
# non-monotonic increasing/decreasing PeriodIndex with duplicate
|
||||
idx2 = pd.PeriodIndex([p2, p1, p2])
|
||||
expected_idx2_p1 = 1
|
||||
expected_idx2_p2 = np.array([True, False, True])
|
||||
|
||||
assert idx2.get_loc(p1) == expected_idx2_p1
|
||||
assert idx2.get_loc(str(p1)) == expected_idx2_p1
|
||||
tm.assert_numpy_array_equal(idx2.get_loc(p2), expected_idx2_p2)
|
||||
tm.assert_numpy_array_equal(idx2.get_loc(str(p2)), expected_idx2_p2)
|
||||
|
||||
def test_is_monotonic_increasing(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_increasing
|
||||
assert idx_inc1.is_monotonic_increasing
|
||||
assert not idx_dec0.is_monotonic_increasing
|
||||
assert not idx_dec1.is_monotonic_increasing
|
||||
assert not idx.is_monotonic_increasing
|
||||
|
||||
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 not idx_inc0.is_monotonic_decreasing
|
||||
assert not idx_inc1.is_monotonic_decreasing
|
||||
assert idx_dec0.is_monotonic_decreasing
|
||||
assert idx_dec1.is_monotonic_decreasing
|
||||
assert not idx.is_monotonic_decreasing
|
||||
|
||||
def test_is_unique(self):
|
||||
# GH 17717
|
||||
p0 = pd.Period('2017-09-01')
|
||||
p1 = pd.Period('2017-09-02')
|
||||
p2 = pd.Period('2017-09-03')
|
||||
|
||||
idx0 = pd.PeriodIndex([p0, p1, p2])
|
||||
assert idx0.is_unique
|
||||
|
||||
idx1 = pd.PeriodIndex([p1, p1, p2])
|
||||
assert not idx1.is_unique
|
||||
|
||||
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
|
||||
|
||||
def test_get_value(self):
|
||||
# GH 17717
|
||||
p0 = pd.Period('2017-09-01')
|
||||
p1 = pd.Period('2017-09-02')
|
||||
p2 = pd.Period('2017-09-03')
|
||||
|
||||
idx0 = pd.PeriodIndex([p0, p1, p2])
|
||||
input0 = np.array([1, 2, 3])
|
||||
expected0 = 2
|
||||
|
||||
result0 = idx0.get_value(input0, p1)
|
||||
assert result0 == expected0
|
||||
|
||||
idx1 = pd.PeriodIndex([p1, p1, p2])
|
||||
input1 = np.array([1, 2, 3])
|
||||
expected1 = np.array([1, 2])
|
||||
|
||||
result1 = idx1.get_value(input1, p1)
|
||||
tm.assert_numpy_array_equal(result1, expected1)
|
||||
|
||||
idx2 = pd.PeriodIndex([p1, p2, p1])
|
||||
input2 = np.array([1, 2, 3])
|
||||
expected2 = np.array([1, 3])
|
||||
|
||||
result2 = idx2.get_value(input2, p1)
|
||||
tm.assert_numpy_array_equal(result2, expected2)
|
||||
|
||||
def test_get_indexer(self):
|
||||
# GH 17717
|
||||
p1 = pd.Period('2017-09-01')
|
||||
p2 = pd.Period('2017-09-04')
|
||||
p3 = pd.Period('2017-09-07')
|
||||
|
||||
tp0 = pd.Period('2017-08-31')
|
||||
tp1 = pd.Period('2017-09-02')
|
||||
tp2 = pd.Period('2017-09-05')
|
||||
tp3 = pd.Period('2017-09-09')
|
||||
|
||||
idx = pd.PeriodIndex([p1, p2, p3])
|
||||
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(idx),
|
||||
np.array([0, 1, 2], dtype=np.intp))
|
||||
|
||||
target = pd.PeriodIndex([tp0, tp1, tp2, tp3])
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(target, 'pad'),
|
||||
np.array([-1, 0, 1, 2], dtype=np.intp))
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(target, 'backfill'),
|
||||
np.array([0, 1, 2, -1], dtype=np.intp))
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest'),
|
||||
np.array([0, 0, 1, 2], dtype=np.intp))
|
||||
|
||||
res = idx.get_indexer(target, 'nearest',
|
||||
tolerance=pd.Timedelta('1 day'))
|
||||
tm.assert_numpy_array_equal(res,
|
||||
np.array([0, 0, 1, -1], dtype=np.intp))
|
||||
|
||||
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
|
||||
def test_get_loc2(self):
|
||||
idx = pd.period_range('2000-01-01', periods=3)
|
||||
|
||||
for method in [None, 'pad', 'backfill', 'nearest']:
|
||||
assert idx.get_loc(idx[1], method) == 1
|
||||
assert idx.get_loc(idx[1].asfreq('H', how='start'), method) == 1
|
||||
assert idx.get_loc(idx[1].to_timestamp(), method) == 1
|
||||
assert idx.get_loc(idx[1].to_timestamp()
|
||||
.to_pydatetime(), method) == 1
|
||||
assert idx.get_loc(str(idx[1]), method) == 1
|
||||
|
||||
idx = pd.period_range('2000-01-01', periods=5)[::2]
|
||||
assert idx.get_loc('2000-01-02T12', method='nearest',
|
||||
tolerance='1 day') == 1
|
||||
assert idx.get_loc('2000-01-02T12', method='nearest',
|
||||
tolerance=pd.Timedelta('1D')) == 1
|
||||
assert idx.get_loc('2000-01-02T12', method='nearest',
|
||||
tolerance=np.timedelta64(1, 'D')) == 1
|
||||
assert idx.get_loc('2000-01-02T12', method='nearest',
|
||||
tolerance=timedelta(1)) == 1
|
||||
with tm.assert_raises_regex(ValueError,
|
||||
'unit abbreviation w/o a number'):
|
||||
idx.get_loc('2000-01-10', method='nearest', tolerance='foo')
|
||||
|
||||
msg = 'Input has different freq from PeriodIndex\\(freq=D\\)'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
idx.get_loc('2000-01-10', method='nearest', tolerance='1 hour')
|
||||
with pytest.raises(KeyError):
|
||||
idx.get_loc('2000-01-10', method='nearest', tolerance='1 day')
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match='list-like tolerance size must match target index size'):
|
||||
idx.get_loc('2000-01-10', method='nearest',
|
||||
tolerance=[pd.Timedelta('1 day').to_timedelta64(),
|
||||
pd.Timedelta('1 day').to_timedelta64()])
|
||||
|
||||
# TODO: This method came from test_period; de-dup with version above
|
||||
def test_get_indexer2(self):
|
||||
idx = pd.period_range('2000-01-01', periods=3).asfreq('H', how='start')
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(idx),
|
||||
np.array([0, 1, 2], dtype=np.intp))
|
||||
|
||||
target = pd.PeriodIndex(['1999-12-31T23', '2000-01-01T12',
|
||||
'2000-01-02T01'], freq='H')
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(target, 'pad'),
|
||||
np.array([-1, 0, 1], dtype=np.intp))
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(target, 'backfill'),
|
||||
np.array([0, 1, 2], dtype=np.intp))
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest'),
|
||||
np.array([0, 1, 1], dtype=np.intp))
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest',
|
||||
tolerance='1 hour'),
|
||||
np.array([0, -1, 1], dtype=np.intp))
|
||||
|
||||
msg = 'Input has different freq from PeriodIndex\\(freq=H\\)'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
idx.get_indexer(target, 'nearest', tolerance='1 minute')
|
||||
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest',
|
||||
tolerance='1 day'),
|
||||
np.array([0, 1, 1], dtype=np.intp))
|
||||
tol_raw = [pd.Timedelta('1 hour'),
|
||||
pd.Timedelta('1 hour'),
|
||||
np.timedelta64(1, 'D'), ]
|
||||
tm.assert_numpy_array_equal(
|
||||
idx.get_indexer(target, 'nearest',
|
||||
tolerance=[np.timedelta64(x) for x in tol_raw]),
|
||||
np.array([0, -1, 1], dtype=np.intp))
|
||||
tol_bad = [pd.Timedelta('2 hour').to_timedelta64(),
|
||||
pd.Timedelta('1 hour').to_timedelta64(),
|
||||
np.timedelta64(1, 'M'), ]
|
||||
with pytest.raises(
|
||||
libperiod.IncompatibleFrequency,
|
||||
match='Input has different freq from'):
|
||||
idx.get_indexer(target, 'nearest', tolerance=tol_bad)
|
||||
|
||||
def test_indexing(self):
|
||||
# GH 4390, iat incorrectly indexing
|
||||
index = period_range('1/1/2001', periods=10)
|
||||
s = Series(np.random.randn(10), index=index)
|
||||
expected = s[index[0]]
|
||||
result = s.iat[0]
|
||||
assert expected == result
|
||||
|
||||
def test_period_index_indexer(self):
|
||||
# GH4125
|
||||
idx = pd.period_range('2002-01', '2003-12', freq='M')
|
||||
df = pd.DataFrame(pd.np.random.randn(24, 10), index=idx)
|
||||
tm.assert_frame_equal(df, df.loc[idx])
|
||||
tm.assert_frame_equal(df, df.loc[list(idx)])
|
||||
tm.assert_frame_equal(df, df.loc[list(idx)])
|
||||
tm.assert_frame_equal(df.iloc[0:5], df.loc[idx[0:5]])
|
||||
tm.assert_frame_equal(df, df.loc[list(idx)])
|
||||
@@ -1,505 +0,0 @@
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import pandas as pd
|
||||
import pandas._libs.tslib as tslib
|
||||
import pandas.util.testing as tm
|
||||
from pandas import (DatetimeIndex, PeriodIndex, Series, Period,
|
||||
_np_version_under1p10, Index)
|
||||
|
||||
from pandas.tests.test_base import Ops
|
||||
|
||||
|
||||
class TestPeriodIndexOps(Ops):
|
||||
|
||||
def setup_method(self, method):
|
||||
super(TestPeriodIndexOps, self).setup_method(method)
|
||||
mask = lambda x: (isinstance(x, DatetimeIndex) or
|
||||
isinstance(x, PeriodIndex))
|
||||
self.is_valid_objs = [o for o in self.objs if mask(o)]
|
||||
self.not_valid_objs = [o for o in self.objs if not mask(o)]
|
||||
|
||||
def test_ops_properties(self):
|
||||
f = lambda x: isinstance(x, PeriodIndex)
|
||||
self.check_ops_properties(PeriodIndex._field_ops, f)
|
||||
self.check_ops_properties(PeriodIndex._object_ops, f)
|
||||
self.check_ops_properties(PeriodIndex._bool_ops, f)
|
||||
|
||||
def test_minmax(self):
|
||||
|
||||
# monotonic
|
||||
idx1 = pd.PeriodIndex([pd.NaT, '2011-01-01', '2011-01-02',
|
||||
'2011-01-03'], freq='D')
|
||||
assert idx1.is_monotonic
|
||||
|
||||
# non-monotonic
|
||||
idx2 = pd.PeriodIndex(['2011-01-01', pd.NaT, '2011-01-03',
|
||||
'2011-01-02', pd.NaT], freq='D')
|
||||
assert not idx2.is_monotonic
|
||||
|
||||
for idx in [idx1, idx2]:
|
||||
assert idx.min() == pd.Period('2011-01-01', freq='D')
|
||||
assert idx.max() == pd.Period('2011-01-03', freq='D')
|
||||
assert idx1.argmin() == 1
|
||||
assert idx2.argmin() == 0
|
||||
assert idx1.argmax() == 3
|
||||
assert idx2.argmax() == 2
|
||||
|
||||
for op in ['min', 'max']:
|
||||
# Return NaT
|
||||
obj = PeriodIndex([], freq='M')
|
||||
result = getattr(obj, op)()
|
||||
assert result is tslib.NaT
|
||||
|
||||
obj = PeriodIndex([pd.NaT], freq='M')
|
||||
result = getattr(obj, op)()
|
||||
assert result is tslib.NaT
|
||||
|
||||
obj = PeriodIndex([pd.NaT, pd.NaT, pd.NaT], freq='M')
|
||||
result = getattr(obj, op)()
|
||||
assert result is tslib.NaT
|
||||
|
||||
def test_numpy_minmax(self):
|
||||
pr = pd.period_range(start='2016-01-15', end='2016-01-20')
|
||||
|
||||
assert np.min(pr) == Period('2016-01-15', freq='D')
|
||||
assert np.max(pr) == Period('2016-01-20', freq='D')
|
||||
|
||||
errmsg = "the 'out' parameter is not supported"
|
||||
tm.assert_raises_regex(ValueError, errmsg, np.min, pr, out=0)
|
||||
tm.assert_raises_regex(ValueError, errmsg, np.max, pr, out=0)
|
||||
|
||||
assert np.argmin(pr) == 0
|
||||
assert np.argmax(pr) == 5
|
||||
|
||||
if not _np_version_under1p10:
|
||||
errmsg = "the 'out' parameter is not supported"
|
||||
tm.assert_raises_regex(
|
||||
ValueError, errmsg, np.argmin, pr, out=0)
|
||||
tm.assert_raises_regex(
|
||||
ValueError, errmsg, np.argmax, pr, out=0)
|
||||
|
||||
def test_resolution(self):
|
||||
for freq, expected in zip(['A', 'Q', 'M', 'D', 'H',
|
||||
'T', 'S', 'L', 'U'],
|
||||
['day', 'day', 'day', 'day',
|
||||
'hour', 'minute', 'second',
|
||||
'millisecond', 'microsecond']):
|
||||
|
||||
idx = pd.period_range(start='2013-04-01', periods=30, freq=freq)
|
||||
assert idx.resolution == expected
|
||||
|
||||
def test_value_counts_unique(self):
|
||||
# GH 7735
|
||||
idx = pd.period_range('2011-01-01 09:00', freq='H', periods=10)
|
||||
# create repeated values, 'n'th element is repeated by n+1 times
|
||||
idx = PeriodIndex(np.repeat(idx.values, range(1, len(idx) + 1)),
|
||||
freq='H')
|
||||
|
||||
exp_idx = PeriodIndex(['2011-01-01 18:00', '2011-01-01 17:00',
|
||||
'2011-01-01 16:00', '2011-01-01 15:00',
|
||||
'2011-01-01 14:00', '2011-01-01 13:00',
|
||||
'2011-01-01 12:00', '2011-01-01 11:00',
|
||||
'2011-01-01 10:00',
|
||||
'2011-01-01 09:00'], freq='H')
|
||||
expected = Series(range(10, 0, -1), index=exp_idx, dtype='int64')
|
||||
|
||||
for obj in [idx, Series(idx)]:
|
||||
tm.assert_series_equal(obj.value_counts(), expected)
|
||||
|
||||
expected = pd.period_range('2011-01-01 09:00', freq='H',
|
||||
periods=10)
|
||||
tm.assert_index_equal(idx.unique(), expected)
|
||||
|
||||
idx = PeriodIndex(['2013-01-01 09:00', '2013-01-01 09:00',
|
||||
'2013-01-01 09:00', '2013-01-01 08:00',
|
||||
'2013-01-01 08:00', pd.NaT], freq='H')
|
||||
|
||||
exp_idx = PeriodIndex(['2013-01-01 09:00', '2013-01-01 08:00'],
|
||||
freq='H')
|
||||
expected = Series([3, 2], index=exp_idx)
|
||||
|
||||
for obj in [idx, Series(idx)]:
|
||||
tm.assert_series_equal(obj.value_counts(), expected)
|
||||
|
||||
exp_idx = PeriodIndex(['2013-01-01 09:00', '2013-01-01 08:00',
|
||||
pd.NaT], freq='H')
|
||||
expected = Series([3, 2, 1], index=exp_idx)
|
||||
|
||||
for obj in [idx, Series(idx)]:
|
||||
tm.assert_series_equal(obj.value_counts(dropna=False), expected)
|
||||
|
||||
tm.assert_index_equal(idx.unique(), exp_idx)
|
||||
|
||||
def test_drop_duplicates_metadata(self):
|
||||
# GH 10115
|
||||
idx = pd.period_range('2011-01-01', '2011-01-31', freq='D', name='idx')
|
||||
result = idx.drop_duplicates()
|
||||
tm.assert_index_equal(idx, result)
|
||||
assert idx.freq == result.freq
|
||||
|
||||
idx_dup = idx.append(idx) # freq will not be reset
|
||||
result = idx_dup.drop_duplicates()
|
||||
tm.assert_index_equal(idx, result)
|
||||
assert idx.freq == result.freq
|
||||
|
||||
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)))
|
||||
|
||||
def test_order_compat(self):
|
||||
def _check_freq(index, expected_index):
|
||||
if isinstance(index, PeriodIndex):
|
||||
assert index.freq == expected_index.freq
|
||||
|
||||
pidx = PeriodIndex(['2011', '2012', '2013'], name='pidx', freq='A')
|
||||
# for compatibility check
|
||||
iidx = Index([2011, 2012, 2013], name='idx')
|
||||
for idx in [pidx, iidx]:
|
||||
ordered = idx.sort_values()
|
||||
tm.assert_index_equal(ordered, idx)
|
||||
_check_freq(ordered, idx)
|
||||
|
||||
ordered = idx.sort_values(ascending=False)
|
||||
tm.assert_index_equal(ordered, idx[::-1])
|
||||
_check_freq(ordered, idx[::-1])
|
||||
|
||||
ordered, indexer = idx.sort_values(return_indexer=True)
|
||||
tm.assert_index_equal(ordered, idx)
|
||||
tm.assert_numpy_array_equal(indexer, np.array([0, 1, 2]),
|
||||
check_dtype=False)
|
||||
_check_freq(ordered, idx)
|
||||
|
||||
ordered, indexer = idx.sort_values(return_indexer=True,
|
||||
ascending=False)
|
||||
tm.assert_index_equal(ordered, idx[::-1])
|
||||
tm.assert_numpy_array_equal(indexer, np.array([2, 1, 0]),
|
||||
check_dtype=False)
|
||||
_check_freq(ordered, idx[::-1])
|
||||
|
||||
pidx = PeriodIndex(['2011', '2013', '2015', '2012',
|
||||
'2011'], name='pidx', freq='A')
|
||||
pexpected = PeriodIndex(
|
||||
['2011', '2011', '2012', '2013', '2015'], name='pidx', freq='A')
|
||||
# for compatibility check
|
||||
iidx = Index([2011, 2013, 2015, 2012, 2011], name='idx')
|
||||
iexpected = Index([2011, 2011, 2012, 2013, 2015], name='idx')
|
||||
for idx, expected in [(pidx, pexpected), (iidx, iexpected)]:
|
||||
ordered = idx.sort_values()
|
||||
tm.assert_index_equal(ordered, expected)
|
||||
_check_freq(ordered, idx)
|
||||
|
||||
ordered = idx.sort_values(ascending=False)
|
||||
tm.assert_index_equal(ordered, expected[::-1])
|
||||
_check_freq(ordered, idx)
|
||||
|
||||
ordered, indexer = idx.sort_values(return_indexer=True)
|
||||
tm.assert_index_equal(ordered, expected)
|
||||
|
||||
exp = np.array([0, 4, 3, 1, 2])
|
||||
tm.assert_numpy_array_equal(indexer, exp, check_dtype=False)
|
||||
_check_freq(ordered, idx)
|
||||
|
||||
ordered, indexer = idx.sort_values(return_indexer=True,
|
||||
ascending=False)
|
||||
tm.assert_index_equal(ordered, expected[::-1])
|
||||
|
||||
exp = np.array([2, 1, 3, 4, 0])
|
||||
tm.assert_numpy_array_equal(indexer, exp, check_dtype=False)
|
||||
_check_freq(ordered, idx)
|
||||
|
||||
pidx = PeriodIndex(['2011', '2013', 'NaT', '2011'], name='pidx',
|
||||
freq='D')
|
||||
|
||||
result = pidx.sort_values()
|
||||
expected = PeriodIndex(['NaT', '2011', '2011', '2013'],
|
||||
name='pidx', freq='D')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == 'D'
|
||||
|
||||
result = pidx.sort_values(ascending=False)
|
||||
expected = PeriodIndex(
|
||||
['2013', '2011', '2011', 'NaT'], name='pidx', freq='D')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == 'D'
|
||||
|
||||
def test_order(self):
|
||||
for freq in ['D', '2D', '4D']:
|
||||
idx = PeriodIndex(['2011-01-01', '2011-01-02', '2011-01-03'],
|
||||
freq=freq, name='idx')
|
||||
|
||||
ordered = idx.sort_values()
|
||||
tm.assert_index_equal(ordered, idx)
|
||||
assert ordered.freq == idx.freq
|
||||
|
||||
ordered = idx.sort_values(ascending=False)
|
||||
expected = idx[::-1]
|
||||
tm.assert_index_equal(ordered, expected)
|
||||
assert ordered.freq == expected.freq
|
||||
assert ordered.freq == freq
|
||||
|
||||
ordered, indexer = idx.sort_values(return_indexer=True)
|
||||
tm.assert_index_equal(ordered, idx)
|
||||
tm.assert_numpy_array_equal(indexer, np.array([0, 1, 2]),
|
||||
check_dtype=False)
|
||||
assert ordered.freq == idx.freq
|
||||
assert ordered.freq == freq
|
||||
|
||||
ordered, indexer = idx.sort_values(return_indexer=True,
|
||||
ascending=False)
|
||||
expected = idx[::-1]
|
||||
tm.assert_index_equal(ordered, expected)
|
||||
tm.assert_numpy_array_equal(indexer, np.array([2, 1, 0]),
|
||||
check_dtype=False)
|
||||
assert ordered.freq == expected.freq
|
||||
assert ordered.freq == freq
|
||||
|
||||
idx1 = PeriodIndex(['2011-01-01', '2011-01-03', '2011-01-05',
|
||||
'2011-01-02', '2011-01-01'], freq='D', name='idx1')
|
||||
exp1 = PeriodIndex(['2011-01-01', '2011-01-01', '2011-01-02',
|
||||
'2011-01-03', '2011-01-05'], freq='D', name='idx1')
|
||||
|
||||
idx2 = PeriodIndex(['2011-01-01', '2011-01-03', '2011-01-05',
|
||||
'2011-01-02', '2011-01-01'],
|
||||
freq='D', name='idx2')
|
||||
exp2 = PeriodIndex(['2011-01-01', '2011-01-01', '2011-01-02',
|
||||
'2011-01-03', '2011-01-05'],
|
||||
freq='D', name='idx2')
|
||||
|
||||
idx3 = PeriodIndex([pd.NaT, '2011-01-03', '2011-01-05',
|
||||
'2011-01-02', pd.NaT], freq='D', name='idx3')
|
||||
exp3 = PeriodIndex([pd.NaT, pd.NaT, '2011-01-02', '2011-01-03',
|
||||
'2011-01-05'], freq='D', name='idx3')
|
||||
|
||||
for idx, expected in [(idx1, exp1), (idx2, exp2), (idx3, exp3)]:
|
||||
ordered = idx.sort_values()
|
||||
tm.assert_index_equal(ordered, expected)
|
||||
assert ordered.freq == 'D'
|
||||
|
||||
ordered = idx.sort_values(ascending=False)
|
||||
tm.assert_index_equal(ordered, expected[::-1])
|
||||
assert ordered.freq == 'D'
|
||||
|
||||
ordered, indexer = idx.sort_values(return_indexer=True)
|
||||
tm.assert_index_equal(ordered, expected)
|
||||
|
||||
exp = np.array([0, 4, 3, 1, 2])
|
||||
tm.assert_numpy_array_equal(indexer, exp, check_dtype=False)
|
||||
assert ordered.freq == 'D'
|
||||
|
||||
ordered, indexer = idx.sort_values(return_indexer=True,
|
||||
ascending=False)
|
||||
tm.assert_index_equal(ordered, expected[::-1])
|
||||
|
||||
exp = np.array([2, 1, 3, 4, 0])
|
||||
tm.assert_numpy_array_equal(indexer, exp, check_dtype=False)
|
||||
assert ordered.freq == 'D'
|
||||
|
||||
def test_nat_new(self):
|
||||
|
||||
idx = pd.period_range('2011-01', freq='M', periods=5, name='x')
|
||||
result = idx._nat_new()
|
||||
exp = pd.PeriodIndex([pd.NaT] * 5, freq='M', name='x')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
result = idx._nat_new(box=False)
|
||||
exp = np.array([tslib.iNaT] * 5, dtype=np.int64)
|
||||
tm.assert_numpy_array_equal(result, exp)
|
||||
|
||||
def test_shift(self):
|
||||
# This is tested in test_arithmetic
|
||||
pass
|
||||
|
||||
def test_repeat(self):
|
||||
index = pd.period_range('2001-01-01', periods=2, freq='D')
|
||||
exp = pd.PeriodIndex(['2001-01-01', '2001-01-01',
|
||||
'2001-01-02', '2001-01-02'], freq='D')
|
||||
for res in [index.repeat(2), np.repeat(index, 2)]:
|
||||
tm.assert_index_equal(res, exp)
|
||||
|
||||
index = pd.period_range('2001-01-01', periods=2, freq='2D')
|
||||
exp = pd.PeriodIndex(['2001-01-01', '2001-01-01',
|
||||
'2001-01-03', '2001-01-03'], freq='2D')
|
||||
for res in [index.repeat(2), np.repeat(index, 2)]:
|
||||
tm.assert_index_equal(res, exp)
|
||||
|
||||
index = pd.PeriodIndex(['2001-01', 'NaT', '2003-01'], freq='M')
|
||||
exp = pd.PeriodIndex(['2001-01', '2001-01', '2001-01',
|
||||
'NaT', 'NaT', 'NaT',
|
||||
'2003-01', '2003-01', '2003-01'], freq='M')
|
||||
for res in [index.repeat(3), np.repeat(index, 3)]:
|
||||
tm.assert_index_equal(res, exp)
|
||||
|
||||
def test_nat(self):
|
||||
assert pd.PeriodIndex._na_value is pd.NaT
|
||||
assert pd.PeriodIndex([], freq='M')._na_value is pd.NaT
|
||||
|
||||
idx = pd.PeriodIndex(['2011-01-01', '2011-01-02'], freq='D')
|
||||
assert idx._can_hold_na
|
||||
|
||||
tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
|
||||
assert not idx.hasnans
|
||||
tm.assert_numpy_array_equal(idx._nan_idxs,
|
||||
np.array([], dtype=np.intp))
|
||||
|
||||
idx = pd.PeriodIndex(['2011-01-01', 'NaT'], freq='D')
|
||||
assert idx._can_hold_na
|
||||
|
||||
tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
|
||||
assert idx.hasnans
|
||||
tm.assert_numpy_array_equal(idx._nan_idxs,
|
||||
np.array([1], dtype=np.intp))
|
||||
|
||||
@pytest.mark.parametrize('freq', ['D', 'M'])
|
||||
def test_equals(self, freq):
|
||||
# GH#13107
|
||||
idx = pd.PeriodIndex(['2011-01-01', '2011-01-02', 'NaT'],
|
||||
freq=freq)
|
||||
assert idx.equals(idx)
|
||||
assert idx.equals(idx.copy())
|
||||
assert idx.equals(idx.astype(object))
|
||||
assert idx.astype(object).equals(idx)
|
||||
assert idx.astype(object).equals(idx.astype(object))
|
||||
assert not idx.equals(list(idx))
|
||||
assert not idx.equals(pd.Series(idx))
|
||||
|
||||
idx2 = pd.PeriodIndex(['2011-01-01', '2011-01-02', 'NaT'],
|
||||
freq='H')
|
||||
assert not idx.equals(idx2)
|
||||
assert not idx.equals(idx2.copy())
|
||||
assert not idx.equals(idx2.astype(object))
|
||||
assert not idx.astype(object).equals(idx2)
|
||||
assert not idx.equals(list(idx2))
|
||||
assert not idx.equals(pd.Series(idx2))
|
||||
|
||||
# same internal, different tz
|
||||
idx3 = pd.PeriodIndex._simple_new(idx.asi8, freq='H')
|
||||
tm.assert_numpy_array_equal(idx.asi8, idx3.asi8)
|
||||
assert not idx.equals(idx3)
|
||||
assert not idx.equals(idx3.copy())
|
||||
assert not idx.equals(idx3.astype(object))
|
||||
assert not idx.astype(object).equals(idx3)
|
||||
assert not idx.equals(list(idx3))
|
||||
assert not idx.equals(pd.Series(idx3))
|
||||
|
||||
def test_freq_setter_deprecated(self):
|
||||
# GH 20678
|
||||
idx = pd.period_range('2018Q1', periods=4, freq='Q')
|
||||
|
||||
# no warning for getter
|
||||
with tm.assert_produces_warning(None):
|
||||
idx.freq
|
||||
|
||||
# warning for setter
|
||||
with tm.assert_produces_warning(FutureWarning):
|
||||
idx.freq = pd.offsets.Day()
|
||||
|
||||
|
||||
class TestPeriodIndexSeriesMethods(object):
|
||||
""" Test PeriodIndex and Period Series Ops consistency """
|
||||
|
||||
def _check(self, values, func, expected):
|
||||
idx = pd.PeriodIndex(values)
|
||||
result = func(idx)
|
||||
if isinstance(expected, pd.Index):
|
||||
tm.assert_index_equal(result, expected)
|
||||
else:
|
||||
# comp op results in bool
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
s = pd.Series(values)
|
||||
result = func(s)
|
||||
|
||||
exp = pd.Series(expected, name=values.name)
|
||||
tm.assert_series_equal(result, exp)
|
||||
|
||||
def test_pi_comp_period(self):
|
||||
idx = PeriodIndex(['2011-01', '2011-02', '2011-03',
|
||||
'2011-04'], freq='M', name='idx')
|
||||
|
||||
f = lambda x: x == pd.Period('2011-03', freq='M')
|
||||
exp = np.array([False, False, True, False], dtype=np.bool)
|
||||
self._check(idx, f, exp)
|
||||
f = lambda x: pd.Period('2011-03', freq='M') == x
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: x != pd.Period('2011-03', freq='M')
|
||||
exp = np.array([True, True, False, True], dtype=np.bool)
|
||||
self._check(idx, f, exp)
|
||||
f = lambda x: pd.Period('2011-03', freq='M') != x
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: pd.Period('2011-03', freq='M') >= x
|
||||
exp = np.array([True, True, True, False], dtype=np.bool)
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: x > pd.Period('2011-03', freq='M')
|
||||
exp = np.array([False, False, False, True], dtype=np.bool)
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: pd.Period('2011-03', freq='M') >= x
|
||||
exp = np.array([True, True, True, False], dtype=np.bool)
|
||||
self._check(idx, f, exp)
|
||||
|
||||
def test_pi_comp_period_nat(self):
|
||||
idx = PeriodIndex(['2011-01', 'NaT', '2011-03',
|
||||
'2011-04'], freq='M', name='idx')
|
||||
|
||||
f = lambda x: x == pd.Period('2011-03', freq='M')
|
||||
exp = np.array([False, False, True, False], dtype=np.bool)
|
||||
self._check(idx, f, exp)
|
||||
f = lambda x: pd.Period('2011-03', freq='M') == x
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: x == tslib.NaT
|
||||
exp = np.array([False, False, False, False], dtype=np.bool)
|
||||
self._check(idx, f, exp)
|
||||
f = lambda x: tslib.NaT == x
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: x != pd.Period('2011-03', freq='M')
|
||||
exp = np.array([True, True, False, True], dtype=np.bool)
|
||||
self._check(idx, f, exp)
|
||||
f = lambda x: pd.Period('2011-03', freq='M') != x
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: x != tslib.NaT
|
||||
exp = np.array([True, True, True, True], dtype=np.bool)
|
||||
self._check(idx, f, exp)
|
||||
f = lambda x: tslib.NaT != x
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: pd.Period('2011-03', freq='M') >= x
|
||||
exp = np.array([True, False, True, False], dtype=np.bool)
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: x < pd.Period('2011-03', freq='M')
|
||||
exp = np.array([True, False, False, False], dtype=np.bool)
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: x > tslib.NaT
|
||||
exp = np.array([False, False, False, False], dtype=np.bool)
|
||||
self._check(idx, f, exp)
|
||||
|
||||
f = lambda x: tslib.NaT >= x
|
||||
exp = np.array([False, False, False, False], dtype=np.bool)
|
||||
self._check(idx, f, exp)
|
||||
-141
@@ -1,141 +0,0 @@
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
|
||||
import pandas as pd
|
||||
from pandas.util import testing as tm
|
||||
from pandas import (Series, period_range, DatetimeIndex, PeriodIndex,
|
||||
DataFrame, _np_version_under1p12, Period)
|
||||
|
||||
|
||||
class TestPeriodIndex(object):
|
||||
|
||||
def setup_method(self, method):
|
||||
pass
|
||||
|
||||
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])
|
||||
|
||||
def test_slice_with_zero_step_raises(self):
|
||||
ts = Series(np.arange(20),
|
||||
period_range('2014-01', periods=20, freq='M'))
|
||||
tm.assert_raises_regex(ValueError, 'slice step cannot be zero',
|
||||
lambda: ts[::0])
|
||||
tm.assert_raises_regex(ValueError, 'slice step cannot be zero',
|
||||
lambda: ts.loc[::0])
|
||||
tm.assert_raises_regex(ValueError, 'slice step cannot be zero',
|
||||
lambda: ts.loc[::0])
|
||||
|
||||
def test_slice_keep_name(self):
|
||||
idx = period_range('20010101', periods=10, freq='D', name='bob')
|
||||
assert idx.name == idx[1:].name
|
||||
|
||||
def test_pindex_slice_index(self):
|
||||
pi = PeriodIndex(start='1/1/10', end='12/31/12', freq='M')
|
||||
s = Series(np.random.rand(len(pi)), index=pi)
|
||||
res = s['2010']
|
||||
exp = s[0:12]
|
||||
tm.assert_series_equal(res, exp)
|
||||
res = s['2011']
|
||||
exp = s[12:24]
|
||||
tm.assert_series_equal(res, exp)
|
||||
|
||||
def test_range_slice_day(self):
|
||||
# GH 6716
|
||||
didx = DatetimeIndex(start='2013/01/01', freq='D', periods=400)
|
||||
pidx = PeriodIndex(start='2013/01/01', freq='D', periods=400)
|
||||
|
||||
# changed to TypeError in 1.12
|
||||
# https://github.com/numpy/numpy/pull/6271
|
||||
exc = IndexError if _np_version_under1p12 else TypeError
|
||||
|
||||
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(exc):
|
||||
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(exc):
|
||||
idx[v:]
|
||||
|
||||
def test_range_slice_seconds(self):
|
||||
# GH 6716
|
||||
didx = DatetimeIndex(start='2013/01/01 09:00:00', freq='S',
|
||||
periods=4000)
|
||||
pidx = PeriodIndex(start='2013/01/01 09:00:00', freq='S', periods=4000)
|
||||
|
||||
# changed to TypeError in 1.12
|
||||
# https://github.com/numpy/numpy/pull/6271
|
||||
exc = IndexError if _np_version_under1p12 else TypeError
|
||||
|
||||
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(exc):
|
||||
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)
|
||||
|
||||
def test_range_slice_outofbounds(self):
|
||||
# GH 5407
|
||||
didx = DatetimeIndex(start='2013/10/01', freq='D', periods=10)
|
||||
pidx = PeriodIndex(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)
|
||||
@@ -1,546 +0,0 @@
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util._test_decorators as td
|
||||
from pandas.util import testing as tm
|
||||
from pandas import (PeriodIndex, period_range, DatetimeIndex, NaT,
|
||||
Index, Period, Series, DataFrame, date_range,
|
||||
offsets)
|
||||
|
||||
from ..datetimelike import DatetimeLike
|
||||
|
||||
|
||||
class TestPeriodIndex(DatetimeLike):
|
||||
_holder = PeriodIndex
|
||||
|
||||
def setup_method(self, method):
|
||||
self.indices = dict(index=tm.makePeriodIndex(10),
|
||||
index_dec=period_range('20130101', periods=10,
|
||||
freq='D')[::-1])
|
||||
self.setup_indices()
|
||||
|
||||
def create_index(self):
|
||||
return period_range('20130101', periods=5, freq='D')
|
||||
|
||||
def test_pickle_compat_construction(self):
|
||||
pass
|
||||
|
||||
@pytest.mark.parametrize('freq', ['D', 'M', 'A'])
|
||||
def test_pickle_round_trip(self, freq):
|
||||
idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq=freq)
|
||||
result = tm.round_trip_pickle(idx)
|
||||
tm.assert_index_equal(result, idx)
|
||||
|
||||
def test_where(self):
|
||||
# This is handled in test_indexing
|
||||
pass
|
||||
|
||||
def test_repeat(self):
|
||||
# GH10183
|
||||
idx = pd.period_range('2000-01-01', periods=3, freq='D')
|
||||
res = idx.repeat(3)
|
||||
exp = PeriodIndex(idx.values.repeat(3), freq='D')
|
||||
tm.assert_index_equal(res, exp)
|
||||
assert res.freqstr == 'D'
|
||||
|
||||
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)
|
||||
|
||||
def test_no_millisecond_field(self):
|
||||
with pytest.raises(AttributeError):
|
||||
DatetimeIndex.millisecond
|
||||
|
||||
with pytest.raises(AttributeError):
|
||||
DatetimeIndex([]).millisecond
|
||||
|
||||
def test_difference_freq(self):
|
||||
# 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)
|
||||
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)
|
||||
expected = PeriodIndex(["20160920", "20160921"], freq='D')
|
||||
tm.assert_index_equal(idx_diff, expected)
|
||||
tm.assert_attr_equal('freq', idx_diff, expected)
|
||||
|
||||
def test_hash_error(self):
|
||||
index = period_range('20010101', periods=10)
|
||||
with tm.assert_raises_regex(TypeError, "unhashable type: %r" %
|
||||
type(index).__name__):
|
||||
hash(index)
|
||||
|
||||
def test_make_time_series(self):
|
||||
index = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009')
|
||||
series = Series(1, index=index)
|
||||
assert isinstance(series, Series)
|
||||
|
||||
def test_shallow_copy_empty(self):
|
||||
|
||||
# GH13067
|
||||
idx = PeriodIndex([], freq='M')
|
||||
result = idx._shallow_copy()
|
||||
expected = idx
|
||||
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_dtype_str(self):
|
||||
pi = pd.PeriodIndex([], freq='M')
|
||||
assert pi.dtype_str == 'period[M]'
|
||||
assert pi.dtype_str == str(pi.dtype)
|
||||
|
||||
pi = pd.PeriodIndex([], freq='3M')
|
||||
assert pi.dtype_str == 'period[3M]'
|
||||
assert pi.dtype_str == str(pi.dtype)
|
||||
|
||||
def test_view_asi8(self):
|
||||
idx = pd.PeriodIndex([], freq='M')
|
||||
|
||||
exp = np.array([], dtype=np.int64)
|
||||
tm.assert_numpy_array_equal(idx.view('i8'), exp)
|
||||
tm.assert_numpy_array_equal(idx.asi8, exp)
|
||||
|
||||
idx = pd.PeriodIndex(['2011-01', pd.NaT], freq='M')
|
||||
|
||||
exp = np.array([492, -9223372036854775808], dtype=np.int64)
|
||||
tm.assert_numpy_array_equal(idx.view('i8'), exp)
|
||||
tm.assert_numpy_array_equal(idx.asi8, exp)
|
||||
|
||||
exp = np.array([14975, -9223372036854775808], dtype=np.int64)
|
||||
idx = pd.PeriodIndex(['2011-01-01', pd.NaT], freq='D')
|
||||
tm.assert_numpy_array_equal(idx.view('i8'), exp)
|
||||
tm.assert_numpy_array_equal(idx.asi8, exp)
|
||||
|
||||
def test_values(self):
|
||||
idx = pd.PeriodIndex([], freq='M')
|
||||
|
||||
exp = np.array([], dtype=np.object)
|
||||
tm.assert_numpy_array_equal(idx.values, exp)
|
||||
tm.assert_numpy_array_equal(idx.get_values(), exp)
|
||||
exp = np.array([], dtype=np.int64)
|
||||
tm.assert_numpy_array_equal(idx._ndarray_values, 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.values, exp)
|
||||
tm.assert_numpy_array_equal(idx.get_values(), exp)
|
||||
exp = np.array([492, -9223372036854775808], dtype=np.int64)
|
||||
tm.assert_numpy_array_equal(idx._ndarray_values, exp)
|
||||
|
||||
idx = pd.PeriodIndex(['2011-01-01', pd.NaT], freq='D')
|
||||
|
||||
exp = np.array([pd.Period('2011-01-01', freq='D'), pd.NaT],
|
||||
dtype=object)
|
||||
tm.assert_numpy_array_equal(idx.values, exp)
|
||||
tm.assert_numpy_array_equal(idx.get_values(), exp)
|
||||
exp = np.array([14975, -9223372036854775808], dtype=np.int64)
|
||||
tm.assert_numpy_array_equal(idx._ndarray_values, exp)
|
||||
|
||||
def test_period_index_length(self):
|
||||
pi = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009')
|
||||
assert len(pi) == 9
|
||||
|
||||
pi = PeriodIndex(freq='Q', start='1/1/2001', end='12/1/2009')
|
||||
assert len(pi) == 4 * 9
|
||||
|
||||
pi = PeriodIndex(freq='M', start='1/1/2001', end='12/1/2009')
|
||||
assert len(pi) == 12 * 9
|
||||
|
||||
start = Period('02-Apr-2005', 'B')
|
||||
i1 = PeriodIndex(start=start, periods=20)
|
||||
assert len(i1) == 20
|
||||
assert i1.freq == start.freq
|
||||
assert i1[0] == start
|
||||
|
||||
end_intv = Period('2006-12-31', 'W')
|
||||
i1 = PeriodIndex(end=end_intv, periods=10)
|
||||
assert len(i1) == 10
|
||||
assert i1.freq == end_intv.freq
|
||||
assert i1[-1] == end_intv
|
||||
|
||||
end_intv = Period('2006-12-31', '1w')
|
||||
i2 = PeriodIndex(end=end_intv, periods=10)
|
||||
assert len(i1) == len(i2)
|
||||
assert (i1 == i2).all()
|
||||
assert i1.freq == i2.freq
|
||||
|
||||
end_intv = Period('2006-12-31', ('w', 1))
|
||||
i2 = PeriodIndex(end=end_intv, periods=10)
|
||||
assert len(i1) == len(i2)
|
||||
assert (i1 == i2).all()
|
||||
assert i1.freq == i2.freq
|
||||
|
||||
try:
|
||||
PeriodIndex(start=start, end=end_intv)
|
||||
raise AssertionError('Cannot allow mixed freq for start and end')
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
end_intv = Period('2005-05-01', 'B')
|
||||
i1 = PeriodIndex(start=start, end=end_intv)
|
||||
|
||||
try:
|
||||
PeriodIndex(start=start)
|
||||
raise AssertionError(
|
||||
'Must specify periods if missing start or end')
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# infer freq from first element
|
||||
i2 = PeriodIndex([end_intv, Period('2005-05-05', 'B')])
|
||||
assert len(i2) == 2
|
||||
assert i2[0] == end_intv
|
||||
|
||||
i2 = PeriodIndex(np.array([end_intv, Period('2005-05-05', 'B')]))
|
||||
assert len(i2) == 2
|
||||
assert i2[0] == end_intv
|
||||
|
||||
# Mixed freq should fail
|
||||
vals = [end_intv, Period('2006-12-31', 'w')]
|
||||
pytest.raises(ValueError, PeriodIndex, vals)
|
||||
vals = np.array(vals)
|
||||
pytest.raises(ValueError, PeriodIndex, vals)
|
||||
|
||||
def test_fields(self):
|
||||
# year, month, day, hour, minute
|
||||
# second, weekofyear, week, dayofweek, weekday, dayofyear, quarter
|
||||
# qyear
|
||||
pi = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2005')
|
||||
self._check_all_fields(pi)
|
||||
|
||||
pi = PeriodIndex(freq='Q', start='1/1/2001', end='12/1/2002')
|
||||
self._check_all_fields(pi)
|
||||
|
||||
pi = PeriodIndex(freq='M', start='1/1/2001', end='1/1/2002')
|
||||
self._check_all_fields(pi)
|
||||
|
||||
pi = PeriodIndex(freq='D', start='12/1/2001', end='6/1/2001')
|
||||
self._check_all_fields(pi)
|
||||
|
||||
pi = PeriodIndex(freq='B', start='12/1/2001', end='6/1/2001')
|
||||
self._check_all_fields(pi)
|
||||
|
||||
pi = PeriodIndex(freq='H', start='12/31/2001', end='1/1/2002 23:00')
|
||||
self._check_all_fields(pi)
|
||||
|
||||
pi = PeriodIndex(freq='Min', start='12/31/2001', end='1/1/2002 00:20')
|
||||
self._check_all_fields(pi)
|
||||
|
||||
pi = PeriodIndex(freq='S', start='12/31/2001 00:00:00',
|
||||
end='12/31/2001 00:05:00')
|
||||
self._check_all_fields(pi)
|
||||
|
||||
end_intv = Period('2006-12-31', 'W')
|
||||
i1 = PeriodIndex(end=end_intv, periods=10)
|
||||
self._check_all_fields(i1)
|
||||
|
||||
def _check_all_fields(self, periodindex):
|
||||
fields = ['year', 'month', 'day', 'hour', 'minute', 'second',
|
||||
'weekofyear', 'week', 'dayofweek', 'dayofyear',
|
||||
'quarter', 'qyear', 'days_in_month']
|
||||
|
||||
periods = list(periodindex)
|
||||
s = pd.Series(periodindex)
|
||||
|
||||
for field in fields:
|
||||
field_idx = getattr(periodindex, field)
|
||||
assert len(periodindex) == len(field_idx)
|
||||
for x, val in zip(periods, field_idx):
|
||||
assert getattr(x, field) == val
|
||||
|
||||
if len(s) == 0:
|
||||
continue
|
||||
|
||||
field_s = getattr(s.dt, field)
|
||||
assert len(periodindex) == len(field_s)
|
||||
for x, val in zip(periods, field_s):
|
||||
assert getattr(x, field) == val
|
||||
|
||||
def test_period_set_index_reindex(self):
|
||||
# GH 6631
|
||||
df = DataFrame(np.random.random(6))
|
||||
idx1 = period_range('2011/01/01', periods=6, freq='M')
|
||||
idx2 = period_range('2013', periods=6, freq='A')
|
||||
|
||||
df = df.set_index(idx1)
|
||||
tm.assert_index_equal(df.index, idx1)
|
||||
df = df.set_index(idx2)
|
||||
tm.assert_index_equal(df.index, idx2)
|
||||
|
||||
def test_factorize(self):
|
||||
idx1 = PeriodIndex(['2014-01', '2014-01', '2014-02', '2014-02',
|
||||
'2014-03', '2014-03'], freq='M')
|
||||
|
||||
exp_arr = np.array([0, 0, 1, 1, 2, 2], dtype=np.intp)
|
||||
exp_idx = PeriodIndex(['2014-01', '2014-02', '2014-03'], freq='M')
|
||||
|
||||
arr, idx = idx1.factorize()
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, exp_idx)
|
||||
|
||||
arr, idx = idx1.factorize(sort=True)
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, exp_idx)
|
||||
|
||||
idx2 = pd.PeriodIndex(['2014-03', '2014-03', '2014-02', '2014-01',
|
||||
'2014-03', '2014-01'], freq='M')
|
||||
|
||||
exp_arr = np.array([2, 2, 1, 0, 2, 0], dtype=np.intp)
|
||||
arr, idx = idx2.factorize(sort=True)
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, exp_idx)
|
||||
|
||||
exp_arr = np.array([0, 0, 1, 2, 0, 2], dtype=np.intp)
|
||||
exp_idx = PeriodIndex(['2014-03', '2014-02', '2014-01'], freq='M')
|
||||
arr, idx = idx2.factorize()
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, exp_idx)
|
||||
|
||||
def test_is_(self):
|
||||
create_index = lambda: PeriodIndex(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)
|
||||
|
||||
def test_contains(self):
|
||||
rng = period_range('2007-01', freq='M', periods=10)
|
||||
|
||||
assert Period('2007-01', freq='M') in rng
|
||||
assert not Period('2007-01', freq='D') in rng
|
||||
assert not Period('2007-01', freq='2M') in rng
|
||||
|
||||
def test_contains_nat(self):
|
||||
# see gh-13582
|
||||
idx = period_range('2007-01', freq='M', periods=10)
|
||||
assert pd.NaT not in idx
|
||||
assert None not in idx
|
||||
assert float('nan') not in idx
|
||||
assert np.nan not in idx
|
||||
|
||||
idx = pd.PeriodIndex(['2011-01', 'NaT', '2011-02'], freq='M')
|
||||
assert pd.NaT in idx
|
||||
assert None in idx
|
||||
assert float('nan') in idx
|
||||
assert np.nan in idx
|
||||
|
||||
def test_periods_number_check(self):
|
||||
with pytest.raises(ValueError):
|
||||
period_range('2011-1-1', '2012-1-1', 'B')
|
||||
|
||||
def test_index_duplicate_periods(self):
|
||||
# monotonic
|
||||
idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq='A-JUN')
|
||||
ts = Series(np.random.randn(len(idx)), index=idx)
|
||||
|
||||
result = ts[2007]
|
||||
expected = ts[1:3]
|
||||
tm.assert_series_equal(result, expected)
|
||||
result[:] = 1
|
||||
assert (ts[1:3] == 1).all()
|
||||
|
||||
# not monotonic
|
||||
idx = PeriodIndex([2000, 2007, 2007, 2009, 2007], freq='A-JUN')
|
||||
ts = Series(np.random.randn(len(idx)), index=idx)
|
||||
|
||||
result = ts[2007]
|
||||
expected = ts[idx == 2007]
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
def test_index_unique(self):
|
||||
idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq='A-JUN')
|
||||
expected = PeriodIndex([2000, 2007, 2009], freq='A-JUN')
|
||||
tm.assert_index_equal(idx.unique(), expected)
|
||||
assert idx.nunique() == 3
|
||||
|
||||
idx = PeriodIndex([2000, 2007, 2007, 2009, 2007], freq='A-JUN',
|
||||
tz='US/Eastern')
|
||||
expected = PeriodIndex([2000, 2007, 2009], freq='A-JUN',
|
||||
tz='US/Eastern')
|
||||
tm.assert_index_equal(idx.unique(), expected)
|
||||
assert idx.nunique() == 3
|
||||
|
||||
def test_shift(self):
|
||||
# This is tested in test_arithmetic
|
||||
pass
|
||||
|
||||
@td.skip_if_32bit
|
||||
def test_ndarray_compat_properties(self):
|
||||
super(TestPeriodIndex, self).test_ndarray_compat_properties()
|
||||
|
||||
def test_negative_ordinals(self):
|
||||
Period(ordinal=-1000, freq='A')
|
||||
Period(ordinal=0, freq='A')
|
||||
|
||||
idx1 = PeriodIndex(ordinal=[-1, 0, 1], freq='A')
|
||||
idx2 = PeriodIndex(ordinal=np.array([-1, 0, 1]), freq='A')
|
||||
tm.assert_index_equal(idx1, idx2)
|
||||
|
||||
def test_pindex_fieldaccessor_nat(self):
|
||||
idx = PeriodIndex(['2011-01', '2011-02', 'NaT',
|
||||
'2012-03', '2012-04'], freq='D', name='name')
|
||||
|
||||
exp = Index([2011, 2011, -1, 2012, 2012], dtype=np.int64, name='name')
|
||||
tm.assert_index_equal(idx.year, exp)
|
||||
exp = Index([1, 2, -1, 3, 4], dtype=np.int64, name='name')
|
||||
tm.assert_index_equal(idx.month, exp)
|
||||
|
||||
def test_pindex_qaccess(self):
|
||||
pi = PeriodIndex(['2Q05', '3Q05', '4Q05', '1Q06', '2Q06'], freq='Q')
|
||||
s = Series(np.random.rand(len(pi)), index=pi).cumsum()
|
||||
# Todo: fix these accessors!
|
||||
assert s['05Q4'] == s[2]
|
||||
|
||||
def test_numpy_repeat(self):
|
||||
index = period_range('20010101', periods=2)
|
||||
expected = PeriodIndex([Period('2001-01-01'), Period('2001-01-01'),
|
||||
Period('2001-01-02'), Period('2001-01-02')])
|
||||
|
||||
tm.assert_index_equal(np.repeat(index, 2), expected)
|
||||
|
||||
msg = "the 'axis' parameter is not supported"
|
||||
tm.assert_raises_regex(
|
||||
ValueError, msg, np.repeat, index, 2, axis=1)
|
||||
|
||||
def test_pindex_multiples(self):
|
||||
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'
|
||||
|
||||
def test_iteration(self):
|
||||
index = PeriodIndex(start='1/1/10', periods=4, freq='B')
|
||||
|
||||
result = list(index)
|
||||
assert isinstance(result[0], Period)
|
||||
assert result[0].freq == index.freq
|
||||
|
||||
def test_is_full(self):
|
||||
index = PeriodIndex([2005, 2007, 2009], freq='A')
|
||||
assert not index.is_full
|
||||
|
||||
index = PeriodIndex([2005, 2006, 2007], freq='A')
|
||||
assert index.is_full
|
||||
|
||||
index = PeriodIndex([2005, 2005, 2007], freq='A')
|
||||
assert not index.is_full
|
||||
|
||||
index = PeriodIndex([2005, 2005, 2006], freq='A')
|
||||
assert index.is_full
|
||||
|
||||
index = PeriodIndex([2006, 2005, 2005], freq='A')
|
||||
pytest.raises(ValueError, getattr, index, 'is_full')
|
||||
|
||||
assert index[:0].is_full
|
||||
|
||||
def test_with_multi_index(self):
|
||||
# #1705
|
||||
index = date_range('1/1/2012', periods=4, freq='12H')
|
||||
index_as_arrays = [index.to_period(freq='D'), index.hour]
|
||||
|
||||
s = Series([0, 1, 2, 3], index_as_arrays)
|
||||
|
||||
assert isinstance(s.index.levels[0], PeriodIndex)
|
||||
|
||||
assert isinstance(s.index.values[0][0], Period)
|
||||
|
||||
def test_convert_array_of_periods(self):
|
||||
rng = period_range('1/1/2000', periods=20, freq='D')
|
||||
periods = list(rng)
|
||||
|
||||
result = pd.Index(periods)
|
||||
assert isinstance(result, PeriodIndex)
|
||||
|
||||
def test_append_concat(self):
|
||||
# #1815
|
||||
d1 = date_range('12/31/1990', '12/31/1999', freq='A-DEC')
|
||||
d2 = date_range('12/31/2000', '12/31/2009', freq='A-DEC')
|
||||
|
||||
s1 = Series(np.random.randn(10), d1)
|
||||
s2 = Series(np.random.randn(10), d2)
|
||||
|
||||
s1 = s1.to_period()
|
||||
s2 = s2.to_period()
|
||||
|
||||
# drops index
|
||||
result = pd.concat([s1, s2])
|
||||
assert isinstance(result.index, PeriodIndex)
|
||||
assert result.index[0] == s1.index[0]
|
||||
|
||||
def test_pickle_freq(self):
|
||||
# GH2891
|
||||
prng = period_range('1/1/2011', '1/1/2012', freq='M')
|
||||
new_prng = tm.round_trip_pickle(prng)
|
||||
assert new_prng.freq == offsets.MonthEnd()
|
||||
assert new_prng.freqstr == 'M'
|
||||
|
||||
def test_map(self):
|
||||
# test_map_dictlike generally tests
|
||||
|
||||
index = PeriodIndex([2005, 2007, 2009], freq='A')
|
||||
result = index.map(lambda x: x.ordinal)
|
||||
exp = Index([x.ordinal for x in index])
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
def test_join_self(self, join_type):
|
||||
index = period_range('1/1/2000', periods=10)
|
||||
joined = index.join(index, how=join_type)
|
||||
assert index is joined
|
||||
|
||||
def test_insert(self):
|
||||
# GH 18295 (test missing)
|
||||
expected = PeriodIndex(
|
||||
['2017Q1', pd.NaT, '2017Q2', '2017Q3', '2017Q4'], freq='Q')
|
||||
for na in (np.nan, pd.NaT, None):
|
||||
result = period_range('2017Q1', periods=4, freq='Q').insert(1, na)
|
||||
tm.assert_index_equal(result, expected)
|
||||
-94
@@ -1,94 +0,0 @@
|
||||
import pytest
|
||||
import pandas.util.testing as tm
|
||||
from pandas import date_range, NaT, period_range, Period, PeriodIndex
|
||||
|
||||
|
||||
class TestPeriodRange(object):
|
||||
|
||||
@pytest.mark.parametrize('freq', ['D', 'W', 'M', 'Q', 'A'])
|
||||
def test_construction_from_string(self, freq):
|
||||
# non-empty
|
||||
expected = date_range(start='2017-01-01', periods=5,
|
||||
freq=freq, name='foo').to_period()
|
||||
start, end = str(expected[0]), str(expected[-1])
|
||||
|
||||
result = period_range(start=start, end=end, freq=freq, name='foo')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = period_range(start=start, periods=5, freq=freq, name='foo')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = period_range(end=end, periods=5, freq=freq, name='foo')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# empty
|
||||
expected = PeriodIndex([], freq=freq, name='foo')
|
||||
|
||||
result = period_range(start=start, periods=0, freq=freq, name='foo')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = period_range(end=end, periods=0, freq=freq, name='foo')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = period_range(start=end, end=start, freq=freq, name='foo')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_construction_from_period(self):
|
||||
# upsampling
|
||||
start, end = Period('2017Q1', freq='Q'), Period('2018Q1', freq='Q')
|
||||
expected = date_range(start='2017-03-31', end='2018-03-31', freq='M',
|
||||
name='foo').to_period()
|
||||
result = period_range(start=start, end=end, freq='M', name='foo')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# downsampling
|
||||
start, end = Period('2017-1', freq='M'), Period('2019-12', freq='M')
|
||||
expected = date_range(start='2017-01-31', end='2019-12-31', freq='Q',
|
||||
name='foo').to_period()
|
||||
result = period_range(start=start, end=end, freq='Q', name='foo')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# empty
|
||||
expected = PeriodIndex([], freq='W', name='foo')
|
||||
|
||||
result = period_range(start=start, periods=0, freq='W', name='foo')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = period_range(end=end, periods=0, freq='W', name='foo')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = period_range(start=end, end=start, freq='W', name='foo')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_errors(self):
|
||||
# not enough params
|
||||
msg = ('Of the three parameters: start, end, and periods, '
|
||||
'exactly two must be specified')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
period_range(start='2017Q1')
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
period_range(end='2017Q1')
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
period_range(periods=5)
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
period_range()
|
||||
|
||||
# too many params
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
period_range(start='2017Q1', end='2018Q1', periods=8, freq='Q')
|
||||
|
||||
# start/end NaT
|
||||
msg = 'start and end must not be NaT'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
period_range(start=NaT, end='2018Q1')
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
period_range(start='2017Q1', end=NaT)
|
||||
|
||||
# invalid periods param
|
||||
msg = 'periods must be a number, got foo'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
period_range(start='2017Q1', periods='foo')
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Tests for PeriodIndex behaving like a vectorized Period scalar"""
|
||||
|
||||
from pandas import PeriodIndex, date_range
|
||||
import pandas.util.testing as tm
|
||||
|
||||
|
||||
class TestPeriodIndexOps(object):
|
||||
def test_start_time(self):
|
||||
index = PeriodIndex(freq='M', start='2016-01-01', end='2016-05-31')
|
||||
expected_index = date_range('2016-01-01', end='2016-05-31', freq='MS')
|
||||
tm.assert_index_equal(index.start_time, expected_index)
|
||||
|
||||
def test_end_time(self):
|
||||
index = PeriodIndex(freq='M', start='2016-01-01', end='2016-05-31')
|
||||
expected_index = date_range('2016-01-01', end='2016-05-31', freq='M')
|
||||
tm.assert_index_equal(index.end_time, expected_index)
|
||||
@@ -1,247 +0,0 @@
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
import pandas.core.indexes.period as period
|
||||
from pandas import period_range, PeriodIndex, Index, date_range
|
||||
|
||||
|
||||
def _permute(obj):
|
||||
return obj.take(np.random.permutation(len(obj)))
|
||||
|
||||
|
||||
class TestPeriodIndex(object):
|
||||
|
||||
def test_joins(self, join_type):
|
||||
index = period_range('1/1/2000', '1/20/2000', freq='D')
|
||||
|
||||
joined = index.join(index[:-5], how=join_type)
|
||||
|
||||
assert isinstance(joined, PeriodIndex)
|
||||
assert joined.freq == index.freq
|
||||
|
||||
def test_join_self(self, join_type):
|
||||
index = period_range('1/1/2000', '1/20/2000', freq='D')
|
||||
|
||||
res = index.join(index, how=join_type)
|
||||
assert index is res
|
||||
|
||||
def test_join_does_not_recur(self):
|
||||
df = tm.makeCustomDataframe(
|
||||
3, 2, data_gen_f=lambda *args: np.random.randint(2),
|
||||
c_idx_type='p', r_idx_type='dt')
|
||||
s = df.iloc[:2, 0]
|
||||
|
||||
res = s.index.join(df.columns, how='outer')
|
||||
expected = Index([s.index[0], s.index[1],
|
||||
df.columns[0], df.columns[1]], object)
|
||||
tm.assert_index_equal(res, expected)
|
||||
|
||||
def test_union(self):
|
||||
# union
|
||||
rng1 = pd.period_range('1/1/2000', freq='D', periods=5)
|
||||
other1 = pd.period_range('1/6/2000', freq='D', periods=5)
|
||||
expected1 = pd.period_range('1/1/2000', freq='D', periods=10)
|
||||
|
||||
rng2 = pd.period_range('1/1/2000', freq='D', periods=5)
|
||||
other2 = pd.period_range('1/4/2000', freq='D', periods=5)
|
||||
expected2 = pd.period_range('1/1/2000', freq='D', periods=8)
|
||||
|
||||
rng3 = pd.period_range('1/1/2000', freq='D', periods=5)
|
||||
other3 = pd.PeriodIndex([], freq='D')
|
||||
expected3 = pd.period_range('1/1/2000', freq='D', periods=5)
|
||||
|
||||
rng4 = pd.period_range('2000-01-01 09:00', freq='H', periods=5)
|
||||
other4 = pd.period_range('2000-01-02 09:00', freq='H', periods=5)
|
||||
expected4 = pd.PeriodIndex(['2000-01-01 09:00', '2000-01-01 10:00',
|
||||
'2000-01-01 11:00', '2000-01-01 12:00',
|
||||
'2000-01-01 13:00', '2000-01-02 09:00',
|
||||
'2000-01-02 10:00', '2000-01-02 11:00',
|
||||
'2000-01-02 12:00', '2000-01-02 13:00'],
|
||||
freq='H')
|
||||
|
||||
rng5 = pd.PeriodIndex(['2000-01-01 09:01', '2000-01-01 09:03',
|
||||
'2000-01-01 09:05'], freq='T')
|
||||
other5 = pd.PeriodIndex(['2000-01-01 09:01', '2000-01-01 09:05'
|
||||
'2000-01-01 09:08'],
|
||||
freq='T')
|
||||
expected5 = pd.PeriodIndex(['2000-01-01 09:01', '2000-01-01 09:03',
|
||||
'2000-01-01 09:05', '2000-01-01 09:08'],
|
||||
freq='T')
|
||||
|
||||
rng6 = pd.period_range('2000-01-01', freq='M', periods=7)
|
||||
other6 = pd.period_range('2000-04-01', freq='M', periods=7)
|
||||
expected6 = pd.period_range('2000-01-01', freq='M', periods=10)
|
||||
|
||||
rng7 = pd.period_range('2003-01-01', freq='A', periods=5)
|
||||
other7 = pd.period_range('1998-01-01', freq='A', periods=8)
|
||||
expected7 = pd.period_range('1998-01-01', freq='A', periods=10)
|
||||
|
||||
for rng, other, expected in [(rng1, other1, expected1),
|
||||
(rng2, other2, expected2),
|
||||
(rng3, other3, expected3), (rng4, other4,
|
||||
expected4),
|
||||
(rng5, other5, expected5), (rng6, other6,
|
||||
expected6),
|
||||
(rng7, other7, expected7)]:
|
||||
|
||||
result_union = rng.union(other)
|
||||
tm.assert_index_equal(result_union, expected)
|
||||
|
||||
def test_union_misc(self):
|
||||
index = period_range('1/1/2000', '1/20/2000', freq='D')
|
||||
|
||||
result = index[:-5].union(index[10:])
|
||||
tm.assert_index_equal(result, index)
|
||||
|
||||
# not in order
|
||||
result = _permute(index[:-5]).union(_permute(index[10:]))
|
||||
tm.assert_index_equal(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)
|
||||
|
||||
msg = 'can only call with other PeriodIndex-ed objects'
|
||||
with tm.assert_raises_regex(ValueError, 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)
|
||||
|
||||
def test_union_dataframe_index(self):
|
||||
rng1 = pd.period_range('1/1/1999', '1/1/2012', freq='M')
|
||||
s1 = pd.Series(np.random.randn(len(rng1)), rng1)
|
||||
|
||||
rng2 = pd.period_range('1/1/1980', '12/1/2001', freq='M')
|
||||
s2 = pd.Series(np.random.randn(len(rng2)), rng2)
|
||||
df = pd.DataFrame({'s1': s1, 's2': s2})
|
||||
|
||||
exp = pd.period_range('1/1/1980', '1/1/2012', freq='M')
|
||||
tm.assert_index_equal(df.index, exp)
|
||||
|
||||
def test_intersection(self):
|
||||
index = period_range('1/1/2000', '1/20/2000', freq='D')
|
||||
|
||||
result = index[:-5].intersection(index[10:])
|
||||
tm.assert_index_equal(result, index[10:-5])
|
||||
|
||||
# not in order
|
||||
left = _permute(index[:-5])
|
||||
right = _permute(index[10:])
|
||||
result = left.intersection(right).sort_values()
|
||||
tm.assert_index_equal(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)
|
||||
|
||||
index3 = period_range('1/1/2000', '1/20/2000', freq='2D')
|
||||
with pytest.raises(period.IncompatibleFrequency):
|
||||
index.intersection(index3)
|
||||
|
||||
def test_intersection_cases(self):
|
||||
base = period_range('6/1/2000', '6/30/2000', freq='D', name='idx')
|
||||
|
||||
# if target has the same name, it is preserved
|
||||
rng2 = period_range('5/15/2000', '6/20/2000', freq='D', name='idx')
|
||||
expected2 = period_range('6/1/2000', '6/20/2000', freq='D',
|
||||
name='idx')
|
||||
|
||||
# if target name is different, it will be reset
|
||||
rng3 = period_range('5/15/2000', '6/20/2000', freq='D', name='other')
|
||||
expected3 = period_range('6/1/2000', '6/20/2000', freq='D',
|
||||
name=None)
|
||||
|
||||
rng4 = period_range('7/1/2000', '7/31/2000', freq='D', name='idx')
|
||||
expected4 = PeriodIndex([], name='idx', freq='D')
|
||||
|
||||
for (rng, expected) in [(rng2, expected2), (rng3, expected3),
|
||||
(rng4, expected4)]:
|
||||
result = base.intersection(rng)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq == expected.freq
|
||||
|
||||
# non-monotonic
|
||||
base = PeriodIndex(['2011-01-05', '2011-01-04', '2011-01-02',
|
||||
'2011-01-03'], freq='D', name='idx')
|
||||
|
||||
rng2 = PeriodIndex(['2011-01-04', '2011-01-02',
|
||||
'2011-02-02', '2011-02-03'],
|
||||
freq='D', name='idx')
|
||||
expected2 = PeriodIndex(['2011-01-04', '2011-01-02'], freq='D',
|
||||
name='idx')
|
||||
|
||||
rng3 = PeriodIndex(['2011-01-04', '2011-01-02', '2011-02-02',
|
||||
'2011-02-03'],
|
||||
freq='D', name='other')
|
||||
expected3 = PeriodIndex(['2011-01-04', '2011-01-02'], freq='D',
|
||||
name=None)
|
||||
|
||||
rng4 = period_range('7/1/2000', '7/31/2000', freq='D', name='idx')
|
||||
expected4 = PeriodIndex([], freq='D', name='idx')
|
||||
|
||||
for (rng, expected) in [(rng2, expected2), (rng3, expected3),
|
||||
(rng4, expected4)]:
|
||||
result = base.intersection(rng)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq == 'D'
|
||||
|
||||
# empty same freq
|
||||
rng = date_range('6/1/2000', '6/15/2000', freq='T')
|
||||
result = rng[0:0].intersection(rng)
|
||||
assert len(result) == 0
|
||||
|
||||
result = rng.intersection(rng[0:0])
|
||||
assert len(result) == 0
|
||||
|
||||
def test_difference(self):
|
||||
# diff
|
||||
rng1 = pd.period_range('1/1/2000', freq='D', periods=5)
|
||||
other1 = pd.period_range('1/6/2000', freq='D', periods=5)
|
||||
expected1 = pd.period_range('1/1/2000', freq='D', periods=5)
|
||||
|
||||
rng2 = pd.period_range('1/1/2000', freq='D', periods=5)
|
||||
other2 = pd.period_range('1/4/2000', freq='D', periods=5)
|
||||
expected2 = pd.period_range('1/1/2000', freq='D', periods=3)
|
||||
|
||||
rng3 = pd.period_range('1/1/2000', freq='D', periods=5)
|
||||
other3 = pd.PeriodIndex([], freq='D')
|
||||
expected3 = pd.period_range('1/1/2000', freq='D', periods=5)
|
||||
|
||||
rng4 = pd.period_range('2000-01-01 09:00', freq='H', periods=5)
|
||||
other4 = pd.period_range('2000-01-02 09:00', freq='H', periods=5)
|
||||
expected4 = rng4
|
||||
|
||||
rng5 = pd.PeriodIndex(['2000-01-01 09:01', '2000-01-01 09:03',
|
||||
'2000-01-01 09:05'], freq='T')
|
||||
other5 = pd.PeriodIndex(
|
||||
['2000-01-01 09:01', '2000-01-01 09:05'], freq='T')
|
||||
expected5 = pd.PeriodIndex(['2000-01-01 09:03'], freq='T')
|
||||
|
||||
rng6 = pd.period_range('2000-01-01', freq='M', periods=7)
|
||||
other6 = pd.period_range('2000-04-01', freq='M', periods=7)
|
||||
expected6 = pd.period_range('2000-01-01', freq='M', periods=3)
|
||||
|
||||
rng7 = pd.period_range('2003-01-01', freq='A', periods=5)
|
||||
other7 = pd.period_range('1998-01-01', freq='A', periods=8)
|
||||
expected7 = pd.period_range('2006-01-01', freq='A', periods=2)
|
||||
|
||||
for rng, other, expected in [(rng1, other1, expected1),
|
||||
(rng2, other2, expected2),
|
||||
(rng3, other3, expected3),
|
||||
(rng4, other4, expected4),
|
||||
(rng5, other5, expected5),
|
||||
(rng6, other6, expected6),
|
||||
(rng7, other7, expected7), ]:
|
||||
result_union = rng.difference(other)
|
||||
tm.assert_index_equal(result_union, expected)
|
||||
@@ -1,331 +0,0 @@
|
||||
import numpy as np
|
||||
from datetime import datetime, timedelta
|
||||
import pytest
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
import pandas.core.indexes.period as period
|
||||
from pandas.compat import lrange
|
||||
|
||||
from pandas._libs.tslibs.ccalendar import MONTHS
|
||||
|
||||
from pandas import (PeriodIndex, Period, DatetimeIndex, Timestamp, Series,
|
||||
date_range, to_datetime, period_range)
|
||||
|
||||
|
||||
class TestPeriodRepresentation(object):
|
||||
"""
|
||||
Wish to match NumPy units
|
||||
"""
|
||||
|
||||
def _check_freq(self, freq, base_date):
|
||||
rng = PeriodIndex(start=base_date, periods=10, freq=freq)
|
||||
exp = np.arange(10, dtype=np.int64)
|
||||
|
||||
tm.assert_numpy_array_equal(rng.asi8, exp)
|
||||
|
||||
def test_annual(self):
|
||||
self._check_freq('A', 1970)
|
||||
|
||||
def test_monthly(self):
|
||||
self._check_freq('M', '1970-01')
|
||||
|
||||
@pytest.mark.parametrize('freq', ['W-THU', 'D', 'B', 'H', 'T',
|
||||
'S', 'L', 'U', 'N'])
|
||||
def test_freq(self, freq):
|
||||
self._check_freq(freq, '1970-01-01')
|
||||
|
||||
def test_negone_ordinals(self):
|
||||
freqs = ['A', 'M', 'Q', 'D', 'H', 'T', 'S']
|
||||
|
||||
period = Period(ordinal=-1, freq='D')
|
||||
for freq in freqs:
|
||||
repr(period.asfreq(freq))
|
||||
|
||||
for freq in freqs:
|
||||
period = Period(ordinal=-1, freq=freq)
|
||||
repr(period)
|
||||
assert period.year == 1969
|
||||
|
||||
period = Period(ordinal=-1, freq='B')
|
||||
repr(period)
|
||||
period = Period(ordinal=-1, freq='W')
|
||||
repr(period)
|
||||
|
||||
|
||||
class TestPeriodIndex(object):
|
||||
def test_to_timestamp(self):
|
||||
index = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009')
|
||||
series = Series(1, index=index, name='foo')
|
||||
|
||||
exp_index = date_range('1/1/2001', end='12/31/2009', freq='A-DEC')
|
||||
result = series.to_timestamp(how='end')
|
||||
tm.assert_index_equal(result.index, exp_index)
|
||||
assert result.name == 'foo'
|
||||
|
||||
exp_index = date_range('1/1/2001', end='1/1/2009', freq='AS-JAN')
|
||||
result = series.to_timestamp(how='start')
|
||||
tm.assert_index_equal(result.index, exp_index)
|
||||
|
||||
def _get_with_delta(delta, freq='A-DEC'):
|
||||
return date_range(to_datetime('1/1/2001') + delta,
|
||||
to_datetime('12/31/2009') + delta, freq=freq)
|
||||
|
||||
delta = timedelta(hours=23)
|
||||
result = series.to_timestamp('H', 'end')
|
||||
exp_index = _get_with_delta(delta)
|
||||
tm.assert_index_equal(result.index, exp_index)
|
||||
|
||||
delta = timedelta(hours=23, minutes=59)
|
||||
result = series.to_timestamp('T', 'end')
|
||||
exp_index = _get_with_delta(delta)
|
||||
tm.assert_index_equal(result.index, exp_index)
|
||||
|
||||
result = series.to_timestamp('S', 'end')
|
||||
delta = timedelta(hours=23, minutes=59, seconds=59)
|
||||
exp_index = _get_with_delta(delta)
|
||||
tm.assert_index_equal(result.index, exp_index)
|
||||
|
||||
index = PeriodIndex(freq='H', start='1/1/2001', end='1/2/2001')
|
||||
series = Series(1, index=index, name='foo')
|
||||
|
||||
exp_index = date_range('1/1/2001 00:59:59', end='1/2/2001 00:59:59',
|
||||
freq='H')
|
||||
result = series.to_timestamp(how='end')
|
||||
tm.assert_index_equal(result.index, exp_index)
|
||||
assert result.name == 'foo'
|
||||
|
||||
def test_to_timestamp_repr_is_code(self):
|
||||
zs = [Timestamp('99-04-17 00:00:00', tz='UTC'),
|
||||
Timestamp('2001-04-17 00:00:00', tz='UTC'),
|
||||
Timestamp('2001-04-17 00:00:00', tz='America/Los_Angeles'),
|
||||
Timestamp('2001-04-17 00:00:00', tz=None)]
|
||||
for z in zs:
|
||||
assert eval(repr(z)) == z
|
||||
|
||||
def test_to_timestamp_to_period_astype(self):
|
||||
idx = DatetimeIndex([pd.NaT, '2011-01-01', '2011-02-01'], name='idx')
|
||||
|
||||
res = idx.astype('period[M]')
|
||||
exp = PeriodIndex(['NaT', '2011-01', '2011-02'], freq='M', name='idx')
|
||||
tm.assert_index_equal(res, exp)
|
||||
|
||||
res = idx.astype('period[3M]')
|
||||
exp = PeriodIndex(['NaT', '2011-01', '2011-02'], freq='3M', name='idx')
|
||||
tm.assert_index_equal(res, exp)
|
||||
|
||||
def test_dti_to_period(self):
|
||||
dti = DatetimeIndex(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'))
|
||||
|
||||
@pytest.mark.parametrize('month', MONTHS)
|
||||
def test_to_period_quarterly(self, month):
|
||||
# make sure we can make the round trip
|
||||
freq = 'Q-%s' % month
|
||||
rng = period_range('1989Q3', '1991Q3', freq=freq)
|
||||
stamps = rng.to_timestamp()
|
||||
result = stamps.to_period(freq)
|
||||
tm.assert_index_equal(rng, result)
|
||||
|
||||
@pytest.mark.parametrize('off', ['BQ', 'QS', 'BQS'])
|
||||
def test_to_period_quarterlyish(self, off):
|
||||
rng = date_range('01-Jan-2012', periods=8, freq=off)
|
||||
prng = rng.to_period()
|
||||
assert prng.freq == 'Q-DEC'
|
||||
|
||||
@pytest.mark.parametrize('off', ['BA', 'AS', 'BAS'])
|
||||
def test_to_period_annualish(self, off):
|
||||
rng = date_range('01-Jan-2012', periods=8, freq=off)
|
||||
prng = rng.to_period()
|
||||
assert prng.freq == 'A-DEC'
|
||||
|
||||
def test_to_period_monthish(self):
|
||||
offsets = ['MS', 'BM']
|
||||
for off in offsets:
|
||||
rng = date_range('01-Jan-2012', periods=8, freq=off)
|
||||
prng = rng.to_period()
|
||||
assert prng.freq == 'M'
|
||||
|
||||
rng = date_range('01-Jan-2012', periods=8, freq='M')
|
||||
prng = rng.to_period()
|
||||
assert prng.freq == 'M'
|
||||
|
||||
msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
date_range('01-Jan-2012', periods=8, freq='EOM')
|
||||
|
||||
def test_period_dt64_round_trip(self):
|
||||
dti = date_range('1/1/2000', '1/7/2002', freq='B')
|
||||
pi = dti.to_period()
|
||||
tm.assert_index_equal(pi.to_timestamp(), dti)
|
||||
|
||||
dti = date_range('1/1/2000', '1/7/2002', freq='B')
|
||||
pi = dti.to_period(freq='H')
|
||||
tm.assert_index_equal(pi.to_timestamp(), dti)
|
||||
|
||||
def test_combine_first(self):
|
||||
# GH 3367
|
||||
didx = pd.DatetimeIndex(start='1950-01-31', end='1950-07-31', freq='M')
|
||||
pidx = pd.PeriodIndex(start=pd.Period('1950-1'),
|
||||
end=pd.Period('1950-7'), freq='M')
|
||||
# check to be consistent with DatetimeIndex
|
||||
for idx in [didx, pidx]:
|
||||
a = pd.Series([1, np.nan, np.nan, 4, 5, np.nan, 7], index=idx)
|
||||
b = pd.Series([9, 9, 9, 9, 9, 9, 9], index=idx)
|
||||
|
||||
result = a.combine_first(b)
|
||||
expected = pd.Series([1, 9, 9, 4, 5, 9, 7], index=idx,
|
||||
dtype=np.float64)
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize('freq', ['D', '2D'])
|
||||
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 tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
pidx.searchsorted(pd.Period('2014-01-01', freq='H'))
|
||||
|
||||
msg = "Input has different freq=5D from PeriodIndex"
|
||||
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
|
||||
pidx.searchsorted(pd.Period('2014-01-01', freq='5D'))
|
||||
|
||||
with tm.assert_produces_warning(FutureWarning):
|
||||
pidx.searchsorted(key=p2)
|
||||
|
||||
|
||||
class TestPeriodIndexConversion(object):
|
||||
def test_tolist(self):
|
||||
index = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009')
|
||||
rs = index.tolist()
|
||||
for x in rs:
|
||||
assert isinstance(x, Period)
|
||||
|
||||
recon = PeriodIndex(rs)
|
||||
tm.assert_index_equal(index, recon)
|
||||
|
||||
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 tm.assert_raises_regex(ValueError, msg):
|
||||
result.to_period(freq='-2A')
|
||||
|
||||
def test_to_timestamp_preserve_name(self):
|
||||
index = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009',
|
||||
name='foo')
|
||||
assert index.name == 'foo'
|
||||
|
||||
conv = index.to_timestamp('D')
|
||||
assert conv.name == 'foo'
|
||||
|
||||
def test_to_timestamp_quarterly_bug(self):
|
||||
years = np.arange(1960, 2000).repeat(4)
|
||||
quarters = np.tile(lrange(1, 5), 40)
|
||||
|
||||
pindex = PeriodIndex(year=years, quarter=quarters)
|
||||
|
||||
stamps = pindex.to_timestamp('D', 'end')
|
||||
expected = DatetimeIndex([x.to_timestamp('D', 'end') for x in pindex])
|
||||
tm.assert_index_equal(stamps, expected)
|
||||
|
||||
def test_to_timestamp_pi_mult(self):
|
||||
idx = PeriodIndex(['2011-01', 'NaT', '2011-02'],
|
||||
freq='2M', name='idx')
|
||||
|
||||
result = idx.to_timestamp()
|
||||
expected = DatetimeIndex(['2011-01-01', 'NaT', '2011-02-01'],
|
||||
name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = idx.to_timestamp(how='E')
|
||||
expected = DatetimeIndex(['2011-02-28', 'NaT', '2011-03-31'],
|
||||
name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_to_timestamp_pi_combined(self):
|
||||
idx = PeriodIndex(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')
|
||||
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')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
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'])
|
||||
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')
|
||||
res = pi.astype('datetime64[ns, US/Eastern]', how='end')
|
||||
tm.assert_index_equal(res, exp)
|
||||
|
||||
def test_to_timestamp_1703(self):
|
||||
index = period_range('1/1/2012', periods=4, freq='D')
|
||||
|
||||
result = index.to_timestamp()
|
||||
assert result[0] == Timestamp('1/1/2012')
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,71 +0,0 @@
|
||||
import numpy as np
|
||||
from pandas.util import testing as tm
|
||||
from pandas.tests.test_base import CheckImmutable, CheckStringMixin
|
||||
from pandas.core.indexes.frozen import FrozenList, FrozenNDArray
|
||||
from pandas.compat import u
|
||||
|
||||
|
||||
class TestFrozenList(CheckImmutable, CheckStringMixin):
|
||||
mutable_methods = ('extend', 'pop', 'remove', 'insert')
|
||||
unicode_container = FrozenList([u("\u05d0"), u("\u05d1"), "c"])
|
||||
|
||||
def setup_method(self, method):
|
||||
self.lst = [1, 2, 3, 4, 5]
|
||||
self.container = FrozenList(self.lst)
|
||||
self.klass = FrozenList
|
||||
|
||||
def test_add(self):
|
||||
result = self.container + (1, 2, 3)
|
||||
expected = FrozenList(self.lst + [1, 2, 3])
|
||||
self.check_result(result, expected)
|
||||
|
||||
result = (1, 2, 3) + self.container
|
||||
expected = FrozenList([1, 2, 3] + self.lst)
|
||||
self.check_result(result, expected)
|
||||
|
||||
def test_inplace(self):
|
||||
q = r = self.container
|
||||
q += [5]
|
||||
self.check_result(q, self.lst + [5])
|
||||
# other shouldn't be mutated
|
||||
self.check_result(r, self.lst)
|
||||
|
||||
|
||||
class TestFrozenNDArray(CheckImmutable, CheckStringMixin):
|
||||
mutable_methods = ('put', 'itemset', 'fill')
|
||||
unicode_container = FrozenNDArray([u("\u05d0"), u("\u05d1"), "c"])
|
||||
|
||||
def setup_method(self, method):
|
||||
self.lst = [3, 5, 7, -2]
|
||||
self.container = FrozenNDArray(self.lst)
|
||||
self.klass = FrozenNDArray
|
||||
|
||||
def test_shallow_copying(self):
|
||||
original = self.container.copy()
|
||||
assert isinstance(self.container.view(), FrozenNDArray)
|
||||
assert not isinstance(self.container.view(np.ndarray), FrozenNDArray)
|
||||
assert self.container.view() is not self.container
|
||||
tm.assert_numpy_array_equal(self.container, original)
|
||||
|
||||
# Shallow copy should be the same too
|
||||
assert isinstance(self.container._shallow_copy(), FrozenNDArray)
|
||||
|
||||
# setting should not be allowed
|
||||
def testit(container):
|
||||
container[0] = 16
|
||||
|
||||
self.check_mutable_error(testit, self.container)
|
||||
|
||||
def test_values(self):
|
||||
original = self.container.view(np.ndarray).copy()
|
||||
n = original[0] + 15
|
||||
|
||||
vals = self.container.values()
|
||||
tm.assert_numpy_array_equal(original, vals)
|
||||
|
||||
assert original is not vals
|
||||
vals[0] = n
|
||||
|
||||
assert isinstance(self.container, FrozenNDArray)
|
||||
tm.assert_numpy_array_equal(self.container.values(), original)
|
||||
assert vals[0] == n
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
-1123
File diff suppressed because it is too large
Load Diff
-78
@@ -1,78 +0,0 @@
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
|
||||
import pandas.util.testing as tm
|
||||
from pandas import (TimedeltaIndex, timedelta_range, Int64Index, Float64Index,
|
||||
Index, Timedelta, NaT)
|
||||
|
||||
|
||||
class TestTimedeltaIndex(object):
|
||||
def test_astype_object(self):
|
||||
idx = timedelta_range(start='1 days', periods=4, freq='D', name='idx')
|
||||
expected_list = [Timedelta('1 days'), Timedelta('2 days'),
|
||||
Timedelta('3 days'), Timedelta('4 days')]
|
||||
result = idx.astype(object)
|
||||
expected = Index(expected_list, dtype=object, name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert idx.tolist() == expected_list
|
||||
|
||||
def test_astype_object_with_nat(self):
|
||||
idx = TimedeltaIndex([timedelta(days=1), timedelta(days=2), NaT,
|
||||
timedelta(days=4)], name='idx')
|
||||
expected_list = [Timedelta('1 days'), Timedelta('2 days'), NaT,
|
||||
Timedelta('4 days')]
|
||||
result = idx.astype(object)
|
||||
expected = Index(expected_list, dtype=object, name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert idx.tolist() == expected_list
|
||||
|
||||
def test_astype(self):
|
||||
# GH 13149, GH 13209
|
||||
idx = TimedeltaIndex([1e14, 'NaT', NaT, np.NaN])
|
||||
|
||||
result = idx.astype(object)
|
||||
expected = Index([Timedelta('1 days 03:46:40')] + [NaT] * 3,
|
||||
dtype=object)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = idx.astype(int)
|
||||
expected = Int64Index([100000000000000] + [-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)
|
||||
|
||||
rng = timedelta_range('1 days', periods=10)
|
||||
result = rng.astype('i8')
|
||||
tm.assert_index_equal(result, Index(rng.asi8))
|
||||
tm.assert_numpy_array_equal(rng.asi8, result.values)
|
||||
|
||||
def test_astype_timedelta64(self):
|
||||
# GH 13149, GH 13209
|
||||
idx = TimedeltaIndex([1e14, 'NaT', NaT, np.NaN])
|
||||
|
||||
result = idx.astype('timedelta64')
|
||||
expected = Float64Index([1e+14] + [np.NaN] * 3, dtype='float64')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = idx.astype('timedelta64[ns]')
|
||||
tm.assert_index_equal(result, idx)
|
||||
assert result is not idx
|
||||
|
||||
result = idx.astype('timedelta64[ns]', copy=False)
|
||||
tm.assert_index_equal(result, idx)
|
||||
assert result is idx
|
||||
|
||||
@pytest.mark.parametrize('dtype', [
|
||||
float, 'datetime64', 'datetime64[ns]'])
|
||||
def test_astype_raises(self, dtype):
|
||||
# GH 13149, GH 13209
|
||||
idx = TimedeltaIndex([1e14, 'NaT', NaT, np.NaN])
|
||||
msg = 'Cannot cast TimedeltaIndex to dtype'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
idx.astype(dtype)
|
||||
-88
@@ -1,88 +0,0 @@
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
from datetime import timedelta
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
from pandas import TimedeltaIndex, timedelta_range, to_timedelta
|
||||
|
||||
|
||||
class TestTimedeltaIndex(object):
|
||||
|
||||
def test_construction_base_constructor(self):
|
||||
arr = [pd.Timedelta('1 days'), pd.NaT, pd.Timedelta('3 days')]
|
||||
tm.assert_index_equal(pd.Index(arr), pd.TimedeltaIndex(arr))
|
||||
tm.assert_index_equal(pd.Index(np.array(arr)),
|
||||
pd.TimedeltaIndex(np.array(arr)))
|
||||
|
||||
arr = [np.nan, pd.NaT, pd.Timedelta('1 days')]
|
||||
tm.assert_index_equal(pd.Index(arr), pd.TimedeltaIndex(arr))
|
||||
tm.assert_index_equal(pd.Index(np.array(arr)),
|
||||
pd.TimedeltaIndex(np.array(arr)))
|
||||
|
||||
def test_constructor(self):
|
||||
expected = TimedeltaIndex(['1 days', '1 days 00:00:05', '2 days',
|
||||
'2 days 00:00:02', '0 days 00:00:03'])
|
||||
result = TimedeltaIndex(['1 days', '1 days, 00:00:05', np.timedelta64(
|
||||
2, 'D'), timedelta(days=2, seconds=2), pd.offsets.Second(3)])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# unicode
|
||||
result = TimedeltaIndex([u'1 days', '1 days, 00:00:05', np.timedelta64(
|
||||
2, 'D'), timedelta(days=2, seconds=2), pd.offsets.Second(3)])
|
||||
|
||||
expected = TimedeltaIndex(['0 days 00:00:00', '0 days 00:00:01',
|
||||
'0 days 00:00:02'])
|
||||
tm.assert_index_equal(TimedeltaIndex(range(3), unit='s'), expected)
|
||||
expected = TimedeltaIndex(['0 days 00:00:00', '0 days 00:00:05',
|
||||
'0 days 00:00:09'])
|
||||
tm.assert_index_equal(TimedeltaIndex([0, 5, 9], unit='s'), expected)
|
||||
expected = TimedeltaIndex(
|
||||
['0 days 00:00:00.400', '0 days 00:00:00.450',
|
||||
'0 days 00:00:01.200'])
|
||||
tm.assert_index_equal(TimedeltaIndex([400, 450, 1200], unit='ms'),
|
||||
expected)
|
||||
|
||||
def test_constructor_coverage(self):
|
||||
rng = timedelta_range('1 days', periods=10.5)
|
||||
exp = timedelta_range('1 days', periods=10)
|
||||
tm.assert_index_equal(rng, exp)
|
||||
|
||||
msg = 'periods must be a number, got foo'
|
||||
with tm.assert_raises_regex(TypeError, msg):
|
||||
TimedeltaIndex(start='1 days', periods='foo', freq='D')
|
||||
|
||||
pytest.raises(ValueError, TimedeltaIndex, start='1 days',
|
||||
end='10 days')
|
||||
|
||||
pytest.raises(ValueError, TimedeltaIndex, '1 days')
|
||||
|
||||
# generator expression
|
||||
gen = (timedelta(i) for i in range(10))
|
||||
result = TimedeltaIndex(gen)
|
||||
expected = TimedeltaIndex([timedelta(i) for i in range(10)])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# NumPy string array
|
||||
strings = np.array(['1 days', '2 days', '3 days'])
|
||||
result = TimedeltaIndex(strings)
|
||||
expected = to_timedelta([1, 2, 3], unit='d')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
from_ints = TimedeltaIndex(expected.asi8)
|
||||
tm.assert_index_equal(from_ints, expected)
|
||||
|
||||
# non-conforming freq
|
||||
pytest.raises(ValueError, TimedeltaIndex,
|
||||
['1 days', '2 days', '4 days'], freq='D')
|
||||
|
||||
pytest.raises(ValueError, TimedeltaIndex, periods=10, freq='D')
|
||||
|
||||
def test_constructor_name(self):
|
||||
idx = TimedeltaIndex(start='1 days', periods=1, freq='D', name='TEST')
|
||||
assert idx.name == 'TEST'
|
||||
|
||||
# GH10025
|
||||
idx2 = TimedeltaIndex(idx, name='something else')
|
||||
assert idx2.name == 'something else'
|
||||
-96
@@ -1,96 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import pytest
|
||||
|
||||
import pandas as pd
|
||||
from pandas import TimedeltaIndex
|
||||
|
||||
|
||||
class TestTimedeltaIndexRendering(object):
|
||||
@pytest.mark.parametrize('method', ['__repr__', '__unicode__', '__str__'])
|
||||
def test_representation(self, method):
|
||||
idx1 = TimedeltaIndex([], freq='D')
|
||||
idx2 = TimedeltaIndex(['1 days'], freq='D')
|
||||
idx3 = TimedeltaIndex(['1 days', '2 days'], freq='D')
|
||||
idx4 = TimedeltaIndex(['1 days', '2 days', '3 days'], freq='D')
|
||||
idx5 = TimedeltaIndex(['1 days 00:00:01', '2 days', '3 days'])
|
||||
|
||||
exp1 = """TimedeltaIndex([], dtype='timedelta64[ns]', freq='D')"""
|
||||
|
||||
exp2 = ("TimedeltaIndex(['1 days'], dtype='timedelta64[ns]', "
|
||||
"freq='D')")
|
||||
|
||||
exp3 = ("TimedeltaIndex(['1 days', '2 days'], "
|
||||
"dtype='timedelta64[ns]', freq='D')")
|
||||
|
||||
exp4 = ("TimedeltaIndex(['1 days', '2 days', '3 days'], "
|
||||
"dtype='timedelta64[ns]', freq='D')")
|
||||
|
||||
exp5 = ("TimedeltaIndex(['1 days 00:00:01', '2 days 00:00:00', "
|
||||
"'3 days 00:00:00'], dtype='timedelta64[ns]', freq=None)")
|
||||
|
||||
with pd.option_context('display.width', 300):
|
||||
for idx, expected in zip([idx1, idx2, idx3, idx4, idx5],
|
||||
[exp1, exp2, exp3, exp4, exp5]):
|
||||
result = getattr(idx, method)()
|
||||
assert result == expected
|
||||
|
||||
def test_representation_to_series(self):
|
||||
idx1 = TimedeltaIndex([], freq='D')
|
||||
idx2 = TimedeltaIndex(['1 days'], freq='D')
|
||||
idx3 = TimedeltaIndex(['1 days', '2 days'], freq='D')
|
||||
idx4 = TimedeltaIndex(['1 days', '2 days', '3 days'], freq='D')
|
||||
idx5 = TimedeltaIndex(['1 days 00:00:01', '2 days', '3 days'])
|
||||
|
||||
exp1 = """Series([], dtype: timedelta64[ns])"""
|
||||
|
||||
exp2 = ("0 1 days\n"
|
||||
"dtype: timedelta64[ns]")
|
||||
|
||||
exp3 = ("0 1 days\n"
|
||||
"1 2 days\n"
|
||||
"dtype: timedelta64[ns]")
|
||||
|
||||
exp4 = ("0 1 days\n"
|
||||
"1 2 days\n"
|
||||
"2 3 days\n"
|
||||
"dtype: timedelta64[ns]")
|
||||
|
||||
exp5 = ("0 1 days 00:00:01\n"
|
||||
"1 2 days 00:00:00\n"
|
||||
"2 3 days 00:00:00\n"
|
||||
"dtype: timedelta64[ns]")
|
||||
|
||||
with pd.option_context('display.width', 300):
|
||||
for idx, expected in zip([idx1, idx2, idx3, idx4, idx5],
|
||||
[exp1, exp2, exp3, exp4, exp5]):
|
||||
result = repr(pd.Series(idx))
|
||||
assert result == expected
|
||||
|
||||
def test_summary(self):
|
||||
# GH#9116
|
||||
idx1 = TimedeltaIndex([], freq='D')
|
||||
idx2 = TimedeltaIndex(['1 days'], freq='D')
|
||||
idx3 = TimedeltaIndex(['1 days', '2 days'], freq='D')
|
||||
idx4 = TimedeltaIndex(['1 days', '2 days', '3 days'], freq='D')
|
||||
idx5 = TimedeltaIndex(['1 days 00:00:01', '2 days', '3 days'])
|
||||
|
||||
exp1 = ("TimedeltaIndex: 0 entries\n"
|
||||
"Freq: D")
|
||||
|
||||
exp2 = ("TimedeltaIndex: 1 entries, 1 days to 1 days\n"
|
||||
"Freq: D")
|
||||
|
||||
exp3 = ("TimedeltaIndex: 2 entries, 1 days to 2 days\n"
|
||||
"Freq: D")
|
||||
|
||||
exp4 = ("TimedeltaIndex: 3 entries, 1 days to 3 days\n"
|
||||
"Freq: D")
|
||||
|
||||
exp5 = ("TimedeltaIndex: 3 entries, 1 days 00:00:01 to 3 days "
|
||||
"00:00:00")
|
||||
|
||||
for idx, expected in zip([idx1, idx2, idx3, idx4, idx5],
|
||||
[exp1, exp2, exp3, exp4, exp5]):
|
||||
result = idx._summary()
|
||||
assert result == expected
|
||||
-322
@@ -1,322 +0,0 @@
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
import numpy as np
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
from pandas import TimedeltaIndex, timedelta_range, compat, Index, Timedelta
|
||||
|
||||
|
||||
class TestGetItem(object):
|
||||
def test_getitem(self):
|
||||
idx1 = timedelta_range('1 day', '31 day', freq='D', name='idx')
|
||||
|
||||
for idx in [idx1]:
|
||||
result = idx[0]
|
||||
assert result == Timedelta('1 day')
|
||||
|
||||
result = idx[0:5]
|
||||
expected = timedelta_range('1 day', '5 day', freq='D',
|
||||
name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
|
||||
result = idx[0:10:2]
|
||||
expected = timedelta_range('1 day', '9 day', freq='2D',
|
||||
name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
|
||||
result = idx[-20:-5:3]
|
||||
expected = timedelta_range('12 day', '24 day', freq='3D',
|
||||
name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
|
||||
result = idx[4::-1]
|
||||
expected = TimedeltaIndex(['5 day', '4 day', '3 day',
|
||||
'2 day', '1 day'],
|
||||
freq='-1D', name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
|
||||
|
||||
class TestWhere(object):
|
||||
# placeholder for symmetry with DatetimeIndex and PeriodIndex tests
|
||||
pass
|
||||
|
||||
|
||||
class TestTake(object):
|
||||
def test_take(self):
|
||||
# GH 10295
|
||||
idx1 = timedelta_range('1 day', '31 day', freq='D', name='idx')
|
||||
|
||||
for idx in [idx1]:
|
||||
result = idx.take([0])
|
||||
assert result == Timedelta('1 day')
|
||||
|
||||
result = idx.take([-1])
|
||||
assert result == Timedelta('31 day')
|
||||
|
||||
result = idx.take([0, 1, 2])
|
||||
expected = timedelta_range('1 day', '3 day', freq='D',
|
||||
name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
|
||||
result = idx.take([0, 2, 4])
|
||||
expected = timedelta_range('1 day', '5 day', freq='2D',
|
||||
name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
|
||||
result = idx.take([7, 4, 1])
|
||||
expected = timedelta_range('8 day', '2 day', freq='-3D',
|
||||
name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq == expected.freq
|
||||
|
||||
result = idx.take([3, 2, 5])
|
||||
expected = TimedeltaIndex(['4 day', '3 day', '6 day'], name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq is None
|
||||
|
||||
result = idx.take([-3, 2, 5])
|
||||
expected = TimedeltaIndex(['29 day', '3 day', '6 day'], name='idx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freq is None
|
||||
|
||||
def test_take_invalid_kwargs(self):
|
||||
idx = timedelta_range('1 day', '31 day', freq='D', name='idx')
|
||||
indices = [1, 6, 5, 9, 10, 13, 15, 3]
|
||||
|
||||
msg = r"take\(\) got an unexpected keyword argument 'foo'"
|
||||
tm.assert_raises_regex(TypeError, msg, idx.take,
|
||||
indices, foo=2)
|
||||
|
||||
msg = "the 'out' parameter is not supported"
|
||||
tm.assert_raises_regex(ValueError, msg, idx.take,
|
||||
indices, out=indices)
|
||||
|
||||
msg = "the 'mode' parameter is not supported"
|
||||
tm.assert_raises_regex(ValueError, msg, idx.take,
|
||||
indices, mode='clip')
|
||||
|
||||
# TODO: This method came from test_timedelta; de-dup with version above
|
||||
def test_take2(self):
|
||||
tds = ['1day 02:00:00', '1 day 04:00:00', '1 day 10:00:00']
|
||||
idx = TimedeltaIndex(start='1d', end='2d', freq='H', name='idx')
|
||||
expected = TimedeltaIndex(tds, freq=None, name='idx')
|
||||
|
||||
taken1 = idx.take([2, 4, 10])
|
||||
taken2 = idx[[2, 4, 10]]
|
||||
|
||||
for taken in [taken1, taken2]:
|
||||
tm.assert_index_equal(taken, expected)
|
||||
assert isinstance(taken, TimedeltaIndex)
|
||||
assert taken.freq is None
|
||||
assert taken.name == expected.name
|
||||
|
||||
def test_take_fill_value(self):
|
||||
# GH 12631
|
||||
idx = TimedeltaIndex(['1 days', '2 days', '3 days'],
|
||||
name='xxx')
|
||||
result = idx.take(np.array([1, 0, -1]))
|
||||
expected = TimedeltaIndex(['2 days', '1 days', '3 days'],
|
||||
name='xxx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# fill_value
|
||||
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
||||
expected = TimedeltaIndex(['2 days', '1 days', 'NaT'],
|
||||
name='xxx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# allow_fill=False
|
||||
result = idx.take(np.array([1, 0, -1]), allow_fill=False,
|
||||
fill_value=True)
|
||||
expected = TimedeltaIndex(['2 days', '1 days', '3 days'],
|
||||
name='xxx')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
msg = ('When allow_fill=True and fill_value is not None, '
|
||||
'all indices must be >= -1')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
idx.take(np.array([1, 0, -2]), fill_value=True)
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
idx.take(np.array([1, 0, -5]), fill_value=True)
|
||||
|
||||
with pytest.raises(IndexError):
|
||||
idx.take(np.array([1, -5]))
|
||||
|
||||
|
||||
class TestTimedeltaIndex(object):
|
||||
|
||||
def test_insert(self):
|
||||
|
||||
idx = TimedeltaIndex(['4day', '1day', '2day'], name='idx')
|
||||
|
||||
result = idx.insert(2, timedelta(days=5))
|
||||
exp = TimedeltaIndex(['4day', '1day', '5day', '2day'], name='idx')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
# insertion of non-datetime should coerce to object index
|
||||
result = idx.insert(1, 'inserted')
|
||||
expected = Index([Timedelta('4day'), 'inserted', Timedelta('1day'),
|
||||
Timedelta('2day')], name='idx')
|
||||
assert not isinstance(result, TimedeltaIndex)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
|
||||
idx = timedelta_range('1day 00:00:01', periods=3, freq='s', name='idx')
|
||||
|
||||
# preserve freq
|
||||
expected_0 = TimedeltaIndex(['1day', '1day 00:00:01', '1day 00:00:02',
|
||||
'1day 00:00:03'],
|
||||
name='idx', freq='s')
|
||||
expected_3 = TimedeltaIndex(['1day 00:00:01', '1day 00:00:02',
|
||||
'1day 00:00:03', '1day 00:00:04'],
|
||||
name='idx', freq='s')
|
||||
|
||||
# reset freq to None
|
||||
expected_1_nofreq = TimedeltaIndex(['1day 00:00:01', '1day 00:00:01',
|
||||
'1day 00:00:02', '1day 00:00:03'],
|
||||
name='idx', freq=None)
|
||||
expected_3_nofreq = TimedeltaIndex(['1day 00:00:01', '1day 00:00:02',
|
||||
'1day 00:00:03', '1day 00:00:05'],
|
||||
name='idx', freq=None)
|
||||
|
||||
cases = [(0, Timedelta('1day'), expected_0),
|
||||
(-3, Timedelta('1day'), expected_0),
|
||||
(3, Timedelta('1day 00:00:04'), expected_3),
|
||||
(1, Timedelta('1day 00:00:01'), expected_1_nofreq),
|
||||
(3, Timedelta('1day 00:00:05'), expected_3_nofreq)]
|
||||
|
||||
for n, d, expected in cases:
|
||||
result = idx.insert(n, d)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq == expected.freq
|
||||
|
||||
# GH 18295 (test missing)
|
||||
expected = TimedeltaIndex(['1day', pd.NaT, '2day', '3day'])
|
||||
for na in (np.nan, pd.NaT, None):
|
||||
result = timedelta_range('1day', '3day').insert(1, na)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_delete(self):
|
||||
idx = timedelta_range(start='1 Days', periods=5, freq='D', name='idx')
|
||||
|
||||
# prserve freq
|
||||
expected_0 = timedelta_range(start='2 Days', periods=4, freq='D',
|
||||
name='idx')
|
||||
expected_4 = timedelta_range(start='1 Days', periods=4, freq='D',
|
||||
name='idx')
|
||||
|
||||
# reset freq to None
|
||||
expected_1 = TimedeltaIndex(
|
||||
['1 day', '3 day', '4 day', '5 day'], freq=None, name='idx')
|
||||
|
||||
cases = {0: expected_0,
|
||||
-5: expected_0,
|
||||
-1: expected_4,
|
||||
4: expected_4,
|
||||
1: expected_1}
|
||||
for n, expected in compat.iteritems(cases):
|
||||
result = idx.delete(n)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq == expected.freq
|
||||
|
||||
with pytest.raises((IndexError, ValueError)):
|
||||
# either depeidnig on numpy version
|
||||
result = idx.delete(5)
|
||||
|
||||
def test_delete_slice(self):
|
||||
idx = timedelta_range(start='1 days', periods=10, freq='D', name='idx')
|
||||
|
||||
# prserve freq
|
||||
expected_0_2 = timedelta_range(start='4 days', periods=7, freq='D',
|
||||
name='idx')
|
||||
expected_7_9 = timedelta_range(start='1 days', periods=7, freq='D',
|
||||
name='idx')
|
||||
|
||||
# reset freq to None
|
||||
expected_3_5 = TimedeltaIndex(['1 d', '2 d', '3 d',
|
||||
'7 d', '8 d', '9 d', '10d'],
|
||||
freq=None, name='idx')
|
||||
|
||||
cases = {(0, 1, 2): expected_0_2,
|
||||
(7, 8, 9): expected_7_9,
|
||||
(3, 4, 5): expected_3_5}
|
||||
for n, expected in compat.iteritems(cases):
|
||||
result = idx.delete(n)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq == expected.freq
|
||||
|
||||
result = idx.delete(slice(n[0], n[-1] + 1))
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
assert result.freq == expected.freq
|
||||
|
||||
def test_get_loc(self):
|
||||
idx = pd.to_timedelta(['0 days', '1 days', '2 days'])
|
||||
|
||||
for method in [None, 'pad', 'backfill', 'nearest']:
|
||||
assert idx.get_loc(idx[1], method) == 1
|
||||
assert idx.get_loc(idx[1].to_pytimedelta(), method) == 1
|
||||
assert idx.get_loc(str(idx[1]), method) == 1
|
||||
|
||||
assert idx.get_loc(idx[1], 'pad',
|
||||
tolerance=Timedelta(0)) == 1
|
||||
assert idx.get_loc(idx[1], 'pad',
|
||||
tolerance=np.timedelta64(0, 's')) == 1
|
||||
assert idx.get_loc(idx[1], 'pad',
|
||||
tolerance=timedelta(0)) == 1
|
||||
|
||||
with tm.assert_raises_regex(ValueError,
|
||||
'unit abbreviation w/o a number'):
|
||||
idx.get_loc(idx[1], method='nearest', tolerance='foo')
|
||||
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match='tolerance size must match'):
|
||||
idx.get_loc(idx[1], method='nearest',
|
||||
tolerance=[Timedelta(0).to_timedelta64(),
|
||||
Timedelta(0).to_timedelta64()])
|
||||
|
||||
for method, loc in [('pad', 1), ('backfill', 2), ('nearest', 1)]:
|
||||
assert idx.get_loc('1 day 1 hour', method) == loc
|
||||
|
||||
# GH 16909
|
||||
assert idx.get_loc(idx[1].to_timedelta64()) == 1
|
||||
|
||||
# GH 16896
|
||||
assert idx.get_loc('0 days') == 0
|
||||
|
||||
def test_get_loc_nat(self):
|
||||
tidx = TimedeltaIndex(['1 days 01:00:00', 'NaT', '2 days 01:00:00'])
|
||||
|
||||
assert tidx.get_loc(pd.NaT) == 1
|
||||
assert tidx.get_loc(None) == 1
|
||||
assert tidx.get_loc(float('nan')) == 1
|
||||
assert tidx.get_loc(np.nan) == 1
|
||||
|
||||
def test_get_indexer(self):
|
||||
idx = pd.to_timedelta(['0 days', '1 days', '2 days'])
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(idx),
|
||||
np.array([0, 1, 2], dtype=np.intp))
|
||||
|
||||
target = pd.to_timedelta(['-1 hour', '12 hours', '1 day 1 hour'])
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(target, 'pad'),
|
||||
np.array([-1, 0, 1], dtype=np.intp))
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(target, 'backfill'),
|
||||
np.array([0, 1, 2], dtype=np.intp))
|
||||
tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest'),
|
||||
np.array([0, 1, 1], dtype=np.intp))
|
||||
|
||||
res = idx.get_indexer(target, 'nearest',
|
||||
tolerance=Timedelta('1 hour'))
|
||||
tm.assert_numpy_array_equal(res, np.array([0, -1, 1], dtype=np.intp))
|
||||
@@ -1,401 +0,0 @@
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
from datetime import timedelta
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
from pandas import to_timedelta
|
||||
from pandas import (Series, Timedelta, Timestamp, TimedeltaIndex,
|
||||
timedelta_range,
|
||||
_np_version_under1p10)
|
||||
from pandas._libs.tslib import iNaT
|
||||
from pandas.tests.test_base import Ops
|
||||
from pandas.tseries.offsets import Day, Hour
|
||||
from pandas.core.dtypes.generic import ABCDateOffset
|
||||
|
||||
|
||||
class TestTimedeltaIndexOps(Ops):
|
||||
def setup_method(self, method):
|
||||
super(TestTimedeltaIndexOps, self).setup_method(method)
|
||||
mask = lambda x: isinstance(x, TimedeltaIndex)
|
||||
self.is_valid_objs = [o for o in self.objs if mask(o)]
|
||||
self.not_valid_objs = []
|
||||
|
||||
def test_ops_properties(self):
|
||||
f = lambda x: isinstance(x, TimedeltaIndex)
|
||||
self.check_ops_properties(TimedeltaIndex._field_ops, f)
|
||||
self.check_ops_properties(TimedeltaIndex._object_ops, f)
|
||||
|
||||
def test_minmax(self):
|
||||
|
||||
# monotonic
|
||||
idx1 = TimedeltaIndex(['1 days', '2 days', '3 days'])
|
||||
assert idx1.is_monotonic
|
||||
|
||||
# non-monotonic
|
||||
idx2 = TimedeltaIndex(['1 days', np.nan, '3 days', 'NaT'])
|
||||
assert not idx2.is_monotonic
|
||||
|
||||
for idx in [idx1, idx2]:
|
||||
assert idx.min() == Timedelta('1 days')
|
||||
assert idx.max() == Timedelta('3 days')
|
||||
assert idx.argmin() == 0
|
||||
assert idx.argmax() == 2
|
||||
|
||||
for op in ['min', 'max']:
|
||||
# Return NaT
|
||||
obj = TimedeltaIndex([])
|
||||
assert pd.isna(getattr(obj, op)())
|
||||
|
||||
obj = TimedeltaIndex([pd.NaT])
|
||||
assert pd.isna(getattr(obj, op)())
|
||||
|
||||
obj = TimedeltaIndex([pd.NaT, pd.NaT, pd.NaT])
|
||||
assert pd.isna(getattr(obj, op)())
|
||||
|
||||
def test_numpy_minmax(self):
|
||||
dr = pd.date_range(start='2016-01-15', end='2016-01-20')
|
||||
td = TimedeltaIndex(np.asarray(dr))
|
||||
|
||||
assert np.min(td) == Timedelta('16815 days')
|
||||
assert np.max(td) == Timedelta('16820 days')
|
||||
|
||||
errmsg = "the 'out' parameter is not supported"
|
||||
tm.assert_raises_regex(ValueError, errmsg, np.min, td, out=0)
|
||||
tm.assert_raises_regex(ValueError, errmsg, np.max, td, out=0)
|
||||
|
||||
assert np.argmin(td) == 0
|
||||
assert np.argmax(td) == 5
|
||||
|
||||
if not _np_version_under1p10:
|
||||
errmsg = "the 'out' parameter is not supported"
|
||||
tm.assert_raises_regex(
|
||||
ValueError, errmsg, np.argmin, td, out=0)
|
||||
tm.assert_raises_regex(
|
||||
ValueError, errmsg, np.argmax, td, out=0)
|
||||
|
||||
def test_value_counts_unique(self):
|
||||
# GH 7735
|
||||
|
||||
idx = timedelta_range('1 days 09:00:00', freq='H', periods=10)
|
||||
# create repeated values, 'n'th element is repeated by n+1 times
|
||||
idx = TimedeltaIndex(np.repeat(idx.values, range(1, len(idx) + 1)))
|
||||
|
||||
exp_idx = timedelta_range('1 days 18:00:00', freq='-1H', periods=10)
|
||||
expected = Series(range(10, 0, -1), index=exp_idx, dtype='int64')
|
||||
|
||||
for obj in [idx, Series(idx)]:
|
||||
tm.assert_series_equal(obj.value_counts(), expected)
|
||||
|
||||
expected = timedelta_range('1 days 09:00:00', freq='H', periods=10)
|
||||
tm.assert_index_equal(idx.unique(), expected)
|
||||
|
||||
idx = TimedeltaIndex(['1 days 09:00:00', '1 days 09:00:00',
|
||||
'1 days 09:00:00', '1 days 08:00:00',
|
||||
'1 days 08:00:00', pd.NaT])
|
||||
|
||||
exp_idx = TimedeltaIndex(['1 days 09:00:00', '1 days 08:00:00'])
|
||||
expected = Series([3, 2], index=exp_idx)
|
||||
|
||||
for obj in [idx, Series(idx)]:
|
||||
tm.assert_series_equal(obj.value_counts(), expected)
|
||||
|
||||
exp_idx = TimedeltaIndex(['1 days 09:00:00', '1 days 08:00:00',
|
||||
pd.NaT])
|
||||
expected = Series([3, 2, 1], index=exp_idx)
|
||||
|
||||
for obj in [idx, Series(idx)]:
|
||||
tm.assert_series_equal(obj.value_counts(dropna=False), expected)
|
||||
|
||||
tm.assert_index_equal(idx.unique(), exp_idx)
|
||||
|
||||
def test_nonunique_contains(self):
|
||||
# GH 9512
|
||||
for idx in map(TimedeltaIndex, ([0, 1, 0], [0, 0, -1], [0, -1, -1],
|
||||
['00:01:00', '00:01:00', '00:02:00'],
|
||||
['00:01:00', '00:01:00', '00:00:01'])):
|
||||
assert idx[0] in idx
|
||||
|
||||
def test_unknown_attribute(self):
|
||||
# see gh-9680
|
||||
tdi = pd.timedelta_range(start=0, periods=10, freq='1s')
|
||||
ts = pd.Series(np.random.normal(size=10), index=tdi)
|
||||
assert 'foo' not in ts.__dict__.keys()
|
||||
pytest.raises(AttributeError, lambda: ts.foo)
|
||||
|
||||
def test_order(self):
|
||||
# GH 10295
|
||||
idx1 = TimedeltaIndex(['1 day', '2 day', '3 day'], freq='D',
|
||||
name='idx')
|
||||
idx2 = TimedeltaIndex(
|
||||
['1 hour', '2 hour', '3 hour'], freq='H', name='idx')
|
||||
|
||||
for idx in [idx1, idx2]:
|
||||
ordered = idx.sort_values()
|
||||
tm.assert_index_equal(ordered, idx)
|
||||
assert ordered.freq == idx.freq
|
||||
|
||||
ordered = idx.sort_values(ascending=False)
|
||||
expected = idx[::-1]
|
||||
tm.assert_index_equal(ordered, expected)
|
||||
assert ordered.freq == expected.freq
|
||||
assert ordered.freq.n == -1
|
||||
|
||||
ordered, indexer = idx.sort_values(return_indexer=True)
|
||||
tm.assert_index_equal(ordered, idx)
|
||||
tm.assert_numpy_array_equal(indexer, np.array([0, 1, 2]),
|
||||
check_dtype=False)
|
||||
assert ordered.freq == idx.freq
|
||||
|
||||
ordered, indexer = idx.sort_values(return_indexer=True,
|
||||
ascending=False)
|
||||
tm.assert_index_equal(ordered, idx[::-1])
|
||||
assert ordered.freq == expected.freq
|
||||
assert ordered.freq.n == -1
|
||||
|
||||
idx1 = TimedeltaIndex(['1 hour', '3 hour', '5 hour',
|
||||
'2 hour ', '1 hour'], name='idx1')
|
||||
exp1 = TimedeltaIndex(['1 hour', '1 hour', '2 hour',
|
||||
'3 hour', '5 hour'], name='idx1')
|
||||
|
||||
idx2 = TimedeltaIndex(['1 day', '3 day', '5 day',
|
||||
'2 day', '1 day'], name='idx2')
|
||||
|
||||
# TODO(wesm): unused?
|
||||
# exp2 = TimedeltaIndex(['1 day', '1 day', '2 day',
|
||||
# '3 day', '5 day'], name='idx2')
|
||||
|
||||
# idx3 = TimedeltaIndex([pd.NaT, '3 minute', '5 minute',
|
||||
# '2 minute', pd.NaT], name='idx3')
|
||||
# exp3 = TimedeltaIndex([pd.NaT, pd.NaT, '2 minute', '3 minute',
|
||||
# '5 minute'], name='idx3')
|
||||
|
||||
for idx, expected in [(idx1, exp1), (idx1, exp1), (idx1, exp1)]:
|
||||
ordered = idx.sort_values()
|
||||
tm.assert_index_equal(ordered, expected)
|
||||
assert ordered.freq is None
|
||||
|
||||
ordered = idx.sort_values(ascending=False)
|
||||
tm.assert_index_equal(ordered, expected[::-1])
|
||||
assert ordered.freq is None
|
||||
|
||||
ordered, indexer = idx.sort_values(return_indexer=True)
|
||||
tm.assert_index_equal(ordered, expected)
|
||||
|
||||
exp = np.array([0, 4, 3, 1, 2])
|
||||
tm.assert_numpy_array_equal(indexer, exp, check_dtype=False)
|
||||
assert ordered.freq is None
|
||||
|
||||
ordered, indexer = idx.sort_values(return_indexer=True,
|
||||
ascending=False)
|
||||
tm.assert_index_equal(ordered, expected[::-1])
|
||||
|
||||
exp = np.array([2, 1, 3, 4, 0])
|
||||
tm.assert_numpy_array_equal(indexer, exp, check_dtype=False)
|
||||
assert ordered.freq is None
|
||||
|
||||
def test_drop_duplicates_metadata(self):
|
||||
# GH 10115
|
||||
idx = pd.timedelta_range('1 day', '31 day', freq='D', name='idx')
|
||||
result = idx.drop_duplicates()
|
||||
tm.assert_index_equal(idx, result)
|
||||
assert idx.freq == result.freq
|
||||
|
||||
idx_dup = idx.append(idx)
|
||||
assert idx_dup.freq is None # freq is reset
|
||||
result = idx_dup.drop_duplicates()
|
||||
tm.assert_index_equal(idx, result)
|
||||
assert result.freq is None
|
||||
|
||||
def test_drop_duplicates(self):
|
||||
# to check Index/Series compat
|
||||
base = pd.timedelta_range('1 day', '31 day', 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)))
|
||||
|
||||
@pytest.mark.parametrize('freq', ['D', '3D', '-3D',
|
||||
'H', '2H', '-2H',
|
||||
'T', '2T', 'S', '-3S'])
|
||||
def test_infer_freq(self, freq):
|
||||
# GH#11018
|
||||
idx = pd.timedelta_range('1', freq=freq, periods=10)
|
||||
result = pd.TimedeltaIndex(idx.asi8, freq='infer')
|
||||
tm.assert_index_equal(idx, result)
|
||||
assert result.freq == freq
|
||||
|
||||
def test_nat_new(self):
|
||||
|
||||
idx = pd.timedelta_range('1', freq='D', periods=5, name='x')
|
||||
result = idx._nat_new()
|
||||
exp = pd.TimedeltaIndex([pd.NaT] * 5, name='x')
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
result = idx._nat_new(box=False)
|
||||
exp = np.array([iNaT] * 5, dtype=np.int64)
|
||||
tm.assert_numpy_array_equal(result, exp)
|
||||
|
||||
def test_shift(self):
|
||||
pass # handled in test_arithmetic.py
|
||||
|
||||
def test_repeat(self):
|
||||
index = pd.timedelta_range('1 days', periods=2, freq='D')
|
||||
exp = pd.TimedeltaIndex(['1 days', '1 days', '2 days', '2 days'])
|
||||
for res in [index.repeat(2), np.repeat(index, 2)]:
|
||||
tm.assert_index_equal(res, exp)
|
||||
assert res.freq is None
|
||||
|
||||
index = TimedeltaIndex(['1 days', 'NaT', '3 days'])
|
||||
exp = TimedeltaIndex(['1 days', '1 days', '1 days',
|
||||
'NaT', 'NaT', 'NaT',
|
||||
'3 days', '3 days', '3 days'])
|
||||
for res in [index.repeat(3), np.repeat(index, 3)]:
|
||||
tm.assert_index_equal(res, exp)
|
||||
assert res.freq is None
|
||||
|
||||
def test_nat(self):
|
||||
assert pd.TimedeltaIndex._na_value is pd.NaT
|
||||
assert pd.TimedeltaIndex([])._na_value is pd.NaT
|
||||
|
||||
idx = pd.TimedeltaIndex(['1 days', '2 days'])
|
||||
assert idx._can_hold_na
|
||||
|
||||
tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
|
||||
assert not idx.hasnans
|
||||
tm.assert_numpy_array_equal(idx._nan_idxs,
|
||||
np.array([], dtype=np.intp))
|
||||
|
||||
idx = pd.TimedeltaIndex(['1 days', 'NaT'])
|
||||
assert idx._can_hold_na
|
||||
|
||||
tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
|
||||
assert idx.hasnans
|
||||
tm.assert_numpy_array_equal(idx._nan_idxs,
|
||||
np.array([1], dtype=np.intp))
|
||||
|
||||
def test_equals(self):
|
||||
# GH 13107
|
||||
idx = pd.TimedeltaIndex(['1 days', '2 days', 'NaT'])
|
||||
assert idx.equals(idx)
|
||||
assert idx.equals(idx.copy())
|
||||
assert idx.equals(idx.astype(object))
|
||||
assert idx.astype(object).equals(idx)
|
||||
assert idx.astype(object).equals(idx.astype(object))
|
||||
assert not idx.equals(list(idx))
|
||||
assert not idx.equals(pd.Series(idx))
|
||||
|
||||
idx2 = pd.TimedeltaIndex(['2 days', '1 days', 'NaT'])
|
||||
assert not idx.equals(idx2)
|
||||
assert not idx.equals(idx2.copy())
|
||||
assert not idx.equals(idx2.astype(object))
|
||||
assert not idx.astype(object).equals(idx2)
|
||||
assert not idx.astype(object).equals(idx2.astype(object))
|
||||
assert not idx.equals(list(idx2))
|
||||
assert not idx.equals(pd.Series(idx2))
|
||||
|
||||
@pytest.mark.parametrize('values', [['0 days', '2 days', '4 days'], []])
|
||||
@pytest.mark.parametrize('freq', ['2D', Day(2), '48H', Hour(48)])
|
||||
def test_freq_setter(self, values, freq):
|
||||
# GH 20678
|
||||
idx = TimedeltaIndex(values)
|
||||
|
||||
# can set to an offset, converting from string if necessary
|
||||
idx.freq = freq
|
||||
assert idx.freq == freq
|
||||
assert isinstance(idx.freq, ABCDateOffset)
|
||||
|
||||
# can reset to None
|
||||
idx.freq = None
|
||||
assert idx.freq is None
|
||||
|
||||
def test_freq_setter_errors(self):
|
||||
# GH 20678
|
||||
idx = TimedeltaIndex(['0 days', '2 days', '4 days'])
|
||||
|
||||
# setting with an incompatible freq
|
||||
msg = ('Inferred frequency 2D from passed values does not conform to '
|
||||
'passed frequency 5D')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
idx.freq = '5D'
|
||||
|
||||
# setting with a non-fixed frequency
|
||||
msg = '<2 \* BusinessDays> is a non-fixed frequency'
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
idx.freq = '2B'
|
||||
|
||||
# setting with non-freq string
|
||||
with tm.assert_raises_regex(ValueError, 'Invalid frequency'):
|
||||
idx.freq = 'foo'
|
||||
|
||||
|
||||
class TestTimedeltas(object):
|
||||
|
||||
def test_timedelta_ops(self):
|
||||
# GH4984
|
||||
# make sure ops return Timedelta
|
||||
s = Series([Timestamp('20130101') + timedelta(seconds=i * i)
|
||||
for i in range(10)])
|
||||
td = s.diff()
|
||||
|
||||
result = td.mean()
|
||||
expected = to_timedelta(timedelta(seconds=9))
|
||||
assert result == expected
|
||||
|
||||
result = td.to_frame().mean()
|
||||
assert result[0] == expected
|
||||
|
||||
result = td.quantile(.1)
|
||||
expected = Timedelta(np.timedelta64(2600, 'ms'))
|
||||
assert result == expected
|
||||
|
||||
result = td.median()
|
||||
expected = to_timedelta('00:00:09')
|
||||
assert result == expected
|
||||
|
||||
result = td.to_frame().median()
|
||||
assert result[0] == expected
|
||||
|
||||
# GH 6462
|
||||
# consistency in returned values for sum
|
||||
result = td.sum()
|
||||
expected = to_timedelta('00:01:21')
|
||||
assert result == expected
|
||||
|
||||
result = td.to_frame().sum()
|
||||
assert result[0] == expected
|
||||
|
||||
# std
|
||||
result = td.std()
|
||||
expected = to_timedelta(Series(td.dropna().values).std())
|
||||
assert result == expected
|
||||
|
||||
result = td.to_frame().std()
|
||||
assert result[0] == expected
|
||||
|
||||
# invalid ops
|
||||
for op in ['skew', 'kurt', 'sem', 'prod']:
|
||||
pytest.raises(TypeError, getattr(td, op))
|
||||
|
||||
# GH 10040
|
||||
# make sure NaT is properly handled by median()
|
||||
s = Series([Timestamp('2015-02-03'), Timestamp('2015-02-07')])
|
||||
assert s.diff().median() == timedelta(days=4)
|
||||
|
||||
s = Series([Timestamp('2015-02-03'), Timestamp('2015-02-07'),
|
||||
Timestamp('2015-02-15')])
|
||||
assert s.diff().median() == timedelta(days=6)
|
||||
-87
@@ -1,87 +0,0 @@
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
import pandas.util.testing as tm
|
||||
|
||||
import pandas as pd
|
||||
from pandas import Series, timedelta_range, Timedelta
|
||||
from pandas.util.testing import assert_series_equal
|
||||
|
||||
|
||||
class TestSlicing(object):
|
||||
def test_slice_keeps_name(self):
|
||||
# GH4226
|
||||
dr = pd.timedelta_range('1d', '5d', freq='H', name='timebucket')
|
||||
assert dr[1:].name == dr.name
|
||||
|
||||
def test_partial_slice(self):
|
||||
rng = timedelta_range('1 day 10:11:12', freq='h', periods=500)
|
||||
s = Series(np.arange(len(rng)), index=rng)
|
||||
|
||||
result = s['5 day':'6 day']
|
||||
expected = s.iloc[86:134]
|
||||
assert_series_equal(result, expected)
|
||||
|
||||
result = s['5 day':]
|
||||
expected = s.iloc[86:]
|
||||
assert_series_equal(result, expected)
|
||||
|
||||
result = s[:'6 day']
|
||||
expected = s.iloc[:134]
|
||||
assert_series_equal(result, expected)
|
||||
|
||||
result = s['6 days, 23:11:12']
|
||||
assert result == s.iloc[133]
|
||||
|
||||
pytest.raises(KeyError, s.__getitem__, '50 days')
|
||||
|
||||
def test_partial_slice_high_reso(self):
|
||||
|
||||
# higher reso
|
||||
rng = timedelta_range('1 day 10:11:12', freq='us', periods=2000)
|
||||
s = Series(np.arange(len(rng)), index=rng)
|
||||
|
||||
result = s['1 day 10:11:12':]
|
||||
expected = s.iloc[0:]
|
||||
assert_series_equal(result, expected)
|
||||
|
||||
result = s['1 day 10:11:12.001':]
|
||||
expected = s.iloc[1000:]
|
||||
assert_series_equal(result, expected)
|
||||
|
||||
result = s['1 days, 10:11:12.001001']
|
||||
assert result == s.iloc[1001]
|
||||
|
||||
def test_slice_with_negative_step(self):
|
||||
ts = Series(np.arange(20), timedelta_range('0', periods=20, freq='H'))
|
||||
SLC = pd.IndexSlice
|
||||
|
||||
def assert_slices_equivalent(l_slc, i_slc):
|
||||
assert_series_equal(ts[l_slc], ts.iloc[i_slc])
|
||||
assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])
|
||||
assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])
|
||||
|
||||
assert_slices_equivalent(SLC[Timedelta(hours=7)::-1], SLC[7::-1])
|
||||
assert_slices_equivalent(SLC['7 hours'::-1], SLC[7::-1])
|
||||
|
||||
assert_slices_equivalent(SLC[:Timedelta(hours=7):-1], SLC[:6:-1])
|
||||
assert_slices_equivalent(SLC[:'7 hours':-1], SLC[:6:-1])
|
||||
|
||||
assert_slices_equivalent(SLC['15 hours':'7 hours':-1], SLC[15:6:-1])
|
||||
assert_slices_equivalent(SLC[Timedelta(hours=15):Timedelta(hours=7):-
|
||||
1], SLC[15:6:-1])
|
||||
assert_slices_equivalent(SLC['15 hours':Timedelta(hours=7):-1],
|
||||
SLC[15:6:-1])
|
||||
assert_slices_equivalent(SLC[Timedelta(hours=15):'7 hours':-1],
|
||||
SLC[15:6:-1])
|
||||
|
||||
assert_slices_equivalent(SLC['7 hours':'15 hours':-1], SLC[:0])
|
||||
|
||||
def test_slice_with_zero_step_raises(self):
|
||||
ts = Series(np.arange(20), timedelta_range('0', periods=20, freq='H'))
|
||||
tm.assert_raises_regex(ValueError, 'slice step cannot be zero',
|
||||
lambda: ts[::0])
|
||||
tm.assert_raises_regex(ValueError, 'slice step cannot be zero',
|
||||
lambda: ts.loc[::0])
|
||||
tm.assert_raises_regex(ValueError, 'slice step cannot be zero',
|
||||
lambda: ts.loc[::0])
|
||||
-63
@@ -1,63 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Tests for TimedeltaIndex methods behaving like their Timedelta counterparts
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
from pandas import timedelta_range, Timedelta, TimedeltaIndex, Index, Series
|
||||
|
||||
|
||||
class TestVectorizedTimedelta(object):
|
||||
def test_tdi_total_seconds(self):
|
||||
# GH#10939
|
||||
# test index
|
||||
rng = timedelta_range('1 days, 10:11:12.100123456', periods=2,
|
||||
freq='s')
|
||||
expt = [1 * 86400 + 10 * 3600 + 11 * 60 + 12 + 100123456. / 1e9,
|
||||
1 * 86400 + 10 * 3600 + 11 * 60 + 13 + 100123456. / 1e9]
|
||||
tm.assert_almost_equal(rng.total_seconds(), Index(expt))
|
||||
|
||||
# test Series
|
||||
ser = Series(rng)
|
||||
s_expt = Series(expt, index=[0, 1])
|
||||
tm.assert_series_equal(ser.dt.total_seconds(), s_expt)
|
||||
|
||||
# with nat
|
||||
ser[1] = np.nan
|
||||
s_expt = Series([1 * 86400 + 10 * 3600 + 11 * 60 +
|
||||
12 + 100123456. / 1e9, np.nan], index=[0, 1])
|
||||
tm.assert_series_equal(ser.dt.total_seconds(), s_expt)
|
||||
|
||||
# with both nat
|
||||
ser = Series([np.nan, np.nan], dtype='timedelta64[ns]')
|
||||
tm.assert_series_equal(ser.dt.total_seconds(),
|
||||
Series([np.nan, np.nan], index=[0, 1]))
|
||||
|
||||
def test_tdi_round(self):
|
||||
td = pd.timedelta_range(start='16801 days', periods=5, freq='30Min')
|
||||
elt = td[1]
|
||||
|
||||
expected_rng = TimedeltaIndex([Timedelta('16801 days 00:00:00'),
|
||||
Timedelta('16801 days 00:00:00'),
|
||||
Timedelta('16801 days 01:00:00'),
|
||||
Timedelta('16801 days 02:00:00'),
|
||||
Timedelta('16801 days 02:00:00')])
|
||||
expected_elt = expected_rng[1]
|
||||
|
||||
tm.assert_index_equal(td.round(freq='H'), expected_rng)
|
||||
assert elt.round(freq='H') == expected_elt
|
||||
|
||||
msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
td.round(freq='foo')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
elt.round(freq='foo')
|
||||
|
||||
msg = "<MonthEnd> is a non-fixed frequency"
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
td.round(freq='M')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
elt.round(freq='M')
|
||||
-75
@@ -1,75 +0,0 @@
|
||||
import numpy as np
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
from pandas import TimedeltaIndex, timedelta_range, Int64Index
|
||||
|
||||
|
||||
class TestTimedeltaIndex(object):
|
||||
|
||||
def test_union(self):
|
||||
|
||||
i1 = timedelta_range('1day', periods=5)
|
||||
i2 = timedelta_range('3day', periods=5)
|
||||
result = i1.union(i2)
|
||||
expected = timedelta_range('1day', periods=7)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
i1 = Int64Index(np.arange(0, 20, 2))
|
||||
i2 = TimedeltaIndex(start='1 day', periods=10, freq='D')
|
||||
i1.union(i2) # Works
|
||||
i2.union(i1) # Fails with "AttributeError: can't set attribute"
|
||||
|
||||
def test_union_coverage(self):
|
||||
|
||||
idx = TimedeltaIndex(['3d', '1d', '2d'])
|
||||
ordered = TimedeltaIndex(idx.sort_values(), freq='infer')
|
||||
result = ordered.union(idx)
|
||||
tm.assert_index_equal(result, ordered)
|
||||
|
||||
result = ordered[:0].union(ordered)
|
||||
tm.assert_index_equal(result, ordered)
|
||||
assert result.freq == ordered.freq
|
||||
|
||||
def test_union_bug_1730(self):
|
||||
|
||||
rng_a = timedelta_range('1 day', periods=4, freq='3H')
|
||||
rng_b = timedelta_range('1 day', periods=4, freq='4H')
|
||||
|
||||
result = rng_a.union(rng_b)
|
||||
exp = TimedeltaIndex(sorted(set(list(rng_a)) | set(list(rng_b))))
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
def test_union_bug_1745(self):
|
||||
|
||||
left = TimedeltaIndex(['1 day 15:19:49.695000'])
|
||||
right = TimedeltaIndex(['2 day 13:04:21.322000',
|
||||
'1 day 15:27:24.873000',
|
||||
'1 day 15:31:05.350000'])
|
||||
|
||||
result = left.union(right)
|
||||
exp = TimedeltaIndex(sorted(set(list(left)) | set(list(right))))
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
def test_union_bug_4564(self):
|
||||
|
||||
left = timedelta_range("1 day", "30d")
|
||||
right = left + pd.offsets.Minute(15)
|
||||
|
||||
result = left.union(right)
|
||||
exp = TimedeltaIndex(sorted(set(list(left)) | set(list(right))))
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
def test_intersection_bug_1708(self):
|
||||
index_1 = timedelta_range('1 day', periods=4, freq='h')
|
||||
index_2 = index_1 + pd.offsets.Hour(5)
|
||||
|
||||
result = index_1 & index_2
|
||||
assert len(result) == 0
|
||||
|
||||
index_1 = timedelta_range('1 day', periods=4, freq='h')
|
||||
index_2 = index_1 + pd.offsets.Hour(1)
|
||||
|
||||
result = index_1 & index_2
|
||||
expected = timedelta_range('1 day 01:00:00', periods=3, freq='h')
|
||||
tm.assert_index_equal(result, expected)
|
||||
-309
@@ -1,309 +0,0 @@
|
||||
import warnings
|
||||
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
from datetime import timedelta
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
from pandas import (timedelta_range, date_range, Series, Timedelta,
|
||||
TimedeltaIndex, Index, DataFrame,
|
||||
Int64Index)
|
||||
from pandas.util.testing import (assert_almost_equal, assert_series_equal,
|
||||
assert_index_equal)
|
||||
|
||||
from ..datetimelike import DatetimeLike
|
||||
|
||||
randn = np.random.randn
|
||||
|
||||
|
||||
class TestTimedeltaIndex(DatetimeLike):
|
||||
_holder = TimedeltaIndex
|
||||
|
||||
def setup_method(self, method):
|
||||
self.indices = dict(index=tm.makeTimedeltaIndex(10))
|
||||
self.setup_indices()
|
||||
|
||||
def create_index(self):
|
||||
return pd.to_timedelta(range(5), unit='d') + pd.offsets.Hour(1)
|
||||
|
||||
def test_numeric_compat(self):
|
||||
# Dummy method to override super's version; this test is now done
|
||||
# in test_arithmetic.py
|
||||
pass
|
||||
|
||||
def test_shift(self):
|
||||
pass # this is handled in test_arithmetic.py
|
||||
|
||||
def test_pickle_compat_construction(self):
|
||||
pass
|
||||
|
||||
def test_fillna_timedelta(self):
|
||||
# GH 11343
|
||||
idx = pd.TimedeltaIndex(['1 day', pd.NaT, '3 day'])
|
||||
|
||||
exp = pd.TimedeltaIndex(['1 day', '2 day', '3 day'])
|
||||
tm.assert_index_equal(idx.fillna(pd.Timedelta('2 day')), exp)
|
||||
|
||||
exp = pd.TimedeltaIndex(['1 day', '3 hour', '3 day'])
|
||||
idx.fillna(pd.Timedelta('3 hour'))
|
||||
|
||||
exp = pd.Index(
|
||||
[pd.Timedelta('1 day'), 'x', pd.Timedelta('3 day')], dtype=object)
|
||||
tm.assert_index_equal(idx.fillna('x'), exp)
|
||||
|
||||
def test_difference_freq(self):
|
||||
# GH14323: Difference of TimedeltaIndex should not preserve frequency
|
||||
|
||||
index = timedelta_range("0 days", "5 days", freq="D")
|
||||
|
||||
other = timedelta_range("1 days", "4 days", freq="D")
|
||||
expected = TimedeltaIndex(["0 days", "5 days"], freq=None)
|
||||
idx_diff = index.difference(other)
|
||||
tm.assert_index_equal(idx_diff, expected)
|
||||
tm.assert_attr_equal('freq', idx_diff, expected)
|
||||
|
||||
other = timedelta_range("2 days", "5 days", freq="D")
|
||||
idx_diff = index.difference(other)
|
||||
expected = TimedeltaIndex(["0 days", "1 days"], freq=None)
|
||||
tm.assert_index_equal(idx_diff, expected)
|
||||
tm.assert_attr_equal('freq', idx_diff, expected)
|
||||
|
||||
def test_isin(self):
|
||||
|
||||
index = tm.makeTimedeltaIndex(4)
|
||||
result = index.isin(index)
|
||||
assert result.all()
|
||||
|
||||
result = index.isin(list(index))
|
||||
assert result.all()
|
||||
|
||||
assert_almost_equal(index.isin([index[2], 5]),
|
||||
np.array([False, False, True, False]))
|
||||
|
||||
def test_factorize(self):
|
||||
idx1 = TimedeltaIndex(['1 day', '1 day', '2 day', '2 day', '3 day',
|
||||
'3 day'])
|
||||
|
||||
exp_arr = np.array([0, 0, 1, 1, 2, 2], dtype=np.intp)
|
||||
exp_idx = TimedeltaIndex(['1 day', '2 day', '3 day'])
|
||||
|
||||
arr, idx = idx1.factorize()
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, exp_idx)
|
||||
|
||||
arr, idx = idx1.factorize(sort=True)
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, exp_idx)
|
||||
|
||||
# freq must be preserved
|
||||
idx3 = timedelta_range('1 day', periods=4, freq='s')
|
||||
exp_arr = np.array([0, 1, 2, 3], dtype=np.intp)
|
||||
arr, idx = idx3.factorize()
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, idx3)
|
||||
|
||||
def test_join_self(self, join_type):
|
||||
index = timedelta_range('1 day', periods=10)
|
||||
joined = index.join(index, how=join_type)
|
||||
tm.assert_index_equal(index, joined)
|
||||
|
||||
def test_does_not_convert_mixed_integer(self):
|
||||
df = tm.makeCustomDataframe(10, 10,
|
||||
data_gen_f=lambda *args, **kwargs: randn(),
|
||||
r_idx_type='i', c_idx_type='td')
|
||||
str(df)
|
||||
|
||||
cols = df.columns.join(df.index, how='outer')
|
||||
joined = cols.join(df.columns)
|
||||
assert cols.dtype == np.dtype('O')
|
||||
assert cols.dtype == joined.dtype
|
||||
tm.assert_index_equal(cols, joined)
|
||||
|
||||
def test_sort_values(self):
|
||||
|
||||
idx = TimedeltaIndex(['4d', '1d', '2d'])
|
||||
|
||||
ordered = idx.sort_values()
|
||||
assert ordered.is_monotonic
|
||||
|
||||
ordered = idx.sort_values(ascending=False)
|
||||
assert ordered[::-1].is_monotonic
|
||||
|
||||
ordered, dexer = idx.sort_values(return_indexer=True)
|
||||
assert ordered.is_monotonic
|
||||
|
||||
tm.assert_numpy_array_equal(dexer, np.array([1, 2, 0]),
|
||||
check_dtype=False)
|
||||
|
||||
ordered, dexer = idx.sort_values(return_indexer=True, ascending=False)
|
||||
assert ordered[::-1].is_monotonic
|
||||
|
||||
tm.assert_numpy_array_equal(dexer, np.array([0, 2, 1]),
|
||||
check_dtype=False)
|
||||
|
||||
def test_get_duplicates(self):
|
||||
idx = TimedeltaIndex(['1 day', '2 day', '2 day', '3 day', '3day',
|
||||
'4day'])
|
||||
|
||||
with warnings.catch_warnings(record=True):
|
||||
# Deprecated - see GH20239
|
||||
result = idx.get_duplicates()
|
||||
|
||||
ex = TimedeltaIndex(['2 day', '3day'])
|
||||
tm.assert_index_equal(result, ex)
|
||||
|
||||
def test_argmin_argmax(self):
|
||||
idx = TimedeltaIndex(['1 day 00:00:05', '1 day 00:00:01',
|
||||
'1 day 00:00:02'])
|
||||
assert idx.argmin() == 1
|
||||
assert idx.argmax() == 0
|
||||
|
||||
def test_misc_coverage(self):
|
||||
|
||||
rng = timedelta_range('1 day', periods=5)
|
||||
result = rng.groupby(rng.days)
|
||||
assert isinstance(list(result.values())[0][0], Timedelta)
|
||||
|
||||
idx = TimedeltaIndex(['3d', '1d', '2d'])
|
||||
assert not idx.equals(list(idx))
|
||||
|
||||
non_td = Index(list('abc'))
|
||||
assert not idx.equals(list(non_td))
|
||||
|
||||
def test_map(self):
|
||||
# test_map_dictlike generally tests
|
||||
|
||||
rng = timedelta_range('1 day', periods=10)
|
||||
|
||||
f = lambda x: x.days
|
||||
result = rng.map(f)
|
||||
exp = Int64Index([f(x) for x in rng])
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
def test_pass_TimedeltaIndex_to_index(self):
|
||||
|
||||
rng = timedelta_range('1 days', '10 days')
|
||||
idx = Index(rng, dtype=object)
|
||||
|
||||
expected = Index(rng.to_pytimedelta(), dtype=object)
|
||||
|
||||
tm.assert_numpy_array_equal(idx.values, expected.values)
|
||||
|
||||
def test_pickle(self):
|
||||
|
||||
rng = timedelta_range('1 days', periods=10)
|
||||
rng_p = tm.round_trip_pickle(rng)
|
||||
tm.assert_index_equal(rng, rng_p)
|
||||
|
||||
def test_hash_error(self):
|
||||
index = timedelta_range('1 days', periods=10)
|
||||
with tm.assert_raises_regex(TypeError, "unhashable type: %r" %
|
||||
type(index).__name__):
|
||||
hash(index)
|
||||
|
||||
def test_append_join_nondatetimeindex(self):
|
||||
rng = timedelta_range('1 days', periods=10)
|
||||
idx = Index(['a', 'b', 'c', 'd'])
|
||||
|
||||
result = rng.append(idx)
|
||||
assert isinstance(result[0], Timedelta)
|
||||
|
||||
# it works
|
||||
rng.join(idx, how='outer')
|
||||
|
||||
def test_append_numpy_bug_1681(self):
|
||||
|
||||
td = timedelta_range('1 days', '10 days', freq='2D')
|
||||
a = DataFrame()
|
||||
c = DataFrame({'A': 'foo', 'B': td}, index=td)
|
||||
str(c)
|
||||
|
||||
result = a.append(c)
|
||||
assert (result['B'] == td).all()
|
||||
|
||||
def test_fields(self):
|
||||
rng = timedelta_range('1 days, 10:11:12.100123456', periods=2,
|
||||
freq='s')
|
||||
tm.assert_index_equal(rng.days, Index([1, 1], dtype='int64'))
|
||||
tm.assert_index_equal(
|
||||
rng.seconds,
|
||||
Index([10 * 3600 + 11 * 60 + 12, 10 * 3600 + 11 * 60 + 13],
|
||||
dtype='int64'))
|
||||
tm.assert_index_equal(
|
||||
rng.microseconds,
|
||||
Index([100 * 1000 + 123, 100 * 1000 + 123], dtype='int64'))
|
||||
tm.assert_index_equal(rng.nanoseconds,
|
||||
Index([456, 456], dtype='int64'))
|
||||
|
||||
pytest.raises(AttributeError, lambda: rng.hours)
|
||||
pytest.raises(AttributeError, lambda: rng.minutes)
|
||||
pytest.raises(AttributeError, lambda: rng.milliseconds)
|
||||
|
||||
# with nat
|
||||
s = Series(rng)
|
||||
s[1] = np.nan
|
||||
|
||||
tm.assert_series_equal(s.dt.days, Series([1, np.nan], index=[0, 1]))
|
||||
tm.assert_series_equal(s.dt.seconds, Series(
|
||||
[10 * 3600 + 11 * 60 + 12, np.nan], index=[0, 1]))
|
||||
|
||||
# preserve name (GH15589)
|
||||
rng.name = 'name'
|
||||
assert rng.days.name == 'name'
|
||||
|
||||
def test_freq_conversion(self):
|
||||
|
||||
# doc example
|
||||
|
||||
# series
|
||||
td = Series(date_range('20130101', periods=4)) - \
|
||||
Series(date_range('20121201', periods=4))
|
||||
td[2] += timedelta(minutes=5, seconds=3)
|
||||
td[3] = np.nan
|
||||
|
||||
result = td / np.timedelta64(1, 'D')
|
||||
expected = Series([31, 31, (31 * 86400 + 5 * 60 + 3) / 86400.0, np.nan
|
||||
])
|
||||
assert_series_equal(result, expected)
|
||||
|
||||
result = td.astype('timedelta64[D]')
|
||||
expected = Series([31, 31, 31, np.nan])
|
||||
assert_series_equal(result, expected)
|
||||
|
||||
result = td / np.timedelta64(1, 's')
|
||||
expected = Series([31 * 86400, 31 * 86400, 31 * 86400 + 5 * 60 + 3,
|
||||
np.nan])
|
||||
assert_series_equal(result, expected)
|
||||
|
||||
result = td.astype('timedelta64[s]')
|
||||
assert_series_equal(result, expected)
|
||||
|
||||
# tdi
|
||||
td = TimedeltaIndex(td)
|
||||
|
||||
result = td / np.timedelta64(1, 'D')
|
||||
expected = Index([31, 31, (31 * 86400 + 5 * 60 + 3) / 86400.0, np.nan])
|
||||
assert_index_equal(result, expected)
|
||||
|
||||
result = td.astype('timedelta64[D]')
|
||||
expected = Index([31, 31, 31, np.nan])
|
||||
assert_index_equal(result, expected)
|
||||
|
||||
result = td / np.timedelta64(1, 's')
|
||||
expected = Index([31 * 86400, 31 * 86400, 31 * 86400 + 5 * 60 + 3,
|
||||
np.nan])
|
||||
assert_index_equal(result, expected)
|
||||
|
||||
result = td.astype('timedelta64[s]')
|
||||
assert_index_equal(result, expected)
|
||||
|
||||
|
||||
class TestTimeSeries(object):
|
||||
|
||||
def test_series_box_timedelta(self):
|
||||
rng = timedelta_range('1 day 1 s', periods=5, freq='h')
|
||||
s = Series(rng)
|
||||
assert isinstance(s[1], Timedelta)
|
||||
assert isinstance(s.iat[2], Timedelta)
|
||||
-77
@@ -1,77 +0,0 @@
|
||||
import pytest
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
from pandas.tseries.offsets import Day, Second
|
||||
from pandas import to_timedelta, timedelta_range
|
||||
|
||||
|
||||
class TestTimedeltas(object):
|
||||
|
||||
def test_timedelta_range(self):
|
||||
|
||||
expected = to_timedelta(np.arange(5), unit='D')
|
||||
result = timedelta_range('0 days', periods=5, freq='D')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
expected = to_timedelta(np.arange(11), unit='D')
|
||||
result = timedelta_range('0 days', '10 days', freq='D')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
expected = to_timedelta(np.arange(5), unit='D') + Second(2) + Day()
|
||||
result = timedelta_range('1 days, 00:00:02', '5 days, 00:00:02',
|
||||
freq='D')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
expected = to_timedelta([1, 3, 5, 7, 9], unit='D') + Second(2)
|
||||
result = timedelta_range('1 days, 00:00:02', periods=5, freq='2D')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
expected = to_timedelta(np.arange(50), unit='T') * 30
|
||||
result = timedelta_range('0 days', freq='30T', periods=50)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# GH 11776
|
||||
arr = np.arange(10).reshape(2, 5)
|
||||
df = pd.DataFrame(np.arange(10).reshape(2, 5))
|
||||
for arg in (arr, df):
|
||||
with tm.assert_raises_regex(TypeError, "1-d array"):
|
||||
to_timedelta(arg)
|
||||
for errors in ['ignore', 'raise', 'coerce']:
|
||||
with tm.assert_raises_regex(TypeError, "1-d array"):
|
||||
to_timedelta(arg, errors=errors)
|
||||
|
||||
# issue10583
|
||||
df = pd.DataFrame(np.random.normal(size=(10, 4)))
|
||||
df.index = pd.timedelta_range(start='0s', periods=10, freq='s')
|
||||
expected = df.loc[pd.Timedelta('0s'):, :]
|
||||
result = df.loc['0s':, :]
|
||||
tm.assert_frame_equal(expected, result)
|
||||
|
||||
@pytest.mark.parametrize('periods, freq', [
|
||||
(3, '2D'), (5, 'D'), (6, '19H12T'), (7, '16H'), (9, '12H')])
|
||||
def test_linspace_behavior(self, periods, freq):
|
||||
# GH 20976
|
||||
result = timedelta_range(start='0 days', end='4 days', periods=periods)
|
||||
expected = timedelta_range(start='0 days', end='4 days', freq=freq)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_errors(self):
|
||||
# not enough params
|
||||
msg = ('Of the four parameters: start, end, periods, and freq, '
|
||||
'exactly three must be specified')
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
timedelta_range(start='0 days')
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
timedelta_range(end='5 days')
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
timedelta_range(periods=2)
|
||||
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
timedelta_range()
|
||||
|
||||
# too many params
|
||||
with tm.assert_raises_regex(ValueError, msg):
|
||||
timedelta_range(start='0 days', end='5 days', periods=10, freq='H')
|
||||
-174
@@ -1,174 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from datetime import time, timedelta
|
||||
import numpy as np
|
||||
|
||||
import pandas as pd
|
||||
import pandas.util.testing as tm
|
||||
from pandas.util.testing import assert_series_equal
|
||||
from pandas import Series, to_timedelta, isna, TimedeltaIndex
|
||||
from pandas._libs.tslib import iNaT
|
||||
|
||||
|
||||
class TestTimedeltas(object):
|
||||
|
||||
def test_to_timedelta(self):
|
||||
def conv(v):
|
||||
return v.astype('m8[ns]')
|
||||
|
||||
d1 = np.timedelta64(1, 'D')
|
||||
|
||||
assert (to_timedelta('1 days 06:05:01.00003', box=False) ==
|
||||
conv(d1 + np.timedelta64(6 * 3600 + 5 * 60 + 1, 's') +
|
||||
np.timedelta64(30, 'us')))
|
||||
assert (to_timedelta('15.5us', box=False) ==
|
||||
conv(np.timedelta64(15500, 'ns')))
|
||||
|
||||
# empty string
|
||||
result = to_timedelta('', box=False)
|
||||
assert result.astype('int64') == iNaT
|
||||
|
||||
result = to_timedelta(['', ''])
|
||||
assert isna(result).all()
|
||||
|
||||
# pass thru
|
||||
result = to_timedelta(np.array([np.timedelta64(1, 's')]))
|
||||
expected = pd.Index(np.array([np.timedelta64(1, 's')]))
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# ints
|
||||
result = np.timedelta64(0, 'ns')
|
||||
expected = to_timedelta(0, box=False)
|
||||
assert result == expected
|
||||
|
||||
# Series
|
||||
expected = Series([timedelta(days=1), timedelta(days=1, seconds=1)])
|
||||
result = to_timedelta(Series(['1d', '1days 00:00:01']))
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
# with units
|
||||
result = TimedeltaIndex([np.timedelta64(0, 'ns'), np.timedelta64(
|
||||
10, 's').astype('m8[ns]')])
|
||||
expected = to_timedelta([0, 10], unit='s')
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# single element conversion
|
||||
v = timedelta(seconds=1)
|
||||
result = to_timedelta(v, box=False)
|
||||
expected = np.timedelta64(timedelta(seconds=1))
|
||||
assert result == expected
|
||||
|
||||
v = np.timedelta64(timedelta(seconds=1))
|
||||
result = to_timedelta(v, box=False)
|
||||
expected = np.timedelta64(timedelta(seconds=1))
|
||||
assert result == expected
|
||||
|
||||
# arrays of various dtypes
|
||||
arr = np.array([1] * 5, dtype='int64')
|
||||
result = to_timedelta(arr, unit='s')
|
||||
expected = TimedeltaIndex([np.timedelta64(1, 's')] * 5)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
arr = np.array([1] * 5, dtype='int64')
|
||||
result = to_timedelta(arr, unit='m')
|
||||
expected = TimedeltaIndex([np.timedelta64(1, 'm')] * 5)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
arr = np.array([1] * 5, dtype='int64')
|
||||
result = to_timedelta(arr, unit='h')
|
||||
expected = TimedeltaIndex([np.timedelta64(1, 'h')] * 5)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
arr = np.array([1] * 5, dtype='timedelta64[s]')
|
||||
result = to_timedelta(arr)
|
||||
expected = TimedeltaIndex([np.timedelta64(1, 's')] * 5)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
arr = np.array([1] * 5, dtype='timedelta64[D]')
|
||||
result = to_timedelta(arr)
|
||||
expected = TimedeltaIndex([np.timedelta64(1, 'D')] * 5)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# Test with lists as input when box=false
|
||||
expected = np.array(np.arange(3) * 1000000000, dtype='timedelta64[ns]')
|
||||
result = to_timedelta(range(3), unit='s', box=False)
|
||||
tm.assert_numpy_array_equal(expected, result)
|
||||
|
||||
result = to_timedelta(np.arange(3), unit='s', box=False)
|
||||
tm.assert_numpy_array_equal(expected, result)
|
||||
|
||||
result = to_timedelta([0, 1, 2], unit='s', box=False)
|
||||
tm.assert_numpy_array_equal(expected, result)
|
||||
|
||||
# Tests with fractional seconds as input:
|
||||
expected = np.array(
|
||||
[0, 500000000, 800000000, 1200000000], dtype='timedelta64[ns]')
|
||||
result = to_timedelta([0., 0.5, 0.8, 1.2], unit='s', box=False)
|
||||
tm.assert_numpy_array_equal(expected, result)
|
||||
|
||||
def test_to_timedelta_invalid(self):
|
||||
|
||||
# bad value for errors parameter
|
||||
msg = "errors must be one of"
|
||||
tm.assert_raises_regex(ValueError, msg, to_timedelta,
|
||||
['foo'], errors='never')
|
||||
|
||||
# these will error
|
||||
pytest.raises(ValueError, lambda: to_timedelta([1, 2], unit='foo'))
|
||||
pytest.raises(ValueError, lambda: to_timedelta(1, unit='foo'))
|
||||
|
||||
# time not supported ATM
|
||||
pytest.raises(ValueError, lambda: to_timedelta(time(second=1)))
|
||||
assert to_timedelta(time(second=1), errors='coerce') is pd.NaT
|
||||
|
||||
pytest.raises(ValueError, lambda: to_timedelta(['foo', 'bar']))
|
||||
tm.assert_index_equal(TimedeltaIndex([pd.NaT, pd.NaT]),
|
||||
to_timedelta(['foo', 'bar'], errors='coerce'))
|
||||
|
||||
tm.assert_index_equal(TimedeltaIndex(['1 day', pd.NaT, '1 min']),
|
||||
to_timedelta(['1 day', 'bar', '1 min'],
|
||||
errors='coerce'))
|
||||
|
||||
# gh-13613: these should not error because errors='ignore'
|
||||
invalid_data = 'apple'
|
||||
assert invalid_data == to_timedelta(invalid_data, errors='ignore')
|
||||
|
||||
invalid_data = ['apple', '1 days']
|
||||
tm.assert_numpy_array_equal(
|
||||
np.array(invalid_data, dtype=object),
|
||||
to_timedelta(invalid_data, errors='ignore'))
|
||||
|
||||
invalid_data = pd.Index(['apple', '1 days'])
|
||||
tm.assert_index_equal(invalid_data, to_timedelta(
|
||||
invalid_data, errors='ignore'))
|
||||
|
||||
invalid_data = Series(['apple', '1 days'])
|
||||
tm.assert_series_equal(invalid_data, to_timedelta(
|
||||
invalid_data, errors='ignore'))
|
||||
|
||||
def test_to_timedelta_via_apply(self):
|
||||
# GH 5458
|
||||
expected = Series([np.timedelta64(1, 's')])
|
||||
result = Series(['00:00:01']).apply(to_timedelta)
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
result = Series([to_timedelta('00:00:01')])
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
def test_to_timedelta_on_missing_values(self):
|
||||
# GH5438
|
||||
timedelta_NaT = np.timedelta64('NaT')
|
||||
|
||||
actual = pd.to_timedelta(Series(['00:00:01', np.nan]))
|
||||
expected = Series([np.timedelta64(1000000000, 'ns'),
|
||||
timedelta_NaT], dtype='<m8[ns]')
|
||||
assert_series_equal(actual, expected)
|
||||
|
||||
actual = pd.to_timedelta(Series(['00:00:01', pd.NaT]))
|
||||
assert_series_equal(actual, expected)
|
||||
|
||||
actual = pd.to_timedelta(np.nan)
|
||||
assert actual.value == timedelta_NaT.astype('int64')
|
||||
|
||||
actual = pd.to_timedelta(pd.NaT)
|
||||
assert actual.value == timedelta_NaT.astype('int64')
|
||||
Reference in New Issue
Block a user