def setup_method(self, method): super().setup_method(method) self.d = { "string": tm.makeStringIndex(100), "date": tm.makeDateIndex(100), "int": tm.makeIntIndex(100), "rng": tm.makeRangeIndex(100), "float": tm.makeFloatIndex(100), "empty": Index([]), "tuple": Index(zip(["foo", "bar", "baz"], [1, 2, 3])), "period": Index(period_range("2012-1-1", freq="M", periods=3)), "date2": Index(date_range("2013-01-1", periods=10)), "bdate": Index(bdate_range("2013-01-02", periods=10)), "cat": tm.makeCategoricalIndex(100), "interval": tm.makeIntervalIndex(100), "timedelta": tm.makeTimedeltaIndex(100, "H"), } self.mi = { "reg": MultiIndex.from_tuples( [ ("bar", "one"), ("baz", "two"), ("foo", "two"), ("qux", "one"), ("qux", "two"), ], names=["first", "second"], ) }
def setup_method(self, method): super(TestIndex, self).setup_method(method) self.d = { 'string': tm.makeStringIndex(100), 'date': tm.makeDateIndex(100), 'int': tm.makeIntIndex(100), 'rng': tm.makeRangeIndex(100), 'float': tm.makeFloatIndex(100), 'empty': Index([]), 'tuple': Index(zip(['foo', 'bar', 'baz'], [1, 2, 3])), 'period': Index(period_range('2012-1-1', freq='M', periods=3)), 'date2': Index(date_range('2013-01-1', periods=10)), 'bdate': Index(bdate_range('2013-01-02', periods=10)), 'cat': tm.makeCategoricalIndex(100), 'interval': tm.makeIntervalIndex(100), 'timedelta': tm.makeTimedeltaIndex(100, 'H') } self.mi = { 'reg': MultiIndex.from_tuples([('bar', 'one'), ('baz', 'two'), ('foo', 'two'), ('qux', 'one'), ('qux', 'two')], names=['first', 'second']), }
pd.Series(np.linspace(0,9,5)) # allows to specify the number of values to be created btw boundaries pd.Series(np.random.normal(size=5)) np.random.randint(50,101,10) a = np.array([4] * 16) a[1::] = [42] * 15 a[1:8:2] = 16 import pandas.util.testing as tm tm.N, tm.K = 5,3 tm.makeFloatSeries(), tm.makeBoolIndex(), tm.makeCategoricalIndex() tm.makeCustomIndex(nentries=4,nlevels=2), tm.makeFloatIndex(), tm.makeIntIndex() tm.makeMultiIndex(), tm.makeRangeIndex(), tm.makeIntervalIndex() # All possible combinations (Permutations) from itertools import permutations my_list = [1,2,3] perm = list(permutations(my_list)) #(1, 2, 3) #(1, 3, 2) #(2, 1, 3) #(2, 3, 1) #(3, 1, 2) #(3, 2, 1) # Create dataframes
def test_slice_integer(self): # same as above, but for Integer based indexes # these coerce to a like integer # oob indiciates if we are out of bounds # of positional indexing for index, oob in [(tm.makeIntIndex(5), False), (tm.makeRangeIndex(5), False), (tm.makeIntIndex(5) + 10, True)]: # s is an in-range index s = Series(range(5), index=index) # getitem for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: for idxr in [lambda x: x.loc, lambda x: x.ix]: with catch_warnings(record=True): result = idxr(s)[l] # these are all label indexing # except getitem which is positional # empty if oob: indexer = slice(0, 0) else: indexer = slice(3, 5) self.check(result, s, indexer, False) # positional indexing def f(): s[l] pytest.raises(TypeError, f) # getitem out-of-bounds for l in [slice(-6, 6), slice(-6.0, 6.0)]: for idxr in [lambda x: x.loc, lambda x: x.ix]: with catch_warnings(record=True): result = idxr(s)[l] # these are all label indexing # except getitem which is positional # empty if oob: indexer = slice(0, 0) else: indexer = slice(-6, 6) self.check(result, s, indexer, False) # positional indexing def f(): s[slice(-6.0, 6.0)] pytest.raises(TypeError, f) # getitem odd floats for l, res1 in [(slice(2.5, 4), slice(3, 5)), (slice(2, 3.5), slice(2, 4)), (slice(2.5, 3.5), slice(3, 4))]: for idxr in [lambda x: x.loc, lambda x: x.ix]: with catch_warnings(record=True): result = idxr(s)[l] if oob: res = slice(0, 0) else: res = res1 self.check(result, s, res, False) # positional indexing def f(): s[l] pytest.raises(TypeError, f) # setitem for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: for idxr in [lambda x: x.loc, lambda x: x.ix]: sc = s.copy() with catch_warnings(record=True): idxr(sc)[l] = 0 result = idxr(sc)[l].values.ravel() assert (result == 0).all() # positional indexing def f(): s[l] = 0 pytest.raises(TypeError, f)
from pandas.compat import long, lzip import pandas as pd from pandas.core.indexes.api import Index, MultiIndex import pandas.util.testing as tm @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.makeRangeIndex(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
pd.Series([1.0, 2.0, 3.0], name="foo"), pd.Series([1.0, 2.0, 3.0], name="foo", index=[4, 5, 6]), pd.Series([1.0, 2.0, 3.0], name="foo", index=pd.Index([4, 5, 6], name="bar")), pd.DataFrame({"x": ["a", "b", "c"]}), pd.DataFrame({"x": [b"a", b"b", b"c"]}), pd.DataFrame({"x": pd.Categorical(["a", "b", "a"], ordered=True)}), pd.DataFrame({"x": pd.Categorical(["a", "b", "a"], ordered=False)}), tm.makeCategoricalIndex(), tm.makeCustomDataframe(5, 3), tm.makeDataFrame(), tm.makeDateIndex(), tm.makeMissingDataframe(), tm.makeMixedDataFrame(), tm.makeObjectSeries(), tm.makePeriodFrame(), tm.makeRangeIndex(), tm.makeTimeDataFrame(), tm.makeTimeSeries(), tm.makeUnicodeIndex(), ] @pytest.mark.parametrize("df", dfs) def test_dumps_serialize_numpy(df): header, frames = serialize(df) if "compression" in header: frames = decompress(header, frames) df2 = deserialize(header, frames) assert_eq(df, df2)
import numpy as np import pytest import pandas as pd from pandas.core.indexes.api import Index, MultiIndex import pandas.util.testing as tm indices_dict = { "unicode": tm.makeUnicodeIndex(100), "string": tm.makeStringIndex(100), "datetime": tm.makeDateIndex(100), "period": tm.makePeriodIndex(100), "timedelta": tm.makeTimedeltaIndex(100), "int": tm.makeIntIndex(100), "uint": tm.makeUIntIndex(100), "range": tm.makeRangeIndex(100), "float": tm.makeFloatIndex(100), "bool": Index([True, False]), "categorical": tm.makeCategoricalIndex(100), "interval": tm.makeIntervalIndex(100), "empty": Index([]), "tuples": MultiIndex.from_tuples(zip(["foo", "bar", "baz"], [1, 2, 3])), "repeats": Index([0, 0, 1, 1, 2, 2]), } @pytest.fixture(params=indices_dict.keys()) def indices(request): # copy to avoid mutation, e.g. setting .name return indices_dict[request.param].copy()
import numpy as np import pytest import pandas as pd from pandas.core.indexes.api import Index, MultiIndex import pandas.util.testing as tm @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.makeRangeIndex(100), tm.makeFloatIndex(100), Index([True, False]), tm.makeCategoricalIndex(100), Index([]), MultiIndex.from_tuples(zip( ['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
def test_slice_integer(self): # same as above, but for Integer based indexes # these coerce to a like integer # oob indiciates if we are out of bounds # of positional indexing for index, oob in [(tm.makeIntIndex(5), False), (tm.makeRangeIndex(5), False), (tm.makeIntIndex(5) + 10, True)]: # s is an in-range index s = Series(range(5), index=index) # getitem for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: for idxr in [lambda x: x.loc, lambda x: x.ix]: result = idxr(s)[l] # these are all label indexing # except getitem which is positional # empty if oob: indexer = slice(0, 0) else: indexer = slice(3, 5) self.check(result, s, indexer, False) # positional indexing def f(): s[l] self.assertRaises(TypeError, f) # getitem out-of-bounds for l in [slice(-6, 6), slice(-6.0, 6.0)]: for idxr in [lambda x: x.loc, lambda x: x.ix]: result = idxr(s)[l] # these are all label indexing # except getitem which is positional # empty if oob: indexer = slice(0, 0) else: indexer = slice(-6, 6) self.check(result, s, indexer, False) # positional indexing def f(): s[slice(-6.0, 6.0)] self.assertRaises(TypeError, f) # getitem odd floats for l, res1 in [(slice(2.5, 4), slice(3, 5)), (slice(2, 3.5), slice(2, 4)), (slice(2.5, 3.5), slice(3, 4))]: for idxr in [lambda x: x.loc, lambda x: x.ix]: result = idxr(s)[l] if oob: res = slice(0, 0) else: res = res1 self.check(result, s, res, False) # positional indexing def f(): s[l] self.assertRaises(TypeError, f) # setitem for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: for idxr in [lambda x: x.loc, lambda x: x.ix]: sc = s.copy() idxr(sc)[l] = 0 result = idxr(sc)[l].values.ravel() self.assertTrue((result == 0).all()) # positional indexing def f(): s[l] = 0 self.assertRaises(TypeError, f)
index=[4, 5, 6]), pd.Series([1., 2., 3.], name='foo', index=pd.Index([4, 5, 6], name='bar')), pd.DataFrame({'x': ['a', 'b', 'c']}), pd.DataFrame({'x': [b'a', b'b', b'c']}), pd.DataFrame({'x': pd.Categorical(['a', 'b', 'a'], ordered=True)}), pd.DataFrame({'x': pd.Categorical(['a', 'b', 'a'], ordered=False)}), tm.makeCategoricalIndex(), tm.makeCustomDataframe(5, 3), tm.makeDataFrame(), tm.makeDateIndex(), tm.makeMissingDataframe(), tm.makeMixedDataFrame(), tm.makeObjectSeries(), tm.makePeriodFrame(), tm.makeRangeIndex(), tm.makeTimeDataFrame(), tm.makeTimeSeries(), tm.makeUnicodeIndex(), ] @pytest.mark.parametrize('df', dfs) def test_dumps_serialize_numpy(df): header, frames = serialize(df) if 'compression' in header: frames = decompress(header, frames) df2 = deserialize(header, frames) assert_eq(df, df2)