def setUp(self): self.seriesd = common.getSeriesData() self.tsd = common.getTimeSeriesData() self.frame = self.klass(self.seriesd) self.intframe = self.klass(dict((k, v.astype(int)) for k, v in self.seriesd.iteritems())) self.tsframe = self.klass(self.tsd) self.mixed_frame = self.frame.copy() self.mixed_frame['foo'] = 'bar' self.ts1 = common.makeTimeSeries() self.ts2 = common.makeTimeSeries()[5:] self.ts3 = common.makeTimeSeries()[-5:] self.ts4 = common.makeTimeSeries()[1:-1] self.ts_dict = { 'col1' : self.ts1, 'col2' : self.ts2, 'col3' : self.ts3, 'col4' : self.ts4, } self.empty = self.klass({}) self.unsortable = self.klass( {'foo' : [1] * 1000, datetime.today() : [1] * 1000, 'bar' : ['bar'] * 1000, datetime.today() + timedelta(1) : ['bar'] * 1000}, index=np.arange(1000))
def test_loc_with_text_dates(): A = tm.makeTimeSeries(10).iloc[:5] B = tm.makeTimeSeries(10).iloc[5:] s = dd.Series({('df', 0): A, ('df', 1): B}, 'df', None, [A.index.max()]) assert eq(s.loc['2000': '2010'], s) assert len(s.loc['2000-01-03': '2000-01-05'].compute()) == 3
def test_loc_with_text_dates(): A = tm.makeTimeSeries(10).iloc[:5] B = tm.makeTimeSeries(10).iloc[5:] s = dd.Series({("df", 0): A, ("df", 1): B}, "df", None, [A.index.min(), A.index.max(), B.index.max()]) assert s.loc["2000":"2010"].divisions == s.divisions assert eq(s.loc["2000":"2010"], s) assert len(s.loc["2000-01-03":"2000-01-05"].compute()) == 3
def test_series_rhs(self): y = tm.makeTimeSeries() x = tm.makeTimeSeries() model = ols(y=y, x=x) expected = ols(y=y, x={"x": x}) assert_series_equal(model.beta, expected.beta) # GH 5233/5250 assert_series_equal(model.y_predict, model.predict(x=x))
def test_concat_bug_1719(self): ts1 = tm.makeTimeSeries() ts2 = tm.makeTimeSeries()[::2] # to join with union # these two are of different length! left = concat([ts1, ts2], join='outer', axis=1) right = concat([ts2, ts1], join='outer', axis=1) self.assertEqual(len(left), len(right))
def test_series_rhs(self): y = tm.makeTimeSeries() x = tm.makeTimeSeries() with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): model = ols(y=y, x=x) with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): expected = ols(y=y, x={'x': x}) assert_series_equal(model.beta, expected.beta) # GH 5233/5250 assert_series_equal(model.y_predict, model.predict(x=x))
def test_rolling_corr(self): A = self.series B = A + randn(len(A)) result = moments.rolling_corr(A, B, 50, min_periods=25) assert_almost_equal(result[-1], np.corrcoef(A[-50:], B[-50:])[0, 1]) # test for correct bias correction a = tm.makeTimeSeries() b = tm.makeTimeSeries() a[:5] = np.nan b[:10] = np.nan result = moments.rolling_corr(a, b, len(a), min_periods=1) assert_almost_equal(result[-1], a.corr(b))
def test_plot_offset_freq(self): ser = tm.makeTimeSeries() _check_plot_works(ser.plot) dr = date_range(ser.index[0], freq='BQS', periods=10) ser = Series(np.random.randn(len(dr)), dr) _check_plot_works(ser.plot)
def test_series_agg_multikey(): ts = tm.makeTimeSeries() grouped = ts.groupby([lambda x: x.year, lambda x: x.month]) result = grouped.agg(np.sum) expected = grouped.sum() tm.assert_series_equal(result, expected)
def test_concat_series_axis1(self): ts = tm.makeTimeSeries() pieces = [ts[:-2], ts[2:], ts[2:-2]] result = concat(pieces, axis=1) expected = DataFrame(pieces).T assert_frame_equal(result, expected) result = concat(pieces, keys=['A', 'B', 'C'], axis=1) expected = DataFrame(pieces, index=['A', 'B', 'C']).T assert_frame_equal(result, expected) # preserve series names, #2489 s = Series(randn(5), name='A') s2 = Series(randn(5), name='B') result = concat([s, s2], axis=1) expected = DataFrame({'A': s, 'B': s2}) assert_frame_equal(result, expected) s2.name = None result = concat([s, s2], axis=1) self.assertTrue(np.array_equal( result.columns, Index(['A', 0], dtype='object'))) # must reindex, #2603 s = Series(randn(3), index=['c', 'a', 'b'], name='A') s2 = Series(randn(4), index=['d', 'a', 'b', 'c'], name='B') result = concat([s, s2], axis=1) expected = DataFrame({'A': s, 'B': s2}) assert_frame_equal(result, expected)
def test_series_describe_multikey(self): ts = tm.makeTimeSeries() grouped = ts.groupby([lambda x: x.year, lambda x: x.month]) result = grouped.describe() assert_series_equal(result['mean'], grouped.mean(), check_names=False) assert_series_equal(result['std'], grouped.std(), check_names=False) assert_series_equal(result['min'], grouped.min(), check_names=False)
def test_isnull(): assert not isnull(1.) assert isnull(None) assert isnull(np.NaN) assert not isnull(np.inf) assert not isnull(-np.inf) # series for s in [tm.makeFloatSeries(),tm.makeStringSeries(), tm.makeObjectSeries(),tm.makeTimeSeries(),tm.makePeriodSeries()]: assert(isinstance(isnull(s), Series)) # frame for df in [tm.makeTimeDataFrame(),tm.makePeriodFrame(),tm.makeMixedDataFrame()]: result = isnull(df) expected = df.apply(isnull) tm.assert_frame_equal(result, expected) # panel for p in [ tm.makePanel(), tm.makePeriodPanel(), tm.add_nans(tm.makePanel()) ]: result = isnull(p) expected = p.apply(isnull) tm.assert_panel_equal(result, expected) # panel 4d for p in [ tm.makePanel4D(), tm.add_nans_panel4d(tm.makePanel4D()) ]: result = isnull(p) expected = p.apply(isnull) tm.assert_panel4d_equal(result, expected)
def setUp(self): TestPlotBase.setUp(self) import matplotlib as mpl mpl.rcdefaults() self.ts = tm.makeTimeSeries() self.ts.name = 'ts'
def test_catch_regressor_overlap(self): df1 = tm.makeTimeDataFrame().ix[:, ["A", "B"]] df2 = tm.makeTimeDataFrame().ix[:, ["B", "C", "D"]] y = tm.makeTimeSeries() data = {"foo": df1, "bar": df2} self.assertRaises(Exception, ols, y=y, x=data)
def test_both_style_and_color(self): ts = tm.makeTimeSeries() pytest.raises(ValueError, ts.plot, style='b-', color='#000099') s = ts.reset_index(drop=True) pytest.raises(ValueError, s.plot, style='b-', color='#000099')
def test_predict(self): y = tm.makeTimeSeries() x = tm.makeTimeDataFrame() with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): model1 = ols(y=y, x=x) assert_series_equal(model1.predict(), model1.y_predict) assert_series_equal(model1.predict(x=x), model1.y_predict) assert_series_equal(model1.predict(beta=model1.beta), model1.y_predict) exog = x.copy() exog['intercept'] = 1. rs = Series(np.dot(exog.values, model1.beta.values), x.index) assert_series_equal(model1.y_predict, rs) x2 = x.reindex(columns=x.columns[::-1]) assert_series_equal(model1.predict(x=x2), model1.y_predict) x3 = x2 + 10 pred3 = model1.predict(x=x3) x3['intercept'] = 1. x3 = x3.reindex(columns=model1.beta.index) expected = Series(np.dot(x3.values, model1.beta.values), x3.index) assert_series_equal(expected, pred3) beta = Series(0., model1.beta.index) pred4 = model1.predict(beta=beta) assert_series_equal(Series(0., pred4.index), pred4)
def test_predict(self): y = tm.makeTimeSeries() x = tm.makeTimeDataFrame() model1 = ols(y=y, x=x) assert_series_equal(model1.predict(), model1.y_predict) assert_series_equal(model1.predict(x=x), model1.y_predict) assert_series_equal(model1.predict(beta=model1.beta), model1.y_predict) exog = x.copy() exog["intercept"] = 1.0 rs = Series(np.dot(exog.values, model1.beta.values), x.index) assert_series_equal(model1.y_predict, rs) x2 = x.reindex(columns=x.columns[::-1]) assert_series_equal(model1.predict(x=x2), model1.y_predict) x3 = x2 + 10 pred3 = model1.predict(x=x3) x3["intercept"] = 1.0 x3 = x3.reindex(columns=model1.beta.index) expected = Series(np.dot(x3.values, model1.beta.values), x3.index) assert_series_equal(expected, pred3) beta = Series(0.0, model1.beta.index) pred4 = model1.predict(beta=beta) assert_series_equal(Series(0.0, pred4.index), pred4)
def test_remove(self): ts = tm.makeTimeSeries() df = tm.makeDataFrame() self.store['a'] = ts self.store['b'] = df self.store.remove('a') self.assertEquals(len(self.store), 1) tm.assert_frame_equal(df, self.store['b']) self.store.remove('b') self.assertEquals(len(self.store), 0) # pathing self.store['a'] = ts self.store['b/foo'] = df self.store.remove('foo') self.store.remove('b/foo') self.assertEquals(len(self.store), 1) self.store['a'] = ts self.store['b/foo'] = df self.store.remove('b') self.assertEquals(len(self.store), 1) # __delitem__ self.store['a'] = ts self.store['b'] = df del self.store['a'] del self.store['b'] self.assertEquals(len(self.store), 0)
def test_mixed_freq_irreg_period(self): ts = tm.makeTimeSeries() irreg = ts[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16, 17, 18, 29]] rng = period_range('1/3/2000', periods=30, freq='B') ps = Series(np.random.randn(len(rng)), rng) irreg.plot() ps.plot()
def test_notnull(): assert notnull(1.) assert not notnull(None) assert not notnull(np.NaN) with cf.option_context("mode.use_inf_as_null", False): assert notnull(np.inf) assert notnull(-np.inf) arr = np.array([1.5, np.inf, 3.5, -np.inf]) result = notnull(arr) assert result.all() with cf.option_context("mode.use_inf_as_null", True): assert not notnull(np.inf) assert not notnull(-np.inf) arr = np.array([1.5, np.inf, 3.5, -np.inf]) result = notnull(arr) assert result.sum() == 2 with cf.option_context("mode.use_inf_as_null", False): for s in [tm.makeFloatSeries(),tm.makeStringSeries(), tm.makeObjectSeries(),tm.makeTimeSeries(),tm.makePeriodSeries()]: assert(isinstance(isnull(s), Series))
def test_len_keys(self): self.store['a'] = tm.makeTimeSeries() self.store['b'] = tm.makeStringSeries() self.store['c'] = tm.makeDataFrame() self.store['d'] = tm.makePanel() self.assertEquals(len(self.store), 4) self.assert_(set(self.store.keys()) == set(['a', 'b', 'c', 'd']))
def setup_method(self, method): TestPlotBase.setup_method(self, method) import matplotlib as mpl mpl.rcdefaults() self.ts = tm.makeTimeSeries() self.ts.name = 'ts'
def test_get(self): self.store['a'] = tm.makeTimeSeries() left = self.store.get('a') right = self.store['a'] tm.assert_series_equal(left, right) self.assertRaises(AttributeError, self.store.get, 'b')
def test_repr(self): repr(self.store) self.store['a'] = tm.makeTimeSeries() self.store['b'] = tm.makeStringSeries() self.store['c'] = tm.makeDataFrame() self.store['d'] = tm.makePanel() repr(self.store)
def setUp(self): self.ts = tm.makeTimeSeries() self.seriesd = tm.getSeriesData() self.tsd = tm.getTimeSeriesData() self.frame = DataFrame(self.seriesd) self.tsframe = DataFrame(self.tsd) self.df = df() self.df_mixed_floats = DataFrame( {'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'], 'B': ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'], 'C': np.random.randn(8), 'D': np.array( np.random.randn(8), dtype='float32')}) self.mframe = mframe() self.three_group = DataFrame( {'A': ['foo', 'foo', 'foo', 'foo', 'bar', 'bar', 'bar', 'bar', 'foo', 'foo', 'foo'], 'B': ['one', 'one', 'one', 'two', 'one', 'one', 'one', 'two', 'two', 'two', 'one'], 'C': ['dull', 'dull', 'shiny', 'dull', 'dull', 'shiny', 'shiny', 'dull', 'shiny', 'shiny', 'shiny'], 'D': np.random.randn(11), 'E': np.random.randn(11), 'F': np.random.randn(11)})
def test_catch_regressor_overlap(self): df1 = tm.makeTimeDataFrame().ix[:, ['A', 'B']] df2 = tm.makeTimeDataFrame().ix[:, ['B', 'C', 'D']] y = tm.makeTimeSeries() data = {'foo' : df1, 'bar' : df2} self.assertRaises(Exception, ols, y=y, x=data)
def test_y_predict(self): y = tm.makeTimeSeries() x = tm.makeTimeDataFrame() with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): model1 = ols(y=y, x=x) assert_series_equal(model1.y_predict, model1.y_fitted) assert_almost_equal(model1._y_predict_raw, model1._y_fitted_raw)
def datetime_series(): """ Fixture for Series of floats with DatetimeIndex """ s = tm.makeTimeSeries() s.name = 'ts' return s
def test_flex_method_equivalence(self, opname, ts): # check that Series.{opname} behaves like Series.__{opname}__, tser = tm.makeTimeSeries().rename('ts') series = ts[0](tser) other = ts[1](tser) check_reverse = ts[2] if opname == 'div' and compat.PY3: pytest.skip('div test only for Py3') op = getattr(Series, opname) if op == 'div': alt = operator.truediv else: alt = getattr(operator, opname) result = op(series, other) expected = alt(series, other) tm.assert_almost_equal(result, expected) if check_reverse: rop = getattr(Series, "r" + opname) result = rop(series, other) expected = alt(other, series) tm.assert_almost_equal(result, expected)
def test_divmod(self): def check(series, other): results = divmod(series, other) if isinstance(other, Iterable) and len(series) != len(other): # if the lengths don't match, this is the test where we use # `tser[::2]`. Pad every other value in `other_np` with nan. other_np = [] for n in other: other_np.append(n) other_np.append(np.nan) else: other_np = other other_np = np.asarray(other_np) with np.errstate(all='ignore'): expecteds = divmod(series.values, np.asarray(other_np)) for result, expected in zip(results, expecteds): # check the values, name, and index separately tm.assert_almost_equal(np.asarray(result), expected) assert result.name == series.name tm.assert_index_equal(result.index, series.index) tser = tm.makeTimeSeries().rename('ts') check(tser, tser * 2) check(tser, tser * 0) check(tser, tser[::2]) check(tser, 5)
def test_series_operators(self): def _check_op(series, other, op, pos_only=False, check_dtype=True): left = np.abs(series) if pos_only else series right = np.abs(other) if pos_only else other cython_or_numpy = op(left, right) python = left.combine(right, op) tm.assert_series_equal(cython_or_numpy, python, check_dtype=check_dtype) def check(series, other): simple_ops = ["add", "sub", "mul", "truediv", "floordiv", "mod"] for opname in simple_ops: _check_op(series, other, getattr(operator, opname)) _check_op(series, other, operator.pow, pos_only=True) _check_op(series, other, lambda x, y: operator.add(y, x)) _check_op(series, other, lambda x, y: operator.sub(y, x)) _check_op(series, other, lambda x, y: operator.truediv(y, x)) _check_op(series, other, lambda x, y: operator.floordiv(y, x)) _check_op(series, other, lambda x, y: operator.mul(y, x)) _check_op(series, other, lambda x, y: operator.pow(y, x), pos_only=True) _check_op(series, other, lambda x, y: operator.mod(y, x)) tser = tm.makeTimeSeries().rename("ts") check(tser, tser * 2) check(tser, tser * 0) check(tser, tser[::2]) check(tser, 5) def check_comparators(series, other, check_dtype=True): _check_op(series, other, operator.gt, check_dtype=check_dtype) _check_op(series, other, operator.ge, check_dtype=check_dtype) _check_op(series, other, operator.eq, check_dtype=check_dtype) _check_op(series, other, operator.lt, check_dtype=check_dtype) _check_op(series, other, operator.le, check_dtype=check_dtype) check_comparators(tser, 5) check_comparators(tser, tser + 1, check_dtype=False)
def setup_method(self, method): self.ts = tm.makeTimeSeries() self.seriesd = tm.getSeriesData() self.tsd = tm.getTimeSeriesData() self.frame = DataFrame(self.seriesd) self.tsframe = DataFrame(self.tsd) self.df = df() self.df_mixed_floats = DataFrame({ 'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'], 'B': ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'], 'C': np.random.randn(8), 'D': np.array(np.random.randn(8), dtype='float32') }) self.mframe = mframe() self.three_group = DataFrame({ 'A': [ 'foo', 'foo', 'foo', 'foo', 'bar', 'bar', 'bar', 'bar', 'foo', 'foo', 'foo' ], 'B': [ 'one', 'one', 'one', 'two', 'one', 'one', 'one', 'two', 'two', 'two', 'one' ], 'C': [ 'dull', 'dull', 'shiny', 'dull', 'dull', 'shiny', 'shiny', 'dull', 'shiny', 'shiny', 'shiny' ], 'D': np.random.randn(11), 'E': np.random.randn(11), 'F': np.random.randn(11) })
def test_isna_isnull(self, isna_f): assert not isna_f(1.) assert isna_f(None) assert isna_f(np.NaN) assert float('nan') assert not isna_f(np.inf) assert not isna_f(-np.inf) # series for s in [ tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries(), tm.makeTimeSeries(), tm.makePeriodSeries() ]: assert isinstance(isna_f(s), Series) # frame for df in [ tm.makeTimeDataFrame(), tm.makePeriodFrame(), tm.makeMixedDataFrame() ]: result = isna_f(df) expected = df.apply(isna_f) tm.assert_frame_equal(result, expected) # panel with catch_warnings(record=True): simplefilter("ignore", FutureWarning) for p in [ tm.makePanel(), tm.makePeriodPanel(), tm.add_nans(tm.makePanel()) ]: result = isna_f(p) expected = p.apply(isna_f) tm.assert_panel_equal(result, expected)
def test_isnull(): assert not isnull(1.) assert isnull(None) assert isnull(np.NaN) assert not isnull(np.inf) assert not isnull(-np.inf) for s in [ tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries(), tm.makeTimeSeries(), tm.makePeriodSeries() ]: assert (isinstance(isnull(s), Series)) # call on DataFrame df = DataFrame(np.random.randn(10, 5)) df['foo'] = 'bar' result = isnull(df) expected = result.apply(isnull) tm.assert_frame_equal(result, expected)
def test_concat_series(self): ts = tm.makeTimeSeries() ts.name = 'foo' pieces = [ts[:5], ts[5:15], ts[15:]] result = concat(pieces) tm.assert_series_equal(result, ts) self.assertEqual(result.name, ts.name) result = concat(pieces, keys=[0, 1, 2]) expected = ts.copy() ts.index = DatetimeIndex(np.array(ts.index.values, dtype='M8[ns]')) exp_labels = [np.repeat([0, 1, 2], [len(x) for x in pieces]), np.arange(len(ts))] exp_index = MultiIndex(levels=[[0, 1, 2], ts.index], labels=exp_labels) expected.index = exp_index tm.assert_series_equal(result, expected)
def setUp(self): self.ts = tm.makeTimeSeries() self.seriesd = tm.getSeriesData() self.tsd = tm.getTimeSeriesData() self.frame = DataFrame(self.seriesd) self.tsframe = DataFrame(self.tsd) self.df = DataFrame( {'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'], 'B': ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'], 'C': np.random.randn(8), 'D': np.random.randn(8)}) self.df_mixed_floats = DataFrame( {'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'], 'B': ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'], 'C': np.random.randn(8), 'D': np.array( np.random.randn(8), dtype='float32')}) index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'], ['one', 'two', 'three']], labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], names=['first', 'second']) self.mframe = DataFrame(np.random.randn(10, 3), index=index, columns=['A', 'B', 'C']) self.three_group = DataFrame( {'A': ['foo', 'foo', 'foo', 'foo', 'bar', 'bar', 'bar', 'bar', 'foo', 'foo', 'foo'], 'B': ['one', 'one', 'one', 'two', 'one', 'one', 'one', 'two', 'two', 'two', 'one'], 'C': ['dull', 'dull', 'shiny', 'dull', 'dull', 'shiny', 'shiny', 'dull', 'shiny', 'shiny', 'shiny'], 'D': np.random.randn(11), 'E': np.random.randn(11), 'F': np.random.randn(11)})
def test_concat_series(self): ts = tm.makeTimeSeries() ts.name = 'foo' pieces = [ts[:5], ts[5:15], ts[15:]] result = concat(pieces) tm.assert_series_equal(result, ts) self.assertEqual(result.name, ts.name) result = concat(pieces, keys=[0, 1, 2]) expected = ts.copy() exp_labels = [ np.repeat([0, 1, 2], [len(x) for x in pieces]), np.arange(len(ts)) ] exp_index = MultiIndex(levels=[[0, 1, 2], ts.index], labels=exp_labels) expected.index = exp_index tm.assert_series_equal(result, expected) self.assertRaises(Exception, concat, pieces, axis=1)
def test_series_frame_radd_bug(self): # GH#353 vals = pd.Series(tm.rands_array(5, 10)) result = 'foo_' + vals expected = vals.map(lambda x: 'foo_' + x) tm.assert_series_equal(result, expected) frame = pd.DataFrame({'vals': vals}) result = 'foo_' + frame expected = pd.DataFrame({'vals': vals.map(lambda x: 'foo_' + x)}) tm.assert_frame_equal(result, expected) ts = tm.makeTimeSeries() ts.name = 'ts' # really raise this time now = pd.Timestamp.now().to_pydatetime() with pytest.raises(TypeError): now + ts with pytest.raises(TypeError): ts + now
def test_tsplot(self): from pandas.tseries.plotting import tsplot import matplotlib.pyplot as plt plt.close('all') ax = plt.gca() ts = tm.makeTimeSeries() tsplot(ts, plt.Axes.plot) f = lambda *args, **kwds: tsplot(s, plt.Axes.plot, *args, **kwds) plt.close('all') for s in self.period_ser: _check_plot_works(f, s.index.freq, ax=ax, series=s) plt.close('all') for s in self.datetime_ser: _check_plot_works(f, s.index.freq.rule_code, ax=ax, series=s) plt.close('all') plt.close('all') ax = ts.plot(style='k') self.assert_((0., 0., 0.) == ax.get_lines()[0].get_color())
def test_axis_limits(self): def _test(ax): xlim = ax.get_xlim() ax.set_xlim(xlim[0] - 5, xlim[1] + 10) ax.get_figure().canvas.draw() result = ax.get_xlim() self.assertEqual(result[0], xlim[0] - 5) self.assertEqual(result[1], xlim[1] + 10) # string expected = (Period('1/1/2000', ax.freq), Period('4/1/2000', ax.freq)) ax.set_xlim('1/1/2000', '4/1/2000') ax.get_figure().canvas.draw() result = ax.get_xlim() self.assertEqual(int(result[0]), expected[0].ordinal) self.assertEqual(int(result[1]), expected[1].ordinal) # datetim expected = (Period('1/1/2000', ax.freq), Period('4/1/2000', ax.freq)) ax.set_xlim(datetime(2000, 1, 1), datetime(2000, 4, 1)) ax.get_figure().canvas.draw() result = ax.get_xlim() self.assertEqual(int(result[0]), expected[0].ordinal) self.assertEqual(int(result[1]), expected[1].ordinal) ser = tm.makeTimeSeries() ax = ser.plot() _test(ax) df = DataFrame({'a': ser, 'b': ser + 1}) ax = df.plot() _test(ax) df = DataFrame({'a': ser, 'b': ser + 1}) axes = df.plot(subplots=True) [_test(ax) for ax in axes]
def test_hash_pandas_object(self): for obj in [ Series([1, 2, 3]), Series([1.0, 1.5, 3.2]), Series([1.0, 1.5, np.nan]), Series([1.0, 1.5, 3.2], index=[1.5, 1.1, 3.3]), Series(['a', 'b', 'c']), Series(['a', np.nan, 'c']), Series(['a', None, 'c']), Series([True, False, True]), Series(), Index([1, 2, 3]), Index([True, False, True]), DataFrame({ 'x': ['a', 'b', 'c'], 'y': [1, 2, 3] }), DataFrame(), tm.makeMissingDataframe(), tm.makeMixedDataFrame(), tm.makeTimeDataFrame(), tm.makeTimeSeries(), tm.makeTimedeltaIndex(), tm.makePeriodIndex(), Series(tm.makePeriodIndex()), Series(pd.date_range('20130101', periods=3, tz='US/Eastern')), MultiIndex.from_product([ range(5), ['foo', 'bar', 'baz'], pd.date_range('20130101', periods=2) ]), MultiIndex.from_product( [pd.CategoricalIndex(list('aabc')), range(3)]) ]: self.check_equal(obj) self.check_not_equal_with_index(obj)
def test_r2_no_intercept(self): y = tm.makeTimeSeries() x = tm.makeTimeDataFrame() x_with = x.copy() x_with['intercept'] = 1. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): model1 = ols(y=y, x=x) with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): model2 = ols(y=y, x=x_with, intercept=False) assert_series_equal(model1.beta, model2.beta) # TODO: can we infer whether the intercept is there... self.assertNotEqual(model1.r2, model2.r2) # rolling with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): model1 = ols(y=y, x=x, window=20) with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): model2 = ols(y=y, x=x_with, window=20, intercept=False) assert_frame_equal(model1.beta, model2.beta) self.assertTrue((model1.r2 != model2.r2).all())
def test_tsplot(self): from pandas.tseries.plotting import tsplot import matplotlib.pyplot as plt ax = plt.gca() ts = tm.makeTimeSeries() f = lambda *args, **kwds: tsplot(s, plt.Axes.plot, *args, **kwds) for s in self.period_ser: _check_plot_works(f, s.index.freq, ax=ax, series=s) for s in self.datetime_ser: _check_plot_works(f, s.index.freq.rule_code, ax=ax, series=s) for s in self.period_ser: _check_plot_works(s.plot, ax=ax) for s in self.datetime_ser: _check_plot_works(s.plot, ax=ax) ax = ts.plot(style='k') color = (0., 0., 0., 1) if self.mpl_ge_2_0_0 else (0., 0., 0.) self.assertEqual(color, ax.get_lines()[0].get_color())
def test_reopen_handle(self): self.store['a'] = tm.makeTimeSeries() self.store.open('w', warn=False) self.assert_(self.store.handle.isopen) self.assertEquals(len(self.store), 0)
def test_flush(self): self.store['a'] = tm.makeTimeSeries() self.store.flush()
def setUp(self): self.ts = tm.makeTimeSeries()
def setup_method(self): self.ts = tm.makeTimeSeries() # Was at top level in test_series self.ts.name = "ts" self.series = tm.makeStringSeries() self.series.name = "series"
def test_y_predict(self): y = tm.makeTimeSeries() x = tm.makeTimeDataFrame() model1 = ols(y=y, x=x) assert_series_equal(model1.y_predict, model1.y_fitted) assert_almost_equal(model1._y_predict_raw, model1._y_fitted_raw)
def datetime_series_short(): """ Fixture for Series of floats with DatetimeIndex """ return tm.makeTimeSeries(nper=30)[5:]
def setUp(self): self.ts = tm.makeTimeSeries() # Was at top level in test_series self.ts.name = 'ts' self.series = tm.makeStringSeries() self.series.name = 'series'
from pandas.util._decorators import cache_readonly import pandas.util.testing as tm import pandas as pd _ts = tm.makeTimeSeries() class TestData(object): @cache_readonly def ts(self): ts = _ts.copy() ts.name = 'ts' return ts @cache_readonly def series(self): series = tm.makeStringSeries() series.name = 'series' return series @cache_readonly def objSeries(self): objSeries = tm.makeObjectSeries() objSeries.name = 'objects' return objSeries @cache_readonly def empty(self): return pd.Series([], index=[])
def ts(): return tm.makeTimeSeries()
def test_series_rhs(self): y = tm.makeTimeSeries() x = tm.makeTimeSeries() model = ols(y=y, x=x) expected = ols(y=y, x={'x' : x}) assert_series_equal(model.beta, expected.beta)
def test_dataframe(self): bts = DataFrame({'a': tm.makeTimeSeries()}) ax = bts.plot() idx = ax.get_lines()[0].get_xdata()
def test_overwrite_node(self): self.store['a'] = tm.makeTimeDataFrame() ts = tm.makeTimeSeries() self.store['a'] = ts tm.assert_series_equal(self.store['a'], ts)
def test_dataframe(self): bts = DataFrame({'a': tm.makeTimeSeries()}) ax = bts.plot() idx = ax.get_lines()[0].get_xdata() tm.assert_index_equal(bts.index.to_period(), PeriodIndex(idx))
def ts2(self): return tm.makeTimeSeries(nper=30)[5:]
def test_series_describe_single(): ts = tm.makeTimeSeries() grouped = ts.groupby(lambda x: x.month) result = grouped.apply(lambda x: x.describe()) expected = grouped.describe().stack() tm.assert_series_equal(result, expected)
def test_reindex(self, float_frame): datetime_series = tm.makeTimeSeries(nper=30) newFrame = float_frame.reindex(datetime_series.index) for col in newFrame.columns: for idx, val in newFrame[col].items(): if idx in float_frame.index: if np.isnan(val): assert np.isnan(float_frame[col][idx]) else: assert val == float_frame[col][idx] else: assert np.isnan(val) for col, series in newFrame.items(): assert tm.equalContents(series.index, newFrame.index) emptyFrame = float_frame.reindex(Index([])) assert len(emptyFrame.index) == 0 # Cython code should be unit-tested directly nonContigFrame = float_frame.reindex(datetime_series.index[::2]) for col in nonContigFrame.columns: for idx, val in nonContigFrame[col].items(): if idx in float_frame.index: if np.isnan(val): assert np.isnan(float_frame[col][idx]) else: assert val == float_frame[col][idx] else: assert np.isnan(val) for col, series in nonContigFrame.items(): assert tm.equalContents(series.index, nonContigFrame.index) # corner cases # Same index, copies values but not index if copy=False newFrame = float_frame.reindex(float_frame.index, copy=False) assert newFrame.index is float_frame.index # length zero newFrame = float_frame.reindex([]) assert newFrame.empty assert len(newFrame.columns) == len(float_frame.columns) # length zero with columns reindexed with non-empty index newFrame = float_frame.reindex([]) newFrame = newFrame.reindex(float_frame.index) assert len(newFrame.index) == len(float_frame.index) assert len(newFrame.columns) == len(float_frame.columns) # pass non-Index newFrame = float_frame.reindex(list(datetime_series.index)) tm.assert_index_equal(newFrame.index, datetime_series.index) # copy with no axes result = float_frame.reindex() assert_frame_equal(result, float_frame) assert result is not float_frame
def test_pickle_preserve_name(self): for n in [777, 777., 'name', datetime(2001, 11, 11), (1, 2)]: unpickled = self._pickle_roundtrip_name(tm.makeTimeSeries(name=n)) assert unpickled.name == n