class Take(object): params = ['int', 'datetime'] param_names = ['index'] def setup(self, index): N = 100000 indexes = {'int': Int64Index(np.arange(N)), 'datetime': date_range('2011-01-01', freq='S', periods=N)} index = indexes[index] self.s = Series(np.random.rand(N), index=index) self.indexer = [True, False, True, True, False] * 20000 def time_take(self, index): self.s.take(self.indexer)
class Take: params = ['int', 'datetime'] param_names = ['index'] def setup(self, index): N = 100000 indexes = {'int': Int64Index(np.arange(N)), 'datetime': date_range('2011-01-01', freq='S', periods=N)} index = indexes[index] self.s = Series(np.random.rand(N), index=index) self.indexer = [True, False, True, True, False] * 20000 def time_take(self, index): self.s.take(self.indexer)
def test_depr_take_kwarg_is_copy(self, is_copy): # GH 27357 df = DataFrame({"A": [1, 2, 3]}) msg = ( "is_copy is deprecated and will be removed in a future version. " "'take' always returns a copy, so there is no need to specify this." ) with tm.assert_produces_warning(FutureWarning) as w: df.take([0, 1], is_copy=is_copy) assert w[0].message.args[0] == msg s = Series([1, 2, 3]) with tm.assert_produces_warning(FutureWarning): s.take([0, 1], is_copy=is_copy)
def test_take(): s = Series([-1, 5, 6, 2, 4]) actual = s.take([1, 3, 4]) expected = Series([5, 2, 4], index=[1, 3, 4]) tm.assert_series_equal(actual, expected) actual = s.take([-1, 3, 4]) expected = Series([4, 2, 4], index=[4, 3, 4]) tm.assert_series_equal(actual, expected) pytest.raises(IndexError, s.take, [1, 10]) pytest.raises(IndexError, s.take, [2, 5]) with tm.assert_produces_warning(FutureWarning): s.take([-1, 3, 4], convert=False)
def test_take(self): s = Series([-1, 5, 6, 2, 4]) actual = s.take([1, 3, 4]) expected = Series([5, 2, 4], index=[1, 3, 4]) tm.assert_series_equal(actual, expected) actual = s.take([-1, 3, 4]) expected = Series([4, 2, 4], index=[4, 3, 4]) tm.assert_series_equal(actual, expected) pytest.raises(IndexError, s.take, [1, 10]) pytest.raises(IndexError, s.take, [2, 5]) with tm.assert_produces_warning(FutureWarning): s.take([-1, 3, 4], convert=False)
def test_take_categorical(): # https://github.com/pandas-dev/pandas/issues/20664 s = Series(pd.Categorical(["a", "b", "c"])) result = s.take([-2, -2, 0]) expected = Series(pd.Categorical(["b", "b", "a"], categories=["a", "b", "c"]), index=[1, 1, 0]) tm.assert_series_equal(result, expected)
def test_take_categorical(): # https://github.com/pandas-dev/pandas/issues/20664 s = Series(pd.Categorical(['a', 'b', 'c'])) result = s.take([-2, -2, 0]) expected = Series(pd.Categorical(['b', 'b', 'a'], categories=['a', 'b', 'c']), index=[1, 1, 0]) assert_series_equal(result, expected)
class Take: params = ["int", "datetime"] param_names = ["index"] def setup(self, index): N = 100000 indexes = { "int": Int64Index(np.arange(N)), "datetime": date_range("2011-01-01", freq="S", periods=N), } index = indexes[index] self.s = Series(np.random.rand(N), index=index) self.indexer = [True, False, True, True, False] * 20000 def time_take(self, index): self.s.take(self.indexer)
def test_resample_not_monotonic(self): rng = pd.date_range('2012-06-12', periods=200, freq='h') ts = Series(np.random.randn(len(rng)), index=rng) ts = ts.take(np.random.permutation(len(ts))) result = ts.resample('D', how='sum') exp = ts.sort_index().resample('D', how='sum') assert_series_equal(result, exp)
def test_resample_not_monotonic(): rng = pd.date_range("2012-06-12", periods=200, freq="h") ts = Series(np.random.randn(len(rng)), index=rng) ts = ts.take(np.random.permutation(len(ts))) result = ts.resample("D").sum() exp = ts.sort_index().resample("D").sum() tm.assert_series_equal(result, exp)
def test_getitem_slice_date(self, slc, positions): # https://github.com/pandas-dev/pandas/issues/31501 ser = Series( [0, 1, 2], DatetimeIndex(["2019-01-01", "2019-01-01T06:00:00", "2019-01-02"]), ) result = ser[slc] expected = ser.take(positions) tm.assert_series_equal(result, expected)
def test_indexing_unordered2(): # diff freq rng = date_range(datetime(2005, 1, 1), periods=20, freq="M") ts = Series(np.arange(len(rng)), index=rng) ts = ts.take(np.random.permutation(20)) result = ts["2005"] for t in result.index: assert t.year == 2005
def test_take(): s = Series([-1, 5, 6, 2, 4]) actual = s.take([1, 3, 4]) expected = Series([5, 2, 4], index=[1, 3, 4]) tm.assert_series_equal(actual, expected) actual = s.take([-1, 3, 4]) expected = Series([4, 2, 4], index=[4, 3, 4]) tm.assert_series_equal(actual, expected) msg = "index {} is out of bounds for size 5" with pytest.raises(IndexError, match=msg.format(10)): s.take([1, 10]) with pytest.raises(IndexError, match=msg.format(5)): s.take([2, 5]) with tm.assert_produces_warning(FutureWarning): s.take([-1, 3, 4], convert=False)
def test_indexing_unordered(): # GH 2437 rng = date_range(start="2011-01-01", end="2011-01-15") ts = Series(np.random.rand(len(rng)), index=rng) ts2 = pd.concat([ts[0:4], ts[-4:], ts[4:-4]]) for t in ts.index: # TODO: unused? s = str(t) # noqa expected = ts[t] result = ts2[t] assert expected == result # GH 3448 (ranges) def compare(slobj): result = ts2[slobj].copy() result = result.sort_index() expected = ts[slobj] expected.index = expected.index._with_freq(None) tm.assert_series_equal(result, expected) compare(slice("2011-01-01", "2011-01-15")) compare(slice("2010-12-30", "2011-01-15")) compare(slice("2011-01-01", "2011-01-16")) # partial ranges compare(slice("2011-01-01", "2011-01-6")) compare(slice("2011-01-06", "2011-01-8")) compare(slice("2011-01-06", "2011-01-12")) # single values result = ts2["2011"].sort_index() expected = ts["2011"] expected.index = expected.index._with_freq(None) tm.assert_series_equal(result, expected) # diff freq rng = date_range(datetime(2005, 1, 1), periods=20, freq="M") ts = Series(np.arange(len(rng)), index=rng) ts = ts.take(np.random.permutation(20)) result = ts["2005"] for t in result.index: assert t.year == 2005
def test_indexing_unordered(): # GH 2437 rng = date_range(start='2011-01-01', end='2011-01-15') ts = Series(np.random.rand(len(rng)), index=rng) ts2 = pd.concat([ts[0:4], ts[-4:], ts[4:-4]]) for t in ts.index: # TODO: unused? s = str(t) # noqa expected = ts[t] result = ts2[t] assert expected == result # GH 3448 (ranges) def compare(slobj): result = ts2[slobj].copy() result = result.sort_index() expected = ts[slobj] assert_series_equal(result, expected) compare(slice('2011-01-01', '2011-01-15')) compare(slice('2010-12-30', '2011-01-15')) compare(slice('2011-01-01', '2011-01-16')) # partial ranges compare(slice('2011-01-01', '2011-01-6')) compare(slice('2011-01-06', '2011-01-8')) compare(slice('2011-01-06', '2011-01-12')) # single values result = ts2['2011'].sort_index() expected = ts['2011'] assert_series_equal(result, expected) # diff freq rng = date_range(datetime(2005, 1, 1), periods=20, freq='M') ts = Series(np.arange(len(rng)), index=rng) ts = ts.take(np.random.permutation(20)) result = ts['2005'] for t in result.index: assert t.year == 2005
def test_indexing_unordered(self): # GH 2437 rng = date_range(start='2011-01-01', end='2011-01-15') ts = Series(np.random.rand(len(rng)), index=rng) ts2 = pd.concat([ts[0:4], ts[-4:], ts[4:-4]]) for t in ts.index: # TODO: unused? s = str(t) # noqa expected = ts[t] result = ts2[t] assert expected == result # GH 3448 (ranges) def compare(slobj): result = ts2[slobj].copy() result = result.sort_index() expected = ts[slobj] assert_series_equal(result, expected) compare(slice('2011-01-01', '2011-01-15')) compare(slice('2010-12-30', '2011-01-15')) compare(slice('2011-01-01', '2011-01-16')) # partial ranges compare(slice('2011-01-01', '2011-01-6')) compare(slice('2011-01-06', '2011-01-8')) compare(slice('2011-01-06', '2011-01-12')) # single values result = ts2['2011'].sort_index() expected = ts['2011'] assert_series_equal(result, expected) # diff freq rng = date_range(datetime(2005, 1, 1), periods=20, freq='M') ts = Series(np.arange(len(rng)), index=rng) ts = ts.take(np.random.permutation(20)) result = ts['2005'] for t in result.index: assert t.year == 2005
def test_legacy_time_rule_arg(self): # suppress deprecation warnings sys.stderr = StringIO() rng = bdate_range('1/1/2000', periods=20) ts = Series(np.random.randn(20), index=rng) ts = ts.take(np.random.permutation(len(ts))[:12]).sort_index() try: result = mom.rolling_mean(ts, 1, min_periods=1, freq='B') expected = mom.rolling_mean(ts, 1, min_periods=1, time_rule='WEEKDAY') tm.assert_series_equal(result, expected) result = mom.ewma(ts, span=5, freq='B') expected = mom.ewma(ts, span=5, time_rule='WEEKDAY') tm.assert_series_equal(result, expected) finally: sys.stderr = sys.__stderr__
def test_take(): s = Series([-1, 5, 6, 2, 4]) actual = s.take([1, 3, 4]) expected = Series([5, 2, 4], index=[1, 3, 4]) tm.assert_series_equal(actual, expected) actual = s.take([-1, 3, 4]) expected = Series([4, 2, 4], index=[4, 3, 4]) tm.assert_series_equal(actual, expected) msg = "index {} is out of bounds for( axis 0 with)? size 5" with pytest.raises(IndexError, match=msg.format(10)): s.take([1, 10]) with pytest.raises(IndexError, match=msg.format(5)): s.take([2, 5])
def test_partial_not_monotonic(self): rng = date_range(datetime(2005, 1, 1), periods=20, freq="M") ts = Series(np.arange(len(rng)), index=rng) ts = ts.take(np.random.permutation(20)) self.assertRaises(Exception, ts.__getitem__, "2005")
# In[296]: rng[0] # In[297]: rng[1] ##### observe how the date is increasing step by step ##### Notice that the offset is D so the data is increasing day by day # In[298]: ts2 = ts.take(np.random.permutation(len(ts))) # In[299]: ts2 # In[300]: ts2.sort_index() ##### sort_index returns a time series sorted by date # In[301]: ts['2012-7-26']
rng[0] # In[297]: rng[1] ##### observe how the date is increasing step by step ##### Notice that the offset is D so the data is increasing day by day # In[298]: ts2=ts.take(np.random.permutation(len(ts))) # In[299]: ts2 # In[300]: ts2.sort_index() ##### sort_index returns a time series sorted by date # In[301]: