Python pandas.Panel() Examples
The following are 30
code examples of pandas.Panel().
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
, or try the search function
.
Example #1
Source File: pre_submission.py From MPContribs with MIT License | 7 votes |
def load_data(file_path): d = binarywave.load(file_path) dat = d["wave"]["wData"] if dat.shape[-1] not in [4, 6]: print ("invalid file") return labels = ( [ "Height", "Amplitude 1", "Amplitude 2", "Phase 1", "Phase 2", "Resonance Frequency", ] if dat.shape[-1] == 6 else ["Height", "Amplitude", "Phase", "z sensor"] ) return Panel(dat, minor_axis=labels).transpose(2, 0, 1).to_frame()
Example #2
Source File: test_concat.py From recruit with Apache License 2.0 | 6 votes |
def test_panel_concat_buglet(self, sort): # #2257 def make_panel(): index = 5 cols = 3 def df(): return DataFrame(np.random.randn(index, cols), index=["I%s" % i for i in range(index)], columns=["C%s" % i for i in range(cols)]) return Panel({"Item%s" % x: df() for x in ['A', 'B', 'C']}) panel1 = make_panel() panel2 = make_panel() panel2 = panel2.rename(major_axis={x: "%s_1" % x for x in panel2.major_axis}) panel3 = panel2.rename(major_axis=lambda x: '%s_1' % x) panel3 = panel3.rename(minor_axis=lambda x: '%s_1' % x) # it works! concat([panel1, panel3], axis=1, verify_integrity=True, sort=sort)
Example #3
Source File: test_datetime_index.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def test_resample_panel_numpy(): rng = date_range('1/1/2000', '6/30/2000') n = len(rng) with catch_warnings(record=True): panel = Panel(np.random.randn(3, n, 5), items=['one', 'two', 'three'], major_axis=rng, minor_axis=['a', 'b', 'c', 'd', 'e']) result = panel.resample('M', axis=1).apply(lambda x: x.mean(1)) expected = panel.resample('M', axis=1).mean() tm.assert_panel_equal(result, expected) panel = panel.swapaxes(1, 2) result = panel.resample('M', axis=2).apply(lambda x: x.mean(2)) expected = panel.resample('M', axis=2).mean() tm.assert_panel_equal(result, expected)
Example #4
Source File: test_panel.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def test_panel_assignment(self): with catch_warnings(record=True): # GH3777 wp = Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'], major_axis=date_range('1/1/2000', periods=5), minor_axis=['A', 'B', 'C', 'D']) wp2 = Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'], major_axis=date_range('1/1/2000', periods=5), minor_axis=['A', 'B', 'C', 'D']) # TODO: unused? # expected = wp.loc[['Item1', 'Item2'], :, ['A', 'B']] with pytest.raises(NotImplementedError): wp.loc[['Item1', 'Item2'], :, ['A', 'B']] = wp2.loc[ ['Item1', 'Item2'], :, ['A', 'B']] # to_assign = wp2.loc[['Item1', 'Item2'], :, ['A', 'B']] # wp.loc[['Item1', 'Item2'], :, ['A', 'B']] = to_assign # result = wp.loc[['Item1', 'Item2'], :, ['A', 'B']] # tm.assert_panel_equal(result,expected)
Example #5
Source File: test_subclass.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def test_to_panel_expanddim(self): # GH 9762 class SubclassedFrame(DataFrame): @property def _constructor_expanddim(self): return SubclassedPanel class SubclassedPanel(Panel): pass index = MultiIndex.from_tuples([(0, 0), (0, 1), (0, 2)]) df = SubclassedFrame({'X': [1, 2, 3], 'Y': [4, 5, 6]}, index=index) result = df.to_panel() assert isinstance(result, SubclassedPanel) expected = SubclassedPanel([[[1, 2, 3]], [[4, 5, 6]]], items=['X', 'Y'], major_axis=[0], minor_axis=[0, 1, 2], dtype='int64') tm.assert_panel_equal(result, expected)
Example #6
Source File: test_time_grouper.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def test_panel_aggregation(): ind = pd.date_range('1/1/2000', periods=100) data = np.random.randn(2, len(ind), 4) wp = Panel(data, items=['Item1', 'Item2'], major_axis=ind, minor_axis=['A', 'B', 'C', 'D']) tg = TimeGrouper('M', axis=1) _, grouper, _ = tg._get_grouper(wp) bingrouped = wp.groupby(grouper) binagg = bingrouped.mean() def f(x): assert (isinstance(x, Panel)) return x.mean(1) result = bingrouped.agg(f) tm.assert_panel_equal(result, binagg)
Example #7
Source File: Omlette.py From OpenTrader with GNU Lesser General Public License v3.0 | 6 votes |
def vAppendHdf(self, sKey, gData, gMetaData=None): oHdfStore = self.oHdfStore if oHdfStore is None: return if gData is None: # we need to check if the key exists pass # drop through # assert sCat in ['recipe', 'feed'], "ERROR: unrecognized category: " +sCat elif type(gData) in [pandas.Series, pandas.DataFrame, pandas.Panel]: self.oFd.write("INFO: HDF putting " +sKey +'\n') oHdfStore.put('/' +sKey, gData, format='table', data_columns=True) oHdfStore.flush() else: self.oFd.write("ERROR: unsupported datatype for %s: %r \n" % \ (sKey, type(gData),)) return if gMetaData is None: return if type(gMetaData) in [dict, OrderedDict]: # o = getattr(getattr(oHdfStore.root, sCat), sInst) o = oHdfStore.get_node('/'+sKey) o._v_attrs.metadata = [gMetaData] oHdfStore.flush()
Example #8
Source File: test_subclass.py From recruit with Apache License 2.0 | 6 votes |
def test_to_panel_expanddim(self): # GH 9762 class SubclassedFrame(DataFrame): @property def _constructor_expanddim(self): return SubclassedPanel class SubclassedPanel(Panel): pass index = MultiIndex.from_tuples([(0, 0), (0, 1), (0, 2)]) df = SubclassedFrame({'X': [1, 2, 3], 'Y': [4, 5, 6]}, index=index) result = df.to_panel() assert isinstance(result, SubclassedPanel) expected = SubclassedPanel([[[1, 2, 3]], [[4, 5, 6]]], items=['X', 'Y'], major_axis=[0], minor_axis=[0, 1, 2], dtype='int64') tm.assert_panel_equal(result, expected)
Example #9
Source File: test_panel.py From recruit with Apache License 2.0 | 6 votes |
def test_panel_assignment(self): with catch_warnings(record=True): # GH3777 wp = Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'], major_axis=date_range('1/1/2000', periods=5), minor_axis=['A', 'B', 'C', 'D']) wp2 = Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'], major_axis=date_range('1/1/2000', periods=5), minor_axis=['A', 'B', 'C', 'D']) # TODO: unused? # expected = wp.loc[['Item1', 'Item2'], :, ['A', 'B']] with pytest.raises(NotImplementedError): wp.loc[['Item1', 'Item2'], :, ['A', 'B']] = wp2.loc[ ['Item1', 'Item2'], :, ['A', 'B']] # to_assign = wp2.loc[['Item1', 'Item2'], :, ['A', 'B']] # wp.loc[['Item1', 'Item2'], :, ['A', 'B']] = to_assign # result = wp.loc[['Item1', 'Item2'], :, ['A', 'B']] # tm.assert_panel_equal(result,expected)
Example #10
Source File: test_time_grouper.py From recruit with Apache License 2.0 | 6 votes |
def test_panel_aggregation(): ind = pd.date_range('1/1/2000', periods=100) data = np.random.randn(2, len(ind), 4) wp = Panel(data, items=['Item1', 'Item2'], major_axis=ind, minor_axis=['A', 'B', 'C', 'D']) tg = TimeGrouper('M', axis=1) _, grouper, _ = tg._get_grouper(wp) bingrouped = wp.groupby(grouper) binagg = bingrouped.mean() def f(x): assert (isinstance(x, Panel)) return x.mean(1) result = bingrouped.agg(f) tm.assert_panel_equal(result, binagg)
Example #11
Source File: test_datetime_index.py From recruit with Apache License 2.0 | 6 votes |
def test_resample_panel_numpy(): rng = date_range('1/1/2000', '6/30/2000') n = len(rng) with catch_warnings(record=True): panel = Panel(np.random.randn(3, n, 5), items=['one', 'two', 'three'], major_axis=rng, minor_axis=['a', 'b', 'c', 'd', 'e']) result = panel.resample('M', axis=1).apply(lambda x: x.mean(1)) expected = panel.resample('M', axis=1).mean() tm.assert_panel_equal(result, expected) panel = panel.swapaxes(1, 2) result = panel.resample('M', axis=2).apply(lambda x: x.mean(2)) expected = panel.resample('M', axis=2).mean() tm.assert_panel_equal(result, expected)
Example #12
Source File: test_concat.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def test_panel_concat_buglet(self, sort): # #2257 def make_panel(): index = 5 cols = 3 def df(): return DataFrame(np.random.randn(index, cols), index=["I%s" % i for i in range(index)], columns=["C%s" % i for i in range(cols)]) return Panel({"Item%s" % x: df() for x in ['A', 'B', 'C']}) panel1 = make_panel() panel2 = make_panel() panel2 = panel2.rename(major_axis={x: "%s_1" % x for x in panel2.major_axis}) panel3 = panel2.rename(major_axis=lambda x: '%s_1' % x) panel3 = panel3.rename(minor_axis=lambda x: '%s_1' % x) # it works! concat([panel1, panel3], axis=1, verify_integrity=True, sort=sort)
Example #13
Source File: test_base.py From vnpy_crypto with MIT License | 6 votes |
def test_binary_ops_docs(self): from pandas import DataFrame, Panel op_map = {'add': '+', 'sub': '-', 'mul': '*', 'mod': '%', 'pow': '**', 'truediv': '/', 'floordiv': '//'} for op_name in ['add', 'sub', 'mul', 'mod', 'pow', 'truediv', 'floordiv']: for klass in [Series, DataFrame, Panel]: operand1 = klass.__name__.lower() operand2 = 'other' op = op_map[op_name] expected_str = ' '.join([operand1, op, operand2]) assert expected_str in getattr(klass, op_name).__doc__ # reverse version of the binary ops expected_str = ' '.join([operand2, op, operand1]) assert expected_str in getattr(klass, 'r' + op_name).__doc__
Example #14
Source File: test_label_or_level_utils.py From vnpy_crypto with MIT License | 6 votes |
def test_drop_labels_or_levels_series(df): # Make series with L1 as index s = df.set_index('L1').L2 assert_levels_dropped(s, ['L1'], axis=0) with tm.assert_raises_regex(ValueError, "not valid labels or levels"): s._drop_labels_or_levels('L4', axis=0) # Make series with L1 and L2 as index s = df.set_index(['L1', 'L2']).L3 assert_levels_dropped(s, ['L1', 'L2'], axis=0) with tm.assert_raises_regex(ValueError, "not valid labels or levels"): s._drop_labels_or_levels('L4', axis=0) # Panel # -----
Example #15
Source File: run_pandas.py From recipy with Apache License 2.0 | 6 votes |
def get_panel(self): """ Return a Panel consisting of two DataFrames, each consisting of two Series, each created using get_series. :return: Panel :rtype: pandas.Panel """ data1 = {'seriesOne': self.get_series(0, 5), 'seriesTwo': self.get_series(0, 5, 10)} frame1 = pd.DataFrame(data1) data2 = {'seriesThree': self.get_series(6, 10), 'seriesFour': self.get_series(6, 10, 10)} frame2 = pd.DataFrame(data2) data = {'frameOne': frame1, 'frameTwo': frame2} return pd.Panel(data)
Example #16
Source File: test_panel.py From vnpy_crypto with MIT License | 6 votes |
def test_panel_assignment(self): with catch_warnings(record=True): # GH3777 wp = Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'], major_axis=date_range('1/1/2000', periods=5), minor_axis=['A', 'B', 'C', 'D']) wp2 = Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'], major_axis=date_range('1/1/2000', periods=5), minor_axis=['A', 'B', 'C', 'D']) # TODO: unused? # expected = wp.loc[['Item1', 'Item2'], :, ['A', 'B']] def f(): wp.loc[['Item1', 'Item2'], :, ['A', 'B']] = wp2.loc[ ['Item1', 'Item2'], :, ['A', 'B']] pytest.raises(NotImplementedError, f) # to_assign = wp2.loc[['Item1', 'Item2'], :, ['A', 'B']] # wp.loc[['Item1', 'Item2'], :, ['A', 'B']] = to_assign # result = wp.loc[['Item1', 'Item2'], :, ['A', 'B']] # tm.assert_panel_equal(result,expected)
Example #17
Source File: test_subclass.py From vnpy_crypto with MIT License | 6 votes |
def test_to_panel_expanddim(self): # GH 9762 with catch_warnings(record=True): class SubclassedFrame(DataFrame): @property def _constructor_expanddim(self): return SubclassedPanel class SubclassedPanel(Panel): pass index = MultiIndex.from_tuples([(0, 0), (0, 1), (0, 2)]) df = SubclassedFrame({'X': [1, 2, 3], 'Y': [4, 5, 6]}, index=index) result = df.to_panel() assert isinstance(result, SubclassedPanel) expected = SubclassedPanel([[[1, 2, 3]], [[4, 5, 6]]], items=['X', 'Y'], major_axis=[0], minor_axis=[0, 1, 2], dtype='int64') tm.assert_panel_equal(result, expected)
Example #18
Source File: data.py From Computable with MIT License | 6 votes |
def _dl_mult_symbols(symbols, start, end, chunksize, retry_count, pause, method): stocks = {} for sym_group in _in_chunks(symbols, chunksize): for sym in sym_group: try: stocks[sym] = method(sym, start, end, retry_count, pause) except IOError: warnings.warn('Failed to read symbol: {0!r}, replacing with ' 'NaN.'.format(sym), SymbolWarning) stocks[sym] = np.nan try: return Panel(stocks).swapaxes('items', 'minor') except AttributeError: # cannot construct a panel with just 1D nans indicating no data raise RemoteDataError("No data fetched using " "{0!r}".format(method.__name__))
Example #19
Source File: test_resample.py From Computable with MIT License | 6 votes |
def test_resample_panel(self): rng = date_range('1/1/2000', '6/30/2000') n = len(rng) panel = Panel(np.random.randn(3, n, 5), items=['one', 'two', 'three'], major_axis=rng, minor_axis=['a', 'b', 'c', 'd', 'e']) result = panel.resample('M', axis=1) def p_apply(panel, f): result = {} for item in panel.items: result[item] = f(panel[item]) return Panel(result, items=panel.items) expected = p_apply(panel, lambda x: x.resample('M')) tm.assert_panel_equal(result, expected) panel2 = panel.swapaxes(1, 2) result = panel2.resample('M', axis=2) expected = p_apply(panel2, lambda x: x.resample('M', axis=1)) tm.assert_panel_equal(result, expected)
Example #20
Source File: test_resample.py From Computable with MIT License | 6 votes |
def test_resample_panel_numpy(self): rng = date_range('1/1/2000', '6/30/2000') n = len(rng) panel = Panel(np.random.randn(3, n, 5), items=['one', 'two', 'three'], major_axis=rng, minor_axis=['a', 'b', 'c', 'd', 'e']) result = panel.resample('M', how=lambda x: x.mean(1), axis=1) expected = panel.resample('M', how='mean', axis=1) tm.assert_panel_equal(result, expected) panel = panel.swapaxes(1, 2) result = panel.resample('M', how=lambda x: x.mean(2), axis=2) expected = panel.resample('M', how='mean', axis=2) tm.assert_panel_equal(result, expected)
Example #21
Source File: test_resample.py From Computable with MIT License | 6 votes |
def test_panel_aggregation(self): ind = pd.date_range('1/1/2000', periods=100) data = np.random.randn(2, len(ind), 4) wp = pd.Panel(data, items=['Item1', 'Item2'], major_axis=ind, minor_axis=['A', 'B', 'C', 'D']) tg = TimeGrouper('M', axis=1) grouper = tg.get_grouper(wp) bingrouped = wp.groupby(grouper) binagg = bingrouped.mean() def f(x): assert(isinstance(x, Panel)) return x.mean(1) result = bingrouped.agg(f) tm.assert_panel_equal(result, binagg)
Example #22
Source File: market.py From tushare with BSD 3-Clause "New" or "Revised" License | 5 votes |
def coins_snapshot(broker='hb', code='btc', size='5'): """ 获取实时快照数据 params: broker:hb,ok,chbtc code:btc,ltc,eth,etc,bcc size:<150 return Panel: asks,bids """ try: js = _get_data(URL[broker]['snapshot'] % (code, size)) if js is None: return js if broker == 'hb': timestr = js['ts'] timestr = int2time(timestr / 1000) if broker == 'ok': timestr = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) if broker == 'chbtc': timestr = js['timestamp'] timestr = int2time(timestr) asks = pd.DataFrame(js['asks'], columns = ['price', 'vol']) bids = pd.DataFrame(js['bids'], columns = ['price', 'vol']) asks['time'] = timestr bids['time'] = timestr djs = {"asks": asks, "bids": bids} pf = pd.Panel(djs) return pf except Exception: print(traceback.print_exc())
Example #23
Source File: interactive_brokers_price_handler.py From Odin with MIT License | 5 votes |
def __init__(self): """Initialize parameters of the Interactive Brokers price handler object. """ super(InteractiveBrokersPriceHandler, self).__init__() self.conn = ibConnection( clientId=IB.data_handler_id.value, port=IB.port.value ) self.conn.register(self.__tick_price_handler, message.tickPrice) if not self.conn.connect(): raise ValueError( "Odin was unable to connect to the Trader Workstation." ) # Set the target field to download data from. today = dt.datetime.today() open_t, close_t = dt.time(9, 30), dt.time(16) cur_t = today.time() # If today is a weekday and the timing is correct, then we use the most # recently observed price. Otherwise we use the close price. if today.weekday() < 5 and cur_t >= open_t and cur_t <= close_t: self.field = TickType.LAST else: self.field = TickType.CLOSE # Initialize a pandas panel to store the price data. self.bar = pd.Panel(items=[PriceFields.current_price.value])
Example #24
Source File: test_eval.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_panel_fails(self): x = Panel(randn(3, 4, 5)) y = Series(randn(10)) with pytest.raises(NotImplementedError): self.eval('x + y', local_dict={'x': x, 'y': y})
Example #25
Source File: data.py From Computable with MIT License | 5 votes |
def get_data_google(symbols=None, start=None, end=None, retry_count=3, pause=0.001, adjust_price=False, ret_index=False, chunksize=25, name=None): """ Returns DataFrame/Panel of historical stock prices from symbols, over date range, start to end. To avoid being penalized by Google Finance servers, pauses between downloading 'chunks' of symbols can be specified. Parameters ---------- symbols : string, array-like object (list, tuple, Series), or DataFrame Single stock symbol (ticker), array-like object of symbols or DataFrame with index containing stock symbols. start : string, (defaults to '1/1/2010') Starting date, timestamp. Parses many different kind of date representations (e.g., 'JAN-01-2010', '1/1/10', 'Jan, 1, 1980') end : string, (defaults to today) Ending date, timestamp. Same format as starting date. retry_count : int, default 3 Number of times to retry query request. pause : int, default 0 Time, in seconds, to pause between consecutive queries of chunks. If single value given for symbol, represents the pause between retries. chunksize : int, default 25 Number of symbols to download consecutively before intiating pause. Returns ------- hist_data : DataFrame (str) or Panel (array-like object, DataFrame) """ return _get_data_from(symbols, start, end, retry_count, pause, adjust_price, ret_index, chunksize, 'google', name)
Example #26
Source File: data.py From PGPortfolio with GNU General Public License v3.0 | 5 votes |
def panel_fillna(panel, type="bfill"): """ fill nan along the 3rd axis :param panel: the panel to be filled :param type: bfill or ffill """ frames = {} for item in panel.items: if type == "both": frames[item] = panel.loc[item].fillna(axis=1, method="bfill").\ fillna(axis=1, method="ffill") else: frames[item] = panel.loc[item].fillna(axis=1, method=type) return pd.Panel(frames)
Example #27
Source File: run_pandas.py From recipy with Apache License 2.0 | 5 votes |
def panel_to_excel(self): """ Use pandas.Panel.to_excel to save a file out.xls. """ file_name = os.path.join(self.data_dir, "out.xls") panel = self.get_panel() panel.to_excel(file_name) os.remove(file_name)
Example #28
Source File: test_datetime_index.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_resample_panel(): rng = date_range('1/1/2000', '6/30/2000') n = len(rng) with catch_warnings(record=True): simplefilter("ignore", FutureWarning) panel = Panel(np.random.randn(3, n, 5), items=['one', 'two', 'three'], major_axis=rng, minor_axis=['a', 'b', 'c', 'd', 'e']) result = panel.resample('M', axis=1).mean() def p_apply(panel, f): result = {} for item in panel.items: result[item] = f(panel[item]) return Panel(result, items=panel.items) expected = p_apply(panel, lambda x: x.resample('M').mean()) tm.assert_panel_equal(result, expected) panel2 = panel.swapaxes(1, 2) result = panel2.resample('M', axis=2).mean() expected = p_apply(panel2, lambda x: x.resample('M', axis=1).mean()) tm.assert_panel_equal(result, expected)
Example #29
Source File: indexing.py From Computable with MIT License | 5 votes |
def _align_panel(self, indexer, df): is_frame = self.obj.ndim == 2 is_panel = self.obj.ndim >= 3 raise NotImplementedError("cannot set using an indexer with a Panel " "yet!")
Example #30
Source File: run_pandas.py From recipy with Apache License 2.0 | 5 votes |
def panel_to_hdf(self): """ Use pandas.Panel.to_hdf to save a file out.hdf. """ file_name = os.path.join(self.data_dir, "out.hdf") panel = self.get_panel() panel.to_hdf(file_name, key="Sample", mode="w") os.remove(file_name)