Python pandas.compat.range() Examples
The following are 30
code examples of pandas.compat.range().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pandas.compat
, or try the search function
.
Example #1
Source File: test_numeric.py From recruit with Apache License 2.0 | 6 votes |
def test_index_groupby(self): int_idx = Index(range(6)) float_idx = Index(np.arange(0, 0.6, 0.1)) obj_idx = Index('A B C D E F'.split()) dt_idx = pd.date_range('2013-01-01', freq='M', periods=6) for idx in [int_idx, float_idx, obj_idx, dt_idx]: to_groupby = np.array([1, 2, np.nan, np.nan, 2, 1]) tm.assert_dict_equal(idx.groupby(to_groupby), {1.0: idx[[0, 5]], 2.0: idx[[1, 4]]}) to_groupby = Index([datetime(2011, 11, 1), datetime(2011, 12, 1), pd.NaT, pd.NaT, datetime(2011, 12, 1), datetime(2011, 11, 1)], tz='UTC').values ex_keys = [Timestamp('2011-11-01'), Timestamp('2011-12-01')] expected = {ex_keys[0]: idx[[0, 5]], ex_keys[1]: idx[[1, 4]]} tm.assert_dict_equal(idx.groupby(to_groupby), expected)
Example #2
Source File: test_series.py From recruit with Apache License 2.0 | 6 votes |
def test_to_xarray_index_types(self, index): from xarray import DataArray index = getattr(tm, 'make{}'.format(index)) s = Series(range(6), index=index(6)) s.index.name = 'foo' result = s.to_xarray() repr(result) assert len(result) == 6 assert len(result.coords) == 1 assert_almost_equal(list(result.coords.keys()), ['foo']) assert isinstance(result, DataArray) # idempotency assert_series_equal(result.to_series(), s, check_index_type=False, check_categorical=True)
Example #3
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_expanding_axis(self, axis_frame): # see gh-23372. df = DataFrame(np.ones((10, 20))) axis = df._get_axis_number(axis_frame) if axis == 0: expected = DataFrame({ i: [np.nan] * 2 + [float(j) for j in range(3, 11)] for i in range(20) }) else: # axis == 1 expected = DataFrame([ [np.nan] * 2 + [float(i) for i in range(3, 21)] ] * 10) result = df.expanding(3, axis=axis_frame).sum() tm.assert_frame_equal(result, expected)
Example #4
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_cmov_window_special_linear_range(self, win_types_special): # GH 8238 kwds = { 'kaiser': {'beta': 1.}, 'gaussian': {'std': 1.}, 'general_gaussian': {'power': 2., 'width': 2.}, 'slepian': {'width': 0.5}} vals = np.array(range(10), dtype=np.float) xp = vals.copy() xp[:2] = np.nan xp[-2:] = np.nan xp = Series(xp) rs = Series(vals).rolling( 5, win_type=win_types_special, center=True).mean( **kwds[win_types_special]) tm.assert_series_equal(xp, rs)
Example #5
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_corr_sanity(self): # GH 3155 df = DataFrame(np.array( [[0.87024726, 0.18505595], [0.64355431, 0.3091617], [0.92372966, 0.50552513], [0.00203756, 0.04520709], [0.84780328, 0.33394331], [0.78369152, 0.63919667]])) res = df[0].rolling(5, center=True).corr(df[1]) assert all(np.abs(np.nan_to_num(x)) <= 1 for x in res) # and some fuzzing for _ in range(10): df = DataFrame(np.random.rand(30, 2)) res = df[0].rolling(5, center=True).corr(df[1]) try: assert all(np.abs(np.nan_to_num(x)) <= 1 for x in res) except AssertionError: print(res)
Example #6
Source File: test_generic.py From recruit with Apache License 2.0 | 6 votes |
def test_get_numeric_data(self): n = 4 kwargs = {self._typ._AXIS_NAMES[i]: list(range(n)) for i in range(self._ndim)} # get the numeric data o = self._construct(n, **kwargs) result = o._get_numeric_data() self._compare(result, o) # non-inclusion result = o._get_bool_data() expected = self._construct(n, value='empty', **kwargs) self._compare(result, expected) # get the bool data arr = np.array([True, True, False, True]) o = self._construct(n, value=arr, **kwargs) result = o._get_numeric_data() self._compare(result, o) # _get_numeric_data is includes _get_bool_data, so can't test for # non-inclusion
Example #7
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_rolling_axis(self, axis_frame): # see gh-23372. df = DataFrame(np.ones((10, 20))) axis = df._get_axis_number(axis_frame) if axis == 0: expected = DataFrame({ i: [np.nan] * 2 + [3.0] * 8 for i in range(20) }) else: # axis == 1 expected = DataFrame([ [np.nan] * 2 + [3.0] * 18 ] * 10) result = df.rolling(3, axis=axis_frame).sum() tm.assert_frame_equal(result, expected)
Example #8
Source File: test_series.py From recruit with Apache License 2.0 | 6 votes |
def test_to_xarray(self): from xarray import DataArray s = Series([]) s.index.name = 'foo' result = s.to_xarray() assert len(result) == 0 assert len(result.coords) == 1 assert_almost_equal(list(result.coords.keys()), ['foo']) assert isinstance(result, DataArray) s = Series(range(6)) s.index.name = 'foo' s.index = pd.MultiIndex.from_product([['a', 'b'], range(3)], names=['one', 'two']) result = s.to_xarray() assert len(result) == 2 assert_almost_equal(list(result.coords.keys()), ['one', 'two']) assert isinstance(result, DataArray) assert_series_equal(result.to_series(), s)
Example #9
Source File: test_melt.py From recruit with Apache License 2.0 | 6 votes |
def test_pandas_dtypes(self, col): # GH 15785 df = DataFrame({'klass': range(5), 'col': col, 'attr1': [1, 0, 0, 0, 0], 'attr2': col}) expected_value = pd.concat([pd.Series([1, 0, 0, 0, 0]), col], ignore_index=True) result = melt(df, id_vars=['klass', 'col'], var_name='attribute', value_name='value') expected = DataFrame({0: list(range(5)) * 2, 1: pd.concat([col] * 2, ignore_index=True), 2: ['attr1'] * 5 + ['attr2'] * 5, 3: expected_value}) expected.columns = ['klass', 'col', 'attribute', 'value'] tm.assert_frame_equal(result, expected)
Example #10
Source File: test_chaining_and_caching.py From recruit with Apache License 2.0 | 6 votes |
def test_cache_updating(): # 5216 # make sure that we don't try to set a dead cache a = np.random.rand(10, 3) df = DataFrame(a, columns=['x', 'y', 'z']) tuples = [(i, j) for i in range(5) for j in range(2)] index = MultiIndex.from_tuples(tuples) df.index = index # setting via chained assignment # but actually works, since everything is a view df.loc[0]['z'].iloc[0] = 1. result = df.loc[(0, 0), 'z'] assert result == 1 # correct setting df.loc[(0, 0), 'z'] = 2 result = df.loc[(0, 0), 'z'] assert result == 2
Example #11
Source File: offsets.py From recruit with Apache License 2.0 | 6 votes |
def apply(self, other): if self._use_relativedelta: other = as_datetime(other) if len(self.kwds) > 0: tzinfo = getattr(other, 'tzinfo', None) if tzinfo is not None and self._use_relativedelta: # perform calculation in UTC other = other.replace(tzinfo=None) if self.n > 0: for i in range(self.n): other = other + self._offset else: for i in range(-self.n): other = other - self._offset if tzinfo is not None and self._use_relativedelta: # bring tz back from UTC calculation other = conversion.localize_pydatetime(other, tzinfo) return as_timestamp(other) else: return other + timedelta(self.n)
Example #12
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_agg_consistency(self): df = DataFrame({'A': range(5), 'B': range(0, 10, 2)}) r = df.rolling(window=3) result = r.agg([np.sum, np.mean]).columns expected = pd.MultiIndex.from_product([list('AB'), ['sum', 'mean']]) tm.assert_index_equal(result, expected) result = r['A'].agg([np.sum, np.mean]).columns expected = Index(['sum', 'mean']) tm.assert_index_equal(result, expected) result = r.agg({'A': [np.sum, np.mean]}).columns expected = pd.MultiIndex.from_tuples([('A', 'sum'), ('A', 'mean')]) tm.assert_index_equal(result, expected)
Example #13
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_rolling_functions_window_non_shrinkage(self, f): # GH 7764 s = Series(range(4)) s_expected = Series(np.nan, index=s.index) df = DataFrame([[1, 5], [3, 2], [3, 9], [-1, 0]], columns=['A', 'B']) df_expected = DataFrame(np.nan, index=df.index, columns=df.columns) try: s_result = f(s) tm.assert_series_equal(s_result, s_expected) df_result = f(df) tm.assert_frame_equal(df_result, df_expected) except (ImportError): # scipy needed for rolling_window pytest.skip("scipy not available")
Example #14
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_expanding_corr_pairwise_diff_length(self): # GH 7512 df1 = DataFrame([[1, 2], [3, 2], [3, 4]], columns=['A', 'B'], index=Index(range(3), name='bar')) df1a = DataFrame([[1, 2], [3, 4]], index=Index([0, 2], name='bar'), columns=['A', 'B']) df2 = DataFrame([[5, 6], [None, None], [2, 1]], columns=['X', 'Y'], index=Index(range(3), name='bar')) df2a = DataFrame([[5, 6], [2, 1]], index=Index([0, 2], name='bar'), columns=['X', 'Y']) result1 = df1.expanding().corr(df2, pairwise=True).loc[2] result2 = df1.expanding().corr(df2a, pairwise=True).loc[2] result3 = df1a.expanding().corr(df2, pairwise=True).loc[2] result4 = df1a.expanding().corr(df2a, pairwise=True).loc[2] expected = DataFrame([[-1.0, -1.0], [-1.0, -1.0]], columns=['A', 'B'], index=Index(['X', 'Y'])) tm.assert_frame_equal(result1, expected) tm.assert_frame_equal(result2, expected) tm.assert_frame_equal(result3, expected) tm.assert_frame_equal(result4, expected)
Example #15
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_rolling_max_gh6297(self): """Replicate result expected in GH #6297""" indices = [datetime(1975, 1, i) for i in range(1, 6)] # So that we can have 2 datapoints on one of the days indices.append(datetime(1975, 1, 3, 6, 0)) series = Series(range(1, 7), index=indices) # Use floats instead of ints as values series = series.map(lambda x: float(x)) # Sort chronologically series = series.sort_index() expected = Series([1.0, 2.0, 6.0, 4.0, 5.0], index=[datetime(1975, 1, i, 0) for i in range(1, 6)]) x = series.resample('D').max().rolling(window=1).max() tm.assert_series_equal(expected, x)
Example #16
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_rolling_min_resample(self): indices = [datetime(1975, 1, i) for i in range(1, 6)] # So that we can have 3 datapoints on last day (4, 10, and 20) indices.append(datetime(1975, 1, 5, 1)) indices.append(datetime(1975, 1, 5, 2)) series = Series(list(range(0, 5)) + [10, 20], index=indices) # Use floats instead of ints as values series = series.map(lambda x: float(x)) # Sort chronologically series = series.sort_index() # Default how should be min expected = Series([0.0, 1.0, 2.0, 3.0, 4.0], index=[datetime(1975, 1, i, 0) for i in range(1, 6)]) r = series.resample('D').min().rolling(window=1) tm.assert_series_equal(expected, r.min())
Example #17
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_rolling_median_resample(self): indices = [datetime(1975, 1, i) for i in range(1, 6)] # So that we can have 3 datapoints on last day (4, 10, and 20) indices.append(datetime(1975, 1, 5, 1)) indices.append(datetime(1975, 1, 5, 2)) series = Series(list(range(0, 5)) + [10, 20], index=indices) # Use floats instead of ints as values series = series.map(lambda x: float(x)) # Sort chronologically series = series.sort_index() # Default how should be median expected = Series([0.0, 1.0, 2.0, 3.0, 10], index=[datetime(1975, 1, i, 0) for i in range(1, 6)]) x = series.resample('D').median().rolling(window=1).median() tm.assert_series_equal(expected, x)
Example #18
Source File: test_integrity.py From recruit with Apache License 2.0 | 6 votes |
def test_rangeindex_fallback_coercion_bug(): # GH 12893 foo = pd.DataFrame(np.arange(100).reshape((10, 10))) bar = pd.DataFrame(np.arange(100).reshape((10, 10))) df = pd.concat({'foo': foo.stack(), 'bar': bar.stack()}, axis=1) df.index.names = ['fizz', 'buzz'] str(df) expected = pd.DataFrame({'bar': np.arange(100), 'foo': np.arange(100)}, index=pd.MultiIndex.from_product( [range(10), range(10)], names=['fizz', 'buzz'])) tm.assert_frame_equal(df, expected, check_like=True) result = df.index.get_level_values('fizz') expected = pd.Int64Index(np.arange(10), name='fizz').repeat(10) tm.assert_index_equal(result, expected) result = df.index.get_level_values('buzz') expected = pd.Int64Index(np.tile(np.arange(10), 10), name='buzz') tm.assert_index_equal(result, expected)
Example #19
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_monotonic_on(self): # on/index must be monotonic df = DataFrame({'A': pd.date_range('20130101', periods=5, freq='s'), 'B': range(5)}) assert df.A.is_monotonic df.rolling('2s', on='A').sum() df = df.set_index('A') assert df.index.is_monotonic df.rolling('2s').sum() # non-monotonic df.index = reversed(df.index.tolist()) assert not df.index.is_monotonic with pytest.raises(ValueError): df.rolling('2s').sum() df = df.reset_index() with pytest.raises(ValueError): df.rolling('2s', on='A').sum()
Example #20
Source File: test_frequencies.py From recruit with Apache License 2.0 | 6 votes |
def _check_tick(self, base_delta, code): b = Timestamp(datetime.now()) for i in range(1, 5): inc = base_delta * i index = _dti([b + inc * j for j in range(3)]) if i > 1: exp_freq = '%d%s' % (i, code) else: exp_freq = code assert frequencies.infer_freq(index) == exp_freq index = _dti([b + base_delta * 7] + [b + base_delta * j for j in range( 3)]) assert frequencies.infer_freq(index) is None index = _dti([b + base_delta * j for j in range(3)] + [b + base_delta * 7]) assert frequencies.infer_freq(index) is None
Example #21
Source File: test_integrity.py From recruit with Apache License 2.0 | 6 votes |
def test_large_multiindex_error(): # GH12527 df_below_1000000 = pd.DataFrame( 1, index=pd.MultiIndex.from_product([[1, 2], range(499999)]), columns=['dest']) with pytest.raises(KeyError): df_below_1000000.loc[(-1, 0), 'dest'] with pytest.raises(KeyError): df_below_1000000.loc[(3, 0), 'dest'] df_above_1000000 = pd.DataFrame( 1, index=pd.MultiIndex.from_product([[1, 2], range(500001)]), columns=['dest']) with pytest.raises(KeyError): df_above_1000000.loc[(-1, 0), 'dest'] with pytest.raises(KeyError): df_above_1000000.loc[(3, 0), 'dest']
Example #22
Source File: test_integrity.py From recruit with Apache License 2.0 | 6 votes |
def test_labels_dtypes(): # GH 8456 i = MultiIndex.from_tuples([('A', 1), ('A', 2)]) assert i.codes[0].dtype == 'int8' assert i.codes[1].dtype == 'int8' i = MultiIndex.from_product([['a'], range(40)]) assert i.codes[1].dtype == 'int8' i = MultiIndex.from_product([['a'], range(400)]) assert i.codes[1].dtype == 'int16' i = MultiIndex.from_product([['a'], range(40000)]) assert i.codes[1].dtype == 'int32' i = pd.MultiIndex.from_product([['a'], range(1000)]) assert (i.codes[0] >= 0).all() assert (i.codes[1] >= 0).all()
Example #23
Source File: test_offsets.py From recruit with Apache License 2.0 | 6 votes |
def test_onOffset(self, offset_types): dt = self.expecteds[offset_types.__name__] offset_s = self._get_offset(offset_types) assert offset_s.onOffset(dt) # when normalize=True, onOffset checks time is 00:00:00 if issubclass(offset_types, Tick): # normalize=True disallowed for Tick subclasses GH#21427 return offset_n = self._get_offset(offset_types, normalize=True) assert not offset_n.onOffset(dt) if offset_types in (BusinessHour, CustomBusinessHour): # In default BusinessHour (9:00-17:00), normalized time # cannot be in business hour range return date = datetime(dt.year, dt.month, dt.day) assert offset_n.onOffset(date)
Example #24
Source File: test_frame.py From recruit with Apache License 2.0 | 6 votes |
def test_pie_df_nan(self): df = DataFrame(np.random.rand(4, 4)) for i in range(4): df.iloc[i, i] = np.nan fig, axes = self.plt.subplots(ncols=4) df.plot.pie(subplots=True, ax=axes, legend=True) base_expected = ['0', '1', '2', '3'] for i, ax in enumerate(axes): expected = list(base_expected) # force copy expected[i] = '' result = [x.get_text() for x in ax.texts] assert result == expected # legend labels # NaN's not included in legend with subplots # see https://github.com/pandas-dev/pandas/issues/8390 assert ([x.get_text() for x in ax.get_legend().get_texts()] == base_expected[:i] + base_expected[i + 1:])
Example #25
Source File: test_get_set.py From recruit with Apache License 2.0 | 6 votes |
def test_set_labels_deprecated(): # GH23752 ind = pd.MultiIndex.from_tuples([(0, i) for i in range(130)]) new_labels = range(129, -1, -1) expected = pd.MultiIndex.from_tuples( [(0, i) for i in new_labels]) # [w/o mutation] with tm.assert_produces_warning(FutureWarning): result = ind.set_labels(labels=new_labels, level=1) assert result.equals(expected) # [w/ mutation] result = ind.copy() with tm.assert_produces_warning(FutureWarning): result.set_labels(labels=new_labels, level=1, inplace=True) assert result.equals(expected)
Example #26
Source File: test_category.py From recruit with Apache License 2.0 | 6 votes |
def test_construction_with_dtype(self): # specify dtype ci = self.create_index(categories=list('abc')) result = Index(np.array(ci), dtype='category') tm.assert_index_equal(result, ci, exact=True) result = Index(np.array(ci).tolist(), dtype='category') tm.assert_index_equal(result, ci, exact=True) # these are generally only equal when the categories are reordered ci = self.create_index() result = Index( np.array(ci), dtype='category').reorder_categories(ci.categories) tm.assert_index_equal(result, ci, exact=True) # make sure indexes are handled expected = CategoricalIndex([0, 1, 2], categories=[0, 1, 2], ordered=True) idx = Index(range(3)) result = CategoricalIndex(idx, categories=idx, ordered=True) tm.assert_index_equal(result, expected, exact=True)
Example #27
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_rolling_functions_window_non_shrinkage_binary(self): # corr/cov return a MI DataFrame df = DataFrame([[1, 5], [3, 2], [3, 9], [-1, 0]], columns=Index(['A', 'B'], name='foo'), index=Index(range(4), name='bar')) df_expected = DataFrame( columns=Index(['A', 'B'], name='foo'), index=pd.MultiIndex.from_product([df.index, df.columns], names=['bar', 'foo']), dtype='float64') functions = [lambda x: (x.rolling(window=10, min_periods=5) .cov(x, pairwise=True)), lambda x: (x.rolling(window=10, min_periods=5) .corr(x, pairwise=True))] for f in functions: df_result = f(df) tm.assert_frame_equal(df_result, df_expected)
Example #28
Source File: test_frame.py From recruit with Apache License 2.0 | 5 votes |
def test_errorbar_scatter(self): df = DataFrame( np.random.randn(5, 2), index=range(5), columns=['x', 'y']) df_err = DataFrame(np.random.randn(5, 2) / 5, index=range(5), columns=['x', 'y']) ax = _check_plot_works(df.plot.scatter, x='x', y='y') self._check_has_errorbars(ax, xerr=0, yerr=0) ax = _check_plot_works(df.plot.scatter, x='x', y='y', xerr=df_err) self._check_has_errorbars(ax, xerr=1, yerr=0) ax = _check_plot_works(df.plot.scatter, x='x', y='y', yerr=df_err) self._check_has_errorbars(ax, xerr=0, yerr=1) ax = _check_plot_works(df.plot.scatter, x='x', y='y', xerr=df_err, yerr=df_err) self._check_has_errorbars(ax, xerr=1, yerr=1) def _check_errorbar_color(containers, expected, has_err='has_xerr'): lines = [] errs = [c.lines for c in ax.containers if getattr(c, has_err, False)][0] for el in errs: if is_list_like(el): lines.extend(el) else: lines.append(el) err_lines = [x for x in lines if x in ax.collections] self._check_colors( err_lines, linecolors=np.array([expected] * len(err_lines))) # GH 8081 df = DataFrame( np.random.randn(10, 5), columns=['a', 'b', 'c', 'd', 'e']) ax = df.plot.scatter(x='a', y='b', xerr='d', yerr='e', c='red') self._check_has_errorbars(ax, xerr=1, yerr=1) _check_errorbar_color(ax.containers, 'red', has_err='has_xerr') _check_errorbar_color(ax.containers, 'red', has_err='has_yerr') ax = df.plot.scatter(x='a', y='b', yerr='e', color='green') self._check_has_errorbars(ax, xerr=0, yerr=1) _check_errorbar_color(ax.containers, 'green', has_err='has_yerr')
Example #29
Source File: test_pivot.py From recruit with Apache License 2.0 | 5 votes |
def test_pivot_integer_columns(self): # caused by upstream bug in unstack d = date.min data = list(product(['foo', 'bar'], ['A', 'B', 'C'], ['x1', 'x2'], [d + timedelta(i) for i in range(20)], [1.0])) df = DataFrame(data) table = df.pivot_table(values=4, index=[0, 1, 3], columns=[2]) df2 = df.rename(columns=str) table2 = df2.pivot_table( values='4', index=['0', '1', '3'], columns=['2']) tm.assert_frame_equal(table, table2, check_names=False)
Example #30
Source File: test_pivot.py From recruit with Apache License 2.0 | 5 votes |
def test_monthly(self): rng = date_range('1/1/2000', '12/31/2004', freq='M') ts = Series(np.random.randn(len(rng)), index=rng) annual = pivot_table(pd.DataFrame(ts), index=ts.index.year, columns=ts.index.month) annual.columns = annual.columns.droplevel(0) month = ts.index.month for i in range(1, 13): subset = ts[month == i] subset.index = subset.index.year result = annual[i].dropna() tm.assert_series_equal(result, subset, check_names=False) assert result.name == i