def isnull(obj): ''' Replacement for numpy.isnan / -numpy.isfinite which is suitable for use on object arrays. Parameters ---------- arr: ndarray or object value Returns ------- boolean ndarray or boolean ''' if lib.isscalar(obj): return lib.checknull(obj) from pandas.core.generic import PandasObject if isinstance(obj, np.ndarray): return _isnull_ndarraylike(obj) elif isinstance(obj, PandasObject): # TODO: optimize for DataFrame, etc. return obj.apply(isnull) elif isinstance(obj, list) or hasattr(obj, '__array__'): return _isnull_ndarraylike(obj) else: return obj is None
def isnull(obj): ''' Replacement for numpy.isnan / -numpy.isfinite which is suitable for use on object arrays. Parameters ---------- arr: ndarray or object value Returns ------- boolean ndarray or boolean ''' if lib.isscalar(obj): return lib.checknull(obj) from pandas.core.generic import PandasObject if isinstance(obj, np.ndarray): return _isnull_ndarraylike(obj) elif isinstance(obj, PandasObject): # TODO: optimize for DataFrame, etc. return obj.apply(isnull) elif hasattr(obj, '__array__'): return _isnull_ndarraylike(obj) else: return obj is None
def _format(x): if self.na_rep is not None and lib.checknull(x): if x is None: return 'None' return self.na_rep else: # object dtype return '%s' % formatter(x)
def _isnull_new(obj): if is_scalar(obj): return lib.checknull(obj) # hack (for now) because MI registers as ndarray elif isinstance(obj, ABCMultiIndex): raise NotImplementedError("isnull is not defined for MultiIndex") elif isinstance(obj, (ABCSeries, np.ndarray, ABCIndexClass)): return _isnull_ndarraylike(obj) elif isinstance(obj, ABCGeneric): return obj._constructor(obj._data.isnull(func=isnull)) elif isinstance(obj, list) or hasattr(obj, '__array__'): return _isnull_ndarraylike(np.asarray(obj)) else: return obj is None
def _isnull_new(obj): if lib.isscalar(obj): return lib.checknull(obj) from pandas.core.generic import PandasObject if isinstance(obj, np.ndarray): return _isnull_ndarraylike(obj) elif isinstance(obj, PandasObject): # TODO: optimize for DataFrame, etc. return obj.apply(isnull) elif isinstance(obj, list) or hasattr(obj, '__array__'): return _isnull_ndarraylike(obj) else: return obj is None
def update_last_sale(self, event): # NOTE, PerformanceTracker already vetted as TRADE type sid = event.sid if sid not in self.positions: return 0 price = event.price if checknull(price): return 0 pos = self.positions[sid] pos.last_sale_date = event.dt pos.last_sale_price = price
def update_last_sale(self, event): # NOTE, PerformanceTracker already vetted as TRADE type sid = event.sid if sid not in self.positions: return price = event.price if not checknull(price): pos = self.positions[sid] pos.last_sale_date = event.dt pos.last_sale_price = price self._position_last_sale_prices[sid] = price self._position_values = None # invalidate cache sid = event.sid price = event.price
def update_last_sale(self, event): sid = event.sid if sid not in self.positions: return if event.type != TRADE_TYPE: return price = event.price if not checknull(price): pos = self.positions[sid] pos.last_sale_date = event.dt pos.last_sale_price = price self._position_last_sale_prices[sid] = price self._position_values = None # invalidate cache
def update_last_sale(self, event): # NOTE, PerformanceTracker already vetted as TRADE type,注意,PerformanceTracker已经审核为TRADE类型 sid = event.sid #print sid,self.positions if sid not in self.positions: return 0 price = event.price if checknull(price): return 0 pos = self.positions[sid] #print u'进入up_date_last_sale' #print pos pos.last_sale_date = event.dt pos.last_sale_price = price
def update_last_sale(self, event): # NOTE, PerformanceTracker already vetted as TRADE type sid = event.sid if sid not in self.positions: return 0 price = event.price if checknull(price): return 0 pos = self.positions[sid] old_price = pos.last_sale_price pos.last_sale_date = event.dt pos.last_sale_price = price # Calculate cash adjustment on assets with multipliers return ((price - old_price) * self._position_payout_multipliers[sid] * pos.amount)
def isnull(obj): ''' Replacement for numpy.isnan / -numpy.isfinite which is suitable for use on object arrays. Parameters ---------- arr: ndarray or object value Returns ------- boolean ndarray or boolean ''' if lib.isscalar(obj): return lib.checknull(obj) from pandas.core.generic import PandasObject from pandas import Series if isinstance(obj, np.ndarray): if obj.dtype.kind in ('O', 'S'): # Working around NumPy ticket 1542 shape = obj.shape result = np.empty(shape, dtype=bool) vec = lib.isnullobj(obj.ravel()) result[:] = vec.reshape(shape) if isinstance(obj, Series): result = Series(result, index=obj.index, copy=False) elif obj.dtype == np.dtype('M8[ns]'): # this is the NaT pattern result = np.array(obj).view('i8') == lib.iNaT else: result = -np.isfinite(obj) return result elif isinstance(obj, PandasObject): # TODO: optimize for DataFrame, etc. return obj.apply(isnull) else: return obj is None
def _format_value(self, val): if lib.checknull(val): val = self.na_rep if self.float_format is not None and com.is_float(val): val = float(self.float_format % val) return val
def _my_helper_csv(self, writer, na_rep=None, cols=None, header=True, index=True, index_label=None, float_format=None, write_dtypes=None): if cols is None: cols = self.columns series = {} for k, v in self._series.iteritems(): series[k] = v.values has_aliases = isinstance(header, (tuple, list, np.ndarray)) if has_aliases or header: if index: # should write something for index label if index_label is not False: if index_label is None: if isinstance(self.index, MultiIndex): index_label = [] for i, name in enumerate(self.index.names): if name is None: name = '' index_label.append(name) else: index_label = self.index.name if index_label is None: index_label = [''] else: index_label = [index_label] elif not isinstance(index_label, (list, tuple, np.ndarray)): # given a string for a DF with Index index_label = [index_label] encoded_labels = list(index_label) else: encoded_labels = [] if has_aliases: if len(header) != len(cols): raise ValueError(('Writing %d cols but got %d aliases' % (len(cols), len(header)))) else: write_cols = header else: write_cols = cols encoded_cols = list(write_cols) if write_dtypes: for j, col in enumerate(cols): encoded_cols[j] = "{0}:{1}".format(col, self._series[col].dtype) writer.writerow(encoded_labels + encoded_cols) else: if write_dtypes: for j, col in enumerate(cols): encoded_cols[j] = "{0}:{1}".format(col, self._series[col].dtype) writer.writerow(encoded_cols) encoded_cols = list(cols) writer.writerow(encoded_cols) data_index = self.index if isinstance(self.index, PeriodIndex): data_index = self.index.to_timestamp() nlevels = getattr(data_index, 'nlevels', 1) for j, idx in enumerate(data_index): row_fields = [] if index: if nlevels == 1: row_fields = [idx] else: # handle MultiIndex row_fields = list(idx) for i, col in enumerate(cols): val = series[col][j] if lib.checknull(val): val = na_rep if float_format is not None and com.is_float(val): val = float_format % val elif isinstance(val, np.datetime64): val = lib.Timestamp(val)._repr_base row_fields.append(val) writer.writerow(row_fields)