def test_setitem(self): # not sure what else to do here series = self.frame['A'][::2] self.frame['col5'] = series self.assert_('col5' in self.frame) common.assert_dict_equal(series, self.frame['col5'], compare_keys=False) series = self.frame['A'] self.frame['col6'] = series common.assert_dict_equal(series, self.frame['col6'], compare_keys=False) self.assertRaises(Exception, self.frame.__setitem__, randn(len(self.frame) + 1)) # set ndarray arr = randn(len(self.frame)) self.frame['col9'] = arr self.assert_((self.frame['col9'] == arr).all()) # set value, do out of order for DataMatrix self.frame['col7'] = 5 assert ((self.frame['col7'] == 5).all()) self.frame['col8'] = 'foo' assert ((self.frame['col8'] == 'foo').all()) smaller = self.frame[:2] smaller['col10'] = ['1', '2'] self.assertEqual(smaller['col10'].dtype, np.object_) self.assert_((smaller['col10'] == ['1', '2']).all())
def test_setitem(self): # not sure what else to do here series = self.frame['A'][::2] self.frame['col5'] = series self.assert_('col5' in self.frame) common.assert_dict_equal(series, self.frame['col5'], compare_keys=False) series = self.frame['A'] self.frame['col6'] = series common.assert_dict_equal(series, self.frame['col6'], compare_keys=False) self.assertRaises(Exception, self.frame.__setitem__, randn(len(self.frame) + 1)) # set ndarray arr = randn(len(self.frame)) self.frame['col9'] = arr self.assert_((self.frame['col9'] == arr).all()) # set value, do out of order for DataMatrix self.frame['col7'] = 5 assert((self.frame['col7'] == 5).all()) self.frame['col8'] = 'foo' assert((self.frame['col8'] == 'foo').all()) smaller = self.frame[:2] smaller['col10'] = ['1', '2'] self.assertEqual(smaller['col10'].dtype, np.object_) self.assert_((smaller['col10'] == ['1', '2']).all())
def test_repr(self): str(self.ts) str(self.series) str(self.series.astype(int)) str(self.objSeries) str(Series(tm.randn(1000), index=np.arange(1000))) str(Series(tm.randn(1000), index=np.arange(1000, 0, step=-1))) # empty str(self.empty) # with NaNs self.series[5:7] = np.NaN str(self.series) # with Nones ots = self.ts.astype('O') ots[::2] = None repr(ots) # various names for name in [ '', 1, 1.2, 'foo', u('\u03B1\u03B2\u03B3'), 'loooooooooooooooooooooooooooooooooooooooooooooooooooong', ('foo', 'bar', 'baz'), (1, 2), ('foo', 1, 2.3), (u('\u03B1'), u('\u03B2'), u('\u03B3')), (u('\u03B1'), 'bar') ]: self.series.name = name repr(self.series) biggie = Series(tm.randn(1000), index=np.arange(1000), name=('foo', 'bar', 'baz')) repr(biggie) # 0 as name ser = Series(np.random.randn(100), name=0) rep_str = repr(ser) assert "Name: 0" in rep_str # tidy repr ser = Series(np.random.randn(1001), name=0) rep_str = repr(ser) assert "Name: 0" in rep_str ser = Series(["a\n\r\tb"], name="a\n\r\td", index=["a\n\r\tf"]) assert "\t" not in repr(ser) assert "\r" not in repr(ser) assert "a\n" not in repr(ser) # with empty series (#4651) s = Series([], dtype=np.int64, name='foo') self.assertEqual(repr(s), 'Series([], Name: foo, dtype: int64)') s = Series([], dtype=np.int64, name=None) self.assertEqual(repr(s), 'Series([], dtype: int64)')
def test_consolidate_ordering_issues(self): self.mgr.set('f', randn(N)) self.mgr.set('d', randn(N)) self.mgr.set('b', randn(N)) self.mgr.set('g', randn(N)) cons = self.mgr.consolidate() self.assertEquals(cons.nblocks, 1) self.assert_(cons.blocks[0].items.equals(cons.items))
def test_consolidate_ordering_issues(self): self.mgr.set("f", randn(N)) self.mgr.set("d", randn(N)) self.mgr.set("b", randn(N)) self.mgr.set("g", randn(N)) self.mgr.set("h", randn(N)) cons = self.mgr.consolidate() self.assertEqual(cons.nblocks, 1) assert_almost_equal(cons.blocks[0].mgr_locs, np.arange(len(cons.items)))
def test_repr(self): str(self.ts) str(self.series) str(self.series.astype(int)) str(self.objSeries) str(Series(tm.randn(1000), index=np.arange(1000))) str(Series(tm.randn(1000), index=np.arange(1000, 0, step=-1))) # empty str(self.empty) # with NaNs self.series[5:7] = np.NaN str(self.series) # with Nones ots = self.ts.astype('O') ots[::2] = None repr(ots) # various names for name in ['', 1, 1.2, 'foo', u('\u03B1\u03B2\u03B3'), 'loooooooooooooooooooooooooooooooooooooooooooooooooooong', ('foo', 'bar', 'baz'), (1, 2), ('foo', 1, 2.3), (u('\u03B1'), u('\u03B2'), u('\u03B3')), (u('\u03B1'), 'bar')]: self.series.name = name repr(self.series) biggie = Series(tm.randn(1000), index=np.arange(1000), name=('foo', 'bar', 'baz')) repr(biggie) # 0 as name ser = Series(np.random.randn(100), name=0) rep_str = repr(ser) assert "Name: 0" in rep_str # tidy repr ser = Series(np.random.randn(1001), name=0) rep_str = repr(ser) assert "Name: 0" in rep_str ser = Series(["a\n\r\tb"], name="a\n\r\td", index=["a\n\r\tf"]) assert "\t" not in repr(ser) assert "\r" not in repr(ser) assert "a\n" not in repr(ser) # with empty series (#4651) s = Series([], dtype=np.int64, name='foo') assert repr(s) == 'Series([], Name: foo, dtype: int64)' s = Series([], dtype=np.int64, name=None) assert repr(s) == 'Series([], dtype: int64)'
def test_consolidate_ordering_issues(self): self.mgr.set('f', randn(N)) self.mgr.set('d', randn(N)) self.mgr.set('b', randn(N)) self.mgr.set('g', randn(N)) self.mgr.set('h', randn(N)) cons = self.mgr.consolidate() self.assertEqual(cons.nblocks, 1) assert_almost_equal(cons.blocks[0].mgr_locs, np.arange(len(cons.items)))
def test_consolidate_ordering_issues(self): self.mgr.set('f', randn(N)) self.mgr.set('d', randn(N)) self.mgr.set('b', randn(N)) self.mgr.set('g', randn(N)) self.mgr.set('h', randn(N)) cons = self.mgr.consolidate() self.assertEquals(cons.nblocks, 1) assert_almost_equal(cons.blocks[0].mgr_locs, np.arange(len(cons.items)))
def test_merge(self): avals = randn(2, 10) bvals = randn(2, 10) ref_cols = Index(['e', 'a', 'b', 'd', 'f']) ablock = make_block(avals, ref_cols.get_indexer(['e', 'b'])) bblock = make_block(bvals, ref_cols.get_indexer(['a', 'd'])) merged = ablock.merge(bblock) assert_almost_equal(merged.mgr_locs, [0, 1, 2, 3]) assert_almost_equal(merged.values[[0, 2]], avals) assert_almost_equal(merged.values[[1, 3]], bvals)
def test_merge(self): avals = randn(2, 10) bvals = randn(2, 10) ref_cols = Index(["e", "a", "b", "d", "f"]) ablock = make_block(avals, ref_cols.get_indexer(["e", "b"])) bblock = make_block(bvals, ref_cols.get_indexer(["a", "d"])) merged = ablock.merge(bblock) assert_almost_equal(merged.mgr_locs, [0, 1, 2, 3]) assert_almost_equal(merged.values[[0, 2]], avals) assert_almost_equal(merged.values[[1, 3]], bvals)
def test_merge(self): avals = randn(2, 10) bvals = randn(2, 10) ref_cols = Index(['e', 'a', 'b', 'd', 'f']) ablock = make_block(avals, ref_cols.get_indexer(['e', 'b'])) bblock = make_block(bvals, ref_cols.get_indexer(['a', 'd'])) merged = ablock.merge(bblock) tm.assert_numpy_array_equal(merged.mgr_locs.as_array, np.array([0, 1, 2, 3], dtype=np.int64)) tm.assert_numpy_array_equal(merged.values[[0, 2]], np.array(avals)) tm.assert_numpy_array_equal(merged.values[[1, 3]], np.array(bvals))
def test_consolidate_ordering_issues(self): self.mgr.set('f', randn(N)) self.mgr.set('d', randn(N)) self.mgr.set('b', randn(N)) self.mgr.set('g', randn(N)) self.mgr.set('h', randn(N)) # we have datetime/tz blocks in self.mgr cons = self.mgr.consolidate() self.assertEqual(cons.nblocks, 4) cons = self.mgr.consolidate().get_numeric_data() self.assertEqual(cons.nblocks, 1) assert_almost_equal(cons.blocks[0].mgr_locs, np.arange(len(cons.items)))
def test_merge(self): avals = randn(2, 10) bvals = randn(2, 10) ref_cols = ['e', 'a', 'b', 'd', 'f'] ablock = make_block(avals, ['e', 'b'], ref_cols) bblock = make_block(bvals, ['a', 'd'], ref_cols) merged = ablock.merge(bblock) exvals = np.vstack((avals, bvals)) excols = ['e', 'b', 'a', 'd'] eblock = make_block(exvals, excols, ref_cols) eblock = eblock.reindex_items_from(ref_cols) assert_block_equal(merged, eblock)
def test_set_change_dtype(self): self.mgr.set("baz", np.zeros(N, dtype=bool)) self.mgr.set("baz", np.repeat("foo", N)) self.assertEqual(self.mgr.get("baz").dtype, np.object_) mgr2 = self.mgr.consolidate() mgr2.set("baz", np.repeat("foo", N)) self.assertEqual(mgr2.get("baz").dtype, np.object_) mgr2.set("quux", randn(N).astype(int)) self.assertEqual(mgr2.get("quux").dtype, np.int_) mgr2.set("quux", randn(N)) self.assertEqual(mgr2.get("quux").dtype, np.float_)
def test_consolidate_ordering_issues(self): self.mgr.set('f', randn(N)) self.mgr.set('d', randn(N)) self.mgr.set('b', randn(N)) self.mgr.set('g', randn(N)) self.mgr.set('h', randn(N)) # we have datetime/tz blocks in self.mgr cons = self.mgr.consolidate() self.assertEqual(cons.nblocks, 4) cons = self.mgr.consolidate().get_numeric_data() self.assertEqual(cons.nblocks, 1) tm.assertIsInstance(cons.blocks[0].mgr_locs, lib.BlockPlacement) tm.assert_numpy_array_equal(cons.blocks[0].mgr_locs.as_array, np.arange(len(cons.items), dtype=np.int64))
def test_set_change_dtype(self, mgr): mgr.set('baz', np.zeros(N, dtype=bool)) mgr.set('baz', np.repeat('foo', N)) assert mgr.get('baz').dtype == np.object_ mgr2 = mgr.consolidate() mgr2.set('baz', np.repeat('foo', N)) assert mgr2.get('baz').dtype == np.object_ mgr2.set('quux', randn(N).astype(int)) assert mgr2.get('quux').dtype == np.int_ mgr2.set('quux', randn(N)) assert mgr2.get('quux').dtype == np.float_
def test_set_change_dtype(self, mgr): mgr.set("baz", np.zeros(N, dtype=bool)) mgr.set("baz", np.repeat("foo", N)) assert mgr.get("baz").dtype == np.object_ mgr2 = mgr.consolidate() mgr2.set("baz", np.repeat("foo", N)) assert mgr2.get("baz").dtype == np.object_ mgr2.set("quux", tm.randn(N).astype(int)) assert mgr2.get("quux").dtype == np.int_ mgr2.set("quux", tm.randn(N)) assert mgr2.get("quux").dtype == np.float_
def test_consolidate_ordering_issues(self, mgr): mgr.set('f', randn(N)) mgr.set('d', randn(N)) mgr.set('b', randn(N)) mgr.set('g', randn(N)) mgr.set('h', randn(N)) # we have datetime/tz blocks in mgr cons = mgr.consolidate() assert cons.nblocks == 4 cons = mgr.consolidate().get_numeric_data() assert cons.nblocks == 1 assert isinstance(cons.blocks[0].mgr_locs, BlockPlacement) tm.assert_numpy_array_equal(cons.blocks[0].mgr_locs.as_array, np.arange(len(cons.items), dtype=np.int64))
def test_set_change_dtype(self): self.mgr.set('baz', np.zeros(N, dtype=bool)) self.mgr.set('baz', np.repeat('foo', N)) self.assert_(self.mgr.get('baz').dtype == np.object_) mgr2 = self.mgr.consolidate() mgr2.set('baz', np.repeat('foo', N)) self.assert_(mgr2.get('baz').dtype == np.object_) mgr2.set('quux', randn(N).astype(int)) self.assert_(mgr2.get('quux').dtype == np.int64) mgr2.set('quux', randn(N)) self.assert_(mgr2.get('quux').dtype == np.float_)
def test_set_change_dtype(self): self.mgr.set('baz', np.zeros(N, dtype=bool)) self.mgr.set('baz', np.repeat('foo', N)) self.assert_(self.mgr.get('baz').dtype == np.object_) mgr2 = self.mgr.consolidate() mgr2.set('baz', np.repeat('foo', N)) self.assert_(mgr2.get('baz').dtype == np.object_) mgr2.set('quux', randn(N).astype(int)) self.assert_(mgr2.get('quux').dtype == np.int_) mgr2.set('quux', randn(N)) self.assert_(mgr2.get('quux').dtype == np.float_)
def test_combine_first(self): series = Series(common.makeIntIndex(20).astype(float), index=common.makeIntIndex(20)) series_copy = series * 2 series_copy[::2] = np.NaN # nothing used from the input combined = series.combine_first(series_copy) self.assert_(np.array_equal(combined, series)) # Holes filled from input combined = series_copy.combine_first(series) self.assert_(np.isfinite(combined).all()) self.assert_(np.array_equal(combined[::2], series[::2])) self.assert_(np.array_equal(combined[1::2], series_copy[1::2])) # mixed types index = common.makeStringIndex(20) floats = Series(common.randn(20), index=index) strings = Series(common.makeStringIndex(10), index=index[::2]) combined = strings.combine_first(floats) common.assert_dict_equal(strings, combined, compare_keys=False) common.assert_dict_equal(floats[1::2], combined, compare_keys=False) # corner case s = Series([1., 2, 3], index=[0, 1, 2]) result = s.combine_first(Series([], index=[])) assert_series_equal(s, result)
def test_constructor_dict_cast(self): # cast float tests test_data = { 'A': { '1': 1, '2': 2 }, 'B': { '1': '1', '2': '2', '3': '3' }, } frame = self.klass(test_data, dtype=float) self.assertEqual(len(frame), 3) self.assert_(frame['B'].dtype == np.float_) self.assert_(frame['A'].dtype == np.float_) frame = self.klass(test_data) self.assertEqual(len(frame), 3) self.assert_(frame['B'].dtype == np.object_) self.assert_(frame['A'].dtype == np.float_) # can't cast to float test_data = { 'A': dict(zip(range(20), common.makeDateIndex(20))), 'B': dict(zip(range(15), randn(15))) } frame = self.klass(test_data, dtype=float) self.assertEqual(len(frame), 20) self.assert_(frame['A'].dtype == np.object_) self.assert_(frame['B'].dtype == np.float_)
def test_combineFirst(self): series = Series(common.makeIntIndex(20).astype(float), index=common.makeIntIndex(20)) series_copy = series * 2 series_copy[::2] = np.NaN # nothing used from the input combined = series.combineFirst(series_copy) self.assert_(np.array_equal(combined, series)) # Holes filled from input combined = series_copy.combineFirst(series) self.assert_(np.isfinite(combined).all()) self.assert_(np.array_equal(combined[::2], series[::2])) self.assert_(np.array_equal(combined[1::2], series_copy[1::2])) # mixed types index = common.makeStringIndex(20) floats = Series(common.randn(20), index=index) strings = Series(common.makeStringIndex(10), index=index[::2]) combined = strings.combineFirst(floats) common.assert_dict_equal(strings, combined, compare_keys=False) common.assert_dict_equal(floats[1::2], combined, compare_keys=False) # corner case s = Series([1., 2, 3], index=[0, 1, 2]) result = s.combineFirst(Series([], index=[])) assert_series_equal(s, result)
def test_combine_first(self): values = tm.makeIntIndex(20).values.astype(float) series = Series(values, index=tm.makeIntIndex(20)) series_copy = series * 2 series_copy[::2] = np.NaN # nothing used from the input combined = series.combine_first(series_copy) tm.assert_series_equal(combined, series) # Holes filled from input combined = series_copy.combine_first(series) assert np.isfinite(combined).all() tm.assert_series_equal(combined[::2], series[::2]) tm.assert_series_equal(combined[1::2], series_copy[1::2]) # mixed types index = tm.makeStringIndex(20) floats = Series(tm.randn(20), index=index) strings = Series(tm.makeStringIndex(10), index=index[::2]) combined = strings.combine_first(floats) tm.assert_series_equal(strings, combined.loc[index[::2]]) tm.assert_series_equal(floats[1::2].astype(object), combined.loc[index[1::2]]) # corner case s = Series([1., 2, 3], index=[0, 1, 2]) result = s.combine_first(Series([], index=[])) assert_series_equal(s, result)
def test_constructor_dict_cast(self): # cast float tests test_data = { 'A' : {'1' : 1, '2' : 2}, 'B' : {'1' : '1', '2' : '2', '3' : '3'}, } frame = self.klass(test_data, dtype=float) self.assertEqual(len(frame), 3) self.assert_(frame['B'].dtype == np.float_) self.assert_(frame['A'].dtype == np.float_) frame = self.klass(test_data) self.assertEqual(len(frame), 3) self.assert_(frame['B'].dtype == np.object_) self.assert_(frame['A'].dtype == np.float_) # can't cast to float test_data = { 'A' : dict(zip(range(20), common.makeDateIndex(20))), 'B' : dict(zip(range(15), randn(15))) } frame = self.klass(test_data, dtype=float) self.assertEqual(len(frame), 20) self.assert_(frame['A'].dtype == np.object_) self.assert_(frame['B'].dtype == np.float_)
def test_repr(self): # empty foo = repr(self.empty) # empty with index frame = self.klass(index=np.arange(1000)) foo = repr(frame) # small one foo = repr(self.frame) # big one biggie = self.klass(np.zeros((1000, 4)), columns=range(4), index=range(1000)) foo = repr(biggie) # mixed foo = repr(self.mixed_frame) # big mixed biggie = self.klass( { 'A': randn(1000), 'B': common.makeStringIndex(1000) }, index=range(1000)) biggie['A'][:20] = np.NaN biggie['B'][:20] = np.NaN foo = repr(biggie) # exhausting cases in DataMatrix.info # columns but no index no_index = self.klass(columns=[0, 1, 3]) foo = repr(no_index) # no columns or index buf = StringIO() self.empty.info(buffer=buf) # columns are not sortable foo = repr(self.unsortable) # do not fail! self.frame.head(buffer=buf) self.frame.tail(buffer=buf) for i in range(5): self.frame['foo%d' % i] = 1 self.frame.head(buffer=buf) self.frame.tail(buffer=buf)
def test_dropEmptyRows(self): N = len(self.frame.index) mat = randn(N) mat[:5] = np.NaN frame = self.klass({'foo': mat}, index=self.frame.index) smaller_frame = frame.dropEmptyRows() self.assert_(np.array_equal(smaller_frame['foo'], mat[5:])) smaller_frame = frame.dropEmptyRows(['foo']) self.assert_(np.array_equal(smaller_frame['foo'], mat[5:]))
def test_dropEmptyRows(self): N = len(self.frame.index) mat = randn(N) mat[:5] = np.NaN frame = self.klass({'foo' : mat}, index=self.frame.index) smaller_frame = frame.dropEmptyRows() self.assert_(np.array_equal(smaller_frame['foo'], mat[5:])) smaller_frame = frame.dropEmptyRows(['foo']) self.assert_(np.array_equal(smaller_frame['foo'], mat[5:]))
def test_reindex_items(self): def _check_cols(before, after, cols): for col in cols: assert_almost_equal(after.get(col), before.get(col)) # not consolidated vals = randn(N) self.mgr.set('g', vals) reindexed = self.mgr.reindex_items(['g', 'c', 'a', 'd']) self.assertEquals(reindexed.nblocks, 2) assert_almost_equal(reindexed.get('g'), vals.squeeze()) _check_cols(self.mgr, reindexed, ['c', 'a', 'd'])
def test_repr(self): str(self.ts) str(self.series) str(self.series.astype(int)) str(self.objSeries) str(Series(tm.randn(1000), index=np.arange(1000))) # empty str(self.empty) # with NaNs self.series[5:7] = np.NaN str(self.series) # tuple name, e.g. from hierarchical index self.series.name = ('foo', 'bar', 'baz') repr(self.series) biggie = Series(tm.randn(1000), index=np.arange(1000), name=('foo', 'bar', 'baz')) repr(biggie)
def test_firstLastValid(self): N = len(self.frame.index) mat = randn(N) mat[:5] = np.NaN mat[-5:] = np.NaN frame = self.klass({'foo' : mat}, index=self.frame.index) index = frame._firstTimeWithValue() self.assert_(index == frame.index[5]) index = frame._lastTimeWithValue() self.assert_(index == frame.index[-6])
def test_dropIncompleteRows(self): N = len(self.frame.index) mat = randn(N) mat[:5] = np.NaN frame = self.klass({'foo' : mat}, index=self.frame.index) frame['bar'] = 5 smaller_frame = frame.dropIncompleteRows() self.assert_(np.array_equal(smaller_frame['foo'], mat[5:])) samesize_frame = frame.dropIncompleteRows(specificColumns=['bar']) self.assert_(samesize_frame.index.equals(self.frame.index))
def test_firstLastValid(self): N = len(self.frame.index) mat = randn(N) mat[:5] = np.NaN mat[-5:] = np.NaN frame = self.klass({'foo': mat}, index=self.frame.index) index = frame._firstTimeWithValue() self.assert_(index == frame.index[5]) index = frame._lastTimeWithValue() self.assert_(index == frame.index[-6])
def test_dropIncompleteRows(self): N = len(self.frame.index) mat = randn(N) mat[:5] = np.NaN frame = self.klass({'foo': mat}, index=self.frame.index) frame['bar'] = 5 smaller_frame = frame.dropIncompleteRows() self.assert_(np.array_equal(smaller_frame['foo'], mat[5:])) samesize_frame = frame.dropIncompleteRows(specificColumns=['bar']) self.assert_(samesize_frame.index.equals(self.frame.index))
def test_repr(self): # empty foo = repr(self.empty) # empty with index frame = self.klass(index=np.arange(1000)) foo = repr(frame) # small one foo = repr(self.frame) # big one biggie = self.klass(np.zeros((1000, 4)), columns=range(4), index=range(1000)) foo = repr(biggie) # mixed foo = repr(self.mixed_frame) # big mixed biggie = self.klass({'A' : randn(1000), 'B' : common.makeStringIndex(1000)}, index=range(1000)) biggie['A'][:20] = np.NaN biggie['B'][:20] = np.NaN foo = repr(biggie) # exhausting cases in DataMatrix.info # columns but no index no_index = self.klass(columns=[0, 1, 3]) foo = repr(no_index) # no columns or index buf = StringIO() self.empty.info(buffer=buf) # columns are not sortable foo = repr(self.unsortable) # do not fail! self.frame.head(buffer=buf) self.frame.tail(buffer=buf) for i in range(5): self.frame['foo%d' % i] = 1 self.frame.head(buffer=buf) self.frame.tail(buffer=buf)
def test_repr(self): str(self.ts) str(self.series) str(self.series.astype(int)) str(self.objSeries) str(Series(common.randn(1000), index=np.arange(1000))) # empty str(self.empty) # with NaNs self.series[5:7] = np.NaN str(self.series)
def test_toString(self): # big mixed biggie = self.klass({'A' : randn(1000), 'B' : common.makeStringIndex(1000)}, index=range(1000)) biggie['A'][:20] = np.NaN biggie['B'][:20] = np.NaN buf = StringIO() biggie.toString(buffer=buf) biggie.toString(buffer=buf, columns=['B', 'A'], colSpace=17) biggie.toString(buffer=buf, columns=['B', 'A'], formatters={'A' : lambda x: '%.1f' % x}) biggie.toString(buffer=buf, columns=['B', 'A'], float_format=str) frame = self.klass(index=np.arange(1000)) frame.toString(buffer=buf)
def test_toString(self): # big mixed biggie = self.klass( { 'A': randn(1000), 'B': common.makeStringIndex(1000) }, index=range(1000)) biggie['A'][:20] = np.NaN biggie['B'][:20] = np.NaN buf = StringIO() biggie.toString(buffer=buf) biggie.toString(buffer=buf, columns=['B', 'A'], colSpace=17) biggie.toString(buffer=buf, columns=['B', 'A'], formatters={'A': lambda x: '%.1f' % x}) biggie.toString(buffer=buf, columns=['B', 'A'], float_format=str) frame = self.klass(index=np.arange(1000)) frame.toString(buffer=buf)
def get_int32_ex(cols): mat = randn(N, 1).astype(np.int32) return make_block(mat.T, cols, TEST_COLS)
def get_int_ex(cols=['g'], dtype = np.int_): mat = randn(N, 1).astype(dtype) return make_block(mat.T, cols, TEST_COLS)
def get_dt_ex(cols=['h']): mat = randn(N, 1).astype(int).astype('M8[ns]') return make_block(mat.T, cols, TEST_COLS)
def get_int_ex(cols=['g']): mat = randn(N, 1).astype(int) return make_block(mat.T, cols, TEST_COLS)
def test_repr(self, datetime_series, string_series, object_series): str(datetime_series) str(string_series) str(string_series.astype(int)) str(object_series) str(Series(tm.randn(1000), index=np.arange(1000))) str(Series(tm.randn(1000), index=np.arange(1000, 0, step=-1))) # empty str(Series()) # with NaNs string_series[5:7] = np.NaN str(string_series) # with Nones ots = datetime_series.astype("O") ots[::2] = None repr(ots) # various names for name in [ "", 1, 1.2, "foo", "\u03B1\u03B2\u03B3", "loooooooooooooooooooooooooooooooooooooooooooooooooooong", ("foo", "bar", "baz"), (1, 2), ("foo", 1, 2.3), ("\u03B1", "\u03B2", "\u03B3"), ("\u03B1", "bar"), ]: string_series.name = name repr(string_series) biggie = Series(tm.randn(1000), index=np.arange(1000), name=("foo", "bar", "baz")) repr(biggie) # 0 as name ser = Series(np.random.randn(100), name=0) rep_str = repr(ser) assert "Name: 0" in rep_str # tidy repr ser = Series(np.random.randn(1001), name=0) rep_str = repr(ser) assert "Name: 0" in rep_str ser = Series(["a\n\r\tb"], name="a\n\r\td", index=["a\n\r\tf"]) assert "\t" not in repr(ser) assert "\r" not in repr(ser) assert "a\n" not in repr(ser) # with empty series (#4651) s = Series([], dtype=np.int64, name="foo") assert repr(s) == "Series([], Name: foo, dtype: int64)" s = Series([], dtype=np.int64, name=None) assert repr(s) == "Series([], dtype: int64)"