partition
stringclasses
3 values
func_name
stringlengths
1
134
docstring
stringlengths
1
46.9k
path
stringlengths
4
223
original_string
stringlengths
75
104k
code
stringlengths
75
104k
docstring_tokens
listlengths
1
1.97k
repo
stringlengths
7
55
language
stringclasses
1 value
url
stringlengths
87
315
code_tokens
listlengths
19
28.4k
sha
stringlengths
40
40
train
Block._replace_coerce
Replace value corresponding to the given boolean array with another value. Parameters ---------- to_replace : object or pattern Scalar to replace or regular expression to match. value : object Replacement object. inplace : bool, default False Perform inplace modification. regex : bool, default False If true, perform regular expression substitution. convert : bool, default True If true, try to coerce any object types to better types. mask : array-like of bool, optional True indicate corresponding element is ignored. Returns ------- A new block if there is anything to replace or the original block.
pandas/core/internals/blocks.py
def _replace_coerce(self, to_replace, value, inplace=True, regex=False, convert=False, mask=None): """ Replace value corresponding to the given boolean array with another value. Parameters ---------- to_replace : object or pattern Scalar to replace or regular expression to match. value : object Replacement object. inplace : bool, default False Perform inplace modification. regex : bool, default False If true, perform regular expression substitution. convert : bool, default True If true, try to coerce any object types to better types. mask : array-like of bool, optional True indicate corresponding element is ignored. Returns ------- A new block if there is anything to replace or the original block. """ if mask.any(): if not regex: self = self.coerce_to_target_dtype(value) return self.putmask(mask, value, inplace=inplace) else: return self._replace_single(to_replace, value, inplace=inplace, regex=regex, convert=convert, mask=mask) return self
def _replace_coerce(self, to_replace, value, inplace=True, regex=False, convert=False, mask=None): """ Replace value corresponding to the given boolean array with another value. Parameters ---------- to_replace : object or pattern Scalar to replace or regular expression to match. value : object Replacement object. inplace : bool, default False Perform inplace modification. regex : bool, default False If true, perform regular expression substitution. convert : bool, default True If true, try to coerce any object types to better types. mask : array-like of bool, optional True indicate corresponding element is ignored. Returns ------- A new block if there is anything to replace or the original block. """ if mask.any(): if not regex: self = self.coerce_to_target_dtype(value) return self.putmask(mask, value, inplace=inplace) else: return self._replace_single(to_replace, value, inplace=inplace, regex=regex, convert=convert, mask=mask) return self
[ "Replace", "value", "corresponding", "to", "the", "given", "boolean", "array", "with", "another", "value", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L1474-L1509
[ "def", "_replace_coerce", "(", "self", ",", "to_replace", ",", "value", ",", "inplace", "=", "True", ",", "regex", "=", "False", ",", "convert", "=", "False", ",", "mask", "=", "None", ")", ":", "if", "mask", ".", "any", "(", ")", ":", "if", "not", "regex", ":", "self", "=", "self", ".", "coerce_to_target_dtype", "(", "value", ")", "return", "self", ".", "putmask", "(", "mask", ",", "value", ",", "inplace", "=", "inplace", ")", "else", ":", "return", "self", ".", "_replace_single", "(", "to_replace", ",", "value", ",", "inplace", "=", "inplace", ",", "regex", "=", "regex", ",", "convert", "=", "convert", ",", "mask", "=", "mask", ")", "return", "self" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
NonConsolidatableMixIn.putmask
putmask the data to the block; we must be a single block and not generate other blocks return the resulting block Parameters ---------- mask : the condition to respect new : a ndarray/object align : boolean, perform alignment on other/cond, default is True inplace : perform inplace modification, default is False Returns ------- a new block, the result of the putmask
pandas/core/internals/blocks.py
def putmask(self, mask, new, align=True, inplace=False, axis=0, transpose=False): """ putmask the data to the block; we must be a single block and not generate other blocks return the resulting block Parameters ---------- mask : the condition to respect new : a ndarray/object align : boolean, perform alignment on other/cond, default is True inplace : perform inplace modification, default is False Returns ------- a new block, the result of the putmask """ inplace = validate_bool_kwarg(inplace, 'inplace') # use block's copy logic. # .values may be an Index which does shallow copy by default new_values = self.values if inplace else self.copy().values new_values, new = self._try_coerce_args(new_values, new) if isinstance(new, np.ndarray) and len(new) == len(mask): new = new[mask] mask = _safe_reshape(mask, new_values.shape) new_values[mask] = new new_values = self._try_coerce_result(new_values) return [self.make_block(values=new_values)]
def putmask(self, mask, new, align=True, inplace=False, axis=0, transpose=False): """ putmask the data to the block; we must be a single block and not generate other blocks return the resulting block Parameters ---------- mask : the condition to respect new : a ndarray/object align : boolean, perform alignment on other/cond, default is True inplace : perform inplace modification, default is False Returns ------- a new block, the result of the putmask """ inplace = validate_bool_kwarg(inplace, 'inplace') # use block's copy logic. # .values may be an Index which does shallow copy by default new_values = self.values if inplace else self.copy().values new_values, new = self._try_coerce_args(new_values, new) if isinstance(new, np.ndarray) and len(new) == len(mask): new = new[mask] mask = _safe_reshape(mask, new_values.shape) new_values[mask] = new new_values = self._try_coerce_result(new_values) return [self.make_block(values=new_values)]
[ "putmask", "the", "data", "to", "the", "block", ";", "we", "must", "be", "a", "single", "block", "and", "not", "generate", "other", "blocks" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L1564-L1597
[ "def", "putmask", "(", "self", ",", "mask", ",", "new", ",", "align", "=", "True", ",", "inplace", "=", "False", ",", "axis", "=", "0", ",", "transpose", "=", "False", ")", ":", "inplace", "=", "validate_bool_kwarg", "(", "inplace", ",", "'inplace'", ")", "# use block's copy logic.", "# .values may be an Index which does shallow copy by default", "new_values", "=", "self", ".", "values", "if", "inplace", "else", "self", ".", "copy", "(", ")", ".", "values", "new_values", ",", "new", "=", "self", ".", "_try_coerce_args", "(", "new_values", ",", "new", ")", "if", "isinstance", "(", "new", ",", "np", ".", "ndarray", ")", "and", "len", "(", "new", ")", "==", "len", "(", "mask", ")", ":", "new", "=", "new", "[", "mask", "]", "mask", "=", "_safe_reshape", "(", "mask", ",", "new_values", ".", "shape", ")", "new_values", "[", "mask", "]", "=", "new", "new_values", "=", "self", ".", "_try_coerce_result", "(", "new_values", ")", "return", "[", "self", ".", "make_block", "(", "values", "=", "new_values", ")", "]" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
NonConsolidatableMixIn._get_unstack_items
Get the placement, values, and mask for a Block unstack. This is shared between ObjectBlock and ExtensionBlock. They differ in that ObjectBlock passes the values, while ExtensionBlock passes the dummy ndarray of positions to be used by a take later. Parameters ---------- unstacker : pandas.core.reshape.reshape._Unstacker new_columns : Index All columns of the unstacked BlockManager. Returns ------- new_placement : ndarray[int] The placement of the new columns in `new_columns`. new_values : Union[ndarray, ExtensionArray] The first return value from _Unstacker.get_new_values. mask : ndarray[bool] The second return value from _Unstacker.get_new_values.
pandas/core/internals/blocks.py
def _get_unstack_items(self, unstacker, new_columns): """ Get the placement, values, and mask for a Block unstack. This is shared between ObjectBlock and ExtensionBlock. They differ in that ObjectBlock passes the values, while ExtensionBlock passes the dummy ndarray of positions to be used by a take later. Parameters ---------- unstacker : pandas.core.reshape.reshape._Unstacker new_columns : Index All columns of the unstacked BlockManager. Returns ------- new_placement : ndarray[int] The placement of the new columns in `new_columns`. new_values : Union[ndarray, ExtensionArray] The first return value from _Unstacker.get_new_values. mask : ndarray[bool] The second return value from _Unstacker.get_new_values. """ # shared with ExtensionBlock new_items = unstacker.get_new_columns() new_placement = new_columns.get_indexer(new_items) new_values, mask = unstacker.get_new_values() mask = mask.any(0) return new_placement, new_values, mask
def _get_unstack_items(self, unstacker, new_columns): """ Get the placement, values, and mask for a Block unstack. This is shared between ObjectBlock and ExtensionBlock. They differ in that ObjectBlock passes the values, while ExtensionBlock passes the dummy ndarray of positions to be used by a take later. Parameters ---------- unstacker : pandas.core.reshape.reshape._Unstacker new_columns : Index All columns of the unstacked BlockManager. Returns ------- new_placement : ndarray[int] The placement of the new columns in `new_columns`. new_values : Union[ndarray, ExtensionArray] The first return value from _Unstacker.get_new_values. mask : ndarray[bool] The second return value from _Unstacker.get_new_values. """ # shared with ExtensionBlock new_items = unstacker.get_new_columns() new_placement = new_columns.get_indexer(new_items) new_values, mask = unstacker.get_new_values() mask = mask.any(0) return new_placement, new_values, mask
[ "Get", "the", "placement", "values", "and", "mask", "for", "a", "Block", "unstack", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L1602-L1632
[ "def", "_get_unstack_items", "(", "self", ",", "unstacker", ",", "new_columns", ")", ":", "# shared with ExtensionBlock", "new_items", "=", "unstacker", ".", "get_new_columns", "(", ")", "new_placement", "=", "new_columns", ".", "get_indexer", "(", "new_items", ")", "new_values", ",", "mask", "=", "unstacker", ".", "get_new_values", "(", ")", "mask", "=", "mask", ".", "any", "(", "0", ")", "return", "new_placement", ",", "new_values", ",", "mask" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
ExtensionBlock._maybe_coerce_values
Unbox to an extension array. This will unbox an ExtensionArray stored in an Index or Series. ExtensionArrays pass through. No dtype coercion is done. Parameters ---------- values : Index, Series, ExtensionArray Returns ------- ExtensionArray
pandas/core/internals/blocks.py
def _maybe_coerce_values(self, values): """Unbox to an extension array. This will unbox an ExtensionArray stored in an Index or Series. ExtensionArrays pass through. No dtype coercion is done. Parameters ---------- values : Index, Series, ExtensionArray Returns ------- ExtensionArray """ if isinstance(values, (ABCIndexClass, ABCSeries)): values = values._values return values
def _maybe_coerce_values(self, values): """Unbox to an extension array. This will unbox an ExtensionArray stored in an Index or Series. ExtensionArrays pass through. No dtype coercion is done. Parameters ---------- values : Index, Series, ExtensionArray Returns ------- ExtensionArray """ if isinstance(values, (ABCIndexClass, ABCSeries)): values = values._values return values
[ "Unbox", "to", "an", "extension", "array", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L1651-L1667
[ "def", "_maybe_coerce_values", "(", "self", ",", "values", ")", ":", "if", "isinstance", "(", "values", ",", "(", "ABCIndexClass", ",", "ABCSeries", ")", ")", ":", "values", "=", "values", ".", "_values", "return", "values" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
ExtensionBlock.setitem
Set the value inplace, returning a same-typed block. This differs from Block.setitem by not allowing setitem to change the dtype of the Block. Parameters ---------- indexer : tuple, list-like, array-like, slice The subset of self.values to set value : object The value being set Returns ------- Block Notes ----- `indexer` is a direct slice/positional indexer. `value` must be a compatible shape.
pandas/core/internals/blocks.py
def setitem(self, indexer, value): """Set the value inplace, returning a same-typed block. This differs from Block.setitem by not allowing setitem to change the dtype of the Block. Parameters ---------- indexer : tuple, list-like, array-like, slice The subset of self.values to set value : object The value being set Returns ------- Block Notes ----- `indexer` is a direct slice/positional indexer. `value` must be a compatible shape. """ if isinstance(indexer, tuple): # we are always 1-D indexer = indexer[0] check_setitem_lengths(indexer, value, self.values) self.values[indexer] = value return self
def setitem(self, indexer, value): """Set the value inplace, returning a same-typed block. This differs from Block.setitem by not allowing setitem to change the dtype of the Block. Parameters ---------- indexer : tuple, list-like, array-like, slice The subset of self.values to set value : object The value being set Returns ------- Block Notes ----- `indexer` is a direct slice/positional indexer. `value` must be a compatible shape. """ if isinstance(indexer, tuple): # we are always 1-D indexer = indexer[0] check_setitem_lengths(indexer, value, self.values) self.values[indexer] = value return self
[ "Set", "the", "value", "inplace", "returning", "a", "same", "-", "typed", "block", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L1693-L1721
[ "def", "setitem", "(", "self", ",", "indexer", ",", "value", ")", ":", "if", "isinstance", "(", "indexer", ",", "tuple", ")", ":", "# we are always 1-D", "indexer", "=", "indexer", "[", "0", "]", "check_setitem_lengths", "(", "indexer", ",", "value", ",", "self", ".", "values", ")", "self", ".", "values", "[", "indexer", "]", "=", "value", "return", "self" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
ExtensionBlock.take_nd
Take values according to indexer and return them as a block.
pandas/core/internals/blocks.py
def take_nd(self, indexer, axis=0, new_mgr_locs=None, fill_tuple=None): """ Take values according to indexer and return them as a block. """ if fill_tuple is None: fill_value = None else: fill_value = fill_tuple[0] # axis doesn't matter; we are really a single-dim object # but are passed the axis depending on the calling routing # if its REALLY axis 0, then this will be a reindex and not a take new_values = self.values.take(indexer, fill_value=fill_value, allow_fill=True) if self.ndim == 1 and new_mgr_locs is None: new_mgr_locs = [0] else: if new_mgr_locs is None: new_mgr_locs = self.mgr_locs return self.make_block_same_class(new_values, new_mgr_locs)
def take_nd(self, indexer, axis=0, new_mgr_locs=None, fill_tuple=None): """ Take values according to indexer and return them as a block. """ if fill_tuple is None: fill_value = None else: fill_value = fill_tuple[0] # axis doesn't matter; we are really a single-dim object # but are passed the axis depending on the calling routing # if its REALLY axis 0, then this will be a reindex and not a take new_values = self.values.take(indexer, fill_value=fill_value, allow_fill=True) if self.ndim == 1 and new_mgr_locs is None: new_mgr_locs = [0] else: if new_mgr_locs is None: new_mgr_locs = self.mgr_locs return self.make_block_same_class(new_values, new_mgr_locs)
[ "Take", "values", "according", "to", "indexer", "and", "return", "them", "as", "a", "block", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L1733-L1754
[ "def", "take_nd", "(", "self", ",", "indexer", ",", "axis", "=", "0", ",", "new_mgr_locs", "=", "None", ",", "fill_tuple", "=", "None", ")", ":", "if", "fill_tuple", "is", "None", ":", "fill_value", "=", "None", "else", ":", "fill_value", "=", "fill_tuple", "[", "0", "]", "# axis doesn't matter; we are really a single-dim object", "# but are passed the axis depending on the calling routing", "# if its REALLY axis 0, then this will be a reindex and not a take", "new_values", "=", "self", ".", "values", ".", "take", "(", "indexer", ",", "fill_value", "=", "fill_value", ",", "allow_fill", "=", "True", ")", "if", "self", ".", "ndim", "==", "1", "and", "new_mgr_locs", "is", "None", ":", "new_mgr_locs", "=", "[", "0", "]", "else", ":", "if", "new_mgr_locs", "is", "None", ":", "new_mgr_locs", "=", "self", ".", "mgr_locs", "return", "self", ".", "make_block_same_class", "(", "new_values", ",", "new_mgr_locs", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
ExtensionBlock._slice
return a slice of my values
pandas/core/internals/blocks.py
def _slice(self, slicer): """ return a slice of my values """ # slice the category # return same dims as we currently have if isinstance(slicer, tuple) and len(slicer) == 2: if not com.is_null_slice(slicer[0]): raise AssertionError("invalid slicing for a 1-ndim " "categorical") slicer = slicer[1] return self.values[slicer]
def _slice(self, slicer): """ return a slice of my values """ # slice the category # return same dims as we currently have if isinstance(slicer, tuple) and len(slicer) == 2: if not com.is_null_slice(slicer[0]): raise AssertionError("invalid slicing for a 1-ndim " "categorical") slicer = slicer[1] return self.values[slicer]
[ "return", "a", "slice", "of", "my", "values" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L1761-L1773
[ "def", "_slice", "(", "self", ",", "slicer", ")", ":", "# slice the category", "# return same dims as we currently have", "if", "isinstance", "(", "slicer", ",", "tuple", ")", "and", "len", "(", "slicer", ")", "==", "2", ":", "if", "not", "com", ".", "is_null_slice", "(", "slicer", "[", "0", "]", ")", ":", "raise", "AssertionError", "(", "\"invalid slicing for a 1-ndim \"", "\"categorical\"", ")", "slicer", "=", "slicer", "[", "1", "]", "return", "self", ".", "values", "[", "slicer", "]" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
ExtensionBlock.concat_same_type
Concatenate list of single blocks of the same type.
pandas/core/internals/blocks.py
def concat_same_type(self, to_concat, placement=None): """ Concatenate list of single blocks of the same type. """ values = self._holder._concat_same_type( [blk.values for blk in to_concat]) placement = placement or slice(0, len(values), 1) return self.make_block_same_class(values, ndim=self.ndim, placement=placement)
def concat_same_type(self, to_concat, placement=None): """ Concatenate list of single blocks of the same type. """ values = self._holder._concat_same_type( [blk.values for blk in to_concat]) placement = placement or slice(0, len(values), 1) return self.make_block_same_class(values, ndim=self.ndim, placement=placement)
[ "Concatenate", "list", "of", "single", "blocks", "of", "the", "same", "type", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L1790-L1798
[ "def", "concat_same_type", "(", "self", ",", "to_concat", ",", "placement", "=", "None", ")", ":", "values", "=", "self", ".", "_holder", ".", "_concat_same_type", "(", "[", "blk", ".", "values", "for", "blk", "in", "to_concat", "]", ")", "placement", "=", "placement", "or", "slice", "(", "0", ",", "len", "(", "values", ")", ",", "1", ")", "return", "self", ".", "make_block_same_class", "(", "values", ",", "ndim", "=", "self", ".", "ndim", ",", "placement", "=", "placement", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
ExtensionBlock.shift
Shift the block by `periods`. Dispatches to underlying ExtensionArray and re-boxes in an ExtensionBlock.
pandas/core/internals/blocks.py
def shift(self, periods: int, axis: libinternals.BlockPlacement = 0, fill_value: Any = None) -> List['ExtensionBlock']: """ Shift the block by `periods`. Dispatches to underlying ExtensionArray and re-boxes in an ExtensionBlock. """ return [ self.make_block_same_class( self.values.shift(periods=periods, fill_value=fill_value), placement=self.mgr_locs, ndim=self.ndim) ]
def shift(self, periods: int, axis: libinternals.BlockPlacement = 0, fill_value: Any = None) -> List['ExtensionBlock']: """ Shift the block by `periods`. Dispatches to underlying ExtensionArray and re-boxes in an ExtensionBlock. """ return [ self.make_block_same_class( self.values.shift(periods=periods, fill_value=fill_value), placement=self.mgr_locs, ndim=self.ndim) ]
[ "Shift", "the", "block", "by", "periods", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L1816-L1830
[ "def", "shift", "(", "self", ",", "periods", ":", "int", ",", "axis", ":", "libinternals", ".", "BlockPlacement", "=", "0", ",", "fill_value", ":", "Any", "=", "None", ")", "->", "List", "[", "'ExtensionBlock'", "]", ":", "return", "[", "self", ".", "make_block_same_class", "(", "self", ".", "values", ".", "shift", "(", "periods", "=", "periods", ",", "fill_value", "=", "fill_value", ")", ",", "placement", "=", "self", ".", "mgr_locs", ",", "ndim", "=", "self", ".", "ndim", ")", "]" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
FloatBlock.to_native_types
convert to our native types format, slicing if desired
pandas/core/internals/blocks.py
def to_native_types(self, slicer=None, na_rep='', float_format=None, decimal='.', quoting=None, **kwargs): """ convert to our native types format, slicing if desired """ values = self.values if slicer is not None: values = values[:, slicer] # see gh-13418: no special formatting is desired at the # output (important for appropriate 'quoting' behaviour), # so do not pass it through the FloatArrayFormatter if float_format is None and decimal == '.': mask = isna(values) if not quoting: values = values.astype(str) else: values = np.array(values, dtype='object') values[mask] = na_rep return values from pandas.io.formats.format import FloatArrayFormatter formatter = FloatArrayFormatter(values, na_rep=na_rep, float_format=float_format, decimal=decimal, quoting=quoting, fixed_width=False) return formatter.get_result_as_array()
def to_native_types(self, slicer=None, na_rep='', float_format=None, decimal='.', quoting=None, **kwargs): """ convert to our native types format, slicing if desired """ values = self.values if slicer is not None: values = values[:, slicer] # see gh-13418: no special formatting is desired at the # output (important for appropriate 'quoting' behaviour), # so do not pass it through the FloatArrayFormatter if float_format is None and decimal == '.': mask = isna(values) if not quoting: values = values.astype(str) else: values = np.array(values, dtype='object') values[mask] = na_rep return values from pandas.io.formats.format import FloatArrayFormatter formatter = FloatArrayFormatter(values, na_rep=na_rep, float_format=float_format, decimal=decimal, quoting=quoting, fixed_width=False) return formatter.get_result_as_array()
[ "convert", "to", "our", "native", "types", "format", "slicing", "if", "desired" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L1952-L1979
[ "def", "to_native_types", "(", "self", ",", "slicer", "=", "None", ",", "na_rep", "=", "''", ",", "float_format", "=", "None", ",", "decimal", "=", "'.'", ",", "quoting", "=", "None", ",", "*", "*", "kwargs", ")", ":", "values", "=", "self", ".", "values", "if", "slicer", "is", "not", "None", ":", "values", "=", "values", "[", ":", ",", "slicer", "]", "# see gh-13418: no special formatting is desired at the", "# output (important for appropriate 'quoting' behaviour),", "# so do not pass it through the FloatArrayFormatter", "if", "float_format", "is", "None", "and", "decimal", "==", "'.'", ":", "mask", "=", "isna", "(", "values", ")", "if", "not", "quoting", ":", "values", "=", "values", ".", "astype", "(", "str", ")", "else", ":", "values", "=", "np", ".", "array", "(", "values", ",", "dtype", "=", "'object'", ")", "values", "[", "mask", "]", "=", "na_rep", "return", "values", "from", "pandas", ".", "io", ".", "formats", ".", "format", "import", "FloatArrayFormatter", "formatter", "=", "FloatArrayFormatter", "(", "values", ",", "na_rep", "=", "na_rep", ",", "float_format", "=", "float_format", ",", "decimal", "=", "decimal", ",", "quoting", "=", "quoting", ",", "fixed_width", "=", "False", ")", "return", "formatter", ".", "get_result_as_array", "(", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
DatetimeLikeBlockMixin.get_values
return object dtype as boxed values, such as Timestamps/Timedelta
pandas/core/internals/blocks.py
def get_values(self, dtype=None): """ return object dtype as boxed values, such as Timestamps/Timedelta """ if is_object_dtype(dtype): values = self.values.ravel() result = self._holder(values).astype(object) return result.reshape(self.values.shape) return self.values
def get_values(self, dtype=None): """ return object dtype as boxed values, such as Timestamps/Timedelta """ if is_object_dtype(dtype): values = self.values.ravel() result = self._holder(values).astype(object) return result.reshape(self.values.shape) return self.values
[ "return", "object", "dtype", "as", "boxed", "values", "such", "as", "Timestamps", "/", "Timedelta" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2040-L2048
[ "def", "get_values", "(", "self", ",", "dtype", "=", "None", ")", ":", "if", "is_object_dtype", "(", "dtype", ")", ":", "values", "=", "self", ".", "values", ".", "ravel", "(", ")", "result", "=", "self", ".", "_holder", "(", "values", ")", ".", "astype", "(", "object", ")", "return", "result", ".", "reshape", "(", "self", ".", "values", ".", "shape", ")", "return", "self", ".", "values" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
DatetimeBlock._maybe_coerce_values
Input validation for values passed to __init__. Ensure that we have datetime64ns, coercing if necessary. Parameters ---------- values : array-like Must be convertible to datetime64 Returns ------- values : ndarray[datetime64ns] Overridden by DatetimeTZBlock.
pandas/core/internals/blocks.py
def _maybe_coerce_values(self, values): """Input validation for values passed to __init__. Ensure that we have datetime64ns, coercing if necessary. Parameters ---------- values : array-like Must be convertible to datetime64 Returns ------- values : ndarray[datetime64ns] Overridden by DatetimeTZBlock. """ if values.dtype != _NS_DTYPE: values = conversion.ensure_datetime64ns(values) if isinstance(values, DatetimeArray): values = values._data assert isinstance(values, np.ndarray), type(values) return values
def _maybe_coerce_values(self, values): """Input validation for values passed to __init__. Ensure that we have datetime64ns, coercing if necessary. Parameters ---------- values : array-like Must be convertible to datetime64 Returns ------- values : ndarray[datetime64ns] Overridden by DatetimeTZBlock. """ if values.dtype != _NS_DTYPE: values = conversion.ensure_datetime64ns(values) if isinstance(values, DatetimeArray): values = values._data assert isinstance(values, np.ndarray), type(values) return values
[ "Input", "validation", "for", "values", "passed", "to", "__init__", ".", "Ensure", "that", "we", "have", "datetime64ns", "coercing", "if", "necessary", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2060-L2082
[ "def", "_maybe_coerce_values", "(", "self", ",", "values", ")", ":", "if", "values", ".", "dtype", "!=", "_NS_DTYPE", ":", "values", "=", "conversion", ".", "ensure_datetime64ns", "(", "values", ")", "if", "isinstance", "(", "values", ",", "DatetimeArray", ")", ":", "values", "=", "values", ".", "_data", "assert", "isinstance", "(", "values", ",", "np", ".", "ndarray", ")", ",", "type", "(", "values", ")", "return", "values" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
DatetimeBlock._astype
these automatically copy, so copy=True has no effect raise on an except if raise == True
pandas/core/internals/blocks.py
def _astype(self, dtype, **kwargs): """ these automatically copy, so copy=True has no effect raise on an except if raise == True """ dtype = pandas_dtype(dtype) # if we are passed a datetime64[ns, tz] if is_datetime64tz_dtype(dtype): values = self.values if getattr(values, 'tz', None) is None: values = DatetimeIndex(values).tz_localize('UTC') values = values.tz_convert(dtype.tz) return self.make_block(values) # delegate return super()._astype(dtype=dtype, **kwargs)
def _astype(self, dtype, **kwargs): """ these automatically copy, so copy=True has no effect raise on an except if raise == True """ dtype = pandas_dtype(dtype) # if we are passed a datetime64[ns, tz] if is_datetime64tz_dtype(dtype): values = self.values if getattr(values, 'tz', None) is None: values = DatetimeIndex(values).tz_localize('UTC') values = values.tz_convert(dtype.tz) return self.make_block(values) # delegate return super()._astype(dtype=dtype, **kwargs)
[ "these", "automatically", "copy", "so", "copy", "=", "True", "has", "no", "effect", "raise", "on", "an", "except", "if", "raise", "==", "True" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2084-L2100
[ "def", "_astype", "(", "self", ",", "dtype", ",", "*", "*", "kwargs", ")", ":", "dtype", "=", "pandas_dtype", "(", "dtype", ")", "# if we are passed a datetime64[ns, tz]", "if", "is_datetime64tz_dtype", "(", "dtype", ")", ":", "values", "=", "self", ".", "values", "if", "getattr", "(", "values", ",", "'tz'", ",", "None", ")", "is", "None", ":", "values", "=", "DatetimeIndex", "(", "values", ")", ".", "tz_localize", "(", "'UTC'", ")", "values", "=", "values", ".", "tz_convert", "(", "dtype", ".", "tz", ")", "return", "self", ".", "make_block", "(", "values", ")", "# delegate", "return", "super", "(", ")", ".", "_astype", "(", "dtype", "=", "dtype", ",", "*", "*", "kwargs", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
DatetimeBlock._try_coerce_args
Coerce values and other to dtype 'i8'. NaN and NaT convert to the smallest i8, and will correctly round-trip to NaT if converted back in _try_coerce_result. values is always ndarray-like, other may not be Parameters ---------- values : ndarray-like other : ndarray-like or scalar Returns ------- base-type values, base-type other
pandas/core/internals/blocks.py
def _try_coerce_args(self, values, other): """ Coerce values and other to dtype 'i8'. NaN and NaT convert to the smallest i8, and will correctly round-trip to NaT if converted back in _try_coerce_result. values is always ndarray-like, other may not be Parameters ---------- values : ndarray-like other : ndarray-like or scalar Returns ------- base-type values, base-type other """ values = values.view('i8') if isinstance(other, bool): raise TypeError elif is_null_datetimelike(other): other = tslibs.iNaT elif isinstance(other, (datetime, np.datetime64, date)): other = self._box_func(other) if getattr(other, 'tz') is not None: raise TypeError("cannot coerce a Timestamp with a tz on a " "naive Block") other = other.asm8.view('i8') elif hasattr(other, 'dtype') and is_datetime64_dtype(other): other = other.astype('i8', copy=False).view('i8') else: # coercion issues # let higher levels handle raise TypeError(other) return values, other
def _try_coerce_args(self, values, other): """ Coerce values and other to dtype 'i8'. NaN and NaT convert to the smallest i8, and will correctly round-trip to NaT if converted back in _try_coerce_result. values is always ndarray-like, other may not be Parameters ---------- values : ndarray-like other : ndarray-like or scalar Returns ------- base-type values, base-type other """ values = values.view('i8') if isinstance(other, bool): raise TypeError elif is_null_datetimelike(other): other = tslibs.iNaT elif isinstance(other, (datetime, np.datetime64, date)): other = self._box_func(other) if getattr(other, 'tz') is not None: raise TypeError("cannot coerce a Timestamp with a tz on a " "naive Block") other = other.asm8.view('i8') elif hasattr(other, 'dtype') and is_datetime64_dtype(other): other = other.astype('i8', copy=False).view('i8') else: # coercion issues # let higher levels handle raise TypeError(other) return values, other
[ "Coerce", "values", "and", "other", "to", "dtype", "i8", ".", "NaN", "and", "NaT", "convert", "to", "the", "smallest", "i8", "and", "will", "correctly", "round", "-", "trip", "to", "NaT", "if", "converted", "back", "in", "_try_coerce_result", ".", "values", "is", "always", "ndarray", "-", "like", "other", "may", "not", "be" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2109-L2145
[ "def", "_try_coerce_args", "(", "self", ",", "values", ",", "other", ")", ":", "values", "=", "values", ".", "view", "(", "'i8'", ")", "if", "isinstance", "(", "other", ",", "bool", ")", ":", "raise", "TypeError", "elif", "is_null_datetimelike", "(", "other", ")", ":", "other", "=", "tslibs", ".", "iNaT", "elif", "isinstance", "(", "other", ",", "(", "datetime", ",", "np", ".", "datetime64", ",", "date", ")", ")", ":", "other", "=", "self", ".", "_box_func", "(", "other", ")", "if", "getattr", "(", "other", ",", "'tz'", ")", "is", "not", "None", ":", "raise", "TypeError", "(", "\"cannot coerce a Timestamp with a tz on a \"", "\"naive Block\"", ")", "other", "=", "other", ".", "asm8", ".", "view", "(", "'i8'", ")", "elif", "hasattr", "(", "other", ",", "'dtype'", ")", "and", "is_datetime64_dtype", "(", "other", ")", ":", "other", "=", "other", ".", "astype", "(", "'i8'", ",", "copy", "=", "False", ")", ".", "view", "(", "'i8'", ")", "else", ":", "# coercion issues", "# let higher levels handle", "raise", "TypeError", "(", "other", ")", "return", "values", ",", "other" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
DatetimeBlock._try_coerce_result
reverse of try_coerce_args
pandas/core/internals/blocks.py
def _try_coerce_result(self, result): """ reverse of try_coerce_args """ if isinstance(result, np.ndarray): if result.dtype.kind in ['i', 'f']: result = result.astype('M8[ns]') elif isinstance(result, (np.integer, np.float, np.datetime64)): result = self._box_func(result) return result
def _try_coerce_result(self, result): """ reverse of try_coerce_args """ if isinstance(result, np.ndarray): if result.dtype.kind in ['i', 'f']: result = result.astype('M8[ns]') elif isinstance(result, (np.integer, np.float, np.datetime64)): result = self._box_func(result) return result
[ "reverse", "of", "try_coerce_args" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2147-L2155
[ "def", "_try_coerce_result", "(", "self", ",", "result", ")", ":", "if", "isinstance", "(", "result", ",", "np", ".", "ndarray", ")", ":", "if", "result", ".", "dtype", ".", "kind", "in", "[", "'i'", ",", "'f'", "]", ":", "result", "=", "result", ".", "astype", "(", "'M8[ns]'", ")", "elif", "isinstance", "(", "result", ",", "(", "np", ".", "integer", ",", "np", ".", "float", ",", "np", ".", "datetime64", ")", ")", ":", "result", "=", "self", ".", "_box_func", "(", "result", ")", "return", "result" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
DatetimeBlock.to_native_types
convert to our native types format, slicing if desired
pandas/core/internals/blocks.py
def to_native_types(self, slicer=None, na_rep=None, date_format=None, quoting=None, **kwargs): """ convert to our native types format, slicing if desired """ values = self.values i8values = self.values.view('i8') if slicer is not None: values = values[..., slicer] i8values = i8values[..., slicer] from pandas.io.formats.format import _get_format_datetime64_from_values fmt = _get_format_datetime64_from_values(values, date_format) result = tslib.format_array_from_datetime( i8values.ravel(), tz=getattr(self.values, 'tz', None), format=fmt, na_rep=na_rep).reshape(i8values.shape) return np.atleast_2d(result)
def to_native_types(self, slicer=None, na_rep=None, date_format=None, quoting=None, **kwargs): """ convert to our native types format, slicing if desired """ values = self.values i8values = self.values.view('i8') if slicer is not None: values = values[..., slicer] i8values = i8values[..., slicer] from pandas.io.formats.format import _get_format_datetime64_from_values fmt = _get_format_datetime64_from_values(values, date_format) result = tslib.format_array_from_datetime( i8values.ravel(), tz=getattr(self.values, 'tz', None), format=fmt, na_rep=na_rep).reshape(i8values.shape) return np.atleast_2d(result)
[ "convert", "to", "our", "native", "types", "format", "slicing", "if", "desired" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2161-L2178
[ "def", "to_native_types", "(", "self", ",", "slicer", "=", "None", ",", "na_rep", "=", "None", ",", "date_format", "=", "None", ",", "quoting", "=", "None", ",", "*", "*", "kwargs", ")", ":", "values", "=", "self", ".", "values", "i8values", "=", "self", ".", "values", ".", "view", "(", "'i8'", ")", "if", "slicer", "is", "not", "None", ":", "values", "=", "values", "[", "...", ",", "slicer", "]", "i8values", "=", "i8values", "[", "...", ",", "slicer", "]", "from", "pandas", ".", "io", ".", "formats", ".", "format", "import", "_get_format_datetime64_from_values", "fmt", "=", "_get_format_datetime64_from_values", "(", "values", ",", "date_format", ")", "result", "=", "tslib", ".", "format_array_from_datetime", "(", "i8values", ".", "ravel", "(", ")", ",", "tz", "=", "getattr", "(", "self", ".", "values", ",", "'tz'", ",", "None", ")", ",", "format", "=", "fmt", ",", "na_rep", "=", "na_rep", ")", ".", "reshape", "(", "i8values", ".", "shape", ")", "return", "np", ".", "atleast_2d", "(", "result", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
DatetimeBlock.set
Modify Block in-place with new item value Returns ------- None
pandas/core/internals/blocks.py
def set(self, locs, values): """ Modify Block in-place with new item value Returns ------- None """ values = conversion.ensure_datetime64ns(values, copy=False) self.values[locs] = values
def set(self, locs, values): """ Modify Block in-place with new item value Returns ------- None """ values = conversion.ensure_datetime64ns(values, copy=False) self.values[locs] = values
[ "Modify", "Block", "in", "-", "place", "with", "new", "item", "value" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2185-L2195
[ "def", "set", "(", "self", ",", "locs", ",", "values", ")", ":", "values", "=", "conversion", ".", "ensure_datetime64ns", "(", "values", ",", "copy", "=", "False", ")", "self", ".", "values", "[", "locs", "]", "=", "values" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
DatetimeTZBlock._maybe_coerce_values
Input validation for values passed to __init__. Ensure that we have datetime64TZ, coercing if necessary. Parametetrs ----------- values : array-like Must be convertible to datetime64 Returns ------- values : DatetimeArray
pandas/core/internals/blocks.py
def _maybe_coerce_values(self, values): """Input validation for values passed to __init__. Ensure that we have datetime64TZ, coercing if necessary. Parametetrs ----------- values : array-like Must be convertible to datetime64 Returns ------- values : DatetimeArray """ if not isinstance(values, self._holder): values = self._holder(values) if values.tz is None: raise ValueError("cannot create a DatetimeTZBlock without a tz") return values
def _maybe_coerce_values(self, values): """Input validation for values passed to __init__. Ensure that we have datetime64TZ, coercing if necessary. Parametetrs ----------- values : array-like Must be convertible to datetime64 Returns ------- values : DatetimeArray """ if not isinstance(values, self._holder): values = self._holder(values) if values.tz is None: raise ValueError("cannot create a DatetimeTZBlock without a tz") return values
[ "Input", "validation", "for", "values", "passed", "to", "__init__", ".", "Ensure", "that", "we", "have", "datetime64TZ", "coercing", "if", "necessary", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2211-L2230
[ "def", "_maybe_coerce_values", "(", "self", ",", "values", ")", ":", "if", "not", "isinstance", "(", "values", ",", "self", ".", "_holder", ")", ":", "values", "=", "self", ".", "_holder", "(", "values", ")", "if", "values", ".", "tz", "is", "None", ":", "raise", "ValueError", "(", "\"cannot create a DatetimeTZBlock without a tz\"", ")", "return", "values" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
DatetimeTZBlock.get_values
Returns an ndarray of values. Parameters ---------- dtype : np.dtype Only `object`-like dtypes are respected here (not sure why). Returns ------- values : ndarray When ``dtype=object``, then and object-dtype ndarray of boxed values is returned. Otherwise, an M8[ns] ndarray is returned. DatetimeArray is always 1-d. ``get_values`` will reshape the return value to be the same dimensionality as the block.
pandas/core/internals/blocks.py
def get_values(self, dtype=None): """ Returns an ndarray of values. Parameters ---------- dtype : np.dtype Only `object`-like dtypes are respected here (not sure why). Returns ------- values : ndarray When ``dtype=object``, then and object-dtype ndarray of boxed values is returned. Otherwise, an M8[ns] ndarray is returned. DatetimeArray is always 1-d. ``get_values`` will reshape the return value to be the same dimensionality as the block. """ values = self.values if is_object_dtype(dtype): values = values._box_values(values._data) values = np.asarray(values) if self.ndim == 2: # Ensure that our shape is correct for DataFrame. # ExtensionArrays are always 1-D, even in a DataFrame when # the analogous NumPy-backed column would be a 2-D ndarray. values = values.reshape(1, -1) return values
def get_values(self, dtype=None): """ Returns an ndarray of values. Parameters ---------- dtype : np.dtype Only `object`-like dtypes are respected here (not sure why). Returns ------- values : ndarray When ``dtype=object``, then and object-dtype ndarray of boxed values is returned. Otherwise, an M8[ns] ndarray is returned. DatetimeArray is always 1-d. ``get_values`` will reshape the return value to be the same dimensionality as the block. """ values = self.values if is_object_dtype(dtype): values = values._box_values(values._data) values = np.asarray(values) if self.ndim == 2: # Ensure that our shape is correct for DataFrame. # ExtensionArrays are always 1-D, even in a DataFrame when # the analogous NumPy-backed column would be a 2-D ndarray. values = values.reshape(1, -1) return values
[ "Returns", "an", "ndarray", "of", "values", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2245-L2277
[ "def", "get_values", "(", "self", ",", "dtype", "=", "None", ")", ":", "values", "=", "self", ".", "values", "if", "is_object_dtype", "(", "dtype", ")", ":", "values", "=", "values", ".", "_box_values", "(", "values", ".", "_data", ")", "values", "=", "np", ".", "asarray", "(", "values", ")", "if", "self", ".", "ndim", "==", "2", ":", "# Ensure that our shape is correct for DataFrame.", "# ExtensionArrays are always 1-D, even in a DataFrame when", "# the analogous NumPy-backed column would be a 2-D ndarray.", "values", "=", "values", ".", "reshape", "(", "1", ",", "-", "1", ")", "return", "values" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
DatetimeTZBlock._slice
return a slice of my values
pandas/core/internals/blocks.py
def _slice(self, slicer): """ return a slice of my values """ if isinstance(slicer, tuple): col, loc = slicer if not com.is_null_slice(col) and col != 0: raise IndexError("{0} only contains one item".format(self)) return self.values[loc] return self.values[slicer]
def _slice(self, slicer): """ return a slice of my values """ if isinstance(slicer, tuple): col, loc = slicer if not com.is_null_slice(col) and col != 0: raise IndexError("{0} only contains one item".format(self)) return self.values[loc] return self.values[slicer]
[ "return", "a", "slice", "of", "my", "values" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2285-L2292
[ "def", "_slice", "(", "self", ",", "slicer", ")", ":", "if", "isinstance", "(", "slicer", ",", "tuple", ")", ":", "col", ",", "loc", "=", "slicer", "if", "not", "com", ".", "is_null_slice", "(", "col", ")", "and", "col", "!=", "0", ":", "raise", "IndexError", "(", "\"{0} only contains one item\"", ".", "format", "(", "self", ")", ")", "return", "self", ".", "values", "[", "loc", "]", "return", "self", ".", "values", "[", "slicer", "]" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
DatetimeTZBlock._try_coerce_args
localize and return i8 for the values Parameters ---------- values : ndarray-like other : ndarray-like or scalar Returns ------- base-type values, base-type other
pandas/core/internals/blocks.py
def _try_coerce_args(self, values, other): """ localize and return i8 for the values Parameters ---------- values : ndarray-like other : ndarray-like or scalar Returns ------- base-type values, base-type other """ # asi8 is a view, needs copy values = _block_shape(values.view("i8"), ndim=self.ndim) if isinstance(other, ABCSeries): other = self._holder(other) if isinstance(other, bool): raise TypeError elif is_datetime64_dtype(other): # add the tz back other = self._holder(other, dtype=self.dtype) elif is_null_datetimelike(other): other = tslibs.iNaT elif isinstance(other, self._holder): if other.tz != self.values.tz: raise ValueError("incompatible or non tz-aware value") other = _block_shape(other.asi8, ndim=self.ndim) elif isinstance(other, (np.datetime64, datetime, date)): other = tslibs.Timestamp(other) tz = getattr(other, 'tz', None) # test we can have an equal time zone if tz is None or str(tz) != str(self.values.tz): raise ValueError("incompatible or non tz-aware value") other = other.value else: raise TypeError(other) return values, other
def _try_coerce_args(self, values, other): """ localize and return i8 for the values Parameters ---------- values : ndarray-like other : ndarray-like or scalar Returns ------- base-type values, base-type other """ # asi8 is a view, needs copy values = _block_shape(values.view("i8"), ndim=self.ndim) if isinstance(other, ABCSeries): other = self._holder(other) if isinstance(other, bool): raise TypeError elif is_datetime64_dtype(other): # add the tz back other = self._holder(other, dtype=self.dtype) elif is_null_datetimelike(other): other = tslibs.iNaT elif isinstance(other, self._holder): if other.tz != self.values.tz: raise ValueError("incompatible or non tz-aware value") other = _block_shape(other.asi8, ndim=self.ndim) elif isinstance(other, (np.datetime64, datetime, date)): other = tslibs.Timestamp(other) tz = getattr(other, 'tz', None) # test we can have an equal time zone if tz is None or str(tz) != str(self.values.tz): raise ValueError("incompatible or non tz-aware value") other = other.value else: raise TypeError(other) return values, other
[ "localize", "and", "return", "i8", "for", "the", "values" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2294-L2336
[ "def", "_try_coerce_args", "(", "self", ",", "values", ",", "other", ")", ":", "# asi8 is a view, needs copy", "values", "=", "_block_shape", "(", "values", ".", "view", "(", "\"i8\"", ")", ",", "ndim", "=", "self", ".", "ndim", ")", "if", "isinstance", "(", "other", ",", "ABCSeries", ")", ":", "other", "=", "self", ".", "_holder", "(", "other", ")", "if", "isinstance", "(", "other", ",", "bool", ")", ":", "raise", "TypeError", "elif", "is_datetime64_dtype", "(", "other", ")", ":", "# add the tz back", "other", "=", "self", ".", "_holder", "(", "other", ",", "dtype", "=", "self", ".", "dtype", ")", "elif", "is_null_datetimelike", "(", "other", ")", ":", "other", "=", "tslibs", ".", "iNaT", "elif", "isinstance", "(", "other", ",", "self", ".", "_holder", ")", ":", "if", "other", ".", "tz", "!=", "self", ".", "values", ".", "tz", ":", "raise", "ValueError", "(", "\"incompatible or non tz-aware value\"", ")", "other", "=", "_block_shape", "(", "other", ".", "asi8", ",", "ndim", "=", "self", ".", "ndim", ")", "elif", "isinstance", "(", "other", ",", "(", "np", ".", "datetime64", ",", "datetime", ",", "date", ")", ")", ":", "other", "=", "tslibs", ".", "Timestamp", "(", "other", ")", "tz", "=", "getattr", "(", "other", ",", "'tz'", ",", "None", ")", "# test we can have an equal time zone", "if", "tz", "is", "None", "or", "str", "(", "tz", ")", "!=", "str", "(", "self", ".", "values", ".", "tz", ")", ":", "raise", "ValueError", "(", "\"incompatible or non tz-aware value\"", ")", "other", "=", "other", ".", "value", "else", ":", "raise", "TypeError", "(", "other", ")", "return", "values", ",", "other" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
DatetimeTZBlock._try_coerce_result
reverse of try_coerce_args
pandas/core/internals/blocks.py
def _try_coerce_result(self, result): """ reverse of try_coerce_args """ if isinstance(result, np.ndarray): if result.dtype.kind in ['i', 'f']: result = result.astype('M8[ns]') elif isinstance(result, (np.integer, np.float, np.datetime64)): result = self._box_func(result) if isinstance(result, np.ndarray): # allow passing of > 1dim if its trivial if result.ndim > 1: result = result.reshape(np.prod(result.shape)) # GH#24096 new values invalidates a frequency result = self._holder._simple_new(result, freq=None, dtype=self.values.dtype) return result
def _try_coerce_result(self, result): """ reverse of try_coerce_args """ if isinstance(result, np.ndarray): if result.dtype.kind in ['i', 'f']: result = result.astype('M8[ns]') elif isinstance(result, (np.integer, np.float, np.datetime64)): result = self._box_func(result) if isinstance(result, np.ndarray): # allow passing of > 1dim if its trivial if result.ndim > 1: result = result.reshape(np.prod(result.shape)) # GH#24096 new values invalidates a frequency result = self._holder._simple_new(result, freq=None, dtype=self.values.dtype) return result
[ "reverse", "of", "try_coerce_args" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2338-L2356
[ "def", "_try_coerce_result", "(", "self", ",", "result", ")", ":", "if", "isinstance", "(", "result", ",", "np", ".", "ndarray", ")", ":", "if", "result", ".", "dtype", ".", "kind", "in", "[", "'i'", ",", "'f'", "]", ":", "result", "=", "result", ".", "astype", "(", "'M8[ns]'", ")", "elif", "isinstance", "(", "result", ",", "(", "np", ".", "integer", ",", "np", ".", "float", ",", "np", ".", "datetime64", ")", ")", ":", "result", "=", "self", ".", "_box_func", "(", "result", ")", "if", "isinstance", "(", "result", ",", "np", ".", "ndarray", ")", ":", "# allow passing of > 1dim if its trivial", "if", "result", ".", "ndim", ">", "1", ":", "result", "=", "result", ".", "reshape", "(", "np", ".", "prod", "(", "result", ".", "shape", ")", ")", "# GH#24096 new values invalidates a frequency", "result", "=", "self", ".", "_holder", ".", "_simple_new", "(", "result", ",", "freq", "=", "None", ",", "dtype", "=", "self", ".", "values", ".", "dtype", ")", "return", "result" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
DatetimeTZBlock.diff
1st discrete difference Parameters ---------- n : int, number of periods to diff axis : int, axis to diff upon. default 0 Return ------ A list with a new TimeDeltaBlock. Note ---- The arguments here are mimicking shift so they are called correctly by apply.
pandas/core/internals/blocks.py
def diff(self, n, axis=0): """1st discrete difference Parameters ---------- n : int, number of periods to diff axis : int, axis to diff upon. default 0 Return ------ A list with a new TimeDeltaBlock. Note ---- The arguments here are mimicking shift so they are called correctly by apply. """ if axis == 0: # Cannot currently calculate diff across multiple blocks since this # function is invoked via apply raise NotImplementedError new_values = (self.values - self.shift(n, axis=axis)[0].values).asi8 # Reshape the new_values like how algos.diff does for timedelta data new_values = new_values.reshape(1, len(new_values)) new_values = new_values.astype('timedelta64[ns]') return [TimeDeltaBlock(new_values, placement=self.mgr_locs.indexer)]
def diff(self, n, axis=0): """1st discrete difference Parameters ---------- n : int, number of periods to diff axis : int, axis to diff upon. default 0 Return ------ A list with a new TimeDeltaBlock. Note ---- The arguments here are mimicking shift so they are called correctly by apply. """ if axis == 0: # Cannot currently calculate diff across multiple blocks since this # function is invoked via apply raise NotImplementedError new_values = (self.values - self.shift(n, axis=axis)[0].values).asi8 # Reshape the new_values like how algos.diff does for timedelta data new_values = new_values.reshape(1, len(new_values)) new_values = new_values.astype('timedelta64[ns]') return [TimeDeltaBlock(new_values, placement=self.mgr_locs.indexer)]
[ "1st", "discrete", "difference" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2362-L2388
[ "def", "diff", "(", "self", ",", "n", ",", "axis", "=", "0", ")", ":", "if", "axis", "==", "0", ":", "# Cannot currently calculate diff across multiple blocks since this", "# function is invoked via apply", "raise", "NotImplementedError", "new_values", "=", "(", "self", ".", "values", "-", "self", ".", "shift", "(", "n", ",", "axis", "=", "axis", ")", "[", "0", "]", ".", "values", ")", ".", "asi8", "# Reshape the new_values like how algos.diff does for timedelta data", "new_values", "=", "new_values", ".", "reshape", "(", "1", ",", "len", "(", "new_values", ")", ")", "new_values", "=", "new_values", ".", "astype", "(", "'timedelta64[ns]'", ")", "return", "[", "TimeDeltaBlock", "(", "new_values", ",", "placement", "=", "self", ".", "mgr_locs", ".", "indexer", ")", "]" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
TimeDeltaBlock._try_coerce_args
Coerce values and other to int64, with null values converted to iNaT. values is always ndarray-like, other may not be Parameters ---------- values : ndarray-like other : ndarray-like or scalar Returns ------- base-type values, base-type other
pandas/core/internals/blocks.py
def _try_coerce_args(self, values, other): """ Coerce values and other to int64, with null values converted to iNaT. values is always ndarray-like, other may not be Parameters ---------- values : ndarray-like other : ndarray-like or scalar Returns ------- base-type values, base-type other """ values = values.view('i8') if isinstance(other, bool): raise TypeError elif is_null_datetimelike(other): other = tslibs.iNaT elif isinstance(other, (timedelta, np.timedelta64)): other = Timedelta(other).value elif hasattr(other, 'dtype') and is_timedelta64_dtype(other): other = other.astype('i8', copy=False).view('i8') else: # coercion issues # let higher levels handle raise TypeError(other) return values, other
def _try_coerce_args(self, values, other): """ Coerce values and other to int64, with null values converted to iNaT. values is always ndarray-like, other may not be Parameters ---------- values : ndarray-like other : ndarray-like or scalar Returns ------- base-type values, base-type other """ values = values.view('i8') if isinstance(other, bool): raise TypeError elif is_null_datetimelike(other): other = tslibs.iNaT elif isinstance(other, (timedelta, np.timedelta64)): other = Timedelta(other).value elif hasattr(other, 'dtype') and is_timedelta64_dtype(other): other = other.astype('i8', copy=False).view('i8') else: # coercion issues # let higher levels handle raise TypeError(other) return values, other
[ "Coerce", "values", "and", "other", "to", "int64", "with", "null", "values", "converted", "to", "iNaT", ".", "values", "is", "always", "ndarray", "-", "like", "other", "may", "not", "be" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2477-L2506
[ "def", "_try_coerce_args", "(", "self", ",", "values", ",", "other", ")", ":", "values", "=", "values", ".", "view", "(", "'i8'", ")", "if", "isinstance", "(", "other", ",", "bool", ")", ":", "raise", "TypeError", "elif", "is_null_datetimelike", "(", "other", ")", ":", "other", "=", "tslibs", ".", "iNaT", "elif", "isinstance", "(", "other", ",", "(", "timedelta", ",", "np", ".", "timedelta64", ")", ")", ":", "other", "=", "Timedelta", "(", "other", ")", ".", "value", "elif", "hasattr", "(", "other", ",", "'dtype'", ")", "and", "is_timedelta64_dtype", "(", "other", ")", ":", "other", "=", "other", ".", "astype", "(", "'i8'", ",", "copy", "=", "False", ")", ".", "view", "(", "'i8'", ")", "else", ":", "# coercion issues", "# let higher levels handle", "raise", "TypeError", "(", "other", ")", "return", "values", ",", "other" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
TimeDeltaBlock._try_coerce_result
reverse of try_coerce_args / try_operate
pandas/core/internals/blocks.py
def _try_coerce_result(self, result): """ reverse of try_coerce_args / try_operate """ if isinstance(result, np.ndarray): mask = isna(result) if result.dtype.kind in ['i', 'f']: result = result.astype('m8[ns]') result[mask] = tslibs.iNaT elif isinstance(result, (np.integer, np.float)): result = self._box_func(result) return result
def _try_coerce_result(self, result): """ reverse of try_coerce_args / try_operate """ if isinstance(result, np.ndarray): mask = isna(result) if result.dtype.kind in ['i', 'f']: result = result.astype('m8[ns]') result[mask] = tslibs.iNaT elif isinstance(result, (np.integer, np.float)): result = self._box_func(result) return result
[ "reverse", "of", "try_coerce_args", "/", "try_operate" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2508-L2519
[ "def", "_try_coerce_result", "(", "self", ",", "result", ")", ":", "if", "isinstance", "(", "result", ",", "np", ".", "ndarray", ")", ":", "mask", "=", "isna", "(", "result", ")", "if", "result", ".", "dtype", ".", "kind", "in", "[", "'i'", ",", "'f'", "]", ":", "result", "=", "result", ".", "astype", "(", "'m8[ns]'", ")", "result", "[", "mask", "]", "=", "tslibs", ".", "iNaT", "elif", "isinstance", "(", "result", ",", "(", "np", ".", "integer", ",", "np", ".", "float", ")", ")", ":", "result", "=", "self", ".", "_box_func", "(", "result", ")", "return", "result" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
TimeDeltaBlock.to_native_types
convert to our native types format, slicing if desired
pandas/core/internals/blocks.py
def to_native_types(self, slicer=None, na_rep=None, quoting=None, **kwargs): """ convert to our native types format, slicing if desired """ values = self.values if slicer is not None: values = values[:, slicer] mask = isna(values) rvalues = np.empty(values.shape, dtype=object) if na_rep is None: na_rep = 'NaT' rvalues[mask] = na_rep imask = (~mask).ravel() # FIXME: # should use the formats.format.Timedelta64Formatter here # to figure what format to pass to the Timedelta # e.g. to not show the decimals say rvalues.flat[imask] = np.array([Timedelta(val)._repr_base(format='all') for val in values.ravel()[imask]], dtype=object) return rvalues
def to_native_types(self, slicer=None, na_rep=None, quoting=None, **kwargs): """ convert to our native types format, slicing if desired """ values = self.values if slicer is not None: values = values[:, slicer] mask = isna(values) rvalues = np.empty(values.shape, dtype=object) if na_rep is None: na_rep = 'NaT' rvalues[mask] = na_rep imask = (~mask).ravel() # FIXME: # should use the formats.format.Timedelta64Formatter here # to figure what format to pass to the Timedelta # e.g. to not show the decimals say rvalues.flat[imask] = np.array([Timedelta(val)._repr_base(format='all') for val in values.ravel()[imask]], dtype=object) return rvalues
[ "convert", "to", "our", "native", "types", "format", "slicing", "if", "desired" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2525-L2547
[ "def", "to_native_types", "(", "self", ",", "slicer", "=", "None", ",", "na_rep", "=", "None", ",", "quoting", "=", "None", ",", "*", "*", "kwargs", ")", ":", "values", "=", "self", ".", "values", "if", "slicer", "is", "not", "None", ":", "values", "=", "values", "[", ":", ",", "slicer", "]", "mask", "=", "isna", "(", "values", ")", "rvalues", "=", "np", ".", "empty", "(", "values", ".", "shape", ",", "dtype", "=", "object", ")", "if", "na_rep", "is", "None", ":", "na_rep", "=", "'NaT'", "rvalues", "[", "mask", "]", "=", "na_rep", "imask", "=", "(", "~", "mask", ")", ".", "ravel", "(", ")", "# FIXME:", "# should use the formats.format.Timedelta64Formatter here", "# to figure what format to pass to the Timedelta", "# e.g. to not show the decimals say", "rvalues", ".", "flat", "[", "imask", "]", "=", "np", ".", "array", "(", "[", "Timedelta", "(", "val", ")", ".", "_repr_base", "(", "format", "=", "'all'", ")", "for", "val", "in", "values", ".", "ravel", "(", ")", "[", "imask", "]", "]", ",", "dtype", "=", "object", ")", "return", "rvalues" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
ObjectBlock.convert
attempt to coerce any object types to better types return a copy of the block (if copy = True) by definition we ARE an ObjectBlock!!!!! can return multiple blocks!
pandas/core/internals/blocks.py
def convert(self, *args, **kwargs): """ attempt to coerce any object types to better types return a copy of the block (if copy = True) by definition we ARE an ObjectBlock!!!!! can return multiple blocks! """ if args: raise NotImplementedError by_item = kwargs.get('by_item', True) new_inputs = ['coerce', 'datetime', 'numeric', 'timedelta'] new_style = False for kw in new_inputs: new_style |= kw in kwargs if new_style: fn = soft_convert_objects fn_inputs = new_inputs else: fn = maybe_convert_objects fn_inputs = ['convert_dates', 'convert_numeric', 'convert_timedeltas'] fn_inputs += ['copy'] fn_kwargs = {key: kwargs[key] for key in fn_inputs if key in kwargs} # operate column-by-column def f(m, v, i): shape = v.shape values = fn(v.ravel(), **fn_kwargs) try: values = values.reshape(shape) values = _block_shape(values, ndim=self.ndim) except (AttributeError, NotImplementedError): pass return values if by_item and not self._is_single_block: blocks = self.split_and_operate(None, f, False) else: values = f(None, self.values.ravel(), None) blocks = [make_block(values, ndim=self.ndim, placement=self.mgr_locs)] return blocks
def convert(self, *args, **kwargs): """ attempt to coerce any object types to better types return a copy of the block (if copy = True) by definition we ARE an ObjectBlock!!!!! can return multiple blocks! """ if args: raise NotImplementedError by_item = kwargs.get('by_item', True) new_inputs = ['coerce', 'datetime', 'numeric', 'timedelta'] new_style = False for kw in new_inputs: new_style |= kw in kwargs if new_style: fn = soft_convert_objects fn_inputs = new_inputs else: fn = maybe_convert_objects fn_inputs = ['convert_dates', 'convert_numeric', 'convert_timedeltas'] fn_inputs += ['copy'] fn_kwargs = {key: kwargs[key] for key in fn_inputs if key in kwargs} # operate column-by-column def f(m, v, i): shape = v.shape values = fn(v.ravel(), **fn_kwargs) try: values = values.reshape(shape) values = _block_shape(values, ndim=self.ndim) except (AttributeError, NotImplementedError): pass return values if by_item and not self._is_single_block: blocks = self.split_and_operate(None, f, False) else: values = f(None, self.values.ravel(), None) blocks = [make_block(values, ndim=self.ndim, placement=self.mgr_locs)] return blocks
[ "attempt", "to", "coerce", "any", "object", "types", "to", "better", "types", "return", "a", "copy", "of", "the", "block", "(", "if", "copy", "=", "True", ")", "by", "definition", "we", "ARE", "an", "ObjectBlock!!!!!" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2597-L2643
[ "def", "convert", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "args", ":", "raise", "NotImplementedError", "by_item", "=", "kwargs", ".", "get", "(", "'by_item'", ",", "True", ")", "new_inputs", "=", "[", "'coerce'", ",", "'datetime'", ",", "'numeric'", ",", "'timedelta'", "]", "new_style", "=", "False", "for", "kw", "in", "new_inputs", ":", "new_style", "|=", "kw", "in", "kwargs", "if", "new_style", ":", "fn", "=", "soft_convert_objects", "fn_inputs", "=", "new_inputs", "else", ":", "fn", "=", "maybe_convert_objects", "fn_inputs", "=", "[", "'convert_dates'", ",", "'convert_numeric'", ",", "'convert_timedeltas'", "]", "fn_inputs", "+=", "[", "'copy'", "]", "fn_kwargs", "=", "{", "key", ":", "kwargs", "[", "key", "]", "for", "key", "in", "fn_inputs", "if", "key", "in", "kwargs", "}", "# operate column-by-column", "def", "f", "(", "m", ",", "v", ",", "i", ")", ":", "shape", "=", "v", ".", "shape", "values", "=", "fn", "(", "v", ".", "ravel", "(", ")", ",", "*", "*", "fn_kwargs", ")", "try", ":", "values", "=", "values", ".", "reshape", "(", "shape", ")", "values", "=", "_block_shape", "(", "values", ",", "ndim", "=", "self", ".", "ndim", ")", "except", "(", "AttributeError", ",", "NotImplementedError", ")", ":", "pass", "return", "values", "if", "by_item", "and", "not", "self", ".", "_is_single_block", ":", "blocks", "=", "self", ".", "split_and_operate", "(", "None", ",", "f", ",", "False", ")", "else", ":", "values", "=", "f", "(", "None", ",", "self", ".", "values", ".", "ravel", "(", ")", ",", "None", ")", "blocks", "=", "[", "make_block", "(", "values", ",", "ndim", "=", "self", ".", "ndim", ",", "placement", "=", "self", ".", "mgr_locs", ")", "]", "return", "blocks" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
ObjectBlock.set
Modify Block in-place with new item value Returns ------- None
pandas/core/internals/blocks.py
def set(self, locs, values): """ Modify Block in-place with new item value Returns ------- None """ try: self.values[locs] = values except (ValueError): # broadcasting error # see GH6171 new_shape = list(values.shape) new_shape[0] = len(self.items) self.values = np.empty(tuple(new_shape), dtype=self.dtype) self.values.fill(np.nan) self.values[locs] = values
def set(self, locs, values): """ Modify Block in-place with new item value Returns ------- None """ try: self.values[locs] = values except (ValueError): # broadcasting error # see GH6171 new_shape = list(values.shape) new_shape[0] = len(self.items) self.values = np.empty(tuple(new_shape), dtype=self.dtype) self.values.fill(np.nan) self.values[locs] = values
[ "Modify", "Block", "in", "-", "place", "with", "new", "item", "value" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2645-L2663
[ "def", "set", "(", "self", ",", "locs", ",", "values", ")", ":", "try", ":", "self", ".", "values", "[", "locs", "]", "=", "values", "except", "(", "ValueError", ")", ":", "# broadcasting error", "# see GH6171", "new_shape", "=", "list", "(", "values", ".", "shape", ")", "new_shape", "[", "0", "]", "=", "len", "(", "self", ".", "items", ")", "self", ".", "values", "=", "np", ".", "empty", "(", "tuple", "(", "new_shape", ")", ",", "dtype", "=", "self", ".", "dtype", ")", "self", ".", "values", ".", "fill", "(", "np", ".", "nan", ")", "self", ".", "values", "[", "locs", "]", "=", "values" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
ObjectBlock._try_coerce_args
provide coercion to our input arguments
pandas/core/internals/blocks.py
def _try_coerce_args(self, values, other): """ provide coercion to our input arguments """ if isinstance(other, ABCDatetimeIndex): # May get a DatetimeIndex here. Unbox it. other = other.array if isinstance(other, DatetimeArray): # hit in pandas/tests/indexing/test_coercion.py # ::TestWhereCoercion::test_where_series_datetime64[datetime64tz] # when falling back to ObjectBlock.where other = other.astype(object) return values, other
def _try_coerce_args(self, values, other): """ provide coercion to our input arguments """ if isinstance(other, ABCDatetimeIndex): # May get a DatetimeIndex here. Unbox it. other = other.array if isinstance(other, DatetimeArray): # hit in pandas/tests/indexing/test_coercion.py # ::TestWhereCoercion::test_where_series_datetime64[datetime64tz] # when falling back to ObjectBlock.where other = other.astype(object) return values, other
[ "provide", "coercion", "to", "our", "input", "arguments" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2677-L2690
[ "def", "_try_coerce_args", "(", "self", ",", "values", ",", "other", ")", ":", "if", "isinstance", "(", "other", ",", "ABCDatetimeIndex", ")", ":", "# May get a DatetimeIndex here. Unbox it.", "other", "=", "other", ".", "array", "if", "isinstance", "(", "other", ",", "DatetimeArray", ")", ":", "# hit in pandas/tests/indexing/test_coercion.py", "# ::TestWhereCoercion::test_where_series_datetime64[datetime64tz]", "# when falling back to ObjectBlock.where", "other", "=", "other", ".", "astype", "(", "object", ")", "return", "values", ",", "other" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
ObjectBlock._replace_single
Replace elements by the given value. Parameters ---------- to_replace : object or pattern Scalar to replace or regular expression to match. value : object Replacement object. inplace : bool, default False Perform inplace modification. filter : list, optional regex : bool, default False If true, perform regular expression substitution. convert : bool, default True If true, try to coerce any object types to better types. mask : array-like of bool, optional True indicate corresponding element is ignored. Returns ------- a new block, the result after replacing
pandas/core/internals/blocks.py
def _replace_single(self, to_replace, value, inplace=False, filter=None, regex=False, convert=True, mask=None): """ Replace elements by the given value. Parameters ---------- to_replace : object or pattern Scalar to replace or regular expression to match. value : object Replacement object. inplace : bool, default False Perform inplace modification. filter : list, optional regex : bool, default False If true, perform regular expression substitution. convert : bool, default True If true, try to coerce any object types to better types. mask : array-like of bool, optional True indicate corresponding element is ignored. Returns ------- a new block, the result after replacing """ inplace = validate_bool_kwarg(inplace, 'inplace') # to_replace is regex compilable to_rep_re = regex and is_re_compilable(to_replace) # regex is regex compilable regex_re = is_re_compilable(regex) # only one will survive if to_rep_re and regex_re: raise AssertionError('only one of to_replace and regex can be ' 'regex compilable') # if regex was passed as something that can be a regex (rather than a # boolean) if regex_re: to_replace = regex regex = regex_re or to_rep_re # try to get the pattern attribute (compiled re) or it's a string try: pattern = to_replace.pattern except AttributeError: pattern = to_replace # if the pattern is not empty and to_replace is either a string or a # regex if regex and pattern: rx = re.compile(to_replace) else: # if the thing to replace is not a string or compiled regex call # the superclass method -> to_replace is some kind of object return super().replace(to_replace, value, inplace=inplace, filter=filter, regex=regex) new_values = self.values if inplace else self.values.copy() # deal with replacing values with objects (strings) that match but # whose replacement is not a string (numeric, nan, object) if isna(value) or not isinstance(value, str): def re_replacer(s): try: return value if rx.search(s) is not None else s except TypeError: return s else: # value is guaranteed to be a string here, s can be either a string # or null if it's null it gets returned def re_replacer(s): try: return rx.sub(value, s) except TypeError: return s f = np.vectorize(re_replacer, otypes=[self.dtype]) if filter is None: filt = slice(None) else: filt = self.mgr_locs.isin(filter).nonzero()[0] if mask is None: new_values[filt] = f(new_values[filt]) else: new_values[filt][mask] = f(new_values[filt][mask]) # convert block = self.make_block(new_values) if convert: block = block.convert(by_item=True, numeric=False) return block
def _replace_single(self, to_replace, value, inplace=False, filter=None, regex=False, convert=True, mask=None): """ Replace elements by the given value. Parameters ---------- to_replace : object or pattern Scalar to replace or regular expression to match. value : object Replacement object. inplace : bool, default False Perform inplace modification. filter : list, optional regex : bool, default False If true, perform regular expression substitution. convert : bool, default True If true, try to coerce any object types to better types. mask : array-like of bool, optional True indicate corresponding element is ignored. Returns ------- a new block, the result after replacing """ inplace = validate_bool_kwarg(inplace, 'inplace') # to_replace is regex compilable to_rep_re = regex and is_re_compilable(to_replace) # regex is regex compilable regex_re = is_re_compilable(regex) # only one will survive if to_rep_re and regex_re: raise AssertionError('only one of to_replace and regex can be ' 'regex compilable') # if regex was passed as something that can be a regex (rather than a # boolean) if regex_re: to_replace = regex regex = regex_re or to_rep_re # try to get the pattern attribute (compiled re) or it's a string try: pattern = to_replace.pattern except AttributeError: pattern = to_replace # if the pattern is not empty and to_replace is either a string or a # regex if regex and pattern: rx = re.compile(to_replace) else: # if the thing to replace is not a string or compiled regex call # the superclass method -> to_replace is some kind of object return super().replace(to_replace, value, inplace=inplace, filter=filter, regex=regex) new_values = self.values if inplace else self.values.copy() # deal with replacing values with objects (strings) that match but # whose replacement is not a string (numeric, nan, object) if isna(value) or not isinstance(value, str): def re_replacer(s): try: return value if rx.search(s) is not None else s except TypeError: return s else: # value is guaranteed to be a string here, s can be either a string # or null if it's null it gets returned def re_replacer(s): try: return rx.sub(value, s) except TypeError: return s f = np.vectorize(re_replacer, otypes=[self.dtype]) if filter is None: filt = slice(None) else: filt = self.mgr_locs.isin(filter).nonzero()[0] if mask is None: new_values[filt] = f(new_values[filt]) else: new_values[filt][mask] = f(new_values[filt][mask]) # convert block = self.make_block(new_values) if convert: block = block.convert(by_item=True, numeric=False) return block
[ "Replace", "elements", "by", "the", "given", "value", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2744-L2841
[ "def", "_replace_single", "(", "self", ",", "to_replace", ",", "value", ",", "inplace", "=", "False", ",", "filter", "=", "None", ",", "regex", "=", "False", ",", "convert", "=", "True", ",", "mask", "=", "None", ")", ":", "inplace", "=", "validate_bool_kwarg", "(", "inplace", ",", "'inplace'", ")", "# to_replace is regex compilable", "to_rep_re", "=", "regex", "and", "is_re_compilable", "(", "to_replace", ")", "# regex is regex compilable", "regex_re", "=", "is_re_compilable", "(", "regex", ")", "# only one will survive", "if", "to_rep_re", "and", "regex_re", ":", "raise", "AssertionError", "(", "'only one of to_replace and regex can be '", "'regex compilable'", ")", "# if regex was passed as something that can be a regex (rather than a", "# boolean)", "if", "regex_re", ":", "to_replace", "=", "regex", "regex", "=", "regex_re", "or", "to_rep_re", "# try to get the pattern attribute (compiled re) or it's a string", "try", ":", "pattern", "=", "to_replace", ".", "pattern", "except", "AttributeError", ":", "pattern", "=", "to_replace", "# if the pattern is not empty and to_replace is either a string or a", "# regex", "if", "regex", "and", "pattern", ":", "rx", "=", "re", ".", "compile", "(", "to_replace", ")", "else", ":", "# if the thing to replace is not a string or compiled regex call", "# the superclass method -> to_replace is some kind of object", "return", "super", "(", ")", ".", "replace", "(", "to_replace", ",", "value", ",", "inplace", "=", "inplace", ",", "filter", "=", "filter", ",", "regex", "=", "regex", ")", "new_values", "=", "self", ".", "values", "if", "inplace", "else", "self", ".", "values", ".", "copy", "(", ")", "# deal with replacing values with objects (strings) that match but", "# whose replacement is not a string (numeric, nan, object)", "if", "isna", "(", "value", ")", "or", "not", "isinstance", "(", "value", ",", "str", ")", ":", "def", "re_replacer", "(", "s", ")", ":", "try", ":", "return", "value", "if", "rx", ".", "search", "(", "s", ")", "is", "not", "None", "else", "s", "except", "TypeError", ":", "return", "s", "else", ":", "# value is guaranteed to be a string here, s can be either a string", "# or null if it's null it gets returned", "def", "re_replacer", "(", "s", ")", ":", "try", ":", "return", "rx", ".", "sub", "(", "value", ",", "s", ")", "except", "TypeError", ":", "return", "s", "f", "=", "np", ".", "vectorize", "(", "re_replacer", ",", "otypes", "=", "[", "self", ".", "dtype", "]", ")", "if", "filter", "is", "None", ":", "filt", "=", "slice", "(", "None", ")", "else", ":", "filt", "=", "self", ".", "mgr_locs", ".", "isin", "(", "filter", ")", ".", "nonzero", "(", ")", "[", "0", "]", "if", "mask", "is", "None", ":", "new_values", "[", "filt", "]", "=", "f", "(", "new_values", "[", "filt", "]", ")", "else", ":", "new_values", "[", "filt", "]", "[", "mask", "]", "=", "f", "(", "new_values", "[", "filt", "]", "[", "mask", "]", ")", "# convert", "block", "=", "self", ".", "make_block", "(", "new_values", ")", "if", "convert", ":", "block", "=", "block", ".", "convert", "(", "by_item", "=", "True", ",", "numeric", "=", "False", ")", "return", "block" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
ObjectBlock._replace_coerce
Replace value corresponding to the given boolean array with another value. Parameters ---------- to_replace : object or pattern Scalar to replace or regular expression to match. value : object Replacement object. inplace : bool, default False Perform inplace modification. regex : bool, default False If true, perform regular expression substitution. convert : bool, default True If true, try to coerce any object types to better types. mask : array-like of bool, optional True indicate corresponding element is ignored. Returns ------- A new block if there is anything to replace or the original block.
pandas/core/internals/blocks.py
def _replace_coerce(self, to_replace, value, inplace=True, regex=False, convert=False, mask=None): """ Replace value corresponding to the given boolean array with another value. Parameters ---------- to_replace : object or pattern Scalar to replace or regular expression to match. value : object Replacement object. inplace : bool, default False Perform inplace modification. regex : bool, default False If true, perform regular expression substitution. convert : bool, default True If true, try to coerce any object types to better types. mask : array-like of bool, optional True indicate corresponding element is ignored. Returns ------- A new block if there is anything to replace or the original block. """ if mask.any(): block = super()._replace_coerce( to_replace=to_replace, value=value, inplace=inplace, regex=regex, convert=convert, mask=mask) if convert: block = [b.convert(by_item=True, numeric=False, copy=True) for b in block] return block return self
def _replace_coerce(self, to_replace, value, inplace=True, regex=False, convert=False, mask=None): """ Replace value corresponding to the given boolean array with another value. Parameters ---------- to_replace : object or pattern Scalar to replace or regular expression to match. value : object Replacement object. inplace : bool, default False Perform inplace modification. regex : bool, default False If true, perform regular expression substitution. convert : bool, default True If true, try to coerce any object types to better types. mask : array-like of bool, optional True indicate corresponding element is ignored. Returns ------- A new block if there is anything to replace or the original block. """ if mask.any(): block = super()._replace_coerce( to_replace=to_replace, value=value, inplace=inplace, regex=regex, convert=convert, mask=mask) if convert: block = [b.convert(by_item=True, numeric=False, copy=True) for b in block] return block return self
[ "Replace", "value", "corresponding", "to", "the", "given", "boolean", "array", "with", "another", "value", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2843-L2876
[ "def", "_replace_coerce", "(", "self", ",", "to_replace", ",", "value", ",", "inplace", "=", "True", ",", "regex", "=", "False", ",", "convert", "=", "False", ",", "mask", "=", "None", ")", ":", "if", "mask", ".", "any", "(", ")", ":", "block", "=", "super", "(", ")", ".", "_replace_coerce", "(", "to_replace", "=", "to_replace", ",", "value", "=", "value", ",", "inplace", "=", "inplace", ",", "regex", "=", "regex", ",", "convert", "=", "convert", ",", "mask", "=", "mask", ")", "if", "convert", ":", "block", "=", "[", "b", ".", "convert", "(", "by_item", "=", "True", ",", "numeric", "=", "False", ",", "copy", "=", "True", ")", "for", "b", "in", "block", "]", "return", "block", "return", "self" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
CategoricalBlock._try_coerce_result
reverse of try_coerce_args
pandas/core/internals/blocks.py
def _try_coerce_result(self, result): """ reverse of try_coerce_args """ # GH12564: CategoricalBlock is 1-dim only # while returned results could be any dim if ((not is_categorical_dtype(result)) and isinstance(result, np.ndarray)): result = _block_shape(result, ndim=self.ndim) return result
def _try_coerce_result(self, result): """ reverse of try_coerce_args """ # GH12564: CategoricalBlock is 1-dim only # while returned results could be any dim if ((not is_categorical_dtype(result)) and isinstance(result, np.ndarray)): result = _block_shape(result, ndim=self.ndim) return result
[ "reverse", "of", "try_coerce_args" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2905-L2914
[ "def", "_try_coerce_result", "(", "self", ",", "result", ")", ":", "# GH12564: CategoricalBlock is 1-dim only", "# while returned results could be any dim", "if", "(", "(", "not", "is_categorical_dtype", "(", "result", ")", ")", "and", "isinstance", "(", "result", ",", "np", ".", "ndarray", ")", ")", ":", "result", "=", "_block_shape", "(", "result", ",", "ndim", "=", "self", ".", "ndim", ")", "return", "result" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
CategoricalBlock.to_native_types
convert to our native types format, slicing if desired
pandas/core/internals/blocks.py
def to_native_types(self, slicer=None, na_rep='', quoting=None, **kwargs): """ convert to our native types format, slicing if desired """ values = self.values if slicer is not None: # Categorical is always one dimension values = values[slicer] mask = isna(values) values = np.array(values, dtype='object') values[mask] = na_rep # we are expected to return a 2-d ndarray return values.reshape(1, len(values))
def to_native_types(self, slicer=None, na_rep='', quoting=None, **kwargs): """ convert to our native types format, slicing if desired """ values = self.values if slicer is not None: # Categorical is always one dimension values = values[slicer] mask = isna(values) values = np.array(values, dtype='object') values[mask] = na_rep # we are expected to return a 2-d ndarray return values.reshape(1, len(values))
[ "convert", "to", "our", "native", "types", "format", "slicing", "if", "desired" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/blocks.py#L2922-L2934
[ "def", "to_native_types", "(", "self", ",", "slicer", "=", "None", ",", "na_rep", "=", "''", ",", "quoting", "=", "None", ",", "*", "*", "kwargs", ")", ":", "values", "=", "self", ".", "values", "if", "slicer", "is", "not", "None", ":", "# Categorical is always one dimension", "values", "=", "values", "[", "slicer", "]", "mask", "=", "isna", "(", "values", ")", "values", "=", "np", ".", "array", "(", "values", ",", "dtype", "=", "'object'", ")", "values", "[", "mask", "]", "=", "na_rep", "# we are expected to return a 2-d ndarray", "return", "values", ".", "reshape", "(", "1", ",", "len", "(", "values", ")", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_XlwtWriter._style_to_xlwt
helper which recursively generate an xlwt easy style string for example: hstyle = {"font": {"bold": True}, "border": {"top": "thin", "right": "thin", "bottom": "thin", "left": "thin"}, "align": {"horiz": "center"}} will be converted to font: bold on; \ border: top thin, right thin, bottom thin, left thin; \ align: horiz center;
pandas/io/excel/_xlwt.py
def _style_to_xlwt(cls, item, firstlevel=True, field_sep=',', line_sep=';'): """helper which recursively generate an xlwt easy style string for example: hstyle = {"font": {"bold": True}, "border": {"top": "thin", "right": "thin", "bottom": "thin", "left": "thin"}, "align": {"horiz": "center"}} will be converted to font: bold on; \ border: top thin, right thin, bottom thin, left thin; \ align: horiz center; """ if hasattr(item, 'items'): if firstlevel: it = ["{key}: {val}" .format(key=key, val=cls._style_to_xlwt(value, False)) for key, value in item.items()] out = "{sep} ".format(sep=(line_sep).join(it)) return out else: it = ["{key} {val}" .format(key=key, val=cls._style_to_xlwt(value, False)) for key, value in item.items()] out = "{sep} ".format(sep=(field_sep).join(it)) return out else: item = "{item}".format(item=item) item = item.replace("True", "on") item = item.replace("False", "off") return item
def _style_to_xlwt(cls, item, firstlevel=True, field_sep=',', line_sep=';'): """helper which recursively generate an xlwt easy style string for example: hstyle = {"font": {"bold": True}, "border": {"top": "thin", "right": "thin", "bottom": "thin", "left": "thin"}, "align": {"horiz": "center"}} will be converted to font: bold on; \ border: top thin, right thin, bottom thin, left thin; \ align: horiz center; """ if hasattr(item, 'items'): if firstlevel: it = ["{key}: {val}" .format(key=key, val=cls._style_to_xlwt(value, False)) for key, value in item.items()] out = "{sep} ".format(sep=(line_sep).join(it)) return out else: it = ["{key} {val}" .format(key=key, val=cls._style_to_xlwt(value, False)) for key, value in item.items()] out = "{sep} ".format(sep=(field_sep).join(it)) return out else: item = "{item}".format(item=item) item = item.replace("True", "on") item = item.replace("False", "off") return item
[ "helper", "which", "recursively", "generate", "an", "xlwt", "easy", "style", "string", "for", "example", ":" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/io/excel/_xlwt.py#L78-L111
[ "def", "_style_to_xlwt", "(", "cls", ",", "item", ",", "firstlevel", "=", "True", ",", "field_sep", "=", "','", ",", "line_sep", "=", "';'", ")", ":", "if", "hasattr", "(", "item", ",", "'items'", ")", ":", "if", "firstlevel", ":", "it", "=", "[", "\"{key}: {val}\"", ".", "format", "(", "key", "=", "key", ",", "val", "=", "cls", ".", "_style_to_xlwt", "(", "value", ",", "False", ")", ")", "for", "key", ",", "value", "in", "item", ".", "items", "(", ")", "]", "out", "=", "\"{sep} \"", ".", "format", "(", "sep", "=", "(", "line_sep", ")", ".", "join", "(", "it", ")", ")", "return", "out", "else", ":", "it", "=", "[", "\"{key} {val}\"", ".", "format", "(", "key", "=", "key", ",", "val", "=", "cls", ".", "_style_to_xlwt", "(", "value", ",", "False", ")", ")", "for", "key", ",", "value", "in", "item", ".", "items", "(", ")", "]", "out", "=", "\"{sep} \"", ".", "format", "(", "sep", "=", "(", "field_sep", ")", ".", "join", "(", "it", ")", ")", "return", "out", "else", ":", "item", "=", "\"{item}\"", ".", "format", "(", "item", "=", "item", ")", "item", "=", "item", ".", "replace", "(", "\"True\"", ",", "\"on\"", ")", "item", "=", "item", ".", "replace", "(", "\"False\"", ",", "\"off\"", ")", "return", "item" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_XlwtWriter._convert_to_style
converts a style_dict to an xlwt style object Parameters ---------- style_dict : style dictionary to convert num_format_str : optional number format string
pandas/io/excel/_xlwt.py
def _convert_to_style(cls, style_dict, num_format_str=None): """ converts a style_dict to an xlwt style object Parameters ---------- style_dict : style dictionary to convert num_format_str : optional number format string """ import xlwt if style_dict: xlwt_stylestr = cls._style_to_xlwt(style_dict) style = xlwt.easyxf(xlwt_stylestr, field_sep=',', line_sep=';') else: style = xlwt.XFStyle() if num_format_str is not None: style.num_format_str = num_format_str return style
def _convert_to_style(cls, style_dict, num_format_str=None): """ converts a style_dict to an xlwt style object Parameters ---------- style_dict : style dictionary to convert num_format_str : optional number format string """ import xlwt if style_dict: xlwt_stylestr = cls._style_to_xlwt(style_dict) style = xlwt.easyxf(xlwt_stylestr, field_sep=',', line_sep=';') else: style = xlwt.XFStyle() if num_format_str is not None: style.num_format_str = num_format_str return style
[ "converts", "a", "style_dict", "to", "an", "xlwt", "style", "object", "Parameters", "----------", "style_dict", ":", "style", "dictionary", "to", "convert", "num_format_str", ":", "optional", "number", "format", "string" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/io/excel/_xlwt.py#L114-L132
[ "def", "_convert_to_style", "(", "cls", ",", "style_dict", ",", "num_format_str", "=", "None", ")", ":", "import", "xlwt", "if", "style_dict", ":", "xlwt_stylestr", "=", "cls", ".", "_style_to_xlwt", "(", "style_dict", ")", "style", "=", "xlwt", ".", "easyxf", "(", "xlwt_stylestr", ",", "field_sep", "=", "','", ",", "line_sep", "=", "';'", ")", "else", ":", "style", "=", "xlwt", ".", "XFStyle", "(", ")", "if", "num_format_str", "is", "not", "None", ":", "style", ".", "num_format_str", "=", "num_format_str", "return", "style" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_XlsxStyler.convert
converts a style_dict to an xlsxwriter format dict Parameters ---------- style_dict : style dictionary to convert num_format_str : optional number format string
pandas/io/excel/_xlsxwriter.py
def convert(cls, style_dict, num_format_str=None): """ converts a style_dict to an xlsxwriter format dict Parameters ---------- style_dict : style dictionary to convert num_format_str : optional number format string """ # Create a XlsxWriter format object. props = {} if num_format_str is not None: props['num_format'] = num_format_str if style_dict is None: return props if 'borders' in style_dict: style_dict = style_dict.copy() style_dict['border'] = style_dict.pop('borders') for style_group_key, style_group in style_dict.items(): for src, dst in cls.STYLE_MAPPING.get(style_group_key, []): # src is a sequence of keys into a nested dict # dst is a flat key if dst in props: continue v = style_group for k in src: try: v = v[k] except (KeyError, TypeError): break else: props[dst] = v if isinstance(props.get('pattern'), str): # TODO: support other fill patterns props['pattern'] = 0 if props['pattern'] == 'none' else 1 for k in ['border', 'top', 'right', 'bottom', 'left']: if isinstance(props.get(k), str): try: props[k] = ['none', 'thin', 'medium', 'dashed', 'dotted', 'thick', 'double', 'hair', 'mediumDashed', 'dashDot', 'mediumDashDot', 'dashDotDot', 'mediumDashDotDot', 'slantDashDot'].index(props[k]) except ValueError: props[k] = 2 if isinstance(props.get('font_script'), str): props['font_script'] = ['baseline', 'superscript', 'subscript'].index(props['font_script']) if isinstance(props.get('underline'), str): props['underline'] = {'none': 0, 'single': 1, 'double': 2, 'singleAccounting': 33, 'doubleAccounting': 34}[props['underline']] return props
def convert(cls, style_dict, num_format_str=None): """ converts a style_dict to an xlsxwriter format dict Parameters ---------- style_dict : style dictionary to convert num_format_str : optional number format string """ # Create a XlsxWriter format object. props = {} if num_format_str is not None: props['num_format'] = num_format_str if style_dict is None: return props if 'borders' in style_dict: style_dict = style_dict.copy() style_dict['border'] = style_dict.pop('borders') for style_group_key, style_group in style_dict.items(): for src, dst in cls.STYLE_MAPPING.get(style_group_key, []): # src is a sequence of keys into a nested dict # dst is a flat key if dst in props: continue v = style_group for k in src: try: v = v[k] except (KeyError, TypeError): break else: props[dst] = v if isinstance(props.get('pattern'), str): # TODO: support other fill patterns props['pattern'] = 0 if props['pattern'] == 'none' else 1 for k in ['border', 'top', 'right', 'bottom', 'left']: if isinstance(props.get(k), str): try: props[k] = ['none', 'thin', 'medium', 'dashed', 'dotted', 'thick', 'double', 'hair', 'mediumDashed', 'dashDot', 'mediumDashDot', 'dashDotDot', 'mediumDashDotDot', 'slantDashDot'].index(props[k]) except ValueError: props[k] = 2 if isinstance(props.get('font_script'), str): props['font_script'] = ['baseline', 'superscript', 'subscript'].index(props['font_script']) if isinstance(props.get('underline'), str): props['underline'] = {'none': 0, 'single': 1, 'double': 2, 'singleAccounting': 33, 'doubleAccounting': 34}[props['underline']] return props
[ "converts", "a", "style_dict", "to", "an", "xlsxwriter", "format", "dict" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/io/excel/_xlsxwriter.py#L85-L147
[ "def", "convert", "(", "cls", ",", "style_dict", ",", "num_format_str", "=", "None", ")", ":", "# Create a XlsxWriter format object.", "props", "=", "{", "}", "if", "num_format_str", "is", "not", "None", ":", "props", "[", "'num_format'", "]", "=", "num_format_str", "if", "style_dict", "is", "None", ":", "return", "props", "if", "'borders'", "in", "style_dict", ":", "style_dict", "=", "style_dict", ".", "copy", "(", ")", "style_dict", "[", "'border'", "]", "=", "style_dict", ".", "pop", "(", "'borders'", ")", "for", "style_group_key", ",", "style_group", "in", "style_dict", ".", "items", "(", ")", ":", "for", "src", ",", "dst", "in", "cls", ".", "STYLE_MAPPING", ".", "get", "(", "style_group_key", ",", "[", "]", ")", ":", "# src is a sequence of keys into a nested dict", "# dst is a flat key", "if", "dst", "in", "props", ":", "continue", "v", "=", "style_group", "for", "k", "in", "src", ":", "try", ":", "v", "=", "v", "[", "k", "]", "except", "(", "KeyError", ",", "TypeError", ")", ":", "break", "else", ":", "props", "[", "dst", "]", "=", "v", "if", "isinstance", "(", "props", ".", "get", "(", "'pattern'", ")", ",", "str", ")", ":", "# TODO: support other fill patterns", "props", "[", "'pattern'", "]", "=", "0", "if", "props", "[", "'pattern'", "]", "==", "'none'", "else", "1", "for", "k", "in", "[", "'border'", ",", "'top'", ",", "'right'", ",", "'bottom'", ",", "'left'", "]", ":", "if", "isinstance", "(", "props", ".", "get", "(", "k", ")", ",", "str", ")", ":", "try", ":", "props", "[", "k", "]", "=", "[", "'none'", ",", "'thin'", ",", "'medium'", ",", "'dashed'", ",", "'dotted'", ",", "'thick'", ",", "'double'", ",", "'hair'", ",", "'mediumDashed'", ",", "'dashDot'", ",", "'mediumDashDot'", ",", "'dashDotDot'", ",", "'mediumDashDotDot'", ",", "'slantDashDot'", "]", ".", "index", "(", "props", "[", "k", "]", ")", "except", "ValueError", ":", "props", "[", "k", "]", "=", "2", "if", "isinstance", "(", "props", ".", "get", "(", "'font_script'", ")", ",", "str", ")", ":", "props", "[", "'font_script'", "]", "=", "[", "'baseline'", ",", "'superscript'", ",", "'subscript'", "]", ".", "index", "(", "props", "[", "'font_script'", "]", ")", "if", "isinstance", "(", "props", ".", "get", "(", "'underline'", ")", ",", "str", ")", ":", "props", "[", "'underline'", "]", "=", "{", "'none'", ":", "0", ",", "'single'", ":", "1", ",", "'double'", ":", "2", ",", "'singleAccounting'", ":", "33", ",", "'doubleAccounting'", ":", "34", "}", "[", "props", "[", "'underline'", "]", "]", "return", "props" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_unstack_extension_series
Unstack an ExtensionArray-backed Series. The ExtensionDtype is preserved. Parameters ---------- series : Series A Series with an ExtensionArray for values level : Any The level name or number. fill_value : Any The user-level (not physical storage) fill value to use for missing values introduced by the reshape. Passed to ``series.values.take``. Returns ------- DataFrame Each column of the DataFrame will have the same dtype as the input Series.
pandas/core/reshape/reshape.py
def _unstack_extension_series(series, level, fill_value): """ Unstack an ExtensionArray-backed Series. The ExtensionDtype is preserved. Parameters ---------- series : Series A Series with an ExtensionArray for values level : Any The level name or number. fill_value : Any The user-level (not physical storage) fill value to use for missing values introduced by the reshape. Passed to ``series.values.take``. Returns ------- DataFrame Each column of the DataFrame will have the same dtype as the input Series. """ # Implementation note: the basic idea is to # 1. Do a regular unstack on a dummy array of integers # 2. Followup with a columnwise take. # We use the dummy take to discover newly-created missing values # introduced by the reshape. from pandas.core.reshape.concat import concat dummy_arr = np.arange(len(series)) # fill_value=-1, since we will do a series.values.take later result = _Unstacker(dummy_arr, series.index, level=level, fill_value=-1).get_result() out = [] values = extract_array(series, extract_numpy=False) for col, indices in result.iteritems(): out.append(Series(values.take(indices.values, allow_fill=True, fill_value=fill_value), name=col, index=result.index)) return concat(out, axis='columns', copy=False, keys=result.columns)
def _unstack_extension_series(series, level, fill_value): """ Unstack an ExtensionArray-backed Series. The ExtensionDtype is preserved. Parameters ---------- series : Series A Series with an ExtensionArray for values level : Any The level name or number. fill_value : Any The user-level (not physical storage) fill value to use for missing values introduced by the reshape. Passed to ``series.values.take``. Returns ------- DataFrame Each column of the DataFrame will have the same dtype as the input Series. """ # Implementation note: the basic idea is to # 1. Do a regular unstack on a dummy array of integers # 2. Followup with a columnwise take. # We use the dummy take to discover newly-created missing values # introduced by the reshape. from pandas.core.reshape.concat import concat dummy_arr = np.arange(len(series)) # fill_value=-1, since we will do a series.values.take later result = _Unstacker(dummy_arr, series.index, level=level, fill_value=-1).get_result() out = [] values = extract_array(series, extract_numpy=False) for col, indices in result.iteritems(): out.append(Series(values.take(indices.values, allow_fill=True, fill_value=fill_value), name=col, index=result.index)) return concat(out, axis='columns', copy=False, keys=result.columns)
[ "Unstack", "an", "ExtensionArray", "-", "backed", "Series", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/reshape/reshape.py#L411-L454
[ "def", "_unstack_extension_series", "(", "series", ",", "level", ",", "fill_value", ")", ":", "# Implementation note: the basic idea is to", "# 1. Do a regular unstack on a dummy array of integers", "# 2. Followup with a columnwise take.", "# We use the dummy take to discover newly-created missing values", "# introduced by the reshape.", "from", "pandas", ".", "core", ".", "reshape", ".", "concat", "import", "concat", "dummy_arr", "=", "np", ".", "arange", "(", "len", "(", "series", ")", ")", "# fill_value=-1, since we will do a series.values.take later", "result", "=", "_Unstacker", "(", "dummy_arr", ",", "series", ".", "index", ",", "level", "=", "level", ",", "fill_value", "=", "-", "1", ")", ".", "get_result", "(", ")", "out", "=", "[", "]", "values", "=", "extract_array", "(", "series", ",", "extract_numpy", "=", "False", ")", "for", "col", ",", "indices", "in", "result", ".", "iteritems", "(", ")", ":", "out", ".", "append", "(", "Series", "(", "values", ".", "take", "(", "indices", ".", "values", ",", "allow_fill", "=", "True", ",", "fill_value", "=", "fill_value", ")", ",", "name", "=", "col", ",", "index", "=", "result", ".", "index", ")", ")", "return", "concat", "(", "out", ",", "axis", "=", "'columns'", ",", "copy", "=", "False", ",", "keys", "=", "result", ".", "columns", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
stack
Convert DataFrame to Series with multi-level Index. Columns become the second level of the resulting hierarchical index Returns ------- stacked : Series
pandas/core/reshape/reshape.py
def stack(frame, level=-1, dropna=True): """ Convert DataFrame to Series with multi-level Index. Columns become the second level of the resulting hierarchical index Returns ------- stacked : Series """ def factorize(index): if index.is_unique: return index, np.arange(len(index)) codes, categories = _factorize_from_iterable(index) return categories, codes N, K = frame.shape # Will also convert negative level numbers and check if out of bounds. level_num = frame.columns._get_level_number(level) if isinstance(frame.columns, MultiIndex): return _stack_multi_columns(frame, level_num=level_num, dropna=dropna) elif isinstance(frame.index, MultiIndex): new_levels = list(frame.index.levels) new_codes = [lab.repeat(K) for lab in frame.index.codes] clev, clab = factorize(frame.columns) new_levels.append(clev) new_codes.append(np.tile(clab, N).ravel()) new_names = list(frame.index.names) new_names.append(frame.columns.name) new_index = MultiIndex(levels=new_levels, codes=new_codes, names=new_names, verify_integrity=False) else: levels, (ilab, clab) = zip(*map(factorize, (frame.index, frame.columns))) codes = ilab.repeat(K), np.tile(clab, N).ravel() new_index = MultiIndex(levels=levels, codes=codes, names=[frame.index.name, frame.columns.name], verify_integrity=False) if frame._is_homogeneous_type: # For homogeneous EAs, frame.values will coerce to object. So # we concatenate instead. dtypes = list(frame.dtypes.values) dtype = dtypes[0] if is_extension_array_dtype(dtype): arr = dtype.construct_array_type() new_values = arr._concat_same_type([ col._values for _, col in frame.iteritems() ]) new_values = _reorder_for_extension_array_stack(new_values, N, K) else: # homogeneous, non-EA new_values = frame.values.ravel() else: # non-homogeneous new_values = frame.values.ravel() if dropna: mask = notna(new_values) new_values = new_values[mask] new_index = new_index[mask] return frame._constructor_sliced(new_values, index=new_index)
def stack(frame, level=-1, dropna=True): """ Convert DataFrame to Series with multi-level Index. Columns become the second level of the resulting hierarchical index Returns ------- stacked : Series """ def factorize(index): if index.is_unique: return index, np.arange(len(index)) codes, categories = _factorize_from_iterable(index) return categories, codes N, K = frame.shape # Will also convert negative level numbers and check if out of bounds. level_num = frame.columns._get_level_number(level) if isinstance(frame.columns, MultiIndex): return _stack_multi_columns(frame, level_num=level_num, dropna=dropna) elif isinstance(frame.index, MultiIndex): new_levels = list(frame.index.levels) new_codes = [lab.repeat(K) for lab in frame.index.codes] clev, clab = factorize(frame.columns) new_levels.append(clev) new_codes.append(np.tile(clab, N).ravel()) new_names = list(frame.index.names) new_names.append(frame.columns.name) new_index = MultiIndex(levels=new_levels, codes=new_codes, names=new_names, verify_integrity=False) else: levels, (ilab, clab) = zip(*map(factorize, (frame.index, frame.columns))) codes = ilab.repeat(K), np.tile(clab, N).ravel() new_index = MultiIndex(levels=levels, codes=codes, names=[frame.index.name, frame.columns.name], verify_integrity=False) if frame._is_homogeneous_type: # For homogeneous EAs, frame.values will coerce to object. So # we concatenate instead. dtypes = list(frame.dtypes.values) dtype = dtypes[0] if is_extension_array_dtype(dtype): arr = dtype.construct_array_type() new_values = arr._concat_same_type([ col._values for _, col in frame.iteritems() ]) new_values = _reorder_for_extension_array_stack(new_values, N, K) else: # homogeneous, non-EA new_values = frame.values.ravel() else: # non-homogeneous new_values = frame.values.ravel() if dropna: mask = notna(new_values) new_values = new_values[mask] new_index = new_index[mask] return frame._constructor_sliced(new_values, index=new_index)
[ "Convert", "DataFrame", "to", "Series", "with", "multi", "-", "level", "Index", ".", "Columns", "become", "the", "second", "level", "of", "the", "resulting", "hierarchical", "index" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/reshape/reshape.py#L457-L524
[ "def", "stack", "(", "frame", ",", "level", "=", "-", "1", ",", "dropna", "=", "True", ")", ":", "def", "factorize", "(", "index", ")", ":", "if", "index", ".", "is_unique", ":", "return", "index", ",", "np", ".", "arange", "(", "len", "(", "index", ")", ")", "codes", ",", "categories", "=", "_factorize_from_iterable", "(", "index", ")", "return", "categories", ",", "codes", "N", ",", "K", "=", "frame", ".", "shape", "# Will also convert negative level numbers and check if out of bounds.", "level_num", "=", "frame", ".", "columns", ".", "_get_level_number", "(", "level", ")", "if", "isinstance", "(", "frame", ".", "columns", ",", "MultiIndex", ")", ":", "return", "_stack_multi_columns", "(", "frame", ",", "level_num", "=", "level_num", ",", "dropna", "=", "dropna", ")", "elif", "isinstance", "(", "frame", ".", "index", ",", "MultiIndex", ")", ":", "new_levels", "=", "list", "(", "frame", ".", "index", ".", "levels", ")", "new_codes", "=", "[", "lab", ".", "repeat", "(", "K", ")", "for", "lab", "in", "frame", ".", "index", ".", "codes", "]", "clev", ",", "clab", "=", "factorize", "(", "frame", ".", "columns", ")", "new_levels", ".", "append", "(", "clev", ")", "new_codes", ".", "append", "(", "np", ".", "tile", "(", "clab", ",", "N", ")", ".", "ravel", "(", ")", ")", "new_names", "=", "list", "(", "frame", ".", "index", ".", "names", ")", "new_names", ".", "append", "(", "frame", ".", "columns", ".", "name", ")", "new_index", "=", "MultiIndex", "(", "levels", "=", "new_levels", ",", "codes", "=", "new_codes", ",", "names", "=", "new_names", ",", "verify_integrity", "=", "False", ")", "else", ":", "levels", ",", "(", "ilab", ",", "clab", ")", "=", "zip", "(", "*", "map", "(", "factorize", ",", "(", "frame", ".", "index", ",", "frame", ".", "columns", ")", ")", ")", "codes", "=", "ilab", ".", "repeat", "(", "K", ")", ",", "np", ".", "tile", "(", "clab", ",", "N", ")", ".", "ravel", "(", ")", "new_index", "=", "MultiIndex", "(", "levels", "=", "levels", ",", "codes", "=", "codes", ",", "names", "=", "[", "frame", ".", "index", ".", "name", ",", "frame", ".", "columns", ".", "name", "]", ",", "verify_integrity", "=", "False", ")", "if", "frame", ".", "_is_homogeneous_type", ":", "# For homogeneous EAs, frame.values will coerce to object. So", "# we concatenate instead.", "dtypes", "=", "list", "(", "frame", ".", "dtypes", ".", "values", ")", "dtype", "=", "dtypes", "[", "0", "]", "if", "is_extension_array_dtype", "(", "dtype", ")", ":", "arr", "=", "dtype", ".", "construct_array_type", "(", ")", "new_values", "=", "arr", ".", "_concat_same_type", "(", "[", "col", ".", "_values", "for", "_", ",", "col", "in", "frame", ".", "iteritems", "(", ")", "]", ")", "new_values", "=", "_reorder_for_extension_array_stack", "(", "new_values", ",", "N", ",", "K", ")", "else", ":", "# homogeneous, non-EA", "new_values", "=", "frame", ".", "values", ".", "ravel", "(", ")", "else", ":", "# non-homogeneous", "new_values", "=", "frame", ".", "values", ".", "ravel", "(", ")", "if", "dropna", ":", "mask", "=", "notna", "(", "new_values", ")", "new_values", "=", "new_values", "[", "mask", "]", "new_index", "=", "new_index", "[", "mask", "]", "return", "frame", ".", "_constructor_sliced", "(", "new_values", ",", "index", "=", "new_index", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
get_dummies
Convert categorical variable into dummy/indicator variables. Parameters ---------- data : array-like, Series, or DataFrame Data of which to get dummy indicators. prefix : str, list of str, or dict of str, default None String to append DataFrame column names. Pass a list with length equal to the number of columns when calling get_dummies on a DataFrame. Alternatively, `prefix` can be a dictionary mapping column names to prefixes. prefix_sep : str, default '_' If appending prefix, separator/delimiter to use. Or pass a list or dictionary as with `prefix`. dummy_na : bool, default False Add a column to indicate NaNs, if False NaNs are ignored. columns : list-like, default None Column names in the DataFrame to be encoded. If `columns` is None then all the columns with `object` or `category` dtype will be converted. sparse : bool, default False Whether the dummy-encoded columns should be backed by a :class:`SparseArray` (True) or a regular NumPy array (False). drop_first : bool, default False Whether to get k-1 dummies out of k categorical levels by removing the first level. .. versionadded:: 0.18.0 dtype : dtype, default np.uint8 Data type for new columns. Only a single dtype is allowed. .. versionadded:: 0.23.0 Returns ------- DataFrame Dummy-coded data. See Also -------- Series.str.get_dummies : Convert Series to dummy codes. Examples -------- >>> s = pd.Series(list('abca')) >>> pd.get_dummies(s) a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0 >>> s1 = ['a', 'b', np.nan] >>> pd.get_dummies(s1) a b 0 1 0 1 0 1 2 0 0 >>> pd.get_dummies(s1, dummy_na=True) a b NaN 0 1 0 0 1 0 1 0 2 0 0 1 >>> df = pd.DataFrame({'A': ['a', 'b', 'a'], 'B': ['b', 'a', 'c'], ... 'C': [1, 2, 3]}) >>> pd.get_dummies(df, prefix=['col1', 'col2']) C col1_a col1_b col2_a col2_b col2_c 0 1 1 0 0 1 0 1 2 0 1 1 0 0 2 3 1 0 0 0 1 >>> pd.get_dummies(pd.Series(list('abcaa'))) a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0 4 1 0 0 >>> pd.get_dummies(pd.Series(list('abcaa')), drop_first=True) b c 0 0 0 1 1 0 2 0 1 3 0 0 4 0 0 >>> pd.get_dummies(pd.Series(list('abc')), dtype=float) a b c 0 1.0 0.0 0.0 1 0.0 1.0 0.0 2 0.0 0.0 1.0
pandas/core/reshape/reshape.py
def get_dummies(data, prefix=None, prefix_sep='_', dummy_na=False, columns=None, sparse=False, drop_first=False, dtype=None): """ Convert categorical variable into dummy/indicator variables. Parameters ---------- data : array-like, Series, or DataFrame Data of which to get dummy indicators. prefix : str, list of str, or dict of str, default None String to append DataFrame column names. Pass a list with length equal to the number of columns when calling get_dummies on a DataFrame. Alternatively, `prefix` can be a dictionary mapping column names to prefixes. prefix_sep : str, default '_' If appending prefix, separator/delimiter to use. Or pass a list or dictionary as with `prefix`. dummy_na : bool, default False Add a column to indicate NaNs, if False NaNs are ignored. columns : list-like, default None Column names in the DataFrame to be encoded. If `columns` is None then all the columns with `object` or `category` dtype will be converted. sparse : bool, default False Whether the dummy-encoded columns should be backed by a :class:`SparseArray` (True) or a regular NumPy array (False). drop_first : bool, default False Whether to get k-1 dummies out of k categorical levels by removing the first level. .. versionadded:: 0.18.0 dtype : dtype, default np.uint8 Data type for new columns. Only a single dtype is allowed. .. versionadded:: 0.23.0 Returns ------- DataFrame Dummy-coded data. See Also -------- Series.str.get_dummies : Convert Series to dummy codes. Examples -------- >>> s = pd.Series(list('abca')) >>> pd.get_dummies(s) a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0 >>> s1 = ['a', 'b', np.nan] >>> pd.get_dummies(s1) a b 0 1 0 1 0 1 2 0 0 >>> pd.get_dummies(s1, dummy_na=True) a b NaN 0 1 0 0 1 0 1 0 2 0 0 1 >>> df = pd.DataFrame({'A': ['a', 'b', 'a'], 'B': ['b', 'a', 'c'], ... 'C': [1, 2, 3]}) >>> pd.get_dummies(df, prefix=['col1', 'col2']) C col1_a col1_b col2_a col2_b col2_c 0 1 1 0 0 1 0 1 2 0 1 1 0 0 2 3 1 0 0 0 1 >>> pd.get_dummies(pd.Series(list('abcaa'))) a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0 4 1 0 0 >>> pd.get_dummies(pd.Series(list('abcaa')), drop_first=True) b c 0 0 0 1 1 0 2 0 1 3 0 0 4 0 0 >>> pd.get_dummies(pd.Series(list('abc')), dtype=float) a b c 0 1.0 0.0 0.0 1 0.0 1.0 0.0 2 0.0 0.0 1.0 """ from pandas.core.reshape.concat import concat from itertools import cycle dtypes_to_encode = ['object', 'category'] if isinstance(data, DataFrame): # determine columns being encoded if columns is None: data_to_encode = data.select_dtypes( include=dtypes_to_encode) else: data_to_encode = data[columns] # validate prefixes and separator to avoid silently dropping cols def check_len(item, name): len_msg = ("Length of '{name}' ({len_item}) did not match the " "length of the columns being encoded ({len_enc}).") if is_list_like(item): if not len(item) == data_to_encode.shape[1]: len_msg = len_msg.format(name=name, len_item=len(item), len_enc=data_to_encode.shape[1]) raise ValueError(len_msg) check_len(prefix, 'prefix') check_len(prefix_sep, 'prefix_sep') if isinstance(prefix, str): prefix = cycle([prefix]) if isinstance(prefix, dict): prefix = [prefix[col] for col in data_to_encode.columns] if prefix is None: prefix = data_to_encode.columns # validate separators if isinstance(prefix_sep, str): prefix_sep = cycle([prefix_sep]) elif isinstance(prefix_sep, dict): prefix_sep = [prefix_sep[col] for col in data_to_encode.columns] if data_to_encode.shape == data.shape: # Encoding the entire df, do not prepend any dropped columns with_dummies = [] elif columns is not None: # Encoding only cols specified in columns. Get all cols not in # columns to prepend to result. with_dummies = [data.drop(columns, axis=1)] else: # Encoding only object and category dtype columns. Get remaining # columns to prepend to result. with_dummies = [data.select_dtypes(exclude=dtypes_to_encode)] for (col, pre, sep) in zip(data_to_encode.iteritems(), prefix, prefix_sep): # col is (column_name, column), use just column data here dummy = _get_dummies_1d(col[1], prefix=pre, prefix_sep=sep, dummy_na=dummy_na, sparse=sparse, drop_first=drop_first, dtype=dtype) with_dummies.append(dummy) result = concat(with_dummies, axis=1) else: result = _get_dummies_1d(data, prefix, prefix_sep, dummy_na, sparse=sparse, drop_first=drop_first, dtype=dtype) return result
def get_dummies(data, prefix=None, prefix_sep='_', dummy_na=False, columns=None, sparse=False, drop_first=False, dtype=None): """ Convert categorical variable into dummy/indicator variables. Parameters ---------- data : array-like, Series, or DataFrame Data of which to get dummy indicators. prefix : str, list of str, or dict of str, default None String to append DataFrame column names. Pass a list with length equal to the number of columns when calling get_dummies on a DataFrame. Alternatively, `prefix` can be a dictionary mapping column names to prefixes. prefix_sep : str, default '_' If appending prefix, separator/delimiter to use. Or pass a list or dictionary as with `prefix`. dummy_na : bool, default False Add a column to indicate NaNs, if False NaNs are ignored. columns : list-like, default None Column names in the DataFrame to be encoded. If `columns` is None then all the columns with `object` or `category` dtype will be converted. sparse : bool, default False Whether the dummy-encoded columns should be backed by a :class:`SparseArray` (True) or a regular NumPy array (False). drop_first : bool, default False Whether to get k-1 dummies out of k categorical levels by removing the first level. .. versionadded:: 0.18.0 dtype : dtype, default np.uint8 Data type for new columns. Only a single dtype is allowed. .. versionadded:: 0.23.0 Returns ------- DataFrame Dummy-coded data. See Also -------- Series.str.get_dummies : Convert Series to dummy codes. Examples -------- >>> s = pd.Series(list('abca')) >>> pd.get_dummies(s) a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0 >>> s1 = ['a', 'b', np.nan] >>> pd.get_dummies(s1) a b 0 1 0 1 0 1 2 0 0 >>> pd.get_dummies(s1, dummy_na=True) a b NaN 0 1 0 0 1 0 1 0 2 0 0 1 >>> df = pd.DataFrame({'A': ['a', 'b', 'a'], 'B': ['b', 'a', 'c'], ... 'C': [1, 2, 3]}) >>> pd.get_dummies(df, prefix=['col1', 'col2']) C col1_a col1_b col2_a col2_b col2_c 0 1 1 0 0 1 0 1 2 0 1 1 0 0 2 3 1 0 0 0 1 >>> pd.get_dummies(pd.Series(list('abcaa'))) a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0 4 1 0 0 >>> pd.get_dummies(pd.Series(list('abcaa')), drop_first=True) b c 0 0 0 1 1 0 2 0 1 3 0 0 4 0 0 >>> pd.get_dummies(pd.Series(list('abc')), dtype=float) a b c 0 1.0 0.0 0.0 1 0.0 1.0 0.0 2 0.0 0.0 1.0 """ from pandas.core.reshape.concat import concat from itertools import cycle dtypes_to_encode = ['object', 'category'] if isinstance(data, DataFrame): # determine columns being encoded if columns is None: data_to_encode = data.select_dtypes( include=dtypes_to_encode) else: data_to_encode = data[columns] # validate prefixes and separator to avoid silently dropping cols def check_len(item, name): len_msg = ("Length of '{name}' ({len_item}) did not match the " "length of the columns being encoded ({len_enc}).") if is_list_like(item): if not len(item) == data_to_encode.shape[1]: len_msg = len_msg.format(name=name, len_item=len(item), len_enc=data_to_encode.shape[1]) raise ValueError(len_msg) check_len(prefix, 'prefix') check_len(prefix_sep, 'prefix_sep') if isinstance(prefix, str): prefix = cycle([prefix]) if isinstance(prefix, dict): prefix = [prefix[col] for col in data_to_encode.columns] if prefix is None: prefix = data_to_encode.columns # validate separators if isinstance(prefix_sep, str): prefix_sep = cycle([prefix_sep]) elif isinstance(prefix_sep, dict): prefix_sep = [prefix_sep[col] for col in data_to_encode.columns] if data_to_encode.shape == data.shape: # Encoding the entire df, do not prepend any dropped columns with_dummies = [] elif columns is not None: # Encoding only cols specified in columns. Get all cols not in # columns to prepend to result. with_dummies = [data.drop(columns, axis=1)] else: # Encoding only object and category dtype columns. Get remaining # columns to prepend to result. with_dummies = [data.select_dtypes(exclude=dtypes_to_encode)] for (col, pre, sep) in zip(data_to_encode.iteritems(), prefix, prefix_sep): # col is (column_name, column), use just column data here dummy = _get_dummies_1d(col[1], prefix=pre, prefix_sep=sep, dummy_na=dummy_na, sparse=sparse, drop_first=drop_first, dtype=dtype) with_dummies.append(dummy) result = concat(with_dummies, axis=1) else: result = _get_dummies_1d(data, prefix, prefix_sep, dummy_na, sparse=sparse, drop_first=drop_first, dtype=dtype) return result
[ "Convert", "categorical", "variable", "into", "dummy", "/", "indicator", "variables", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/reshape/reshape.py#L697-L865
[ "def", "get_dummies", "(", "data", ",", "prefix", "=", "None", ",", "prefix_sep", "=", "'_'", ",", "dummy_na", "=", "False", ",", "columns", "=", "None", ",", "sparse", "=", "False", ",", "drop_first", "=", "False", ",", "dtype", "=", "None", ")", ":", "from", "pandas", ".", "core", ".", "reshape", ".", "concat", "import", "concat", "from", "itertools", "import", "cycle", "dtypes_to_encode", "=", "[", "'object'", ",", "'category'", "]", "if", "isinstance", "(", "data", ",", "DataFrame", ")", ":", "# determine columns being encoded", "if", "columns", "is", "None", ":", "data_to_encode", "=", "data", ".", "select_dtypes", "(", "include", "=", "dtypes_to_encode", ")", "else", ":", "data_to_encode", "=", "data", "[", "columns", "]", "# validate prefixes and separator to avoid silently dropping cols", "def", "check_len", "(", "item", ",", "name", ")", ":", "len_msg", "=", "(", "\"Length of '{name}' ({len_item}) did not match the \"", "\"length of the columns being encoded ({len_enc}).\"", ")", "if", "is_list_like", "(", "item", ")", ":", "if", "not", "len", "(", "item", ")", "==", "data_to_encode", ".", "shape", "[", "1", "]", ":", "len_msg", "=", "len_msg", ".", "format", "(", "name", "=", "name", ",", "len_item", "=", "len", "(", "item", ")", ",", "len_enc", "=", "data_to_encode", ".", "shape", "[", "1", "]", ")", "raise", "ValueError", "(", "len_msg", ")", "check_len", "(", "prefix", ",", "'prefix'", ")", "check_len", "(", "prefix_sep", ",", "'prefix_sep'", ")", "if", "isinstance", "(", "prefix", ",", "str", ")", ":", "prefix", "=", "cycle", "(", "[", "prefix", "]", ")", "if", "isinstance", "(", "prefix", ",", "dict", ")", ":", "prefix", "=", "[", "prefix", "[", "col", "]", "for", "col", "in", "data_to_encode", ".", "columns", "]", "if", "prefix", "is", "None", ":", "prefix", "=", "data_to_encode", ".", "columns", "# validate separators", "if", "isinstance", "(", "prefix_sep", ",", "str", ")", ":", "prefix_sep", "=", "cycle", "(", "[", "prefix_sep", "]", ")", "elif", "isinstance", "(", "prefix_sep", ",", "dict", ")", ":", "prefix_sep", "=", "[", "prefix_sep", "[", "col", "]", "for", "col", "in", "data_to_encode", ".", "columns", "]", "if", "data_to_encode", ".", "shape", "==", "data", ".", "shape", ":", "# Encoding the entire df, do not prepend any dropped columns", "with_dummies", "=", "[", "]", "elif", "columns", "is", "not", "None", ":", "# Encoding only cols specified in columns. Get all cols not in", "# columns to prepend to result.", "with_dummies", "=", "[", "data", ".", "drop", "(", "columns", ",", "axis", "=", "1", ")", "]", "else", ":", "# Encoding only object and category dtype columns. Get remaining", "# columns to prepend to result.", "with_dummies", "=", "[", "data", ".", "select_dtypes", "(", "exclude", "=", "dtypes_to_encode", ")", "]", "for", "(", "col", ",", "pre", ",", "sep", ")", "in", "zip", "(", "data_to_encode", ".", "iteritems", "(", ")", ",", "prefix", ",", "prefix_sep", ")", ":", "# col is (column_name, column), use just column data here", "dummy", "=", "_get_dummies_1d", "(", "col", "[", "1", "]", ",", "prefix", "=", "pre", ",", "prefix_sep", "=", "sep", ",", "dummy_na", "=", "dummy_na", ",", "sparse", "=", "sparse", ",", "drop_first", "=", "drop_first", ",", "dtype", "=", "dtype", ")", "with_dummies", ".", "append", "(", "dummy", ")", "result", "=", "concat", "(", "with_dummies", ",", "axis", "=", "1", ")", "else", ":", "result", "=", "_get_dummies_1d", "(", "data", ",", "prefix", ",", "prefix_sep", ",", "dummy_na", ",", "sparse", "=", "sparse", ",", "drop_first", "=", "drop_first", ",", "dtype", "=", "dtype", ")", "return", "result" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
make_axis_dummies
Construct 1-0 dummy variables corresponding to designated axis labels Parameters ---------- frame : DataFrame axis : {'major', 'minor'}, default 'minor' transform : function, default None Function to apply to axis labels first. For example, to get "day of week" dummies in a time series regression you might call:: make_axis_dummies(panel, axis='major', transform=lambda d: d.weekday()) Returns ------- dummies : DataFrame Column names taken from chosen axis
pandas/core/reshape/reshape.py
def make_axis_dummies(frame, axis='minor', transform=None): """ Construct 1-0 dummy variables corresponding to designated axis labels Parameters ---------- frame : DataFrame axis : {'major', 'minor'}, default 'minor' transform : function, default None Function to apply to axis labels first. For example, to get "day of week" dummies in a time series regression you might call:: make_axis_dummies(panel, axis='major', transform=lambda d: d.weekday()) Returns ------- dummies : DataFrame Column names taken from chosen axis """ numbers = {'major': 0, 'minor': 1} num = numbers.get(axis, axis) items = frame.index.levels[num] codes = frame.index.codes[num] if transform is not None: mapped_items = items.map(transform) codes, items = _factorize_from_iterable(mapped_items.take(codes)) values = np.eye(len(items), dtype=float) values = values.take(codes, axis=0) return DataFrame(values, columns=items, index=frame.index)
def make_axis_dummies(frame, axis='minor', transform=None): """ Construct 1-0 dummy variables corresponding to designated axis labels Parameters ---------- frame : DataFrame axis : {'major', 'minor'}, default 'minor' transform : function, default None Function to apply to axis labels first. For example, to get "day of week" dummies in a time series regression you might call:: make_axis_dummies(panel, axis='major', transform=lambda d: d.weekday()) Returns ------- dummies : DataFrame Column names taken from chosen axis """ numbers = {'major': 0, 'minor': 1} num = numbers.get(axis, axis) items = frame.index.levels[num] codes = frame.index.codes[num] if transform is not None: mapped_items = items.map(transform) codes, items = _factorize_from_iterable(mapped_items.take(codes)) values = np.eye(len(items), dtype=float) values = values.take(codes, axis=0) return DataFrame(values, columns=items, index=frame.index)
[ "Construct", "1", "-", "0", "dummy", "variables", "corresponding", "to", "designated", "axis", "labels" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/reshape/reshape.py#L970-L1003
[ "def", "make_axis_dummies", "(", "frame", ",", "axis", "=", "'minor'", ",", "transform", "=", "None", ")", ":", "numbers", "=", "{", "'major'", ":", "0", ",", "'minor'", ":", "1", "}", "num", "=", "numbers", ".", "get", "(", "axis", ",", "axis", ")", "items", "=", "frame", ".", "index", ".", "levels", "[", "num", "]", "codes", "=", "frame", ".", "index", ".", "codes", "[", "num", "]", "if", "transform", "is", "not", "None", ":", "mapped_items", "=", "items", ".", "map", "(", "transform", ")", "codes", ",", "items", "=", "_factorize_from_iterable", "(", "mapped_items", ".", "take", "(", "codes", ")", ")", "values", "=", "np", ".", "eye", "(", "len", "(", "items", ")", ",", "dtype", "=", "float", ")", "values", "=", "values", ".", "take", "(", "codes", ",", "axis", "=", "0", ")", "return", "DataFrame", "(", "values", ",", "columns", "=", "items", ",", "index", "=", "frame", ".", "index", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_reorder_for_extension_array_stack
Re-orders the values when stacking multiple extension-arrays. The indirect stacking method used for EAs requires a followup take to get the order correct. Parameters ---------- arr : ExtensionArray n_rows, n_columns : int The number of rows and columns in the original DataFrame. Returns ------- taken : ExtensionArray The original `arr` with elements re-ordered appropriately Examples -------- >>> arr = np.array(['a', 'b', 'c', 'd', 'e', 'f']) >>> _reorder_for_extension_array_stack(arr, 2, 3) array(['a', 'c', 'e', 'b', 'd', 'f'], dtype='<U1') >>> _reorder_for_extension_array_stack(arr, 3, 2) array(['a', 'd', 'b', 'e', 'c', 'f'], dtype='<U1')
pandas/core/reshape/reshape.py
def _reorder_for_extension_array_stack(arr, n_rows, n_columns): """ Re-orders the values when stacking multiple extension-arrays. The indirect stacking method used for EAs requires a followup take to get the order correct. Parameters ---------- arr : ExtensionArray n_rows, n_columns : int The number of rows and columns in the original DataFrame. Returns ------- taken : ExtensionArray The original `arr` with elements re-ordered appropriately Examples -------- >>> arr = np.array(['a', 'b', 'c', 'd', 'e', 'f']) >>> _reorder_for_extension_array_stack(arr, 2, 3) array(['a', 'c', 'e', 'b', 'd', 'f'], dtype='<U1') >>> _reorder_for_extension_array_stack(arr, 3, 2) array(['a', 'd', 'b', 'e', 'c', 'f'], dtype='<U1') """ # final take to get the order correct. # idx is an indexer like # [c0r0, c1r0, c2r0, ..., # c0r1, c1r1, c2r1, ...] idx = np.arange(n_rows * n_columns).reshape(n_columns, n_rows).T.ravel() return arr.take(idx)
def _reorder_for_extension_array_stack(arr, n_rows, n_columns): """ Re-orders the values when stacking multiple extension-arrays. The indirect stacking method used for EAs requires a followup take to get the order correct. Parameters ---------- arr : ExtensionArray n_rows, n_columns : int The number of rows and columns in the original DataFrame. Returns ------- taken : ExtensionArray The original `arr` with elements re-ordered appropriately Examples -------- >>> arr = np.array(['a', 'b', 'c', 'd', 'e', 'f']) >>> _reorder_for_extension_array_stack(arr, 2, 3) array(['a', 'c', 'e', 'b', 'd', 'f'], dtype='<U1') >>> _reorder_for_extension_array_stack(arr, 3, 2) array(['a', 'd', 'b', 'e', 'c', 'f'], dtype='<U1') """ # final take to get the order correct. # idx is an indexer like # [c0r0, c1r0, c2r0, ..., # c0r1, c1r1, c2r1, ...] idx = np.arange(n_rows * n_columns).reshape(n_columns, n_rows).T.ravel() return arr.take(idx)
[ "Re", "-", "orders", "the", "values", "when", "stacking", "multiple", "extension", "-", "arrays", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/reshape/reshape.py#L1006-L1038
[ "def", "_reorder_for_extension_array_stack", "(", "arr", ",", "n_rows", ",", "n_columns", ")", ":", "# final take to get the order correct.", "# idx is an indexer like", "# [c0r0, c1r0, c2r0, ...,", "# c0r1, c1r1, c2r1, ...]", "idx", "=", "np", ".", "arange", "(", "n_rows", "*", "n_columns", ")", ".", "reshape", "(", "n_columns", ",", "n_rows", ")", ".", "T", ".", "ravel", "(", ")", "return", "arr", ".", "take", "(", "idx", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_split_line
Parameters ---------- s: string Fixed-length string to split parts: list of (name, length) pairs Used to break up string, name '_' will be filtered from output. Returns ------- Dict of name:contents of string at given location.
pandas/io/sas/sas_xport.py
def _split_line(s, parts): """ Parameters ---------- s: string Fixed-length string to split parts: list of (name, length) pairs Used to break up string, name '_' will be filtered from output. Returns ------- Dict of name:contents of string at given location. """ out = {} start = 0 for name, length in parts: out[name] = s[start:start + length].strip() start += length del out['_'] return out
def _split_line(s, parts): """ Parameters ---------- s: string Fixed-length string to split parts: list of (name, length) pairs Used to break up string, name '_' will be filtered from output. Returns ------- Dict of name:contents of string at given location. """ out = {} start = 0 for name, length in parts: out[name] = s[start:start + length].strip() start += length del out['_'] return out
[ "Parameters", "----------", "s", ":", "string", "Fixed", "-", "length", "string", "to", "split", "parts", ":", "list", "of", "(", "name", "length", ")", "pairs", "Used", "to", "break", "up", "string", "name", "_", "will", "be", "filtered", "from", "output", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/io/sas/sas_xport.py#L129-L148
[ "def", "_split_line", "(", "s", ",", "parts", ")", ":", "out", "=", "{", "}", "start", "=", "0", "for", "name", ",", "length", "in", "parts", ":", "out", "[", "name", "]", "=", "s", "[", "start", ":", "start", "+", "length", "]", ".", "strip", "(", ")", "start", "+=", "length", "del", "out", "[", "'_'", "]", "return", "out" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_parse_float_vec
Parse a vector of float values representing IBM 8 byte floats into native 8 byte floats.
pandas/io/sas/sas_xport.py
def _parse_float_vec(vec): """ Parse a vector of float values representing IBM 8 byte floats into native 8 byte floats. """ dtype = np.dtype('>u4,>u4') vec1 = vec.view(dtype=dtype) xport1 = vec1['f0'] xport2 = vec1['f1'] # Start by setting first half of ieee number to first half of IBM # number sans exponent ieee1 = xport1 & 0x00ffffff # The fraction bit to the left of the binary point in the ieee # format was set and the number was shifted 0, 1, 2, or 3 # places. This will tell us how to adjust the ibm exponent to be a # power of 2 ieee exponent and how to shift the fraction bits to # restore the correct magnitude. shift = np.zeros(len(vec), dtype=np.uint8) shift[np.where(xport1 & 0x00200000)] = 1 shift[np.where(xport1 & 0x00400000)] = 2 shift[np.where(xport1 & 0x00800000)] = 3 # shift the ieee number down the correct number of places then # set the second half of the ieee number to be the second half # of the ibm number shifted appropriately, ored with the bits # from the first half that would have been shifted in if we # could shift a double. All we are worried about are the low # order 3 bits of the first half since we're only shifting by # 1, 2, or 3. ieee1 >>= shift ieee2 = (xport2 >> shift) | ((xport1 & 0x00000007) << (29 + (3 - shift))) # clear the 1 bit to the left of the binary point ieee1 &= 0xffefffff # set the exponent of the ieee number to be the actual exponent # plus the shift count + 1023. Or this into the first half of the # ieee number. The ibm exponent is excess 64 but is adjusted by 65 # since during conversion to ibm format the exponent is # incremented by 1 and the fraction bits left 4 positions to the # right of the radix point. (had to add >> 24 because C treats & # 0x7f as 0x7f000000 and Python doesn't) ieee1 |= ((((((xport1 >> 24) & 0x7f) - 65) << 2) + shift + 1023) << 20) | (xport1 & 0x80000000) ieee = np.empty((len(ieee1),), dtype='>u4,>u4') ieee['f0'] = ieee1 ieee['f1'] = ieee2 ieee = ieee.view(dtype='>f8') ieee = ieee.astype('f8') return ieee
def _parse_float_vec(vec): """ Parse a vector of float values representing IBM 8 byte floats into native 8 byte floats. """ dtype = np.dtype('>u4,>u4') vec1 = vec.view(dtype=dtype) xport1 = vec1['f0'] xport2 = vec1['f1'] # Start by setting first half of ieee number to first half of IBM # number sans exponent ieee1 = xport1 & 0x00ffffff # The fraction bit to the left of the binary point in the ieee # format was set and the number was shifted 0, 1, 2, or 3 # places. This will tell us how to adjust the ibm exponent to be a # power of 2 ieee exponent and how to shift the fraction bits to # restore the correct magnitude. shift = np.zeros(len(vec), dtype=np.uint8) shift[np.where(xport1 & 0x00200000)] = 1 shift[np.where(xport1 & 0x00400000)] = 2 shift[np.where(xport1 & 0x00800000)] = 3 # shift the ieee number down the correct number of places then # set the second half of the ieee number to be the second half # of the ibm number shifted appropriately, ored with the bits # from the first half that would have been shifted in if we # could shift a double. All we are worried about are the low # order 3 bits of the first half since we're only shifting by # 1, 2, or 3. ieee1 >>= shift ieee2 = (xport2 >> shift) | ((xport1 & 0x00000007) << (29 + (3 - shift))) # clear the 1 bit to the left of the binary point ieee1 &= 0xffefffff # set the exponent of the ieee number to be the actual exponent # plus the shift count + 1023. Or this into the first half of the # ieee number. The ibm exponent is excess 64 but is adjusted by 65 # since during conversion to ibm format the exponent is # incremented by 1 and the fraction bits left 4 positions to the # right of the radix point. (had to add >> 24 because C treats & # 0x7f as 0x7f000000 and Python doesn't) ieee1 |= ((((((xport1 >> 24) & 0x7f) - 65) << 2) + shift + 1023) << 20) | (xport1 & 0x80000000) ieee = np.empty((len(ieee1),), dtype='>u4,>u4') ieee['f0'] = ieee1 ieee['f1'] = ieee2 ieee = ieee.view(dtype='>f8') ieee = ieee.astype('f8') return ieee
[ "Parse", "a", "vector", "of", "float", "values", "representing", "IBM", "8", "byte", "floats", "into", "native", "8", "byte", "floats", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/io/sas/sas_xport.py#L170-L224
[ "def", "_parse_float_vec", "(", "vec", ")", ":", "dtype", "=", "np", ".", "dtype", "(", "'>u4,>u4'", ")", "vec1", "=", "vec", ".", "view", "(", "dtype", "=", "dtype", ")", "xport1", "=", "vec1", "[", "'f0'", "]", "xport2", "=", "vec1", "[", "'f1'", "]", "# Start by setting first half of ieee number to first half of IBM", "# number sans exponent", "ieee1", "=", "xport1", "&", "0x00ffffff", "# The fraction bit to the left of the binary point in the ieee", "# format was set and the number was shifted 0, 1, 2, or 3", "# places. This will tell us how to adjust the ibm exponent to be a", "# power of 2 ieee exponent and how to shift the fraction bits to", "# restore the correct magnitude.", "shift", "=", "np", ".", "zeros", "(", "len", "(", "vec", ")", ",", "dtype", "=", "np", ".", "uint8", ")", "shift", "[", "np", ".", "where", "(", "xport1", "&", "0x00200000", ")", "]", "=", "1", "shift", "[", "np", ".", "where", "(", "xport1", "&", "0x00400000", ")", "]", "=", "2", "shift", "[", "np", ".", "where", "(", "xport1", "&", "0x00800000", ")", "]", "=", "3", "# shift the ieee number down the correct number of places then", "# set the second half of the ieee number to be the second half", "# of the ibm number shifted appropriately, ored with the bits", "# from the first half that would have been shifted in if we", "# could shift a double. All we are worried about are the low", "# order 3 bits of the first half since we're only shifting by", "# 1, 2, or 3.", "ieee1", ">>=", "shift", "ieee2", "=", "(", "xport2", ">>", "shift", ")", "|", "(", "(", "xport1", "&", "0x00000007", ")", "<<", "(", "29", "+", "(", "3", "-", "shift", ")", ")", ")", "# clear the 1 bit to the left of the binary point", "ieee1", "&=", "0xffefffff", "# set the exponent of the ieee number to be the actual exponent", "# plus the shift count + 1023. Or this into the first half of the", "# ieee number. The ibm exponent is excess 64 but is adjusted by 65", "# since during conversion to ibm format the exponent is", "# incremented by 1 and the fraction bits left 4 positions to the", "# right of the radix point. (had to add >> 24 because C treats &", "# 0x7f as 0x7f000000 and Python doesn't)", "ieee1", "|=", "(", "(", "(", "(", "(", "(", "xport1", ">>", "24", ")", "&", "0x7f", ")", "-", "65", ")", "<<", "2", ")", "+", "shift", "+", "1023", ")", "<<", "20", ")", "|", "(", "xport1", "&", "0x80000000", ")", "ieee", "=", "np", ".", "empty", "(", "(", "len", "(", "ieee1", ")", ",", ")", ",", "dtype", "=", "'>u4,>u4'", ")", "ieee", "[", "'f0'", "]", "=", "ieee1", "ieee", "[", "'f1'", "]", "=", "ieee2", "ieee", "=", "ieee", ".", "view", "(", "dtype", "=", "'>f8'", ")", "ieee", "=", "ieee", ".", "astype", "(", "'f8'", ")", "return", "ieee" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
XportReader._record_count
Get number of records in file. This is maybe suboptimal because we have to seek to the end of the file. Side effect: returns file position to record_start.
pandas/io/sas/sas_xport.py
def _record_count(self): """ Get number of records in file. This is maybe suboptimal because we have to seek to the end of the file. Side effect: returns file position to record_start. """ self.filepath_or_buffer.seek(0, 2) total_records_length = (self.filepath_or_buffer.tell() - self.record_start) if total_records_length % 80 != 0: warnings.warn("xport file may be corrupted") if self.record_length > 80: self.filepath_or_buffer.seek(self.record_start) return total_records_length // self.record_length self.filepath_or_buffer.seek(-80, 2) last_card = self.filepath_or_buffer.read(80) last_card = np.frombuffer(last_card, dtype=np.uint64) # 8 byte blank ix = np.flatnonzero(last_card == 2314885530818453536) if len(ix) == 0: tail_pad = 0 else: tail_pad = 8 * len(ix) self.filepath_or_buffer.seek(self.record_start) return (total_records_length - tail_pad) // self.record_length
def _record_count(self): """ Get number of records in file. This is maybe suboptimal because we have to seek to the end of the file. Side effect: returns file position to record_start. """ self.filepath_or_buffer.seek(0, 2) total_records_length = (self.filepath_or_buffer.tell() - self.record_start) if total_records_length % 80 != 0: warnings.warn("xport file may be corrupted") if self.record_length > 80: self.filepath_or_buffer.seek(self.record_start) return total_records_length // self.record_length self.filepath_or_buffer.seek(-80, 2) last_card = self.filepath_or_buffer.read(80) last_card = np.frombuffer(last_card, dtype=np.uint64) # 8 byte blank ix = np.flatnonzero(last_card == 2314885530818453536) if len(ix) == 0: tail_pad = 0 else: tail_pad = 8 * len(ix) self.filepath_or_buffer.seek(self.record_start) return (total_records_length - tail_pad) // self.record_length
[ "Get", "number", "of", "records", "in", "file", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/io/sas/sas_xport.py#L364-L399
[ "def", "_record_count", "(", "self", ")", ":", "self", ".", "filepath_or_buffer", ".", "seek", "(", "0", ",", "2", ")", "total_records_length", "=", "(", "self", ".", "filepath_or_buffer", ".", "tell", "(", ")", "-", "self", ".", "record_start", ")", "if", "total_records_length", "%", "80", "!=", "0", ":", "warnings", ".", "warn", "(", "\"xport file may be corrupted\"", ")", "if", "self", ".", "record_length", ">", "80", ":", "self", ".", "filepath_or_buffer", ".", "seek", "(", "self", ".", "record_start", ")", "return", "total_records_length", "//", "self", ".", "record_length", "self", ".", "filepath_or_buffer", ".", "seek", "(", "-", "80", ",", "2", ")", "last_card", "=", "self", ".", "filepath_or_buffer", ".", "read", "(", "80", ")", "last_card", "=", "np", ".", "frombuffer", "(", "last_card", ",", "dtype", "=", "np", ".", "uint64", ")", "# 8 byte blank", "ix", "=", "np", ".", "flatnonzero", "(", "last_card", "==", "2314885530818453536", ")", "if", "len", "(", "ix", ")", "==", "0", ":", "tail_pad", "=", "0", "else", ":", "tail_pad", "=", "8", "*", "len", "(", "ix", ")", "self", ".", "filepath_or_buffer", ".", "seek", "(", "self", ".", "record_start", ")", "return", "(", "total_records_length", "-", "tail_pad", ")", "//", "self", ".", "record_length" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
XportReader.get_chunk
Reads lines from Xport file and returns as dataframe Parameters ---------- size : int, defaults to None Number of lines to read. If None, reads whole file. Returns ------- DataFrame
pandas/io/sas/sas_xport.py
def get_chunk(self, size=None): """ Reads lines from Xport file and returns as dataframe Parameters ---------- size : int, defaults to None Number of lines to read. If None, reads whole file. Returns ------- DataFrame """ if size is None: size = self._chunksize return self.read(nrows=size)
def get_chunk(self, size=None): """ Reads lines from Xport file and returns as dataframe Parameters ---------- size : int, defaults to None Number of lines to read. If None, reads whole file. Returns ------- DataFrame """ if size is None: size = self._chunksize return self.read(nrows=size)
[ "Reads", "lines", "from", "Xport", "file", "and", "returns", "as", "dataframe" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/io/sas/sas_xport.py#L401-L416
[ "def", "get_chunk", "(", "self", ",", "size", "=", "None", ")", ":", "if", "size", "is", "None", ":", "size", "=", "self", ".", "_chunksize", "return", "self", ".", "read", "(", "nrows", "=", "size", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
construction_error
raise a helpful message about our construction
pandas/core/internals/managers.py
def construction_error(tot_items, block_shape, axes, e=None): """ raise a helpful message about our construction """ passed = tuple(map(int, [tot_items] + list(block_shape))) # Correcting the user facing error message during dataframe construction if len(passed) <= 2: passed = passed[::-1] implied = tuple(len(ax) for ax in axes) # Correcting the user facing error message during dataframe construction if len(implied) <= 2: implied = implied[::-1] if passed == implied and e is not None: raise e if block_shape[0] == 0: raise ValueError("Empty data passed with indices specified.") raise ValueError("Shape of passed values is {0}, indices imply {1}".format( passed, implied))
def construction_error(tot_items, block_shape, axes, e=None): """ raise a helpful message about our construction """ passed = tuple(map(int, [tot_items] + list(block_shape))) # Correcting the user facing error message during dataframe construction if len(passed) <= 2: passed = passed[::-1] implied = tuple(len(ax) for ax in axes) # Correcting the user facing error message during dataframe construction if len(implied) <= 2: implied = implied[::-1] if passed == implied and e is not None: raise e if block_shape[0] == 0: raise ValueError("Empty data passed with indices specified.") raise ValueError("Shape of passed values is {0}, indices imply {1}".format( passed, implied))
[ "raise", "a", "helpful", "message", "about", "our", "construction" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1670-L1687
[ "def", "construction_error", "(", "tot_items", ",", "block_shape", ",", "axes", ",", "e", "=", "None", ")", ":", "passed", "=", "tuple", "(", "map", "(", "int", ",", "[", "tot_items", "]", "+", "list", "(", "block_shape", ")", ")", ")", "# Correcting the user facing error message during dataframe construction", "if", "len", "(", "passed", ")", "<=", "2", ":", "passed", "=", "passed", "[", ":", ":", "-", "1", "]", "implied", "=", "tuple", "(", "len", "(", "ax", ")", "for", "ax", "in", "axes", ")", "# Correcting the user facing error message during dataframe construction", "if", "len", "(", "implied", ")", "<=", "2", ":", "implied", "=", "implied", "[", ":", ":", "-", "1", "]", "if", "passed", "==", "implied", "and", "e", "is", "not", "None", ":", "raise", "e", "if", "block_shape", "[", "0", "]", "==", "0", ":", "raise", "ValueError", "(", "\"Empty data passed with indices specified.\"", ")", "raise", "ValueError", "(", "\"Shape of passed values is {0}, indices imply {1}\"", ".", "format", "(", "passed", ",", "implied", ")", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_simple_blockify
return a single array of a block that has a single dtype; if dtype is not None, coerce to this dtype
pandas/core/internals/managers.py
def _simple_blockify(tuples, dtype): """ return a single array of a block that has a single dtype; if dtype is not None, coerce to this dtype """ values, placement = _stack_arrays(tuples, dtype) # CHECK DTYPE? if dtype is not None and values.dtype != dtype: # pragma: no cover values = values.astype(dtype) block = make_block(values, placement=placement) return [block]
def _simple_blockify(tuples, dtype): """ return a single array of a block that has a single dtype; if dtype is not None, coerce to this dtype """ values, placement = _stack_arrays(tuples, dtype) # CHECK DTYPE? if dtype is not None and values.dtype != dtype: # pragma: no cover values = values.astype(dtype) block = make_block(values, placement=placement) return [block]
[ "return", "a", "single", "array", "of", "a", "block", "that", "has", "a", "single", "dtype", ";", "if", "dtype", "is", "not", "None", "coerce", "to", "this", "dtype" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1792-L1803
[ "def", "_simple_blockify", "(", "tuples", ",", "dtype", ")", ":", "values", ",", "placement", "=", "_stack_arrays", "(", "tuples", ",", "dtype", ")", "# CHECK DTYPE?", "if", "dtype", "is", "not", "None", "and", "values", ".", "dtype", "!=", "dtype", ":", "# pragma: no cover", "values", "=", "values", ".", "astype", "(", "dtype", ")", "block", "=", "make_block", "(", "values", ",", "placement", "=", "placement", ")", "return", "[", "block", "]" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_multi_blockify
return an array of blocks that potentially have different dtypes
pandas/core/internals/managers.py
def _multi_blockify(tuples, dtype=None): """ return an array of blocks that potentially have different dtypes """ # group by dtype grouper = itertools.groupby(tuples, lambda x: x[2].dtype) new_blocks = [] for dtype, tup_block in grouper: values, placement = _stack_arrays(list(tup_block), dtype) block = make_block(values, placement=placement) new_blocks.append(block) return new_blocks
def _multi_blockify(tuples, dtype=None): """ return an array of blocks that potentially have different dtypes """ # group by dtype grouper = itertools.groupby(tuples, lambda x: x[2].dtype) new_blocks = [] for dtype, tup_block in grouper: values, placement = _stack_arrays(list(tup_block), dtype) block = make_block(values, placement=placement) new_blocks.append(block) return new_blocks
[ "return", "an", "array", "of", "blocks", "that", "potentially", "have", "different", "dtypes" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1806-L1820
[ "def", "_multi_blockify", "(", "tuples", ",", "dtype", "=", "None", ")", ":", "# group by dtype", "grouper", "=", "itertools", ".", "groupby", "(", "tuples", ",", "lambda", "x", ":", "x", "[", "2", "]", ".", "dtype", ")", "new_blocks", "=", "[", "]", "for", "dtype", ",", "tup_block", "in", "grouper", ":", "values", ",", "placement", "=", "_stack_arrays", "(", "list", "(", "tup_block", ")", ",", "dtype", ")", "block", "=", "make_block", "(", "values", ",", "placement", "=", "placement", ")", "new_blocks", ".", "append", "(", "block", ")", "return", "new_blocks" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_sparse_blockify
return an array of blocks that potentially have different dtypes (and are sparse)
pandas/core/internals/managers.py
def _sparse_blockify(tuples, dtype=None): """ return an array of blocks that potentially have different dtypes (and are sparse) """ new_blocks = [] for i, names, array in tuples: array = _maybe_to_sparse(array) block = make_block(array, placement=[i]) new_blocks.append(block) return new_blocks
def _sparse_blockify(tuples, dtype=None): """ return an array of blocks that potentially have different dtypes (and are sparse) """ new_blocks = [] for i, names, array in tuples: array = _maybe_to_sparse(array) block = make_block(array, placement=[i]) new_blocks.append(block) return new_blocks
[ "return", "an", "array", "of", "blocks", "that", "potentially", "have", "different", "dtypes", "(", "and", "are", "sparse", ")" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1823-L1834
[ "def", "_sparse_blockify", "(", "tuples", ",", "dtype", "=", "None", ")", ":", "new_blocks", "=", "[", "]", "for", "i", ",", "names", ",", "array", "in", "tuples", ":", "array", "=", "_maybe_to_sparse", "(", "array", ")", "block", "=", "make_block", "(", "array", ",", "placement", "=", "[", "i", "]", ")", "new_blocks", ".", "append", "(", "block", ")", "return", "new_blocks" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_interleaved_dtype
Find the common dtype for `blocks`. Parameters ---------- blocks : List[Block] Returns ------- dtype : Optional[Union[np.dtype, ExtensionDtype]] None is returned when `blocks` is empty.
pandas/core/internals/managers.py
def _interleaved_dtype( blocks: List[Block] ) -> Optional[Union[np.dtype, ExtensionDtype]]: """Find the common dtype for `blocks`. Parameters ---------- blocks : List[Block] Returns ------- dtype : Optional[Union[np.dtype, ExtensionDtype]] None is returned when `blocks` is empty. """ if not len(blocks): return None return find_common_type([b.dtype for b in blocks])
def _interleaved_dtype( blocks: List[Block] ) -> Optional[Union[np.dtype, ExtensionDtype]]: """Find the common dtype for `blocks`. Parameters ---------- blocks : List[Block] Returns ------- dtype : Optional[Union[np.dtype, ExtensionDtype]] None is returned when `blocks` is empty. """ if not len(blocks): return None return find_common_type([b.dtype for b in blocks])
[ "Find", "the", "common", "dtype", "for", "blocks", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1864-L1881
[ "def", "_interleaved_dtype", "(", "blocks", ":", "List", "[", "Block", "]", ")", "->", "Optional", "[", "Union", "[", "np", ".", "dtype", ",", "ExtensionDtype", "]", "]", ":", "if", "not", "len", "(", "blocks", ")", ":", "return", "None", "return", "find_common_type", "(", "[", "b", ".", "dtype", "for", "b", "in", "blocks", "]", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_consolidate
Merge blocks having same dtype, exclude non-consolidating blocks
pandas/core/internals/managers.py
def _consolidate(blocks): """ Merge blocks having same dtype, exclude non-consolidating blocks """ # sort by _can_consolidate, dtype gkey = lambda x: x._consolidate_key grouper = itertools.groupby(sorted(blocks, key=gkey), gkey) new_blocks = [] for (_can_consolidate, dtype), group_blocks in grouper: merged_blocks = _merge_blocks(list(group_blocks), dtype=dtype, _can_consolidate=_can_consolidate) new_blocks = _extend_blocks(merged_blocks, new_blocks) return new_blocks
def _consolidate(blocks): """ Merge blocks having same dtype, exclude non-consolidating blocks """ # sort by _can_consolidate, dtype gkey = lambda x: x._consolidate_key grouper = itertools.groupby(sorted(blocks, key=gkey), gkey) new_blocks = [] for (_can_consolidate, dtype), group_blocks in grouper: merged_blocks = _merge_blocks(list(group_blocks), dtype=dtype, _can_consolidate=_can_consolidate) new_blocks = _extend_blocks(merged_blocks, new_blocks) return new_blocks
[ "Merge", "blocks", "having", "same", "dtype", "exclude", "non", "-", "consolidating", "blocks" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1884-L1898
[ "def", "_consolidate", "(", "blocks", ")", ":", "# sort by _can_consolidate, dtype", "gkey", "=", "lambda", "x", ":", "x", ".", "_consolidate_key", "grouper", "=", "itertools", ".", "groupby", "(", "sorted", "(", "blocks", ",", "key", "=", "gkey", ")", ",", "gkey", ")", "new_blocks", "=", "[", "]", "for", "(", "_can_consolidate", ",", "dtype", ")", ",", "group_blocks", "in", "grouper", ":", "merged_blocks", "=", "_merge_blocks", "(", "list", "(", "group_blocks", ")", ",", "dtype", "=", "dtype", ",", "_can_consolidate", "=", "_can_consolidate", ")", "new_blocks", "=", "_extend_blocks", "(", "merged_blocks", ",", "new_blocks", ")", "return", "new_blocks" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_compare_or_regex_search
Compare two array_like inputs of the same shape or two scalar values Calls operator.eq or re.search, depending on regex argument. If regex is True, perform an element-wise regex matching. Parameters ---------- a : array_like or scalar b : array_like or scalar regex : bool, default False Returns ------- mask : array_like of bool
pandas/core/internals/managers.py
def _compare_or_regex_search(a, b, regex=False): """ Compare two array_like inputs of the same shape or two scalar values Calls operator.eq or re.search, depending on regex argument. If regex is True, perform an element-wise regex matching. Parameters ---------- a : array_like or scalar b : array_like or scalar regex : bool, default False Returns ------- mask : array_like of bool """ if not regex: op = lambda x: operator.eq(x, b) else: op = np.vectorize(lambda x: bool(re.search(b, x)) if isinstance(x, str) else False) is_a_array = isinstance(a, np.ndarray) is_b_array = isinstance(b, np.ndarray) # numpy deprecation warning to have i8 vs integer comparisons if is_datetimelike_v_numeric(a, b): result = False # numpy deprecation warning if comparing numeric vs string-like elif is_numeric_v_string_like(a, b): result = False else: result = op(a) if is_scalar(result) and (is_a_array or is_b_array): type_names = [type(a).__name__, type(b).__name__] if is_a_array: type_names[0] = 'ndarray(dtype={dtype})'.format(dtype=a.dtype) if is_b_array: type_names[1] = 'ndarray(dtype={dtype})'.format(dtype=b.dtype) raise TypeError( "Cannot compare types {a!r} and {b!r}".format(a=type_names[0], b=type_names[1])) return result
def _compare_or_regex_search(a, b, regex=False): """ Compare two array_like inputs of the same shape or two scalar values Calls operator.eq or re.search, depending on regex argument. If regex is True, perform an element-wise regex matching. Parameters ---------- a : array_like or scalar b : array_like or scalar regex : bool, default False Returns ------- mask : array_like of bool """ if not regex: op = lambda x: operator.eq(x, b) else: op = np.vectorize(lambda x: bool(re.search(b, x)) if isinstance(x, str) else False) is_a_array = isinstance(a, np.ndarray) is_b_array = isinstance(b, np.ndarray) # numpy deprecation warning to have i8 vs integer comparisons if is_datetimelike_v_numeric(a, b): result = False # numpy deprecation warning if comparing numeric vs string-like elif is_numeric_v_string_like(a, b): result = False else: result = op(a) if is_scalar(result) and (is_a_array or is_b_array): type_names = [type(a).__name__, type(b).__name__] if is_a_array: type_names[0] = 'ndarray(dtype={dtype})'.format(dtype=a.dtype) if is_b_array: type_names[1] = 'ndarray(dtype={dtype})'.format(dtype=b.dtype) raise TypeError( "Cannot compare types {a!r} and {b!r}".format(a=type_names[0], b=type_names[1])) return result
[ "Compare", "two", "array_like", "inputs", "of", "the", "same", "shape", "or", "two", "scalar", "values" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1901-L1949
[ "def", "_compare_or_regex_search", "(", "a", ",", "b", ",", "regex", "=", "False", ")", ":", "if", "not", "regex", ":", "op", "=", "lambda", "x", ":", "operator", ".", "eq", "(", "x", ",", "b", ")", "else", ":", "op", "=", "np", ".", "vectorize", "(", "lambda", "x", ":", "bool", "(", "re", ".", "search", "(", "b", ",", "x", ")", ")", "if", "isinstance", "(", "x", ",", "str", ")", "else", "False", ")", "is_a_array", "=", "isinstance", "(", "a", ",", "np", ".", "ndarray", ")", "is_b_array", "=", "isinstance", "(", "b", ",", "np", ".", "ndarray", ")", "# numpy deprecation warning to have i8 vs integer comparisons", "if", "is_datetimelike_v_numeric", "(", "a", ",", "b", ")", ":", "result", "=", "False", "# numpy deprecation warning if comparing numeric vs string-like", "elif", "is_numeric_v_string_like", "(", "a", ",", "b", ")", ":", "result", "=", "False", "else", ":", "result", "=", "op", "(", "a", ")", "if", "is_scalar", "(", "result", ")", "and", "(", "is_a_array", "or", "is_b_array", ")", ":", "type_names", "=", "[", "type", "(", "a", ")", ".", "__name__", ",", "type", "(", "b", ")", ".", "__name__", "]", "if", "is_a_array", ":", "type_names", "[", "0", "]", "=", "'ndarray(dtype={dtype})'", ".", "format", "(", "dtype", "=", "a", ".", "dtype", ")", "if", "is_b_array", ":", "type_names", "[", "1", "]", "=", "'ndarray(dtype={dtype})'", ".", "format", "(", "dtype", "=", "b", ".", "dtype", ")", "raise", "TypeError", "(", "\"Cannot compare types {a!r} and {b!r}\"", ".", "format", "(", "a", "=", "type_names", "[", "0", "]", ",", "b", "=", "type_names", "[", "1", "]", ")", ")", "return", "result" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
items_overlap_with_suffix
If two indices overlap, add suffixes to overlapping entries. If corresponding suffix is empty, the entry is simply converted to string.
pandas/core/internals/managers.py
def items_overlap_with_suffix(left, lsuffix, right, rsuffix): """ If two indices overlap, add suffixes to overlapping entries. If corresponding suffix is empty, the entry is simply converted to string. """ to_rename = left.intersection(right) if len(to_rename) == 0: return left, right else: if not lsuffix and not rsuffix: raise ValueError('columns overlap but no suffix specified: ' '{rename}'.format(rename=to_rename)) def renamer(x, suffix): """Rename the left and right indices. If there is overlap, and suffix is not None, add suffix, otherwise, leave it as-is. Parameters ---------- x : original column name suffix : str or None Returns ------- x : renamed column name """ if x in to_rename and suffix is not None: return '{x}{suffix}'.format(x=x, suffix=suffix) return x lrenamer = partial(renamer, suffix=lsuffix) rrenamer = partial(renamer, suffix=rsuffix) return (_transform_index(left, lrenamer), _transform_index(right, rrenamer))
def items_overlap_with_suffix(left, lsuffix, right, rsuffix): """ If two indices overlap, add suffixes to overlapping entries. If corresponding suffix is empty, the entry is simply converted to string. """ to_rename = left.intersection(right) if len(to_rename) == 0: return left, right else: if not lsuffix and not rsuffix: raise ValueError('columns overlap but no suffix specified: ' '{rename}'.format(rename=to_rename)) def renamer(x, suffix): """Rename the left and right indices. If there is overlap, and suffix is not None, add suffix, otherwise, leave it as-is. Parameters ---------- x : original column name suffix : str or None Returns ------- x : renamed column name """ if x in to_rename and suffix is not None: return '{x}{suffix}'.format(x=x, suffix=suffix) return x lrenamer = partial(renamer, suffix=lsuffix) rrenamer = partial(renamer, suffix=rsuffix) return (_transform_index(left, lrenamer), _transform_index(right, rrenamer))
[ "If", "two", "indices", "overlap", "add", "suffixes", "to", "overlapping", "entries", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1956-L1994
[ "def", "items_overlap_with_suffix", "(", "left", ",", "lsuffix", ",", "right", ",", "rsuffix", ")", ":", "to_rename", "=", "left", ".", "intersection", "(", "right", ")", "if", "len", "(", "to_rename", ")", "==", "0", ":", "return", "left", ",", "right", "else", ":", "if", "not", "lsuffix", "and", "not", "rsuffix", ":", "raise", "ValueError", "(", "'columns overlap but no suffix specified: '", "'{rename}'", ".", "format", "(", "rename", "=", "to_rename", ")", ")", "def", "renamer", "(", "x", ",", "suffix", ")", ":", "\"\"\"Rename the left and right indices.\n\n If there is overlap, and suffix is not None, add\n suffix, otherwise, leave it as-is.\n\n Parameters\n ----------\n x : original column name\n suffix : str or None\n\n Returns\n -------\n x : renamed column name\n \"\"\"", "if", "x", "in", "to_rename", "and", "suffix", "is", "not", "None", ":", "return", "'{x}{suffix}'", ".", "format", "(", "x", "=", "x", ",", "suffix", "=", "suffix", ")", "return", "x", "lrenamer", "=", "partial", "(", "renamer", ",", "suffix", "=", "lsuffix", ")", "rrenamer", "=", "partial", "(", "renamer", ",", "suffix", "=", "rsuffix", ")", "return", "(", "_transform_index", "(", "left", ",", "lrenamer", ")", ",", "_transform_index", "(", "right", ",", "rrenamer", ")", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_transform_index
Apply function to all values found in index. This includes transforming multiindex entries separately. Only apply function to one level of the MultiIndex if level is specified.
pandas/core/internals/managers.py
def _transform_index(index, func, level=None): """ Apply function to all values found in index. This includes transforming multiindex entries separately. Only apply function to one level of the MultiIndex if level is specified. """ if isinstance(index, MultiIndex): if level is not None: items = [tuple(func(y) if i == level else y for i, y in enumerate(x)) for x in index] else: items = [tuple(func(y) for y in x) for x in index] return MultiIndex.from_tuples(items, names=index.names) else: items = [func(x) for x in index] return Index(items, name=index.name, tupleize_cols=False)
def _transform_index(index, func, level=None): """ Apply function to all values found in index. This includes transforming multiindex entries separately. Only apply function to one level of the MultiIndex if level is specified. """ if isinstance(index, MultiIndex): if level is not None: items = [tuple(func(y) if i == level else y for i, y in enumerate(x)) for x in index] else: items = [tuple(func(y) for y in x) for x in index] return MultiIndex.from_tuples(items, names=index.names) else: items = [func(x) for x in index] return Index(items, name=index.name, tupleize_cols=False)
[ "Apply", "function", "to", "all", "values", "found", "in", "index", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1997-L2014
[ "def", "_transform_index", "(", "index", ",", "func", ",", "level", "=", "None", ")", ":", "if", "isinstance", "(", "index", ",", "MultiIndex", ")", ":", "if", "level", "is", "not", "None", ":", "items", "=", "[", "tuple", "(", "func", "(", "y", ")", "if", "i", "==", "level", "else", "y", "for", "i", ",", "y", "in", "enumerate", "(", "x", ")", ")", "for", "x", "in", "index", "]", "else", ":", "items", "=", "[", "tuple", "(", "func", "(", "y", ")", "for", "y", "in", "x", ")", "for", "x", "in", "index", "]", "return", "MultiIndex", ".", "from_tuples", "(", "items", ",", "names", "=", "index", ".", "names", ")", "else", ":", "items", "=", "[", "func", "(", "x", ")", "for", "x", "in", "index", "]", "return", "Index", "(", "items", ",", "name", "=", "index", ".", "name", ",", "tupleize_cols", "=", "False", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_fast_count_smallints
Faster version of set(arr) for sequences of small numbers.
pandas/core/internals/managers.py
def _fast_count_smallints(arr): """Faster version of set(arr) for sequences of small numbers.""" counts = np.bincount(arr.astype(np.int_)) nz = counts.nonzero()[0] return np.c_[nz, counts[nz]]
def _fast_count_smallints(arr): """Faster version of set(arr) for sequences of small numbers.""" counts = np.bincount(arr.astype(np.int_)) nz = counts.nonzero()[0] return np.c_[nz, counts[nz]]
[ "Faster", "version", "of", "set", "(", "arr", ")", "for", "sequences", "of", "small", "numbers", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L2017-L2021
[ "def", "_fast_count_smallints", "(", "arr", ")", ":", "counts", "=", "np", ".", "bincount", "(", "arr", ".", "astype", "(", "np", ".", "int_", ")", ")", "nz", "=", "counts", ".", "nonzero", "(", ")", "[", "0", "]", "return", "np", ".", "c_", "[", "nz", ",", "counts", "[", "nz", "]", "]" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
concatenate_block_managers
Concatenate block managers into one. Parameters ---------- mgrs_indexers : list of (BlockManager, {axis: indexer,...}) tuples axes : list of Index concat_axis : int copy : bool
pandas/core/internals/managers.py
def concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy): """ Concatenate block managers into one. Parameters ---------- mgrs_indexers : list of (BlockManager, {axis: indexer,...}) tuples axes : list of Index concat_axis : int copy : bool """ concat_plans = [get_mgr_concatenation_plan(mgr, indexers) for mgr, indexers in mgrs_indexers] concat_plan = combine_concat_plans(concat_plans, concat_axis) blocks = [] for placement, join_units in concat_plan: if len(join_units) == 1 and not join_units[0].indexers: b = join_units[0].block values = b.values if copy: values = values.copy() elif not copy: values = values.view() b = b.make_block_same_class(values, placement=placement) elif is_uniform_join_units(join_units): b = join_units[0].block.concat_same_type( [ju.block for ju in join_units], placement=placement) else: b = make_block( concatenate_join_units(join_units, concat_axis, copy=copy), placement=placement) blocks.append(b) return BlockManager(blocks, axes)
def concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy): """ Concatenate block managers into one. Parameters ---------- mgrs_indexers : list of (BlockManager, {axis: indexer,...}) tuples axes : list of Index concat_axis : int copy : bool """ concat_plans = [get_mgr_concatenation_plan(mgr, indexers) for mgr, indexers in mgrs_indexers] concat_plan = combine_concat_plans(concat_plans, concat_axis) blocks = [] for placement, join_units in concat_plan: if len(join_units) == 1 and not join_units[0].indexers: b = join_units[0].block values = b.values if copy: values = values.copy() elif not copy: values = values.view() b = b.make_block_same_class(values, placement=placement) elif is_uniform_join_units(join_units): b = join_units[0].block.concat_same_type( [ju.block for ju in join_units], placement=placement) else: b = make_block( concatenate_join_units(join_units, concat_axis, copy=copy), placement=placement) blocks.append(b) return BlockManager(blocks, axes)
[ "Concatenate", "block", "managers", "into", "one", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L2038-L2074
[ "def", "concatenate_block_managers", "(", "mgrs_indexers", ",", "axes", ",", "concat_axis", ",", "copy", ")", ":", "concat_plans", "=", "[", "get_mgr_concatenation_plan", "(", "mgr", ",", "indexers", ")", "for", "mgr", ",", "indexers", "in", "mgrs_indexers", "]", "concat_plan", "=", "combine_concat_plans", "(", "concat_plans", ",", "concat_axis", ")", "blocks", "=", "[", "]", "for", "placement", ",", "join_units", "in", "concat_plan", ":", "if", "len", "(", "join_units", ")", "==", "1", "and", "not", "join_units", "[", "0", "]", ".", "indexers", ":", "b", "=", "join_units", "[", "0", "]", ".", "block", "values", "=", "b", ".", "values", "if", "copy", ":", "values", "=", "values", ".", "copy", "(", ")", "elif", "not", "copy", ":", "values", "=", "values", ".", "view", "(", ")", "b", "=", "b", ".", "make_block_same_class", "(", "values", ",", "placement", "=", "placement", ")", "elif", "is_uniform_join_units", "(", "join_units", ")", ":", "b", "=", "join_units", "[", "0", "]", ".", "block", ".", "concat_same_type", "(", "[", "ju", ".", "block", "for", "ju", "in", "join_units", "]", ",", "placement", "=", "placement", ")", "else", ":", "b", "=", "make_block", "(", "concatenate_join_units", "(", "join_units", ",", "concat_axis", ",", "copy", "=", "copy", ")", ",", "placement", "=", "placement", ")", "blocks", ".", "append", "(", "b", ")", "return", "BlockManager", "(", "blocks", ",", "axes", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.make_empty
return an empty BlockManager with the items axis of len 0
pandas/core/internals/managers.py
def make_empty(self, axes=None): """ return an empty BlockManager with the items axis of len 0 """ if axes is None: axes = [ensure_index([])] + [ensure_index(a) for a in self.axes[1:]] # preserve dtype if possible if self.ndim == 1: blocks = np.array([], dtype=self.array_dtype) else: blocks = [] return self.__class__(blocks, axes)
def make_empty(self, axes=None): """ return an empty BlockManager with the items axis of len 0 """ if axes is None: axes = [ensure_index([])] + [ensure_index(a) for a in self.axes[1:]] # preserve dtype if possible if self.ndim == 1: blocks = np.array([], dtype=self.array_dtype) else: blocks = [] return self.__class__(blocks, axes)
[ "return", "an", "empty", "BlockManager", "with", "the", "items", "axis", "of", "len", "0" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L120-L131
[ "def", "make_empty", "(", "self", ",", "axes", "=", "None", ")", ":", "if", "axes", "is", "None", ":", "axes", "=", "[", "ensure_index", "(", "[", "]", ")", "]", "+", "[", "ensure_index", "(", "a", ")", "for", "a", "in", "self", ".", "axes", "[", "1", ":", "]", "]", "# preserve dtype if possible", "if", "self", ".", "ndim", "==", "1", ":", "blocks", "=", "np", ".", "array", "(", "[", "]", ",", "dtype", "=", "self", ".", "array_dtype", ")", "else", ":", "blocks", "=", "[", "]", "return", "self", ".", "__class__", "(", "blocks", ",", "axes", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.rename_axis
Rename one of axes. Parameters ---------- mapper : unary callable axis : int copy : boolean, default True level : int, default None
pandas/core/internals/managers.py
def rename_axis(self, mapper, axis, copy=True, level=None): """ Rename one of axes. Parameters ---------- mapper : unary callable axis : int copy : boolean, default True level : int, default None """ obj = self.copy(deep=copy) obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level)) return obj
def rename_axis(self, mapper, axis, copy=True, level=None): """ Rename one of axes. Parameters ---------- mapper : unary callable axis : int copy : boolean, default True level : int, default None """ obj = self.copy(deep=copy) obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level)) return obj
[ "Rename", "one", "of", "axes", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L159-L172
[ "def", "rename_axis", "(", "self", ",", "mapper", ",", "axis", ",", "copy", "=", "True", ",", "level", "=", "None", ")", ":", "obj", "=", "self", ".", "copy", "(", "deep", "=", "copy", ")", "obj", ".", "set_axis", "(", "axis", ",", "_transform_index", "(", "self", ".", "axes", "[", "axis", "]", ",", "mapper", ",", "level", ")", ")", "return", "obj" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager._rebuild_blknos_and_blklocs
Update mgr._blknos / mgr._blklocs.
pandas/core/internals/managers.py
def _rebuild_blknos_and_blklocs(self): """ Update mgr._blknos / mgr._blklocs. """ new_blknos = np.empty(self.shape[0], dtype=np.int64) new_blklocs = np.empty(self.shape[0], dtype=np.int64) new_blknos.fill(-1) new_blklocs.fill(-1) for blkno, blk in enumerate(self.blocks): rl = blk.mgr_locs new_blknos[rl.indexer] = blkno new_blklocs[rl.indexer] = np.arange(len(rl)) if (new_blknos == -1).any(): raise AssertionError("Gaps in blk ref_locs") self._blknos = new_blknos self._blklocs = new_blklocs
def _rebuild_blknos_and_blklocs(self): """ Update mgr._blknos / mgr._blklocs. """ new_blknos = np.empty(self.shape[0], dtype=np.int64) new_blklocs = np.empty(self.shape[0], dtype=np.int64) new_blknos.fill(-1) new_blklocs.fill(-1) for blkno, blk in enumerate(self.blocks): rl = blk.mgr_locs new_blknos[rl.indexer] = blkno new_blklocs[rl.indexer] = np.arange(len(rl)) if (new_blknos == -1).any(): raise AssertionError("Gaps in blk ref_locs") self._blknos = new_blknos self._blklocs = new_blklocs
[ "Update", "mgr", ".", "_blknos", "/", "mgr", ".", "_blklocs", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L186-L204
[ "def", "_rebuild_blknos_and_blklocs", "(", "self", ")", ":", "new_blknos", "=", "np", ".", "empty", "(", "self", ".", "shape", "[", "0", "]", ",", "dtype", "=", "np", ".", "int64", ")", "new_blklocs", "=", "np", ".", "empty", "(", "self", ".", "shape", "[", "0", "]", ",", "dtype", "=", "np", ".", "int64", ")", "new_blknos", ".", "fill", "(", "-", "1", ")", "new_blklocs", ".", "fill", "(", "-", "1", ")", "for", "blkno", ",", "blk", "in", "enumerate", "(", "self", ".", "blocks", ")", ":", "rl", "=", "blk", ".", "mgr_locs", "new_blknos", "[", "rl", ".", "indexer", "]", "=", "blkno", "new_blklocs", "[", "rl", ".", "indexer", "]", "=", "np", ".", "arange", "(", "len", "(", "rl", ")", ")", "if", "(", "new_blknos", "==", "-", "1", ")", ".", "any", "(", ")", ":", "raise", "AssertionError", "(", "\"Gaps in blk ref_locs\"", ")", "self", ".", "_blknos", "=", "new_blknos", "self", ".", "_blklocs", "=", "new_blklocs" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager._get_counts
return a dict of the counts of the function in BlockManager
pandas/core/internals/managers.py
def _get_counts(self, f): """ return a dict of the counts of the function in BlockManager """ self._consolidate_inplace() counts = dict() for b in self.blocks: v = f(b) counts[v] = counts.get(v, 0) + b.shape[0] return counts
def _get_counts(self, f): """ return a dict of the counts of the function in BlockManager """ self._consolidate_inplace() counts = dict() for b in self.blocks: v = f(b) counts[v] = counts.get(v, 0) + b.shape[0] return counts
[ "return", "a", "dict", "of", "the", "counts", "of", "the", "function", "in", "BlockManager" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L210-L217
[ "def", "_get_counts", "(", "self", ",", "f", ")", ":", "self", ".", "_consolidate_inplace", "(", ")", "counts", "=", "dict", "(", ")", "for", "b", "in", "self", ".", "blocks", ":", "v", "=", "f", "(", "b", ")", "counts", "[", "v", "]", "=", "counts", ".", "get", "(", "v", ",", "0", ")", "+", "b", ".", "shape", "[", "0", "]", "return", "counts" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.apply
iterate over the blocks, collect and create a new block manager Parameters ---------- f : the callable or function name to operate on at the block level axes : optional (if not supplied, use self.axes) filter : list, if supplied, only call the block if the filter is in the block do_integrity_check : boolean, default False. Do the block manager integrity check consolidate: boolean, default True. Join together blocks having same dtype Returns ------- Block Manager (new object)
pandas/core/internals/managers.py
def apply(self, f, axes=None, filter=None, do_integrity_check=False, consolidate=True, **kwargs): """ iterate over the blocks, collect and create a new block manager Parameters ---------- f : the callable or function name to operate on at the block level axes : optional (if not supplied, use self.axes) filter : list, if supplied, only call the block if the filter is in the block do_integrity_check : boolean, default False. Do the block manager integrity check consolidate: boolean, default True. Join together blocks having same dtype Returns ------- Block Manager (new object) """ result_blocks = [] # filter kwarg is used in replace-* family of methods if filter is not None: filter_locs = set(self.items.get_indexer_for(filter)) if len(filter_locs) == len(self.items): # All items are included, as if there were no filtering filter = None else: kwargs['filter'] = filter_locs if consolidate: self._consolidate_inplace() if f == 'where': align_copy = True if kwargs.get('align', True): align_keys = ['other', 'cond'] else: align_keys = ['cond'] elif f == 'putmask': align_copy = False if kwargs.get('align', True): align_keys = ['new', 'mask'] else: align_keys = ['mask'] elif f == 'fillna': # fillna internally does putmask, maybe it's better to do this # at mgr, not block level? align_copy = False align_keys = ['value'] else: align_keys = [] # TODO(EA): may interfere with ExtensionBlock.setitem for blocks # with a .values attribute. aligned_args = {k: kwargs[k] for k in align_keys if hasattr(kwargs[k], 'values') and not isinstance(kwargs[k], ABCExtensionArray)} for b in self.blocks: if filter is not None: if not b.mgr_locs.isin(filter_locs).any(): result_blocks.append(b) continue if aligned_args: b_items = self.items[b.mgr_locs.indexer] for k, obj in aligned_args.items(): axis = getattr(obj, '_info_axis_number', 0) kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy) applied = getattr(b, f)(**kwargs) result_blocks = _extend_blocks(applied, result_blocks) if len(result_blocks) == 0: return self.make_empty(axes or self.axes) bm = self.__class__(result_blocks, axes or self.axes, do_integrity_check=do_integrity_check) bm._consolidate_inplace() return bm
def apply(self, f, axes=None, filter=None, do_integrity_check=False, consolidate=True, **kwargs): """ iterate over the blocks, collect and create a new block manager Parameters ---------- f : the callable or function name to operate on at the block level axes : optional (if not supplied, use self.axes) filter : list, if supplied, only call the block if the filter is in the block do_integrity_check : boolean, default False. Do the block manager integrity check consolidate: boolean, default True. Join together blocks having same dtype Returns ------- Block Manager (new object) """ result_blocks = [] # filter kwarg is used in replace-* family of methods if filter is not None: filter_locs = set(self.items.get_indexer_for(filter)) if len(filter_locs) == len(self.items): # All items are included, as if there were no filtering filter = None else: kwargs['filter'] = filter_locs if consolidate: self._consolidate_inplace() if f == 'where': align_copy = True if kwargs.get('align', True): align_keys = ['other', 'cond'] else: align_keys = ['cond'] elif f == 'putmask': align_copy = False if kwargs.get('align', True): align_keys = ['new', 'mask'] else: align_keys = ['mask'] elif f == 'fillna': # fillna internally does putmask, maybe it's better to do this # at mgr, not block level? align_copy = False align_keys = ['value'] else: align_keys = [] # TODO(EA): may interfere with ExtensionBlock.setitem for blocks # with a .values attribute. aligned_args = {k: kwargs[k] for k in align_keys if hasattr(kwargs[k], 'values') and not isinstance(kwargs[k], ABCExtensionArray)} for b in self.blocks: if filter is not None: if not b.mgr_locs.isin(filter_locs).any(): result_blocks.append(b) continue if aligned_args: b_items = self.items[b.mgr_locs.indexer] for k, obj in aligned_args.items(): axis = getattr(obj, '_info_axis_number', 0) kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy) applied = getattr(b, f)(**kwargs) result_blocks = _extend_blocks(applied, result_blocks) if len(result_blocks) == 0: return self.make_empty(axes or self.axes) bm = self.__class__(result_blocks, axes or self.axes, do_integrity_check=do_integrity_check) bm._consolidate_inplace() return bm
[ "iterate", "over", "the", "blocks", "collect", "and", "create", "a", "new", "block", "manager" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L318-L403
[ "def", "apply", "(", "self", ",", "f", ",", "axes", "=", "None", ",", "filter", "=", "None", ",", "do_integrity_check", "=", "False", ",", "consolidate", "=", "True", ",", "*", "*", "kwargs", ")", ":", "result_blocks", "=", "[", "]", "# filter kwarg is used in replace-* family of methods", "if", "filter", "is", "not", "None", ":", "filter_locs", "=", "set", "(", "self", ".", "items", ".", "get_indexer_for", "(", "filter", ")", ")", "if", "len", "(", "filter_locs", ")", "==", "len", "(", "self", ".", "items", ")", ":", "# All items are included, as if there were no filtering", "filter", "=", "None", "else", ":", "kwargs", "[", "'filter'", "]", "=", "filter_locs", "if", "consolidate", ":", "self", ".", "_consolidate_inplace", "(", ")", "if", "f", "==", "'where'", ":", "align_copy", "=", "True", "if", "kwargs", ".", "get", "(", "'align'", ",", "True", ")", ":", "align_keys", "=", "[", "'other'", ",", "'cond'", "]", "else", ":", "align_keys", "=", "[", "'cond'", "]", "elif", "f", "==", "'putmask'", ":", "align_copy", "=", "False", "if", "kwargs", ".", "get", "(", "'align'", ",", "True", ")", ":", "align_keys", "=", "[", "'new'", ",", "'mask'", "]", "else", ":", "align_keys", "=", "[", "'mask'", "]", "elif", "f", "==", "'fillna'", ":", "# fillna internally does putmask, maybe it's better to do this", "# at mgr, not block level?", "align_copy", "=", "False", "align_keys", "=", "[", "'value'", "]", "else", ":", "align_keys", "=", "[", "]", "# TODO(EA): may interfere with ExtensionBlock.setitem for blocks", "# with a .values attribute.", "aligned_args", "=", "{", "k", ":", "kwargs", "[", "k", "]", "for", "k", "in", "align_keys", "if", "hasattr", "(", "kwargs", "[", "k", "]", ",", "'values'", ")", "and", "not", "isinstance", "(", "kwargs", "[", "k", "]", ",", "ABCExtensionArray", ")", "}", "for", "b", "in", "self", ".", "blocks", ":", "if", "filter", "is", "not", "None", ":", "if", "not", "b", ".", "mgr_locs", ".", "isin", "(", "filter_locs", ")", ".", "any", "(", ")", ":", "result_blocks", ".", "append", "(", "b", ")", "continue", "if", "aligned_args", ":", "b_items", "=", "self", ".", "items", "[", "b", ".", "mgr_locs", ".", "indexer", "]", "for", "k", ",", "obj", "in", "aligned_args", ".", "items", "(", ")", ":", "axis", "=", "getattr", "(", "obj", ",", "'_info_axis_number'", ",", "0", ")", "kwargs", "[", "k", "]", "=", "obj", ".", "reindex", "(", "b_items", ",", "axis", "=", "axis", ",", "copy", "=", "align_copy", ")", "applied", "=", "getattr", "(", "b", ",", "f", ")", "(", "*", "*", "kwargs", ")", "result_blocks", "=", "_extend_blocks", "(", "applied", ",", "result_blocks", ")", "if", "len", "(", "result_blocks", ")", "==", "0", ":", "return", "self", ".", "make_empty", "(", "axes", "or", "self", ".", "axes", ")", "bm", "=", "self", ".", "__class__", "(", "result_blocks", ",", "axes", "or", "self", ".", "axes", ",", "do_integrity_check", "=", "do_integrity_check", ")", "bm", ".", "_consolidate_inplace", "(", ")", "return", "bm" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.quantile
Iterate over blocks applying quantile reduction. This routine is intended for reduction type operations and will do inference on the generated blocks. Parameters ---------- axis: reduction axis, default 0 consolidate: boolean, default True. Join together blocks having same dtype transposed: boolean, default False we are holding transposed data interpolation : type of interpolation, default 'linear' qs : a scalar or list of the quantiles to be computed numeric_only : ignored Returns ------- Block Manager (new object)
pandas/core/internals/managers.py
def quantile(self, axis=0, consolidate=True, transposed=False, interpolation='linear', qs=None, numeric_only=None): """ Iterate over blocks applying quantile reduction. This routine is intended for reduction type operations and will do inference on the generated blocks. Parameters ---------- axis: reduction axis, default 0 consolidate: boolean, default True. Join together blocks having same dtype transposed: boolean, default False we are holding transposed data interpolation : type of interpolation, default 'linear' qs : a scalar or list of the quantiles to be computed numeric_only : ignored Returns ------- Block Manager (new object) """ # Series dispatches to DataFrame for quantile, which allows us to # simplify some of the code here and in the blocks assert self.ndim >= 2 if consolidate: self._consolidate_inplace() def get_axe(block, qs, axes): from pandas import Float64Index if is_list_like(qs): ax = Float64Index(qs) elif block.ndim == 1: ax = Float64Index([qs]) else: ax = axes[0] return ax axes, blocks = [], [] for b in self.blocks: block = b.quantile(axis=axis, qs=qs, interpolation=interpolation) axe = get_axe(b, qs, axes=self.axes) axes.append(axe) blocks.append(block) # note that some DatetimeTZ, Categorical are always ndim==1 ndim = {b.ndim for b in blocks} assert 0 not in ndim, ndim if 2 in ndim: new_axes = list(self.axes) # multiple blocks that are reduced if len(blocks) > 1: new_axes[1] = axes[0] # reset the placement to the original for b, sb in zip(blocks, self.blocks): b.mgr_locs = sb.mgr_locs else: new_axes[axis] = Index(np.concatenate( [ax.values for ax in axes])) if transposed: new_axes = new_axes[::-1] blocks = [b.make_block(b.values.T, placement=np.arange(b.shape[1]) ) for b in blocks] return self.__class__(blocks, new_axes) # single block, i.e. ndim == {1} values = _concat._concat_compat([b.values for b in blocks]) # compute the orderings of our original data if len(self.blocks) > 1: indexer = np.empty(len(self.axes[0]), dtype=np.intp) i = 0 for b in self.blocks: for j in b.mgr_locs: indexer[j] = i i = i + 1 values = values.take(indexer) return SingleBlockManager( [make_block(values, ndim=1, placement=np.arange(len(values)))], axes[0])
def quantile(self, axis=0, consolidate=True, transposed=False, interpolation='linear', qs=None, numeric_only=None): """ Iterate over blocks applying quantile reduction. This routine is intended for reduction type operations and will do inference on the generated blocks. Parameters ---------- axis: reduction axis, default 0 consolidate: boolean, default True. Join together blocks having same dtype transposed: boolean, default False we are holding transposed data interpolation : type of interpolation, default 'linear' qs : a scalar or list of the quantiles to be computed numeric_only : ignored Returns ------- Block Manager (new object) """ # Series dispatches to DataFrame for quantile, which allows us to # simplify some of the code here and in the blocks assert self.ndim >= 2 if consolidate: self._consolidate_inplace() def get_axe(block, qs, axes): from pandas import Float64Index if is_list_like(qs): ax = Float64Index(qs) elif block.ndim == 1: ax = Float64Index([qs]) else: ax = axes[0] return ax axes, blocks = [], [] for b in self.blocks: block = b.quantile(axis=axis, qs=qs, interpolation=interpolation) axe = get_axe(b, qs, axes=self.axes) axes.append(axe) blocks.append(block) # note that some DatetimeTZ, Categorical are always ndim==1 ndim = {b.ndim for b in blocks} assert 0 not in ndim, ndim if 2 in ndim: new_axes = list(self.axes) # multiple blocks that are reduced if len(blocks) > 1: new_axes[1] = axes[0] # reset the placement to the original for b, sb in zip(blocks, self.blocks): b.mgr_locs = sb.mgr_locs else: new_axes[axis] = Index(np.concatenate( [ax.values for ax in axes])) if transposed: new_axes = new_axes[::-1] blocks = [b.make_block(b.values.T, placement=np.arange(b.shape[1]) ) for b in blocks] return self.__class__(blocks, new_axes) # single block, i.e. ndim == {1} values = _concat._concat_compat([b.values for b in blocks]) # compute the orderings of our original data if len(self.blocks) > 1: indexer = np.empty(len(self.axes[0]), dtype=np.intp) i = 0 for b in self.blocks: for j in b.mgr_locs: indexer[j] = i i = i + 1 values = values.take(indexer) return SingleBlockManager( [make_block(values, ndim=1, placement=np.arange(len(values)))], axes[0])
[ "Iterate", "over", "blocks", "applying", "quantile", "reduction", ".", "This", "routine", "is", "intended", "for", "reduction", "type", "operations", "and", "will", "do", "inference", "on", "the", "generated", "blocks", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L405-L501
[ "def", "quantile", "(", "self", ",", "axis", "=", "0", ",", "consolidate", "=", "True", ",", "transposed", "=", "False", ",", "interpolation", "=", "'linear'", ",", "qs", "=", "None", ",", "numeric_only", "=", "None", ")", ":", "# Series dispatches to DataFrame for quantile, which allows us to", "# simplify some of the code here and in the blocks", "assert", "self", ".", "ndim", ">=", "2", "if", "consolidate", ":", "self", ".", "_consolidate_inplace", "(", ")", "def", "get_axe", "(", "block", ",", "qs", ",", "axes", ")", ":", "from", "pandas", "import", "Float64Index", "if", "is_list_like", "(", "qs", ")", ":", "ax", "=", "Float64Index", "(", "qs", ")", "elif", "block", ".", "ndim", "==", "1", ":", "ax", "=", "Float64Index", "(", "[", "qs", "]", ")", "else", ":", "ax", "=", "axes", "[", "0", "]", "return", "ax", "axes", ",", "blocks", "=", "[", "]", ",", "[", "]", "for", "b", "in", "self", ".", "blocks", ":", "block", "=", "b", ".", "quantile", "(", "axis", "=", "axis", ",", "qs", "=", "qs", ",", "interpolation", "=", "interpolation", ")", "axe", "=", "get_axe", "(", "b", ",", "qs", ",", "axes", "=", "self", ".", "axes", ")", "axes", ".", "append", "(", "axe", ")", "blocks", ".", "append", "(", "block", ")", "# note that some DatetimeTZ, Categorical are always ndim==1", "ndim", "=", "{", "b", ".", "ndim", "for", "b", "in", "blocks", "}", "assert", "0", "not", "in", "ndim", ",", "ndim", "if", "2", "in", "ndim", ":", "new_axes", "=", "list", "(", "self", ".", "axes", ")", "# multiple blocks that are reduced", "if", "len", "(", "blocks", ")", ">", "1", ":", "new_axes", "[", "1", "]", "=", "axes", "[", "0", "]", "# reset the placement to the original", "for", "b", ",", "sb", "in", "zip", "(", "blocks", ",", "self", ".", "blocks", ")", ":", "b", ".", "mgr_locs", "=", "sb", ".", "mgr_locs", "else", ":", "new_axes", "[", "axis", "]", "=", "Index", "(", "np", ".", "concatenate", "(", "[", "ax", ".", "values", "for", "ax", "in", "axes", "]", ")", ")", "if", "transposed", ":", "new_axes", "=", "new_axes", "[", ":", ":", "-", "1", "]", "blocks", "=", "[", "b", ".", "make_block", "(", "b", ".", "values", ".", "T", ",", "placement", "=", "np", ".", "arange", "(", "b", ".", "shape", "[", "1", "]", ")", ")", "for", "b", "in", "blocks", "]", "return", "self", ".", "__class__", "(", "blocks", ",", "new_axes", ")", "# single block, i.e. ndim == {1}", "values", "=", "_concat", ".", "_concat_compat", "(", "[", "b", ".", "values", "for", "b", "in", "blocks", "]", ")", "# compute the orderings of our original data", "if", "len", "(", "self", ".", "blocks", ")", ">", "1", ":", "indexer", "=", "np", ".", "empty", "(", "len", "(", "self", ".", "axes", "[", "0", "]", ")", ",", "dtype", "=", "np", ".", "intp", ")", "i", "=", "0", "for", "b", "in", "self", ".", "blocks", ":", "for", "j", "in", "b", ".", "mgr_locs", ":", "indexer", "[", "j", "]", "=", "i", "i", "=", "i", "+", "1", "values", "=", "values", ".", "take", "(", "indexer", ")", "return", "SingleBlockManager", "(", "[", "make_block", "(", "values", ",", "ndim", "=", "1", ",", "placement", "=", "np", ".", "arange", "(", "len", "(", "values", ")", ")", ")", "]", ",", "axes", "[", "0", "]", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.replace_list
do a list replace
pandas/core/internals/managers.py
def replace_list(self, src_list, dest_list, inplace=False, regex=False): """ do a list replace """ inplace = validate_bool_kwarg(inplace, 'inplace') # figure out our mask a-priori to avoid repeated replacements values = self.as_array() def comp(s, regex=False): """ Generate a bool array by perform an equality check, or perform an element-wise regular expression matching """ if isna(s): return isna(values) if hasattr(s, 'asm8'): return _compare_or_regex_search(maybe_convert_objects(values), getattr(s, 'asm8'), regex) return _compare_or_regex_search(values, s, regex) masks = [comp(s, regex) for i, s in enumerate(src_list)] result_blocks = [] src_len = len(src_list) - 1 for blk in self.blocks: # its possible to get multiple result blocks here # replace ALWAYS will return a list rb = [blk if inplace else blk.copy()] for i, (s, d) in enumerate(zip(src_list, dest_list)): new_rb = [] for b in rb: m = masks[i][b.mgr_locs.indexer] convert = i == src_len result = b._replace_coerce(mask=m, to_replace=s, value=d, inplace=inplace, convert=convert, regex=regex) if m.any(): new_rb = _extend_blocks(result, new_rb) else: new_rb.append(b) rb = new_rb result_blocks.extend(rb) bm = self.__class__(result_blocks, self.axes) bm._consolidate_inplace() return bm
def replace_list(self, src_list, dest_list, inplace=False, regex=False): """ do a list replace """ inplace = validate_bool_kwarg(inplace, 'inplace') # figure out our mask a-priori to avoid repeated replacements values = self.as_array() def comp(s, regex=False): """ Generate a bool array by perform an equality check, or perform an element-wise regular expression matching """ if isna(s): return isna(values) if hasattr(s, 'asm8'): return _compare_or_regex_search(maybe_convert_objects(values), getattr(s, 'asm8'), regex) return _compare_or_regex_search(values, s, regex) masks = [comp(s, regex) for i, s in enumerate(src_list)] result_blocks = [] src_len = len(src_list) - 1 for blk in self.blocks: # its possible to get multiple result blocks here # replace ALWAYS will return a list rb = [blk if inplace else blk.copy()] for i, (s, d) in enumerate(zip(src_list, dest_list)): new_rb = [] for b in rb: m = masks[i][b.mgr_locs.indexer] convert = i == src_len result = b._replace_coerce(mask=m, to_replace=s, value=d, inplace=inplace, convert=convert, regex=regex) if m.any(): new_rb = _extend_blocks(result, new_rb) else: new_rb.append(b) rb = new_rb result_blocks.extend(rb) bm = self.__class__(result_blocks, self.axes) bm._consolidate_inplace() return bm
[ "do", "a", "list", "replace" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L539-L585
[ "def", "replace_list", "(", "self", ",", "src_list", ",", "dest_list", ",", "inplace", "=", "False", ",", "regex", "=", "False", ")", ":", "inplace", "=", "validate_bool_kwarg", "(", "inplace", ",", "'inplace'", ")", "# figure out our mask a-priori to avoid repeated replacements", "values", "=", "self", ".", "as_array", "(", ")", "def", "comp", "(", "s", ",", "regex", "=", "False", ")", ":", "\"\"\"\n Generate a bool array by perform an equality check, or perform\n an element-wise regular expression matching\n \"\"\"", "if", "isna", "(", "s", ")", ":", "return", "isna", "(", "values", ")", "if", "hasattr", "(", "s", ",", "'asm8'", ")", ":", "return", "_compare_or_regex_search", "(", "maybe_convert_objects", "(", "values", ")", ",", "getattr", "(", "s", ",", "'asm8'", ")", ",", "regex", ")", "return", "_compare_or_regex_search", "(", "values", ",", "s", ",", "regex", ")", "masks", "=", "[", "comp", "(", "s", ",", "regex", ")", "for", "i", ",", "s", "in", "enumerate", "(", "src_list", ")", "]", "result_blocks", "=", "[", "]", "src_len", "=", "len", "(", "src_list", ")", "-", "1", "for", "blk", "in", "self", ".", "blocks", ":", "# its possible to get multiple result blocks here", "# replace ALWAYS will return a list", "rb", "=", "[", "blk", "if", "inplace", "else", "blk", ".", "copy", "(", ")", "]", "for", "i", ",", "(", "s", ",", "d", ")", "in", "enumerate", "(", "zip", "(", "src_list", ",", "dest_list", ")", ")", ":", "new_rb", "=", "[", "]", "for", "b", "in", "rb", ":", "m", "=", "masks", "[", "i", "]", "[", "b", ".", "mgr_locs", ".", "indexer", "]", "convert", "=", "i", "==", "src_len", "result", "=", "b", ".", "_replace_coerce", "(", "mask", "=", "m", ",", "to_replace", "=", "s", ",", "value", "=", "d", ",", "inplace", "=", "inplace", ",", "convert", "=", "convert", ",", "regex", "=", "regex", ")", "if", "m", ".", "any", "(", ")", ":", "new_rb", "=", "_extend_blocks", "(", "result", ",", "new_rb", ")", "else", ":", "new_rb", ".", "append", "(", "b", ")", "rb", "=", "new_rb", "result_blocks", ".", "extend", "(", "rb", ")", "bm", "=", "self", ".", "__class__", "(", "result_blocks", ",", "self", ".", "axes", ")", "bm", ".", "_consolidate_inplace", "(", ")", "return", "bm" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.get_bool_data
Parameters ---------- copy : boolean, default False Whether to copy the blocks
pandas/core/internals/managers.py
def get_bool_data(self, copy=False): """ Parameters ---------- copy : boolean, default False Whether to copy the blocks """ self._consolidate_inplace() return self.combine([b for b in self.blocks if b.is_bool], copy)
def get_bool_data(self, copy=False): """ Parameters ---------- copy : boolean, default False Whether to copy the blocks """ self._consolidate_inplace() return self.combine([b for b in self.blocks if b.is_bool], copy)
[ "Parameters", "----------", "copy", ":", "boolean", "default", "False", "Whether", "to", "copy", "the", "blocks" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L638-L646
[ "def", "get_bool_data", "(", "self", ",", "copy", "=", "False", ")", ":", "self", ".", "_consolidate_inplace", "(", ")", "return", "self", ".", "combine", "(", "[", "b", "for", "b", "in", "self", ".", "blocks", "if", "b", ".", "is_bool", "]", ",", "copy", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.get_numeric_data
Parameters ---------- copy : boolean, default False Whether to copy the blocks
pandas/core/internals/managers.py
def get_numeric_data(self, copy=False): """ Parameters ---------- copy : boolean, default False Whether to copy the blocks """ self._consolidate_inplace() return self.combine([b for b in self.blocks if b.is_numeric], copy)
def get_numeric_data(self, copy=False): """ Parameters ---------- copy : boolean, default False Whether to copy the blocks """ self._consolidate_inplace() return self.combine([b for b in self.blocks if b.is_numeric], copy)
[ "Parameters", "----------", "copy", ":", "boolean", "default", "False", "Whether", "to", "copy", "the", "blocks" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L648-L656
[ "def", "get_numeric_data", "(", "self", ",", "copy", "=", "False", ")", ":", "self", ".", "_consolidate_inplace", "(", ")", "return", "self", ".", "combine", "(", "[", "b", "for", "b", "in", "self", ".", "blocks", "if", "b", ".", "is_numeric", "]", ",", "copy", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.combine
return a new manager with the blocks
pandas/core/internals/managers.py
def combine(self, blocks, copy=True): """ return a new manager with the blocks """ if len(blocks) == 0: return self.make_empty() # FIXME: optimization potential indexer = np.sort(np.concatenate([b.mgr_locs.as_array for b in blocks])) inv_indexer = lib.get_reverse_indexer(indexer, self.shape[0]) new_blocks = [] for b in blocks: b = b.copy(deep=copy) b.mgr_locs = algos.take_1d(inv_indexer, b.mgr_locs.as_array, axis=0, allow_fill=False) new_blocks.append(b) axes = list(self.axes) axes[0] = self.items.take(indexer) return self.__class__(new_blocks, axes, do_integrity_check=False)
def combine(self, blocks, copy=True): """ return a new manager with the blocks """ if len(blocks) == 0: return self.make_empty() # FIXME: optimization potential indexer = np.sort(np.concatenate([b.mgr_locs.as_array for b in blocks])) inv_indexer = lib.get_reverse_indexer(indexer, self.shape[0]) new_blocks = [] for b in blocks: b = b.copy(deep=copy) b.mgr_locs = algos.take_1d(inv_indexer, b.mgr_locs.as_array, axis=0, allow_fill=False) new_blocks.append(b) axes = list(self.axes) axes[0] = self.items.take(indexer) return self.__class__(new_blocks, axes, do_integrity_check=False)
[ "return", "a", "new", "manager", "with", "the", "blocks" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L658-L678
[ "def", "combine", "(", "self", ",", "blocks", ",", "copy", "=", "True", ")", ":", "if", "len", "(", "blocks", ")", "==", "0", ":", "return", "self", ".", "make_empty", "(", ")", "# FIXME: optimization potential", "indexer", "=", "np", ".", "sort", "(", "np", ".", "concatenate", "(", "[", "b", ".", "mgr_locs", ".", "as_array", "for", "b", "in", "blocks", "]", ")", ")", "inv_indexer", "=", "lib", ".", "get_reverse_indexer", "(", "indexer", ",", "self", ".", "shape", "[", "0", "]", ")", "new_blocks", "=", "[", "]", "for", "b", "in", "blocks", ":", "b", "=", "b", ".", "copy", "(", "deep", "=", "copy", ")", "b", ".", "mgr_locs", "=", "algos", ".", "take_1d", "(", "inv_indexer", ",", "b", ".", "mgr_locs", ".", "as_array", ",", "axis", "=", "0", ",", "allow_fill", "=", "False", ")", "new_blocks", ".", "append", "(", "b", ")", "axes", "=", "list", "(", "self", ".", "axes", ")", "axes", "[", "0", "]", "=", "self", ".", "items", ".", "take", "(", "indexer", ")", "return", "self", ".", "__class__", "(", "new_blocks", ",", "axes", ",", "do_integrity_check", "=", "False", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.copy
Make deep or shallow copy of BlockManager Parameters ---------- deep : boolean o rstring, default True If False, return shallow copy (do not copy data) If 'all', copy data and a deep copy of the index Returns ------- copy : BlockManager
pandas/core/internals/managers.py
def copy(self, deep=True): """ Make deep or shallow copy of BlockManager Parameters ---------- deep : boolean o rstring, default True If False, return shallow copy (do not copy data) If 'all', copy data and a deep copy of the index Returns ------- copy : BlockManager """ # this preserves the notion of view copying of axes if deep: if deep == 'all': copy = lambda ax: ax.copy(deep=True) else: copy = lambda ax: ax.view() new_axes = [copy(ax) for ax in self.axes] else: new_axes = list(self.axes) return self.apply('copy', axes=new_axes, deep=deep, do_integrity_check=False)
def copy(self, deep=True): """ Make deep or shallow copy of BlockManager Parameters ---------- deep : boolean o rstring, default True If False, return shallow copy (do not copy data) If 'all', copy data and a deep copy of the index Returns ------- copy : BlockManager """ # this preserves the notion of view copying of axes if deep: if deep == 'all': copy = lambda ax: ax.copy(deep=True) else: copy = lambda ax: ax.view() new_axes = [copy(ax) for ax in self.axes] else: new_axes = list(self.axes) return self.apply('copy', axes=new_axes, deep=deep, do_integrity_check=False)
[ "Make", "deep", "or", "shallow", "copy", "of", "BlockManager" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L706-L730
[ "def", "copy", "(", "self", ",", "deep", "=", "True", ")", ":", "# this preserves the notion of view copying of axes", "if", "deep", ":", "if", "deep", "==", "'all'", ":", "copy", "=", "lambda", "ax", ":", "ax", ".", "copy", "(", "deep", "=", "True", ")", "else", ":", "copy", "=", "lambda", "ax", ":", "ax", ".", "view", "(", ")", "new_axes", "=", "[", "copy", "(", "ax", ")", "for", "ax", "in", "self", ".", "axes", "]", "else", ":", "new_axes", "=", "list", "(", "self", ".", "axes", ")", "return", "self", ".", "apply", "(", "'copy'", ",", "axes", "=", "new_axes", ",", "deep", "=", "deep", ",", "do_integrity_check", "=", "False", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.as_array
Convert the blockmanager data into an numpy array. Parameters ---------- transpose : boolean, default False If True, transpose the return array items : list of strings or None Names of block items that will be included in the returned array. ``None`` means that all block items will be used Returns ------- arr : ndarray
pandas/core/internals/managers.py
def as_array(self, transpose=False, items=None): """Convert the blockmanager data into an numpy array. Parameters ---------- transpose : boolean, default False If True, transpose the return array items : list of strings or None Names of block items that will be included in the returned array. ``None`` means that all block items will be used Returns ------- arr : ndarray """ if len(self.blocks) == 0: arr = np.empty(self.shape, dtype=float) return arr.transpose() if transpose else arr if items is not None: mgr = self.reindex_axis(items, axis=0) else: mgr = self if self._is_single_block and mgr.blocks[0].is_datetimetz: # TODO(Block.get_values): Make DatetimeTZBlock.get_values # always be object dtype. Some callers seem to want the # DatetimeArray (previously DTI) arr = mgr.blocks[0].get_values(dtype=object) elif self._is_single_block or not self.is_mixed_type: arr = np.asarray(mgr.blocks[0].get_values()) else: arr = mgr._interleave() return arr.transpose() if transpose else arr
def as_array(self, transpose=False, items=None): """Convert the blockmanager data into an numpy array. Parameters ---------- transpose : boolean, default False If True, transpose the return array items : list of strings or None Names of block items that will be included in the returned array. ``None`` means that all block items will be used Returns ------- arr : ndarray """ if len(self.blocks) == 0: arr = np.empty(self.shape, dtype=float) return arr.transpose() if transpose else arr if items is not None: mgr = self.reindex_axis(items, axis=0) else: mgr = self if self._is_single_block and mgr.blocks[0].is_datetimetz: # TODO(Block.get_values): Make DatetimeTZBlock.get_values # always be object dtype. Some callers seem to want the # DatetimeArray (previously DTI) arr = mgr.blocks[0].get_values(dtype=object) elif self._is_single_block or not self.is_mixed_type: arr = np.asarray(mgr.blocks[0].get_values()) else: arr = mgr._interleave() return arr.transpose() if transpose else arr
[ "Convert", "the", "blockmanager", "data", "into", "an", "numpy", "array", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L732-L766
[ "def", "as_array", "(", "self", ",", "transpose", "=", "False", ",", "items", "=", "None", ")", ":", "if", "len", "(", "self", ".", "blocks", ")", "==", "0", ":", "arr", "=", "np", ".", "empty", "(", "self", ".", "shape", ",", "dtype", "=", "float", ")", "return", "arr", ".", "transpose", "(", ")", "if", "transpose", "else", "arr", "if", "items", "is", "not", "None", ":", "mgr", "=", "self", ".", "reindex_axis", "(", "items", ",", "axis", "=", "0", ")", "else", ":", "mgr", "=", "self", "if", "self", ".", "_is_single_block", "and", "mgr", ".", "blocks", "[", "0", "]", ".", "is_datetimetz", ":", "# TODO(Block.get_values): Make DatetimeTZBlock.get_values", "# always be object dtype. Some callers seem to want the", "# DatetimeArray (previously DTI)", "arr", "=", "mgr", ".", "blocks", "[", "0", "]", ".", "get_values", "(", "dtype", "=", "object", ")", "elif", "self", ".", "_is_single_block", "or", "not", "self", ".", "is_mixed_type", ":", "arr", "=", "np", ".", "asarray", "(", "mgr", ".", "blocks", "[", "0", "]", ".", "get_values", "(", ")", ")", "else", ":", "arr", "=", "mgr", ".", "_interleave", "(", ")", "return", "arr", ".", "transpose", "(", ")", "if", "transpose", "else", "arr" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager._interleave
Return ndarray from blocks with specified item order Items must be contained in the blocks
pandas/core/internals/managers.py
def _interleave(self): """ Return ndarray from blocks with specified item order Items must be contained in the blocks """ from pandas.core.dtypes.common import is_sparse dtype = _interleaved_dtype(self.blocks) # TODO: https://github.com/pandas-dev/pandas/issues/22791 # Give EAs some input on what happens here. Sparse needs this. if is_sparse(dtype): dtype = dtype.subtype elif is_extension_array_dtype(dtype): dtype = 'object' result = np.empty(self.shape, dtype=dtype) itemmask = np.zeros(self.shape[0]) for blk in self.blocks: rl = blk.mgr_locs result[rl.indexer] = blk.get_values(dtype) itemmask[rl.indexer] = 1 if not itemmask.all(): raise AssertionError('Some items were not contained in blocks') return result
def _interleave(self): """ Return ndarray from blocks with specified item order Items must be contained in the blocks """ from pandas.core.dtypes.common import is_sparse dtype = _interleaved_dtype(self.blocks) # TODO: https://github.com/pandas-dev/pandas/issues/22791 # Give EAs some input on what happens here. Sparse needs this. if is_sparse(dtype): dtype = dtype.subtype elif is_extension_array_dtype(dtype): dtype = 'object' result = np.empty(self.shape, dtype=dtype) itemmask = np.zeros(self.shape[0]) for blk in self.blocks: rl = blk.mgr_locs result[rl.indexer] = blk.get_values(dtype) itemmask[rl.indexer] = 1 if not itemmask.all(): raise AssertionError('Some items were not contained in blocks') return result
[ "Return", "ndarray", "from", "blocks", "with", "specified", "item", "order", "Items", "must", "be", "contained", "in", "the", "blocks" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L768-L795
[ "def", "_interleave", "(", "self", ")", ":", "from", "pandas", ".", "core", ".", "dtypes", ".", "common", "import", "is_sparse", "dtype", "=", "_interleaved_dtype", "(", "self", ".", "blocks", ")", "# TODO: https://github.com/pandas-dev/pandas/issues/22791", "# Give EAs some input on what happens here. Sparse needs this.", "if", "is_sparse", "(", "dtype", ")", ":", "dtype", "=", "dtype", ".", "subtype", "elif", "is_extension_array_dtype", "(", "dtype", ")", ":", "dtype", "=", "'object'", "result", "=", "np", ".", "empty", "(", "self", ".", "shape", ",", "dtype", "=", "dtype", ")", "itemmask", "=", "np", ".", "zeros", "(", "self", ".", "shape", "[", "0", "]", ")", "for", "blk", "in", "self", ".", "blocks", ":", "rl", "=", "blk", ".", "mgr_locs", "result", "[", "rl", ".", "indexer", "]", "=", "blk", ".", "get_values", "(", "dtype", ")", "itemmask", "[", "rl", ".", "indexer", "]", "=", "1", "if", "not", "itemmask", ".", "all", "(", ")", ":", "raise", "AssertionError", "(", "'Some items were not contained in blocks'", ")", "return", "result" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.to_dict
Return a dict of str(dtype) -> BlockManager Parameters ---------- copy : boolean, default True Returns ------- values : a dict of dtype -> BlockManager Notes ----- This consolidates based on str(dtype)
pandas/core/internals/managers.py
def to_dict(self, copy=True): """ Return a dict of str(dtype) -> BlockManager Parameters ---------- copy : boolean, default True Returns ------- values : a dict of dtype -> BlockManager Notes ----- This consolidates based on str(dtype) """ self._consolidate_inplace() bd = {} for b in self.blocks: bd.setdefault(str(b.dtype), []).append(b) return {dtype: self.combine(blocks, copy=copy) for dtype, blocks in bd.items()}
def to_dict(self, copy=True): """ Return a dict of str(dtype) -> BlockManager Parameters ---------- copy : boolean, default True Returns ------- values : a dict of dtype -> BlockManager Notes ----- This consolidates based on str(dtype) """ self._consolidate_inplace() bd = {} for b in self.blocks: bd.setdefault(str(b.dtype), []).append(b) return {dtype: self.combine(blocks, copy=copy) for dtype, blocks in bd.items()}
[ "Return", "a", "dict", "of", "str", "(", "dtype", ")", "-", ">", "BlockManager" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L797-L820
[ "def", "to_dict", "(", "self", ",", "copy", "=", "True", ")", ":", "self", ".", "_consolidate_inplace", "(", ")", "bd", "=", "{", "}", "for", "b", "in", "self", ".", "blocks", ":", "bd", ".", "setdefault", "(", "str", "(", "b", ".", "dtype", ")", ",", "[", "]", ")", ".", "append", "(", "b", ")", "return", "{", "dtype", ":", "self", ".", "combine", "(", "blocks", ",", "copy", "=", "copy", ")", "for", "dtype", ",", "blocks", "in", "bd", ".", "items", "(", ")", "}" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.fast_xs
get a cross sectional for a given location in the items ; handle dups return the result, is *could* be a view in the case of a single block
pandas/core/internals/managers.py
def fast_xs(self, loc): """ get a cross sectional for a given location in the items ; handle dups return the result, is *could* be a view in the case of a single block """ if len(self.blocks) == 1: return self.blocks[0].iget((slice(None), loc)) items = self.items # non-unique (GH4726) if not items.is_unique: result = self._interleave() if self.ndim == 2: result = result.T return result[loc] # unique dtype = _interleaved_dtype(self.blocks) n = len(items) if is_extension_array_dtype(dtype): # we'll eventually construct an ExtensionArray. result = np.empty(n, dtype=object) else: result = np.empty(n, dtype=dtype) for blk in self.blocks: # Such assignment may incorrectly coerce NaT to None # result[blk.mgr_locs] = blk._slice((slice(None), loc)) for i, rl in enumerate(blk.mgr_locs): result[rl] = blk._try_coerce_result(blk.iget((i, loc))) if is_extension_array_dtype(dtype): result = dtype.construct_array_type()._from_sequence( result, dtype=dtype ) return result
def fast_xs(self, loc): """ get a cross sectional for a given location in the items ; handle dups return the result, is *could* be a view in the case of a single block """ if len(self.blocks) == 1: return self.blocks[0].iget((slice(None), loc)) items = self.items # non-unique (GH4726) if not items.is_unique: result = self._interleave() if self.ndim == 2: result = result.T return result[loc] # unique dtype = _interleaved_dtype(self.blocks) n = len(items) if is_extension_array_dtype(dtype): # we'll eventually construct an ExtensionArray. result = np.empty(n, dtype=object) else: result = np.empty(n, dtype=dtype) for blk in self.blocks: # Such assignment may incorrectly coerce NaT to None # result[blk.mgr_locs] = blk._slice((slice(None), loc)) for i, rl in enumerate(blk.mgr_locs): result[rl] = blk._try_coerce_result(blk.iget((i, loc))) if is_extension_array_dtype(dtype): result = dtype.construct_array_type()._from_sequence( result, dtype=dtype ) return result
[ "get", "a", "cross", "sectional", "for", "a", "given", "location", "in", "the", "items", ";", "handle", "dups" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L864-L905
[ "def", "fast_xs", "(", "self", ",", "loc", ")", ":", "if", "len", "(", "self", ".", "blocks", ")", "==", "1", ":", "return", "self", ".", "blocks", "[", "0", "]", ".", "iget", "(", "(", "slice", "(", "None", ")", ",", "loc", ")", ")", "items", "=", "self", ".", "items", "# non-unique (GH4726)", "if", "not", "items", ".", "is_unique", ":", "result", "=", "self", ".", "_interleave", "(", ")", "if", "self", ".", "ndim", "==", "2", ":", "result", "=", "result", ".", "T", "return", "result", "[", "loc", "]", "# unique", "dtype", "=", "_interleaved_dtype", "(", "self", ".", "blocks", ")", "n", "=", "len", "(", "items", ")", "if", "is_extension_array_dtype", "(", "dtype", ")", ":", "# we'll eventually construct an ExtensionArray.", "result", "=", "np", ".", "empty", "(", "n", ",", "dtype", "=", "object", ")", "else", ":", "result", "=", "np", ".", "empty", "(", "n", ",", "dtype", "=", "dtype", ")", "for", "blk", "in", "self", ".", "blocks", ":", "# Such assignment may incorrectly coerce NaT to None", "# result[blk.mgr_locs] = blk._slice((slice(None), loc))", "for", "i", ",", "rl", "in", "enumerate", "(", "blk", ".", "mgr_locs", ")", ":", "result", "[", "rl", "]", "=", "blk", ".", "_try_coerce_result", "(", "blk", ".", "iget", "(", "(", "i", ",", "loc", ")", ")", ")", "if", "is_extension_array_dtype", "(", "dtype", ")", ":", "result", "=", "dtype", ".", "construct_array_type", "(", ")", ".", "_from_sequence", "(", "result", ",", "dtype", "=", "dtype", ")", "return", "result" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.consolidate
Join together blocks having same dtype Returns ------- y : BlockManager
pandas/core/internals/managers.py
def consolidate(self): """ Join together blocks having same dtype Returns ------- y : BlockManager """ if self.is_consolidated(): return self bm = self.__class__(self.blocks, self.axes) bm._is_consolidated = False bm._consolidate_inplace() return bm
def consolidate(self): """ Join together blocks having same dtype Returns ------- y : BlockManager """ if self.is_consolidated(): return self bm = self.__class__(self.blocks, self.axes) bm._is_consolidated = False bm._consolidate_inplace() return bm
[ "Join", "together", "blocks", "having", "same", "dtype" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L907-L921
[ "def", "consolidate", "(", "self", ")", ":", "if", "self", ".", "is_consolidated", "(", ")", ":", "return", "self", "bm", "=", "self", ".", "__class__", "(", "self", ".", "blocks", ",", "self", ".", "axes", ")", "bm", ".", "_is_consolidated", "=", "False", "bm", ".", "_consolidate_inplace", "(", ")", "return", "bm" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.get
Return values for selected item (ndarray or BlockManager).
pandas/core/internals/managers.py
def get(self, item, fastpath=True): """ Return values for selected item (ndarray or BlockManager). """ if self.items.is_unique: if not isna(item): loc = self.items.get_loc(item) else: indexer = np.arange(len(self.items))[isna(self.items)] # allow a single nan location indexer if not is_scalar(indexer): if len(indexer) == 1: loc = indexer.item() else: raise ValueError("cannot label index with a null key") return self.iget(loc, fastpath=fastpath) else: if isna(item): raise TypeError("cannot label index with a null key") indexer = self.items.get_indexer_for([item]) return self.reindex_indexer(new_axis=self.items[indexer], indexer=indexer, axis=0, allow_dups=True)
def get(self, item, fastpath=True): """ Return values for selected item (ndarray or BlockManager). """ if self.items.is_unique: if not isna(item): loc = self.items.get_loc(item) else: indexer = np.arange(len(self.items))[isna(self.items)] # allow a single nan location indexer if not is_scalar(indexer): if len(indexer) == 1: loc = indexer.item() else: raise ValueError("cannot label index with a null key") return self.iget(loc, fastpath=fastpath) else: if isna(item): raise TypeError("cannot label index with a null key") indexer = self.items.get_indexer_for([item]) return self.reindex_indexer(new_axis=self.items[indexer], indexer=indexer, axis=0, allow_dups=True)
[ "Return", "values", "for", "selected", "item", "(", "ndarray", "or", "BlockManager", ")", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L930-L957
[ "def", "get", "(", "self", ",", "item", ",", "fastpath", "=", "True", ")", ":", "if", "self", ".", "items", ".", "is_unique", ":", "if", "not", "isna", "(", "item", ")", ":", "loc", "=", "self", ".", "items", ".", "get_loc", "(", "item", ")", "else", ":", "indexer", "=", "np", ".", "arange", "(", "len", "(", "self", ".", "items", ")", ")", "[", "isna", "(", "self", ".", "items", ")", "]", "# allow a single nan location indexer", "if", "not", "is_scalar", "(", "indexer", ")", ":", "if", "len", "(", "indexer", ")", "==", "1", ":", "loc", "=", "indexer", ".", "item", "(", ")", "else", ":", "raise", "ValueError", "(", "\"cannot label index with a null key\"", ")", "return", "self", ".", "iget", "(", "loc", ",", "fastpath", "=", "fastpath", ")", "else", ":", "if", "isna", "(", "item", ")", ":", "raise", "TypeError", "(", "\"cannot label index with a null key\"", ")", "indexer", "=", "self", ".", "items", ".", "get_indexer_for", "(", "[", "item", "]", ")", "return", "self", ".", "reindex_indexer", "(", "new_axis", "=", "self", ".", "items", "[", "indexer", "]", ",", "indexer", "=", "indexer", ",", "axis", "=", "0", ",", "allow_dups", "=", "True", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.iget
Return the data as a SingleBlockManager if fastpath=True and possible Otherwise return as a ndarray
pandas/core/internals/managers.py
def iget(self, i, fastpath=True): """ Return the data as a SingleBlockManager if fastpath=True and possible Otherwise return as a ndarray """ block = self.blocks[self._blknos[i]] values = block.iget(self._blklocs[i]) if not fastpath or not block._box_to_block_values or values.ndim != 1: return values # fastpath shortcut for select a single-dim from a 2-dim BM return SingleBlockManager( [block.make_block_same_class(values, placement=slice(0, len(values)), ndim=1)], self.axes[1])
def iget(self, i, fastpath=True): """ Return the data as a SingleBlockManager if fastpath=True and possible Otherwise return as a ndarray """ block = self.blocks[self._blknos[i]] values = block.iget(self._blklocs[i]) if not fastpath or not block._box_to_block_values or values.ndim != 1: return values # fastpath shortcut for select a single-dim from a 2-dim BM return SingleBlockManager( [block.make_block_same_class(values, placement=slice(0, len(values)), ndim=1)], self.axes[1])
[ "Return", "the", "data", "as", "a", "SingleBlockManager", "if", "fastpath", "=", "True", "and", "possible" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L959-L975
[ "def", "iget", "(", "self", ",", "i", ",", "fastpath", "=", "True", ")", ":", "block", "=", "self", ".", "blocks", "[", "self", ".", "_blknos", "[", "i", "]", "]", "values", "=", "block", ".", "iget", "(", "self", ".", "_blklocs", "[", "i", "]", ")", "if", "not", "fastpath", "or", "not", "block", ".", "_box_to_block_values", "or", "values", ".", "ndim", "!=", "1", ":", "return", "values", "# fastpath shortcut for select a single-dim from a 2-dim BM", "return", "SingleBlockManager", "(", "[", "block", ".", "make_block_same_class", "(", "values", ",", "placement", "=", "slice", "(", "0", ",", "len", "(", "values", ")", ")", ",", "ndim", "=", "1", ")", "]", ",", "self", ".", "axes", "[", "1", "]", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.delete
Delete selected item (items if non-unique) in-place.
pandas/core/internals/managers.py
def delete(self, item): """ Delete selected item (items if non-unique) in-place. """ indexer = self.items.get_loc(item) is_deleted = np.zeros(self.shape[0], dtype=np.bool_) is_deleted[indexer] = True ref_loc_offset = -is_deleted.cumsum() is_blk_deleted = [False] * len(self.blocks) if isinstance(indexer, int): affected_start = indexer else: affected_start = is_deleted.nonzero()[0][0] for blkno, _ in _fast_count_smallints(self._blknos[affected_start:]): blk = self.blocks[blkno] bml = blk.mgr_locs blk_del = is_deleted[bml.indexer].nonzero()[0] if len(blk_del) == len(bml): is_blk_deleted[blkno] = True continue elif len(blk_del) != 0: blk.delete(blk_del) bml = blk.mgr_locs blk.mgr_locs = bml.add(ref_loc_offset[bml.indexer]) # FIXME: use Index.delete as soon as it uses fastpath=True self.axes[0] = self.items[~is_deleted] self.blocks = tuple(b for blkno, b in enumerate(self.blocks) if not is_blk_deleted[blkno]) self._shape = None self._rebuild_blknos_and_blklocs()
def delete(self, item): """ Delete selected item (items if non-unique) in-place. """ indexer = self.items.get_loc(item) is_deleted = np.zeros(self.shape[0], dtype=np.bool_) is_deleted[indexer] = True ref_loc_offset = -is_deleted.cumsum() is_blk_deleted = [False] * len(self.blocks) if isinstance(indexer, int): affected_start = indexer else: affected_start = is_deleted.nonzero()[0][0] for blkno, _ in _fast_count_smallints(self._blknos[affected_start:]): blk = self.blocks[blkno] bml = blk.mgr_locs blk_del = is_deleted[bml.indexer].nonzero()[0] if len(blk_del) == len(bml): is_blk_deleted[blkno] = True continue elif len(blk_del) != 0: blk.delete(blk_del) bml = blk.mgr_locs blk.mgr_locs = bml.add(ref_loc_offset[bml.indexer]) # FIXME: use Index.delete as soon as it uses fastpath=True self.axes[0] = self.items[~is_deleted] self.blocks = tuple(b for blkno, b in enumerate(self.blocks) if not is_blk_deleted[blkno]) self._shape = None self._rebuild_blknos_and_blklocs()
[ "Delete", "selected", "item", "(", "items", "if", "non", "-", "unique", ")", "in", "-", "place", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L977-L1013
[ "def", "delete", "(", "self", ",", "item", ")", ":", "indexer", "=", "self", ".", "items", ".", "get_loc", "(", "item", ")", "is_deleted", "=", "np", ".", "zeros", "(", "self", ".", "shape", "[", "0", "]", ",", "dtype", "=", "np", ".", "bool_", ")", "is_deleted", "[", "indexer", "]", "=", "True", "ref_loc_offset", "=", "-", "is_deleted", ".", "cumsum", "(", ")", "is_blk_deleted", "=", "[", "False", "]", "*", "len", "(", "self", ".", "blocks", ")", "if", "isinstance", "(", "indexer", ",", "int", ")", ":", "affected_start", "=", "indexer", "else", ":", "affected_start", "=", "is_deleted", ".", "nonzero", "(", ")", "[", "0", "]", "[", "0", "]", "for", "blkno", ",", "_", "in", "_fast_count_smallints", "(", "self", ".", "_blknos", "[", "affected_start", ":", "]", ")", ":", "blk", "=", "self", ".", "blocks", "[", "blkno", "]", "bml", "=", "blk", ".", "mgr_locs", "blk_del", "=", "is_deleted", "[", "bml", ".", "indexer", "]", ".", "nonzero", "(", ")", "[", "0", "]", "if", "len", "(", "blk_del", ")", "==", "len", "(", "bml", ")", ":", "is_blk_deleted", "[", "blkno", "]", "=", "True", "continue", "elif", "len", "(", "blk_del", ")", "!=", "0", ":", "blk", ".", "delete", "(", "blk_del", ")", "bml", "=", "blk", ".", "mgr_locs", "blk", ".", "mgr_locs", "=", "bml", ".", "add", "(", "ref_loc_offset", "[", "bml", ".", "indexer", "]", ")", "# FIXME: use Index.delete as soon as it uses fastpath=True", "self", ".", "axes", "[", "0", "]", "=", "self", ".", "items", "[", "~", "is_deleted", "]", "self", ".", "blocks", "=", "tuple", "(", "b", "for", "blkno", ",", "b", "in", "enumerate", "(", "self", ".", "blocks", ")", "if", "not", "is_blk_deleted", "[", "blkno", "]", ")", "self", ".", "_shape", "=", "None", "self", ".", "_rebuild_blknos_and_blklocs", "(", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.set
Set new item in-place. Does not consolidate. Adds new Block if not contained in the current set of items
pandas/core/internals/managers.py
def set(self, item, value): """ Set new item in-place. Does not consolidate. Adds new Block if not contained in the current set of items """ # FIXME: refactor, clearly separate broadcasting & zip-like assignment # can prob also fix the various if tests for sparse/categorical # TODO(EA): Remove an is_extension_ when all extension types satisfy # the interface value_is_extension_type = (is_extension_type(value) or is_extension_array_dtype(value)) # categorical/spares/datetimetz if value_is_extension_type: def value_getitem(placement): return value else: if value.ndim == self.ndim - 1: value = _safe_reshape(value, (1,) + value.shape) def value_getitem(placement): return value else: def value_getitem(placement): return value[placement.indexer] if value.shape[1:] != self.shape[1:]: raise AssertionError('Shape of new values must be compatible ' 'with manager shape') try: loc = self.items.get_loc(item) except KeyError: # This item wasn't present, just insert at end self.insert(len(self.items), item, value) return if isinstance(loc, int): loc = [loc] blknos = self._blknos[loc] blklocs = self._blklocs[loc].copy() unfit_mgr_locs = [] unfit_val_locs = [] removed_blknos = [] for blkno, val_locs in libinternals.get_blkno_placements(blknos, self.nblocks, group=True): blk = self.blocks[blkno] blk_locs = blklocs[val_locs.indexer] if blk.should_store(value): blk.set(blk_locs, value_getitem(val_locs)) else: unfit_mgr_locs.append(blk.mgr_locs.as_array[blk_locs]) unfit_val_locs.append(val_locs) # If all block items are unfit, schedule the block for removal. if len(val_locs) == len(blk.mgr_locs): removed_blknos.append(blkno) else: self._blklocs[blk.mgr_locs.indexer] = -1 blk.delete(blk_locs) self._blklocs[blk.mgr_locs.indexer] = np.arange(len(blk)) if len(removed_blknos): # Remove blocks & update blknos accordingly is_deleted = np.zeros(self.nblocks, dtype=np.bool_) is_deleted[removed_blknos] = True new_blknos = np.empty(self.nblocks, dtype=np.int64) new_blknos.fill(-1) new_blknos[~is_deleted] = np.arange(self.nblocks - len(removed_blknos)) self._blknos = algos.take_1d(new_blknos, self._blknos, axis=0, allow_fill=False) self.blocks = tuple(blk for i, blk in enumerate(self.blocks) if i not in set(removed_blknos)) if unfit_val_locs: unfit_mgr_locs = np.concatenate(unfit_mgr_locs) unfit_count = len(unfit_mgr_locs) new_blocks = [] if value_is_extension_type: # This code (ab-)uses the fact that sparse blocks contain only # one item. new_blocks.extend( make_block(values=value.copy(), ndim=self.ndim, placement=slice(mgr_loc, mgr_loc + 1)) for mgr_loc in unfit_mgr_locs) self._blknos[unfit_mgr_locs] = (np.arange(unfit_count) + len(self.blocks)) self._blklocs[unfit_mgr_locs] = 0 else: # unfit_val_locs contains BlockPlacement objects unfit_val_items = unfit_val_locs[0].append(unfit_val_locs[1:]) new_blocks.append( make_block(values=value_getitem(unfit_val_items), ndim=self.ndim, placement=unfit_mgr_locs)) self._blknos[unfit_mgr_locs] = len(self.blocks) self._blklocs[unfit_mgr_locs] = np.arange(unfit_count) self.blocks += tuple(new_blocks) # Newly created block's dtype may already be present. self._known_consolidated = False
def set(self, item, value): """ Set new item in-place. Does not consolidate. Adds new Block if not contained in the current set of items """ # FIXME: refactor, clearly separate broadcasting & zip-like assignment # can prob also fix the various if tests for sparse/categorical # TODO(EA): Remove an is_extension_ when all extension types satisfy # the interface value_is_extension_type = (is_extension_type(value) or is_extension_array_dtype(value)) # categorical/spares/datetimetz if value_is_extension_type: def value_getitem(placement): return value else: if value.ndim == self.ndim - 1: value = _safe_reshape(value, (1,) + value.shape) def value_getitem(placement): return value else: def value_getitem(placement): return value[placement.indexer] if value.shape[1:] != self.shape[1:]: raise AssertionError('Shape of new values must be compatible ' 'with manager shape') try: loc = self.items.get_loc(item) except KeyError: # This item wasn't present, just insert at end self.insert(len(self.items), item, value) return if isinstance(loc, int): loc = [loc] blknos = self._blknos[loc] blklocs = self._blklocs[loc].copy() unfit_mgr_locs = [] unfit_val_locs = [] removed_blknos = [] for blkno, val_locs in libinternals.get_blkno_placements(blknos, self.nblocks, group=True): blk = self.blocks[blkno] blk_locs = blklocs[val_locs.indexer] if blk.should_store(value): blk.set(blk_locs, value_getitem(val_locs)) else: unfit_mgr_locs.append(blk.mgr_locs.as_array[blk_locs]) unfit_val_locs.append(val_locs) # If all block items are unfit, schedule the block for removal. if len(val_locs) == len(blk.mgr_locs): removed_blknos.append(blkno) else: self._blklocs[blk.mgr_locs.indexer] = -1 blk.delete(blk_locs) self._blklocs[blk.mgr_locs.indexer] = np.arange(len(blk)) if len(removed_blknos): # Remove blocks & update blknos accordingly is_deleted = np.zeros(self.nblocks, dtype=np.bool_) is_deleted[removed_blknos] = True new_blknos = np.empty(self.nblocks, dtype=np.int64) new_blknos.fill(-1) new_blknos[~is_deleted] = np.arange(self.nblocks - len(removed_blknos)) self._blknos = algos.take_1d(new_blknos, self._blknos, axis=0, allow_fill=False) self.blocks = tuple(blk for i, blk in enumerate(self.blocks) if i not in set(removed_blknos)) if unfit_val_locs: unfit_mgr_locs = np.concatenate(unfit_mgr_locs) unfit_count = len(unfit_mgr_locs) new_blocks = [] if value_is_extension_type: # This code (ab-)uses the fact that sparse blocks contain only # one item. new_blocks.extend( make_block(values=value.copy(), ndim=self.ndim, placement=slice(mgr_loc, mgr_loc + 1)) for mgr_loc in unfit_mgr_locs) self._blknos[unfit_mgr_locs] = (np.arange(unfit_count) + len(self.blocks)) self._blklocs[unfit_mgr_locs] = 0 else: # unfit_val_locs contains BlockPlacement objects unfit_val_items = unfit_val_locs[0].append(unfit_val_locs[1:]) new_blocks.append( make_block(values=value_getitem(unfit_val_items), ndim=self.ndim, placement=unfit_mgr_locs)) self._blknos[unfit_mgr_locs] = len(self.blocks) self._blklocs[unfit_mgr_locs] = np.arange(unfit_count) self.blocks += tuple(new_blocks) # Newly created block's dtype may already be present. self._known_consolidated = False
[ "Set", "new", "item", "in", "-", "place", ".", "Does", "not", "consolidate", ".", "Adds", "new", "Block", "if", "not", "contained", "in", "the", "current", "set", "of", "items" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1015-L1128
[ "def", "set", "(", "self", ",", "item", ",", "value", ")", ":", "# FIXME: refactor, clearly separate broadcasting & zip-like assignment", "# can prob also fix the various if tests for sparse/categorical", "# TODO(EA): Remove an is_extension_ when all extension types satisfy", "# the interface", "value_is_extension_type", "=", "(", "is_extension_type", "(", "value", ")", "or", "is_extension_array_dtype", "(", "value", ")", ")", "# categorical/spares/datetimetz", "if", "value_is_extension_type", ":", "def", "value_getitem", "(", "placement", ")", ":", "return", "value", "else", ":", "if", "value", ".", "ndim", "==", "self", ".", "ndim", "-", "1", ":", "value", "=", "_safe_reshape", "(", "value", ",", "(", "1", ",", ")", "+", "value", ".", "shape", ")", "def", "value_getitem", "(", "placement", ")", ":", "return", "value", "else", ":", "def", "value_getitem", "(", "placement", ")", ":", "return", "value", "[", "placement", ".", "indexer", "]", "if", "value", ".", "shape", "[", "1", ":", "]", "!=", "self", ".", "shape", "[", "1", ":", "]", ":", "raise", "AssertionError", "(", "'Shape of new values must be compatible '", "'with manager shape'", ")", "try", ":", "loc", "=", "self", ".", "items", ".", "get_loc", "(", "item", ")", "except", "KeyError", ":", "# This item wasn't present, just insert at end", "self", ".", "insert", "(", "len", "(", "self", ".", "items", ")", ",", "item", ",", "value", ")", "return", "if", "isinstance", "(", "loc", ",", "int", ")", ":", "loc", "=", "[", "loc", "]", "blknos", "=", "self", ".", "_blknos", "[", "loc", "]", "blklocs", "=", "self", ".", "_blklocs", "[", "loc", "]", ".", "copy", "(", ")", "unfit_mgr_locs", "=", "[", "]", "unfit_val_locs", "=", "[", "]", "removed_blknos", "=", "[", "]", "for", "blkno", ",", "val_locs", "in", "libinternals", ".", "get_blkno_placements", "(", "blknos", ",", "self", ".", "nblocks", ",", "group", "=", "True", ")", ":", "blk", "=", "self", ".", "blocks", "[", "blkno", "]", "blk_locs", "=", "blklocs", "[", "val_locs", ".", "indexer", "]", "if", "blk", ".", "should_store", "(", "value", ")", ":", "blk", ".", "set", "(", "blk_locs", ",", "value_getitem", "(", "val_locs", ")", ")", "else", ":", "unfit_mgr_locs", ".", "append", "(", "blk", ".", "mgr_locs", ".", "as_array", "[", "blk_locs", "]", ")", "unfit_val_locs", ".", "append", "(", "val_locs", ")", "# If all block items are unfit, schedule the block for removal.", "if", "len", "(", "val_locs", ")", "==", "len", "(", "blk", ".", "mgr_locs", ")", ":", "removed_blknos", ".", "append", "(", "blkno", ")", "else", ":", "self", ".", "_blklocs", "[", "blk", ".", "mgr_locs", ".", "indexer", "]", "=", "-", "1", "blk", ".", "delete", "(", "blk_locs", ")", "self", ".", "_blklocs", "[", "blk", ".", "mgr_locs", ".", "indexer", "]", "=", "np", ".", "arange", "(", "len", "(", "blk", ")", ")", "if", "len", "(", "removed_blknos", ")", ":", "# Remove blocks & update blknos accordingly", "is_deleted", "=", "np", ".", "zeros", "(", "self", ".", "nblocks", ",", "dtype", "=", "np", ".", "bool_", ")", "is_deleted", "[", "removed_blknos", "]", "=", "True", "new_blknos", "=", "np", ".", "empty", "(", "self", ".", "nblocks", ",", "dtype", "=", "np", ".", "int64", ")", "new_blknos", ".", "fill", "(", "-", "1", ")", "new_blknos", "[", "~", "is_deleted", "]", "=", "np", ".", "arange", "(", "self", ".", "nblocks", "-", "len", "(", "removed_blknos", ")", ")", "self", ".", "_blknos", "=", "algos", ".", "take_1d", "(", "new_blknos", ",", "self", ".", "_blknos", ",", "axis", "=", "0", ",", "allow_fill", "=", "False", ")", "self", ".", "blocks", "=", "tuple", "(", "blk", "for", "i", ",", "blk", "in", "enumerate", "(", "self", ".", "blocks", ")", "if", "i", "not", "in", "set", "(", "removed_blknos", ")", ")", "if", "unfit_val_locs", ":", "unfit_mgr_locs", "=", "np", ".", "concatenate", "(", "unfit_mgr_locs", ")", "unfit_count", "=", "len", "(", "unfit_mgr_locs", ")", "new_blocks", "=", "[", "]", "if", "value_is_extension_type", ":", "# This code (ab-)uses the fact that sparse blocks contain only", "# one item.", "new_blocks", ".", "extend", "(", "make_block", "(", "values", "=", "value", ".", "copy", "(", ")", ",", "ndim", "=", "self", ".", "ndim", ",", "placement", "=", "slice", "(", "mgr_loc", ",", "mgr_loc", "+", "1", ")", ")", "for", "mgr_loc", "in", "unfit_mgr_locs", ")", "self", ".", "_blknos", "[", "unfit_mgr_locs", "]", "=", "(", "np", ".", "arange", "(", "unfit_count", ")", "+", "len", "(", "self", ".", "blocks", ")", ")", "self", ".", "_blklocs", "[", "unfit_mgr_locs", "]", "=", "0", "else", ":", "# unfit_val_locs contains BlockPlacement objects", "unfit_val_items", "=", "unfit_val_locs", "[", "0", "]", ".", "append", "(", "unfit_val_locs", "[", "1", ":", "]", ")", "new_blocks", ".", "append", "(", "make_block", "(", "values", "=", "value_getitem", "(", "unfit_val_items", ")", ",", "ndim", "=", "self", ".", "ndim", ",", "placement", "=", "unfit_mgr_locs", ")", ")", "self", ".", "_blknos", "[", "unfit_mgr_locs", "]", "=", "len", "(", "self", ".", "blocks", ")", "self", ".", "_blklocs", "[", "unfit_mgr_locs", "]", "=", "np", ".", "arange", "(", "unfit_count", ")", "self", ".", "blocks", "+=", "tuple", "(", "new_blocks", ")", "# Newly created block's dtype may already be present.", "self", ".", "_known_consolidated", "=", "False" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.insert
Insert item at selected position. Parameters ---------- loc : int item : hashable value : array_like allow_duplicates: bool If False, trying to insert non-unique item will raise
pandas/core/internals/managers.py
def insert(self, loc, item, value, allow_duplicates=False): """ Insert item at selected position. Parameters ---------- loc : int item : hashable value : array_like allow_duplicates: bool If False, trying to insert non-unique item will raise """ if not allow_duplicates and item in self.items: # Should this be a different kind of error?? raise ValueError('cannot insert {}, already exists'.format(item)) if not isinstance(loc, int): raise TypeError("loc must be int") # insert to the axis; this could possibly raise a TypeError new_axis = self.items.insert(loc, item) block = make_block(values=value, ndim=self.ndim, placement=slice(loc, loc + 1)) for blkno, count in _fast_count_smallints(self._blknos[loc:]): blk = self.blocks[blkno] if count == len(blk.mgr_locs): blk.mgr_locs = blk.mgr_locs.add(1) else: new_mgr_locs = blk.mgr_locs.as_array.copy() new_mgr_locs[new_mgr_locs >= loc] += 1 blk.mgr_locs = new_mgr_locs if loc == self._blklocs.shape[0]: # np.append is a lot faster, let's use it if we can. self._blklocs = np.append(self._blklocs, 0) self._blknos = np.append(self._blknos, len(self.blocks)) else: self._blklocs = np.insert(self._blklocs, loc, 0) self._blknos = np.insert(self._blknos, loc, len(self.blocks)) self.axes[0] = new_axis self.blocks += (block,) self._shape = None self._known_consolidated = False if len(self.blocks) > 100: self._consolidate_inplace()
def insert(self, loc, item, value, allow_duplicates=False): """ Insert item at selected position. Parameters ---------- loc : int item : hashable value : array_like allow_duplicates: bool If False, trying to insert non-unique item will raise """ if not allow_duplicates and item in self.items: # Should this be a different kind of error?? raise ValueError('cannot insert {}, already exists'.format(item)) if not isinstance(loc, int): raise TypeError("loc must be int") # insert to the axis; this could possibly raise a TypeError new_axis = self.items.insert(loc, item) block = make_block(values=value, ndim=self.ndim, placement=slice(loc, loc + 1)) for blkno, count in _fast_count_smallints(self._blknos[loc:]): blk = self.blocks[blkno] if count == len(blk.mgr_locs): blk.mgr_locs = blk.mgr_locs.add(1) else: new_mgr_locs = blk.mgr_locs.as_array.copy() new_mgr_locs[new_mgr_locs >= loc] += 1 blk.mgr_locs = new_mgr_locs if loc == self._blklocs.shape[0]: # np.append is a lot faster, let's use it if we can. self._blklocs = np.append(self._blklocs, 0) self._blknos = np.append(self._blknos, len(self.blocks)) else: self._blklocs = np.insert(self._blklocs, loc, 0) self._blknos = np.insert(self._blknos, loc, len(self.blocks)) self.axes[0] = new_axis self.blocks += (block,) self._shape = None self._known_consolidated = False if len(self.blocks) > 100: self._consolidate_inplace()
[ "Insert", "item", "at", "selected", "position", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1130-L1180
[ "def", "insert", "(", "self", ",", "loc", ",", "item", ",", "value", ",", "allow_duplicates", "=", "False", ")", ":", "if", "not", "allow_duplicates", "and", "item", "in", "self", ".", "items", ":", "# Should this be a different kind of error??", "raise", "ValueError", "(", "'cannot insert {}, already exists'", ".", "format", "(", "item", ")", ")", "if", "not", "isinstance", "(", "loc", ",", "int", ")", ":", "raise", "TypeError", "(", "\"loc must be int\"", ")", "# insert to the axis; this could possibly raise a TypeError", "new_axis", "=", "self", ".", "items", ".", "insert", "(", "loc", ",", "item", ")", "block", "=", "make_block", "(", "values", "=", "value", ",", "ndim", "=", "self", ".", "ndim", ",", "placement", "=", "slice", "(", "loc", ",", "loc", "+", "1", ")", ")", "for", "blkno", ",", "count", "in", "_fast_count_smallints", "(", "self", ".", "_blknos", "[", "loc", ":", "]", ")", ":", "blk", "=", "self", ".", "blocks", "[", "blkno", "]", "if", "count", "==", "len", "(", "blk", ".", "mgr_locs", ")", ":", "blk", ".", "mgr_locs", "=", "blk", ".", "mgr_locs", ".", "add", "(", "1", ")", "else", ":", "new_mgr_locs", "=", "blk", ".", "mgr_locs", ".", "as_array", ".", "copy", "(", ")", "new_mgr_locs", "[", "new_mgr_locs", ">=", "loc", "]", "+=", "1", "blk", ".", "mgr_locs", "=", "new_mgr_locs", "if", "loc", "==", "self", ".", "_blklocs", ".", "shape", "[", "0", "]", ":", "# np.append is a lot faster, let's use it if we can.", "self", ".", "_blklocs", "=", "np", ".", "append", "(", "self", ".", "_blklocs", ",", "0", ")", "self", ".", "_blknos", "=", "np", ".", "append", "(", "self", ".", "_blknos", ",", "len", "(", "self", ".", "blocks", ")", ")", "else", ":", "self", ".", "_blklocs", "=", "np", ".", "insert", "(", "self", ".", "_blklocs", ",", "loc", ",", "0", ")", "self", ".", "_blknos", "=", "np", ".", "insert", "(", "self", ".", "_blknos", ",", "loc", ",", "len", "(", "self", ".", "blocks", ")", ")", "self", ".", "axes", "[", "0", "]", "=", "new_axis", "self", ".", "blocks", "+=", "(", "block", ",", ")", "self", ".", "_shape", "=", "None", "self", ".", "_known_consolidated", "=", "False", "if", "len", "(", "self", ".", "blocks", ")", ">", "100", ":", "self", ".", "_consolidate_inplace", "(", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.reindex_axis
Conform block manager to new index.
pandas/core/internals/managers.py
def reindex_axis(self, new_index, axis, method=None, limit=None, fill_value=None, copy=True): """ Conform block manager to new index. """ new_index = ensure_index(new_index) new_index, indexer = self.axes[axis].reindex(new_index, method=method, limit=limit) return self.reindex_indexer(new_index, indexer, axis=axis, fill_value=fill_value, copy=copy)
def reindex_axis(self, new_index, axis, method=None, limit=None, fill_value=None, copy=True): """ Conform block manager to new index. """ new_index = ensure_index(new_index) new_index, indexer = self.axes[axis].reindex(new_index, method=method, limit=limit) return self.reindex_indexer(new_index, indexer, axis=axis, fill_value=fill_value, copy=copy)
[ "Conform", "block", "manager", "to", "new", "index", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1182-L1192
[ "def", "reindex_axis", "(", "self", ",", "new_index", ",", "axis", ",", "method", "=", "None", ",", "limit", "=", "None", ",", "fill_value", "=", "None", ",", "copy", "=", "True", ")", ":", "new_index", "=", "ensure_index", "(", "new_index", ")", "new_index", ",", "indexer", "=", "self", ".", "axes", "[", "axis", "]", ".", "reindex", "(", "new_index", ",", "method", "=", "method", ",", "limit", "=", "limit", ")", "return", "self", ".", "reindex_indexer", "(", "new_index", ",", "indexer", ",", "axis", "=", "axis", ",", "fill_value", "=", "fill_value", ",", "copy", "=", "copy", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.reindex_indexer
Parameters ---------- new_axis : Index indexer : ndarray of int64 or None axis : int fill_value : object allow_dups : bool pandas-indexer with -1's only.
pandas/core/internals/managers.py
def reindex_indexer(self, new_axis, indexer, axis, fill_value=None, allow_dups=False, copy=True): """ Parameters ---------- new_axis : Index indexer : ndarray of int64 or None axis : int fill_value : object allow_dups : bool pandas-indexer with -1's only. """ if indexer is None: if new_axis is self.axes[axis] and not copy: return self result = self.copy(deep=copy) result.axes = list(self.axes) result.axes[axis] = new_axis return result self._consolidate_inplace() # some axes don't allow reindexing with dups if not allow_dups: self.axes[axis]._can_reindex(indexer) if axis >= self.ndim: raise IndexError("Requested axis not found in manager") if axis == 0: new_blocks = self._slice_take_blocks_ax0(indexer, fill_tuple=(fill_value,)) else: new_blocks = [blk.take_nd(indexer, axis=axis, fill_tuple=( fill_value if fill_value is not None else blk.fill_value,)) for blk in self.blocks] new_axes = list(self.axes) new_axes[axis] = new_axis return self.__class__(new_blocks, new_axes)
def reindex_indexer(self, new_axis, indexer, axis, fill_value=None, allow_dups=False, copy=True): """ Parameters ---------- new_axis : Index indexer : ndarray of int64 or None axis : int fill_value : object allow_dups : bool pandas-indexer with -1's only. """ if indexer is None: if new_axis is self.axes[axis] and not copy: return self result = self.copy(deep=copy) result.axes = list(self.axes) result.axes[axis] = new_axis return result self._consolidate_inplace() # some axes don't allow reindexing with dups if not allow_dups: self.axes[axis]._can_reindex(indexer) if axis >= self.ndim: raise IndexError("Requested axis not found in manager") if axis == 0: new_blocks = self._slice_take_blocks_ax0(indexer, fill_tuple=(fill_value,)) else: new_blocks = [blk.take_nd(indexer, axis=axis, fill_tuple=( fill_value if fill_value is not None else blk.fill_value,)) for blk in self.blocks] new_axes = list(self.axes) new_axes[axis] = new_axis return self.__class__(new_blocks, new_axes)
[ "Parameters", "----------", "new_axis", ":", "Index", "indexer", ":", "ndarray", "of", "int64", "or", "None", "axis", ":", "int", "fill_value", ":", "object", "allow_dups", ":", "bool" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1194-L1235
[ "def", "reindex_indexer", "(", "self", ",", "new_axis", ",", "indexer", ",", "axis", ",", "fill_value", "=", "None", ",", "allow_dups", "=", "False", ",", "copy", "=", "True", ")", ":", "if", "indexer", "is", "None", ":", "if", "new_axis", "is", "self", ".", "axes", "[", "axis", "]", "and", "not", "copy", ":", "return", "self", "result", "=", "self", ".", "copy", "(", "deep", "=", "copy", ")", "result", ".", "axes", "=", "list", "(", "self", ".", "axes", ")", "result", ".", "axes", "[", "axis", "]", "=", "new_axis", "return", "result", "self", ".", "_consolidate_inplace", "(", ")", "# some axes don't allow reindexing with dups", "if", "not", "allow_dups", ":", "self", ".", "axes", "[", "axis", "]", ".", "_can_reindex", "(", "indexer", ")", "if", "axis", ">=", "self", ".", "ndim", ":", "raise", "IndexError", "(", "\"Requested axis not found in manager\"", ")", "if", "axis", "==", "0", ":", "new_blocks", "=", "self", ".", "_slice_take_blocks_ax0", "(", "indexer", ",", "fill_tuple", "=", "(", "fill_value", ",", ")", ")", "else", ":", "new_blocks", "=", "[", "blk", ".", "take_nd", "(", "indexer", ",", "axis", "=", "axis", ",", "fill_tuple", "=", "(", "fill_value", "if", "fill_value", "is", "not", "None", "else", "blk", ".", "fill_value", ",", ")", ")", "for", "blk", "in", "self", ".", "blocks", "]", "new_axes", "=", "list", "(", "self", ".", "axes", ")", "new_axes", "[", "axis", "]", "=", "new_axis", "return", "self", ".", "__class__", "(", "new_blocks", ",", "new_axes", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager._slice_take_blocks_ax0
Slice/take blocks along axis=0. Overloaded for SingleBlock Returns ------- new_blocks : list of Block
pandas/core/internals/managers.py
def _slice_take_blocks_ax0(self, slice_or_indexer, fill_tuple=None): """ Slice/take blocks along axis=0. Overloaded for SingleBlock Returns ------- new_blocks : list of Block """ allow_fill = fill_tuple is not None sl_type, slobj, sllen = _preprocess_slice_or_indexer( slice_or_indexer, self.shape[0], allow_fill=allow_fill) if self._is_single_block: blk = self.blocks[0] if sl_type in ('slice', 'mask'): return [blk.getitem_block(slobj, new_mgr_locs=slice(0, sllen))] elif not allow_fill or self.ndim == 1: if allow_fill and fill_tuple[0] is None: _, fill_value = maybe_promote(blk.dtype) fill_tuple = (fill_value, ) return [blk.take_nd(slobj, axis=0, new_mgr_locs=slice(0, sllen), fill_tuple=fill_tuple)] if sl_type in ('slice', 'mask'): blknos = self._blknos[slobj] blklocs = self._blklocs[slobj] else: blknos = algos.take_1d(self._blknos, slobj, fill_value=-1, allow_fill=allow_fill) blklocs = algos.take_1d(self._blklocs, slobj, fill_value=-1, allow_fill=allow_fill) # When filling blknos, make sure blknos is updated before appending to # blocks list, that way new blkno is exactly len(blocks). # # FIXME: mgr_groupby_blknos must return mgr_locs in ascending order, # pytables serialization will break otherwise. blocks = [] for blkno, mgr_locs in libinternals.get_blkno_placements(blknos, self.nblocks, group=True): if blkno == -1: # If we've got here, fill_tuple was not None. fill_value = fill_tuple[0] blocks.append(self._make_na_block(placement=mgr_locs, fill_value=fill_value)) else: blk = self.blocks[blkno] # Otherwise, slicing along items axis is necessary. if not blk._can_consolidate: # A non-consolidatable block, it's easy, because there's # only one item and each mgr loc is a copy of that single # item. for mgr_loc in mgr_locs: newblk = blk.copy(deep=True) newblk.mgr_locs = slice(mgr_loc, mgr_loc + 1) blocks.append(newblk) else: blocks.append(blk.take_nd(blklocs[mgr_locs.indexer], axis=0, new_mgr_locs=mgr_locs, fill_tuple=None)) return blocks
def _slice_take_blocks_ax0(self, slice_or_indexer, fill_tuple=None): """ Slice/take blocks along axis=0. Overloaded for SingleBlock Returns ------- new_blocks : list of Block """ allow_fill = fill_tuple is not None sl_type, slobj, sllen = _preprocess_slice_or_indexer( slice_or_indexer, self.shape[0], allow_fill=allow_fill) if self._is_single_block: blk = self.blocks[0] if sl_type in ('slice', 'mask'): return [blk.getitem_block(slobj, new_mgr_locs=slice(0, sllen))] elif not allow_fill or self.ndim == 1: if allow_fill and fill_tuple[0] is None: _, fill_value = maybe_promote(blk.dtype) fill_tuple = (fill_value, ) return [blk.take_nd(slobj, axis=0, new_mgr_locs=slice(0, sllen), fill_tuple=fill_tuple)] if sl_type in ('slice', 'mask'): blknos = self._blknos[slobj] blklocs = self._blklocs[slobj] else: blknos = algos.take_1d(self._blknos, slobj, fill_value=-1, allow_fill=allow_fill) blklocs = algos.take_1d(self._blklocs, slobj, fill_value=-1, allow_fill=allow_fill) # When filling blknos, make sure blknos is updated before appending to # blocks list, that way new blkno is exactly len(blocks). # # FIXME: mgr_groupby_blknos must return mgr_locs in ascending order, # pytables serialization will break otherwise. blocks = [] for blkno, mgr_locs in libinternals.get_blkno_placements(blknos, self.nblocks, group=True): if blkno == -1: # If we've got here, fill_tuple was not None. fill_value = fill_tuple[0] blocks.append(self._make_na_block(placement=mgr_locs, fill_value=fill_value)) else: blk = self.blocks[blkno] # Otherwise, slicing along items axis is necessary. if not blk._can_consolidate: # A non-consolidatable block, it's easy, because there's # only one item and each mgr loc is a copy of that single # item. for mgr_loc in mgr_locs: newblk = blk.copy(deep=True) newblk.mgr_locs = slice(mgr_loc, mgr_loc + 1) blocks.append(newblk) else: blocks.append(blk.take_nd(blklocs[mgr_locs.indexer], axis=0, new_mgr_locs=mgr_locs, fill_tuple=None)) return blocks
[ "Slice", "/", "take", "blocks", "along", "axis", "=", "0", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1237-L1310
[ "def", "_slice_take_blocks_ax0", "(", "self", ",", "slice_or_indexer", ",", "fill_tuple", "=", "None", ")", ":", "allow_fill", "=", "fill_tuple", "is", "not", "None", "sl_type", ",", "slobj", ",", "sllen", "=", "_preprocess_slice_or_indexer", "(", "slice_or_indexer", ",", "self", ".", "shape", "[", "0", "]", ",", "allow_fill", "=", "allow_fill", ")", "if", "self", ".", "_is_single_block", ":", "blk", "=", "self", ".", "blocks", "[", "0", "]", "if", "sl_type", "in", "(", "'slice'", ",", "'mask'", ")", ":", "return", "[", "blk", ".", "getitem_block", "(", "slobj", ",", "new_mgr_locs", "=", "slice", "(", "0", ",", "sllen", ")", ")", "]", "elif", "not", "allow_fill", "or", "self", ".", "ndim", "==", "1", ":", "if", "allow_fill", "and", "fill_tuple", "[", "0", "]", "is", "None", ":", "_", ",", "fill_value", "=", "maybe_promote", "(", "blk", ".", "dtype", ")", "fill_tuple", "=", "(", "fill_value", ",", ")", "return", "[", "blk", ".", "take_nd", "(", "slobj", ",", "axis", "=", "0", ",", "new_mgr_locs", "=", "slice", "(", "0", ",", "sllen", ")", ",", "fill_tuple", "=", "fill_tuple", ")", "]", "if", "sl_type", "in", "(", "'slice'", ",", "'mask'", ")", ":", "blknos", "=", "self", ".", "_blknos", "[", "slobj", "]", "blklocs", "=", "self", ".", "_blklocs", "[", "slobj", "]", "else", ":", "blknos", "=", "algos", ".", "take_1d", "(", "self", ".", "_blknos", ",", "slobj", ",", "fill_value", "=", "-", "1", ",", "allow_fill", "=", "allow_fill", ")", "blklocs", "=", "algos", ".", "take_1d", "(", "self", ".", "_blklocs", ",", "slobj", ",", "fill_value", "=", "-", "1", ",", "allow_fill", "=", "allow_fill", ")", "# When filling blknos, make sure blknos is updated before appending to", "# blocks list, that way new blkno is exactly len(blocks).", "#", "# FIXME: mgr_groupby_blknos must return mgr_locs in ascending order,", "# pytables serialization will break otherwise.", "blocks", "=", "[", "]", "for", "blkno", ",", "mgr_locs", "in", "libinternals", ".", "get_blkno_placements", "(", "blknos", ",", "self", ".", "nblocks", ",", "group", "=", "True", ")", ":", "if", "blkno", "==", "-", "1", ":", "# If we've got here, fill_tuple was not None.", "fill_value", "=", "fill_tuple", "[", "0", "]", "blocks", ".", "append", "(", "self", ".", "_make_na_block", "(", "placement", "=", "mgr_locs", ",", "fill_value", "=", "fill_value", ")", ")", "else", ":", "blk", "=", "self", ".", "blocks", "[", "blkno", "]", "# Otherwise, slicing along items axis is necessary.", "if", "not", "blk", ".", "_can_consolidate", ":", "# A non-consolidatable block, it's easy, because there's", "# only one item and each mgr loc is a copy of that single", "# item.", "for", "mgr_loc", "in", "mgr_locs", ":", "newblk", "=", "blk", ".", "copy", "(", "deep", "=", "True", ")", "newblk", ".", "mgr_locs", "=", "slice", "(", "mgr_loc", ",", "mgr_loc", "+", "1", ")", "blocks", ".", "append", "(", "newblk", ")", "else", ":", "blocks", ".", "append", "(", "blk", ".", "take_nd", "(", "blklocs", "[", "mgr_locs", ".", "indexer", "]", ",", "axis", "=", "0", ",", "new_mgr_locs", "=", "mgr_locs", ",", "fill_tuple", "=", "None", ")", ")", "return", "blocks" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.take
Take items along any axis.
pandas/core/internals/managers.py
def take(self, indexer, axis=1, verify=True, convert=True): """ Take items along any axis. """ self._consolidate_inplace() indexer = (np.arange(indexer.start, indexer.stop, indexer.step, dtype='int64') if isinstance(indexer, slice) else np.asanyarray(indexer, dtype='int64')) n = self.shape[axis] if convert: indexer = maybe_convert_indices(indexer, n) if verify: if ((indexer == -1) | (indexer >= n)).any(): raise Exception('Indices must be nonzero and less than ' 'the axis length') new_labels = self.axes[axis].take(indexer) return self.reindex_indexer(new_axis=new_labels, indexer=indexer, axis=axis, allow_dups=True)
def take(self, indexer, axis=1, verify=True, convert=True): """ Take items along any axis. """ self._consolidate_inplace() indexer = (np.arange(indexer.start, indexer.stop, indexer.step, dtype='int64') if isinstance(indexer, slice) else np.asanyarray(indexer, dtype='int64')) n = self.shape[axis] if convert: indexer = maybe_convert_indices(indexer, n) if verify: if ((indexer == -1) | (indexer >= n)).any(): raise Exception('Indices must be nonzero and less than ' 'the axis length') new_labels = self.axes[axis].take(indexer) return self.reindex_indexer(new_axis=new_labels, indexer=indexer, axis=axis, allow_dups=True)
[ "Take", "items", "along", "any", "axis", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1325-L1346
[ "def", "take", "(", "self", ",", "indexer", ",", "axis", "=", "1", ",", "verify", "=", "True", ",", "convert", "=", "True", ")", ":", "self", ".", "_consolidate_inplace", "(", ")", "indexer", "=", "(", "np", ".", "arange", "(", "indexer", ".", "start", ",", "indexer", ".", "stop", ",", "indexer", ".", "step", ",", "dtype", "=", "'int64'", ")", "if", "isinstance", "(", "indexer", ",", "slice", ")", "else", "np", ".", "asanyarray", "(", "indexer", ",", "dtype", "=", "'int64'", ")", ")", "n", "=", "self", ".", "shape", "[", "axis", "]", "if", "convert", ":", "indexer", "=", "maybe_convert_indices", "(", "indexer", ",", "n", ")", "if", "verify", ":", "if", "(", "(", "indexer", "==", "-", "1", ")", "|", "(", "indexer", ">=", "n", ")", ")", ".", "any", "(", ")", ":", "raise", "Exception", "(", "'Indices must be nonzero and less than '", "'the axis length'", ")", "new_labels", "=", "self", ".", "axes", "[", "axis", "]", ".", "take", "(", "indexer", ")", "return", "self", ".", "reindex_indexer", "(", "new_axis", "=", "new_labels", ",", "indexer", "=", "indexer", ",", "axis", "=", "axis", ",", "allow_dups", "=", "True", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
BlockManager.unstack
Return a blockmanager with all blocks unstacked. Parameters ---------- unstacker_func : callable A (partially-applied) ``pd.core.reshape._Unstacker`` class. fill_value : Any fill_value for newly introduced missing values. Returns ------- unstacked : BlockManager
pandas/core/internals/managers.py
def unstack(self, unstacker_func, fill_value): """Return a blockmanager with all blocks unstacked. Parameters ---------- unstacker_func : callable A (partially-applied) ``pd.core.reshape._Unstacker`` class. fill_value : Any fill_value for newly introduced missing values. Returns ------- unstacked : BlockManager """ n_rows = self.shape[-1] dummy = unstacker_func(np.empty((0, 0)), value_columns=self.items) new_columns = dummy.get_new_columns() new_index = dummy.get_new_index() new_blocks = [] columns_mask = [] for blk in self.blocks: blocks, mask = blk._unstack( partial(unstacker_func, value_columns=self.items[blk.mgr_locs.indexer]), new_columns, n_rows, fill_value ) new_blocks.extend(blocks) columns_mask.extend(mask) new_columns = new_columns[columns_mask] bm = BlockManager(new_blocks, [new_columns, new_index]) return bm
def unstack(self, unstacker_func, fill_value): """Return a blockmanager with all blocks unstacked. Parameters ---------- unstacker_func : callable A (partially-applied) ``pd.core.reshape._Unstacker`` class. fill_value : Any fill_value for newly introduced missing values. Returns ------- unstacked : BlockManager """ n_rows = self.shape[-1] dummy = unstacker_func(np.empty((0, 0)), value_columns=self.items) new_columns = dummy.get_new_columns() new_index = dummy.get_new_index() new_blocks = [] columns_mask = [] for blk in self.blocks: blocks, mask = blk._unstack( partial(unstacker_func, value_columns=self.items[blk.mgr_locs.indexer]), new_columns, n_rows, fill_value ) new_blocks.extend(blocks) columns_mask.extend(mask) new_columns = new_columns[columns_mask] bm = BlockManager(new_blocks, [new_columns, new_index]) return bm
[ "Return", "a", "blockmanager", "with", "all", "blocks", "unstacked", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1392-L1428
[ "def", "unstack", "(", "self", ",", "unstacker_func", ",", "fill_value", ")", ":", "n_rows", "=", "self", ".", "shape", "[", "-", "1", "]", "dummy", "=", "unstacker_func", "(", "np", ".", "empty", "(", "(", "0", ",", "0", ")", ")", ",", "value_columns", "=", "self", ".", "items", ")", "new_columns", "=", "dummy", ".", "get_new_columns", "(", ")", "new_index", "=", "dummy", ".", "get_new_index", "(", ")", "new_blocks", "=", "[", "]", "columns_mask", "=", "[", "]", "for", "blk", "in", "self", ".", "blocks", ":", "blocks", ",", "mask", "=", "blk", ".", "_unstack", "(", "partial", "(", "unstacker_func", ",", "value_columns", "=", "self", ".", "items", "[", "blk", ".", "mgr_locs", ".", "indexer", "]", ")", ",", "new_columns", ",", "n_rows", ",", "fill_value", ")", "new_blocks", ".", "extend", "(", "blocks", ")", "columns_mask", ".", "extend", "(", "mask", ")", "new_columns", "=", "new_columns", "[", "columns_mask", "]", "bm", "=", "BlockManager", "(", "new_blocks", ",", "[", "new_columns", ",", "new_index", "]", ")", "return", "bm" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SingleBlockManager.delete
Delete single item from SingleBlockManager. Ensures that self.blocks doesn't become empty.
pandas/core/internals/managers.py
def delete(self, item): """ Delete single item from SingleBlockManager. Ensures that self.blocks doesn't become empty. """ loc = self.items.get_loc(item) self._block.delete(loc) self.axes[0] = self.axes[0].delete(loc)
def delete(self, item): """ Delete single item from SingleBlockManager. Ensures that self.blocks doesn't become empty. """ loc = self.items.get_loc(item) self._block.delete(loc) self.axes[0] = self.axes[0].delete(loc)
[ "Delete", "single", "item", "from", "SingleBlockManager", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1577-L1585
[ "def", "delete", "(", "self", ",", "item", ")", ":", "loc", "=", "self", ".", "items", ".", "get_loc", "(", "item", ")", "self", ".", "_block", ".", "delete", "(", "loc", ")", "self", ".", "axes", "[", "0", "]", "=", "self", ".", "axes", "[", "0", "]", ".", "delete", "(", "loc", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SingleBlockManager.concat
Concatenate a list of SingleBlockManagers into a single SingleBlockManager. Used for pd.concat of Series objects with axis=0. Parameters ---------- to_concat : list of SingleBlockManagers new_axis : Index of the result Returns ------- SingleBlockManager
pandas/core/internals/managers.py
def concat(self, to_concat, new_axis): """ Concatenate a list of SingleBlockManagers into a single SingleBlockManager. Used for pd.concat of Series objects with axis=0. Parameters ---------- to_concat : list of SingleBlockManagers new_axis : Index of the result Returns ------- SingleBlockManager """ non_empties = [x for x in to_concat if len(x) > 0] # check if all series are of the same block type: if len(non_empties) > 0: blocks = [obj.blocks[0] for obj in non_empties] if len({b.dtype for b in blocks}) == 1: new_block = blocks[0].concat_same_type(blocks) else: values = [x.values for x in blocks] values = _concat._concat_compat(values) new_block = make_block( values, placement=slice(0, len(values), 1)) else: values = [x._block.values for x in to_concat] values = _concat._concat_compat(values) new_block = make_block( values, placement=slice(0, len(values), 1)) mgr = SingleBlockManager(new_block, new_axis) return mgr
def concat(self, to_concat, new_axis): """ Concatenate a list of SingleBlockManagers into a single SingleBlockManager. Used for pd.concat of Series objects with axis=0. Parameters ---------- to_concat : list of SingleBlockManagers new_axis : Index of the result Returns ------- SingleBlockManager """ non_empties = [x for x in to_concat if len(x) > 0] # check if all series are of the same block type: if len(non_empties) > 0: blocks = [obj.blocks[0] for obj in non_empties] if len({b.dtype for b in blocks}) == 1: new_block = blocks[0].concat_same_type(blocks) else: values = [x.values for x in blocks] values = _concat._concat_compat(values) new_block = make_block( values, placement=slice(0, len(values), 1)) else: values = [x._block.values for x in to_concat] values = _concat._concat_compat(values) new_block = make_block( values, placement=slice(0, len(values), 1)) mgr = SingleBlockManager(new_block, new_axis) return mgr
[ "Concatenate", "a", "list", "of", "SingleBlockManagers", "into", "a", "single", "SingleBlockManager", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/internals/managers.py#L1594-L1630
[ "def", "concat", "(", "self", ",", "to_concat", ",", "new_axis", ")", ":", "non_empties", "=", "[", "x", "for", "x", "in", "to_concat", "if", "len", "(", "x", ")", ">", "0", "]", "# check if all series are of the same block type:", "if", "len", "(", "non_empties", ")", ">", "0", ":", "blocks", "=", "[", "obj", ".", "blocks", "[", "0", "]", "for", "obj", "in", "non_empties", "]", "if", "len", "(", "{", "b", ".", "dtype", "for", "b", "in", "blocks", "}", ")", "==", "1", ":", "new_block", "=", "blocks", "[", "0", "]", ".", "concat_same_type", "(", "blocks", ")", "else", ":", "values", "=", "[", "x", ".", "values", "for", "x", "in", "blocks", "]", "values", "=", "_concat", ".", "_concat_compat", "(", "values", ")", "new_block", "=", "make_block", "(", "values", ",", "placement", "=", "slice", "(", "0", ",", "len", "(", "values", ")", ",", "1", ")", ")", "else", ":", "values", "=", "[", "x", ".", "_block", ".", "values", "for", "x", "in", "to_concat", "]", "values", "=", "_concat", ".", "_concat_compat", "(", "values", ")", "new_block", "=", "make_block", "(", "values", ",", "placement", "=", "slice", "(", "0", ",", "len", "(", "values", ")", ",", "1", ")", ")", "mgr", "=", "SingleBlockManager", "(", "new_block", ",", "new_axis", ")", "return", "mgr" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SparseSeries.from_array
Construct SparseSeries from array. .. deprecated:: 0.23.0 Use the pd.SparseSeries(..) constructor instead.
pandas/core/sparse/series.py
def from_array(cls, arr, index=None, name=None, copy=False, fill_value=None, fastpath=False): """Construct SparseSeries from array. .. deprecated:: 0.23.0 Use the pd.SparseSeries(..) constructor instead. """ warnings.warn("'from_array' is deprecated and will be removed in a " "future version. Please use the pd.SparseSeries(..) " "constructor instead.", FutureWarning, stacklevel=2) return cls(arr, index=index, name=name, copy=copy, fill_value=fill_value, fastpath=fastpath)
def from_array(cls, arr, index=None, name=None, copy=False, fill_value=None, fastpath=False): """Construct SparseSeries from array. .. deprecated:: 0.23.0 Use the pd.SparseSeries(..) constructor instead. """ warnings.warn("'from_array' is deprecated and will be removed in a " "future version. Please use the pd.SparseSeries(..) " "constructor instead.", FutureWarning, stacklevel=2) return cls(arr, index=index, name=name, copy=copy, fill_value=fill_value, fastpath=fastpath)
[ "Construct", "SparseSeries", "from", "array", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/sparse/series.py#L181-L192
[ "def", "from_array", "(", "cls", ",", "arr", ",", "index", "=", "None", ",", "name", "=", "None", ",", "copy", "=", "False", ",", "fill_value", "=", "None", ",", "fastpath", "=", "False", ")", ":", "warnings", ".", "warn", "(", "\"'from_array' is deprecated and will be removed in a \"", "\"future version. Please use the pd.SparseSeries(..) \"", "\"constructor instead.\"", ",", "FutureWarning", ",", "stacklevel", "=", "2", ")", "return", "cls", "(", "arr", ",", "index", "=", "index", ",", "name", "=", "name", ",", "copy", "=", "copy", ",", "fill_value", "=", "fill_value", ",", "fastpath", "=", "fastpath", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SparseSeries.as_sparse_array
return my self as a sparse array, do not copy by default
pandas/core/sparse/series.py
def as_sparse_array(self, kind=None, fill_value=None, copy=False): """ return my self as a sparse array, do not copy by default """ if fill_value is None: fill_value = self.fill_value if kind is None: kind = self.kind return SparseArray(self.values, sparse_index=self.sp_index, fill_value=fill_value, kind=kind, copy=copy)
def as_sparse_array(self, kind=None, fill_value=None, copy=False): """ return my self as a sparse array, do not copy by default """ if fill_value is None: fill_value = self.fill_value if kind is None: kind = self.kind return SparseArray(self.values, sparse_index=self.sp_index, fill_value=fill_value, kind=kind, copy=copy)
[ "return", "my", "self", "as", "a", "sparse", "array", "do", "not", "copy", "by", "default" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/sparse/series.py#L210-L218
[ "def", "as_sparse_array", "(", "self", ",", "kind", "=", "None", ",", "fill_value", "=", "None", ",", "copy", "=", "False", ")", ":", "if", "fill_value", "is", "None", ":", "fill_value", "=", "self", ".", "fill_value", "if", "kind", "is", "None", ":", "kind", "=", "self", ".", "kind", "return", "SparseArray", "(", "self", ".", "values", ",", "sparse_index", "=", "self", ".", "sp_index", ",", "fill_value", "=", "fill_value", ",", "kind", "=", "kind", ",", "copy", "=", "copy", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SparseSeries._reduce
perform a reduction operation
pandas/core/sparse/series.py
def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None, filter_type=None, **kwds): """ perform a reduction operation """ return op(self.get_values(), skipna=skipna, **kwds)
def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None, filter_type=None, **kwds): """ perform a reduction operation """ return op(self.get_values(), skipna=skipna, **kwds)
[ "perform", "a", "reduction", "operation" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/sparse/series.py#L227-L230
[ "def", "_reduce", "(", "self", ",", "op", ",", "name", ",", "axis", "=", "0", ",", "skipna", "=", "True", ",", "numeric_only", "=", "None", ",", "filter_type", "=", "None", ",", "*", "*", "kwds", ")", ":", "return", "op", "(", "self", ".", "get_values", "(", ")", ",", "skipna", "=", "skipna", ",", "*", "*", "kwds", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SparseSeries._ixs
Return the i-th value or values in the SparseSeries by location Parameters ---------- i : int, slice, or sequence of integers Returns ------- value : scalar (int) or Series (slice, sequence)
pandas/core/sparse/series.py
def _ixs(self, i, axis=0): """ Return the i-th value or values in the SparseSeries by location Parameters ---------- i : int, slice, or sequence of integers Returns ------- value : scalar (int) or Series (slice, sequence) """ label = self.index[i] if isinstance(label, Index): return self.take(i, axis=axis) else: return self._get_val_at(i)
def _ixs(self, i, axis=0): """ Return the i-th value or values in the SparseSeries by location Parameters ---------- i : int, slice, or sequence of integers Returns ------- value : scalar (int) or Series (slice, sequence) """ label = self.index[i] if isinstance(label, Index): return self.take(i, axis=axis) else: return self._get_val_at(i)
[ "Return", "the", "i", "-", "th", "value", "or", "values", "in", "the", "SparseSeries", "by", "location" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/sparse/series.py#L268-L284
[ "def", "_ixs", "(", "self", ",", "i", ",", "axis", "=", "0", ")", ":", "label", "=", "self", ".", "index", "[", "i", "]", "if", "isinstance", "(", "label", ",", "Index", ")", ":", "return", "self", ".", "take", "(", "i", ",", "axis", "=", "axis", ")", "else", ":", "return", "self", ".", "_get_val_at", "(", "i", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SparseSeries.abs
Return an object with absolute value taken. Only applicable to objects that are all numeric Returns ------- abs: same type as caller
pandas/core/sparse/series.py
def abs(self): """ Return an object with absolute value taken. Only applicable to objects that are all numeric Returns ------- abs: same type as caller """ return self._constructor(np.abs(self.values), index=self.index).__finalize__(self)
def abs(self): """ Return an object with absolute value taken. Only applicable to objects that are all numeric Returns ------- abs: same type as caller """ return self._constructor(np.abs(self.values), index=self.index).__finalize__(self)
[ "Return", "an", "object", "with", "absolute", "value", "taken", ".", "Only", "applicable", "to", "objects", "that", "are", "all", "numeric" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/sparse/series.py#L308-L318
[ "def", "abs", "(", "self", ")", ":", "return", "self", ".", "_constructor", "(", "np", ".", "abs", "(", "self", ".", "values", ")", ",", "index", "=", "self", ".", "index", ")", ".", "__finalize__", "(", "self", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SparseSeries.get
Returns value occupying requested label, default to specified missing value if not present. Analogous to dict.get Parameters ---------- label : object Label value looking for default : object, optional Value to return if label not in index Returns ------- y : scalar
pandas/core/sparse/series.py
def get(self, label, default=None): """ Returns value occupying requested label, default to specified missing value if not present. Analogous to dict.get Parameters ---------- label : object Label value looking for default : object, optional Value to return if label not in index Returns ------- y : scalar """ if label in self.index: loc = self.index.get_loc(label) return self._get_val_at(loc) else: return default
def get(self, label, default=None): """ Returns value occupying requested label, default to specified missing value if not present. Analogous to dict.get Parameters ---------- label : object Label value looking for default : object, optional Value to return if label not in index Returns ------- y : scalar """ if label in self.index: loc = self.index.get_loc(label) return self._get_val_at(loc) else: return default
[ "Returns", "value", "occupying", "requested", "label", "default", "to", "specified", "missing", "value", "if", "not", "present", ".", "Analogous", "to", "dict", ".", "get" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/sparse/series.py#L320-L340
[ "def", "get", "(", "self", ",", "label", ",", "default", "=", "None", ")", ":", "if", "label", "in", "self", ".", "index", ":", "loc", "=", "self", ".", "index", ".", "get_loc", "(", "label", ")", "return", "self", ".", "_get_val_at", "(", "loc", ")", "else", ":", "return", "default" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SparseSeries.get_value
Retrieve single value at passed index label .. deprecated:: 0.21.0 Please use .at[] or .iat[] accessors. Parameters ---------- index : label takeable : interpret the index as indexers, default False Returns ------- value : scalar value
pandas/core/sparse/series.py
def get_value(self, label, takeable=False): """ Retrieve single value at passed index label .. deprecated:: 0.21.0 Please use .at[] or .iat[] accessors. Parameters ---------- index : label takeable : interpret the index as indexers, default False Returns ------- value : scalar value """ warnings.warn("get_value is deprecated and will be removed " "in a future release. Please use " ".at[] or .iat[] accessors instead", FutureWarning, stacklevel=2) return self._get_value(label, takeable=takeable)
def get_value(self, label, takeable=False): """ Retrieve single value at passed index label .. deprecated:: 0.21.0 Please use .at[] or .iat[] accessors. Parameters ---------- index : label takeable : interpret the index as indexers, default False Returns ------- value : scalar value """ warnings.warn("get_value is deprecated and will be removed " "in a future release. Please use " ".at[] or .iat[] accessors instead", FutureWarning, stacklevel=2) return self._get_value(label, takeable=takeable)
[ "Retrieve", "single", "value", "at", "passed", "index", "label" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/sparse/series.py#L342-L364
[ "def", "get_value", "(", "self", ",", "label", ",", "takeable", "=", "False", ")", ":", "warnings", ".", "warn", "(", "\"get_value is deprecated and will be removed \"", "\"in a future release. Please use \"", "\".at[] or .iat[] accessors instead\"", ",", "FutureWarning", ",", "stacklevel", "=", "2", ")", "return", "self", ".", "_get_value", "(", "label", ",", "takeable", "=", "takeable", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SparseSeries.set_value
Quickly set single value at passed label. If label is not contained, a new object is created with the label placed at the end of the result index .. deprecated:: 0.21.0 Please use .at[] or .iat[] accessors. Parameters ---------- label : object Partial indexing with MultiIndex not allowed value : object Scalar value takeable : interpret the index as indexers, default False Notes ----- This method *always* returns a new object. It is not particularly efficient but is provided for API compatibility with Series Returns ------- series : SparseSeries
pandas/core/sparse/series.py
def set_value(self, label, value, takeable=False): """ Quickly set single value at passed label. If label is not contained, a new object is created with the label placed at the end of the result index .. deprecated:: 0.21.0 Please use .at[] or .iat[] accessors. Parameters ---------- label : object Partial indexing with MultiIndex not allowed value : object Scalar value takeable : interpret the index as indexers, default False Notes ----- This method *always* returns a new object. It is not particularly efficient but is provided for API compatibility with Series Returns ------- series : SparseSeries """ warnings.warn("set_value is deprecated and will be removed " "in a future release. Please use " ".at[] or .iat[] accessors instead", FutureWarning, stacklevel=2) return self._set_value(label, value, takeable=takeable)
def set_value(self, label, value, takeable=False): """ Quickly set single value at passed label. If label is not contained, a new object is created with the label placed at the end of the result index .. deprecated:: 0.21.0 Please use .at[] or .iat[] accessors. Parameters ---------- label : object Partial indexing with MultiIndex not allowed value : object Scalar value takeable : interpret the index as indexers, default False Notes ----- This method *always* returns a new object. It is not particularly efficient but is provided for API compatibility with Series Returns ------- series : SparseSeries """ warnings.warn("set_value is deprecated and will be removed " "in a future release. Please use " ".at[] or .iat[] accessors instead", FutureWarning, stacklevel=2) return self._set_value(label, value, takeable=takeable)
[ "Quickly", "set", "single", "value", "at", "passed", "label", ".", "If", "label", "is", "not", "contained", "a", "new", "object", "is", "created", "with", "the", "label", "placed", "at", "the", "end", "of", "the", "result", "index" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/sparse/series.py#L371-L402
[ "def", "set_value", "(", "self", ",", "label", ",", "value", ",", "takeable", "=", "False", ")", ":", "warnings", ".", "warn", "(", "\"set_value is deprecated and will be removed \"", "\"in a future release. Please use \"", "\".at[] or .iat[] accessors instead\"", ",", "FutureWarning", ",", "stacklevel", "=", "2", ")", "return", "self", ".", "_set_value", "(", "label", ",", "value", ",", "takeable", "=", "takeable", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SparseSeries.to_dense
Convert SparseSeries to a Series. Returns ------- s : Series
pandas/core/sparse/series.py
def to_dense(self): """ Convert SparseSeries to a Series. Returns ------- s : Series """ return Series(self.values.to_dense(), index=self.index, name=self.name)
def to_dense(self): """ Convert SparseSeries to a Series. Returns ------- s : Series """ return Series(self.values.to_dense(), index=self.index, name=self.name)
[ "Convert", "SparseSeries", "to", "a", "Series", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/sparse/series.py#L434-L443
[ "def", "to_dense", "(", "self", ")", ":", "return", "Series", "(", "self", ".", "values", ".", "to_dense", "(", ")", ",", "index", "=", "self", ".", "index", ",", "name", "=", "self", ".", "name", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SparseSeries.copy
Make a copy of the SparseSeries. Only the actual sparse values need to be copied
pandas/core/sparse/series.py
def copy(self, deep=True): """ Make a copy of the SparseSeries. Only the actual sparse values need to be copied """ # TODO: https://github.com/pandas-dev/pandas/issues/22314 # We skip the block manager till that is resolved. new_data = self.values.copy(deep=deep) return self._constructor(new_data, sparse_index=self.sp_index, fill_value=self.fill_value, index=self.index.copy(), name=self.name).__finalize__(self)
def copy(self, deep=True): """ Make a copy of the SparseSeries. Only the actual sparse values need to be copied """ # TODO: https://github.com/pandas-dev/pandas/issues/22314 # We skip the block manager till that is resolved. new_data = self.values.copy(deep=deep) return self._constructor(new_data, sparse_index=self.sp_index, fill_value=self.fill_value, index=self.index.copy(), name=self.name).__finalize__(self)
[ "Make", "a", "copy", "of", "the", "SparseSeries", ".", "Only", "the", "actual", "sparse", "values", "need", "to", "be", "copied" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/sparse/series.py#L449-L460
[ "def", "copy", "(", "self", ",", "deep", "=", "True", ")", ":", "# TODO: https://github.com/pandas-dev/pandas/issues/22314", "# We skip the block manager till that is resolved.", "new_data", "=", "self", ".", "values", ".", "copy", "(", "deep", "=", "deep", ")", "return", "self", ".", "_constructor", "(", "new_data", ",", "sparse_index", "=", "self", ".", "sp_index", ",", "fill_value", "=", "self", ".", "fill_value", ",", "index", "=", "self", ".", "index", ".", "copy", "(", ")", ",", "name", "=", "self", ".", "name", ")", ".", "__finalize__", "(", "self", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SparseSeries.sparse_reindex
Conform sparse values to new SparseIndex Parameters ---------- new_index : {BlockIndex, IntIndex} Returns ------- reindexed : SparseSeries
pandas/core/sparse/series.py
def sparse_reindex(self, new_index): """ Conform sparse values to new SparseIndex Parameters ---------- new_index : {BlockIndex, IntIndex} Returns ------- reindexed : SparseSeries """ if not isinstance(new_index, splib.SparseIndex): raise TypeError("new index must be a SparseIndex") values = self.values values = values.sp_index.to_int_index().reindex( values.sp_values.astype('float64'), values.fill_value, new_index) values = SparseArray(values, sparse_index=new_index, fill_value=self.values.fill_value) return self._constructor(values, index=self.index).__finalize__(self)
def sparse_reindex(self, new_index): """ Conform sparse values to new SparseIndex Parameters ---------- new_index : {BlockIndex, IntIndex} Returns ------- reindexed : SparseSeries """ if not isinstance(new_index, splib.SparseIndex): raise TypeError("new index must be a SparseIndex") values = self.values values = values.sp_index.to_int_index().reindex( values.sp_values.astype('float64'), values.fill_value, new_index) values = SparseArray(values, sparse_index=new_index, fill_value=self.values.fill_value) return self._constructor(values, index=self.index).__finalize__(self)
[ "Conform", "sparse", "values", "to", "new", "SparseIndex" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/sparse/series.py#L470-L490
[ "def", "sparse_reindex", "(", "self", ",", "new_index", ")", ":", "if", "not", "isinstance", "(", "new_index", ",", "splib", ".", "SparseIndex", ")", ":", "raise", "TypeError", "(", "\"new index must be a SparseIndex\"", ")", "values", "=", "self", ".", "values", "values", "=", "values", ".", "sp_index", ".", "to_int_index", "(", ")", ".", "reindex", "(", "values", ".", "sp_values", ".", "astype", "(", "'float64'", ")", ",", "values", ".", "fill_value", ",", "new_index", ")", "values", "=", "SparseArray", "(", "values", ",", "sparse_index", "=", "new_index", ",", "fill_value", "=", "self", ".", "values", ".", "fill_value", ")", "return", "self", ".", "_constructor", "(", "values", ",", "index", "=", "self", ".", "index", ")", ".", "__finalize__", "(", "self", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SparseSeries.cumsum
Cumulative sum of non-NA/null values. When performing the cumulative summation, any non-NA/null values will be skipped. The resulting SparseSeries will preserve the locations of NaN values, but the fill value will be `np.nan` regardless. Parameters ---------- axis : {0} Returns ------- cumsum : SparseSeries
pandas/core/sparse/series.py
def cumsum(self, axis=0, *args, **kwargs): """ Cumulative sum of non-NA/null values. When performing the cumulative summation, any non-NA/null values will be skipped. The resulting SparseSeries will preserve the locations of NaN values, but the fill value will be `np.nan` regardless. Parameters ---------- axis : {0} Returns ------- cumsum : SparseSeries """ nv.validate_cumsum(args, kwargs) # Validate axis if axis is not None: self._get_axis_number(axis) new_array = self.values.cumsum() return self._constructor( new_array, index=self.index, sparse_index=new_array.sp_index).__finalize__(self)
def cumsum(self, axis=0, *args, **kwargs): """ Cumulative sum of non-NA/null values. When performing the cumulative summation, any non-NA/null values will be skipped. The resulting SparseSeries will preserve the locations of NaN values, but the fill value will be `np.nan` regardless. Parameters ---------- axis : {0} Returns ------- cumsum : SparseSeries """ nv.validate_cumsum(args, kwargs) # Validate axis if axis is not None: self._get_axis_number(axis) new_array = self.values.cumsum() return self._constructor( new_array, index=self.index, sparse_index=new_array.sp_index).__finalize__(self)
[ "Cumulative", "sum", "of", "non", "-", "NA", "/", "null", "values", "." ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/sparse/series.py#L492-L517
[ "def", "cumsum", "(", "self", ",", "axis", "=", "0", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "nv", ".", "validate_cumsum", "(", "args", ",", "kwargs", ")", "# Validate axis", "if", "axis", "is", "not", "None", ":", "self", ".", "_get_axis_number", "(", "axis", ")", "new_array", "=", "self", ".", "values", ".", "cumsum", "(", ")", "return", "self", ".", "_constructor", "(", "new_array", ",", "index", "=", "self", ".", "index", ",", "sparse_index", "=", "new_array", ".", "sp_index", ")", ".", "__finalize__", "(", "self", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SparseSeries.dropna
Analogous to Series.dropna. If fill_value=NaN, returns a dense Series
pandas/core/sparse/series.py
def dropna(self, axis=0, inplace=False, **kwargs): """ Analogous to Series.dropna. If fill_value=NaN, returns a dense Series """ # TODO: make more efficient # Validate axis self._get_axis_number(axis or 0) dense_valid = self.to_dense().dropna() if inplace: raise NotImplementedError("Cannot perform inplace dropna" " operations on a SparseSeries") if isna(self.fill_value): return dense_valid else: dense_valid = dense_valid[dense_valid != self.fill_value] return dense_valid.to_sparse(fill_value=self.fill_value)
def dropna(self, axis=0, inplace=False, **kwargs): """ Analogous to Series.dropna. If fill_value=NaN, returns a dense Series """ # TODO: make more efficient # Validate axis self._get_axis_number(axis or 0) dense_valid = self.to_dense().dropna() if inplace: raise NotImplementedError("Cannot perform inplace dropna" " operations on a SparseSeries") if isna(self.fill_value): return dense_valid else: dense_valid = dense_valid[dense_valid != self.fill_value] return dense_valid.to_sparse(fill_value=self.fill_value)
[ "Analogous", "to", "Series", ".", "dropna", ".", "If", "fill_value", "=", "NaN", "returns", "a", "dense", "Series" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/sparse/series.py#L537-L552
[ "def", "dropna", "(", "self", ",", "axis", "=", "0", ",", "inplace", "=", "False", ",", "*", "*", "kwargs", ")", ":", "# TODO: make more efficient", "# Validate axis", "self", ".", "_get_axis_number", "(", "axis", "or", "0", ")", "dense_valid", "=", "self", ".", "to_dense", "(", ")", ".", "dropna", "(", ")", "if", "inplace", ":", "raise", "NotImplementedError", "(", "\"Cannot perform inplace dropna\"", "\" operations on a SparseSeries\"", ")", "if", "isna", "(", "self", ".", "fill_value", ")", ":", "return", "dense_valid", "else", ":", "dense_valid", "=", "dense_valid", "[", "dense_valid", "!=", "self", ".", "fill_value", "]", "return", "dense_valid", ".", "to_sparse", "(", "fill_value", "=", "self", ".", "fill_value", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
SparseSeries.combine_first
Combine Series values, choosing the calling Series's values first. Result index will be the union of the two indexes Parameters ---------- other : Series Returns ------- y : Series
pandas/core/sparse/series.py
def combine_first(self, other): """ Combine Series values, choosing the calling Series's values first. Result index will be the union of the two indexes Parameters ---------- other : Series Returns ------- y : Series """ if isinstance(other, SparseSeries): other = other.to_dense() dense_combined = self.to_dense().combine_first(other) return dense_combined.to_sparse(fill_value=self.fill_value)
def combine_first(self, other): """ Combine Series values, choosing the calling Series's values first. Result index will be the union of the two indexes Parameters ---------- other : Series Returns ------- y : Series """ if isinstance(other, SparseSeries): other = other.to_dense() dense_combined = self.to_dense().combine_first(other) return dense_combined.to_sparse(fill_value=self.fill_value)
[ "Combine", "Series", "values", "choosing", "the", "calling", "Series", "s", "values", "first", ".", "Result", "index", "will", "be", "the", "union", "of", "the", "two", "indexes" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/sparse/series.py#L554-L571
[ "def", "combine_first", "(", "self", ",", "other", ")", ":", "if", "isinstance", "(", "other", ",", "SparseSeries", ")", ":", "other", "=", "other", ".", "to_dense", "(", ")", "dense_combined", "=", "self", ".", "to_dense", "(", ")", ".", "combine_first", "(", "other", ")", "return", "dense_combined", ".", "to_sparse", "(", "fill_value", "=", "self", ".", "fill_value", ")" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_maybe_cache
Create a cache of unique dates from an array of dates Parameters ---------- arg : integer, float, string, datetime, list, tuple, 1-d array, Series format : string Strftime format to parse time cache : boolean True attempts to create a cache of converted values convert_listlike : function Conversion function to apply on dates Returns ------- cache_array : Series Cache of converted, unique dates. Can be empty
pandas/core/tools/datetimes.py
def _maybe_cache(arg, format, cache, convert_listlike): """ Create a cache of unique dates from an array of dates Parameters ---------- arg : integer, float, string, datetime, list, tuple, 1-d array, Series format : string Strftime format to parse time cache : boolean True attempts to create a cache of converted values convert_listlike : function Conversion function to apply on dates Returns ------- cache_array : Series Cache of converted, unique dates. Can be empty """ from pandas import Series cache_array = Series() if cache: # Perform a quicker unique check from pandas import Index unique_dates = Index(arg).unique() if len(unique_dates) < len(arg): cache_dates = convert_listlike(unique_dates.to_numpy(), True, format) cache_array = Series(cache_dates, index=unique_dates) return cache_array
def _maybe_cache(arg, format, cache, convert_listlike): """ Create a cache of unique dates from an array of dates Parameters ---------- arg : integer, float, string, datetime, list, tuple, 1-d array, Series format : string Strftime format to parse time cache : boolean True attempts to create a cache of converted values convert_listlike : function Conversion function to apply on dates Returns ------- cache_array : Series Cache of converted, unique dates. Can be empty """ from pandas import Series cache_array = Series() if cache: # Perform a quicker unique check from pandas import Index unique_dates = Index(arg).unique() if len(unique_dates) < len(arg): cache_dates = convert_listlike(unique_dates.to_numpy(), True, format) cache_array = Series(cache_dates, index=unique_dates) return cache_array
[ "Create", "a", "cache", "of", "unique", "dates", "from", "an", "array", "of", "dates" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/tools/datetimes.py#L31-L60
[ "def", "_maybe_cache", "(", "arg", ",", "format", ",", "cache", ",", "convert_listlike", ")", ":", "from", "pandas", "import", "Series", "cache_array", "=", "Series", "(", ")", "if", "cache", ":", "# Perform a quicker unique check", "from", "pandas", "import", "Index", "unique_dates", "=", "Index", "(", "arg", ")", ".", "unique", "(", ")", "if", "len", "(", "unique_dates", ")", "<", "len", "(", "arg", ")", ":", "cache_dates", "=", "convert_listlike", "(", "unique_dates", ".", "to_numpy", "(", ")", ",", "True", ",", "format", ")", "cache_array", "=", "Series", "(", "cache_dates", ",", "index", "=", "unique_dates", ")", "return", "cache_array" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037
train
_convert_and_box_cache
Convert array of dates with a cache and box the result Parameters ---------- arg : integer, float, string, datetime, list, tuple, 1-d array, Series cache_array : Series Cache of converted, unique dates box : boolean True boxes result as an Index-like, False returns an ndarray errors : string 'ignore' plus box=True will convert result to Index name : string, default None Name for a DatetimeIndex Returns ------- result : datetime of converted dates Returns: - Index-like if box=True - ndarray if box=False
pandas/core/tools/datetimes.py
def _convert_and_box_cache(arg, cache_array, box, errors, name=None): """ Convert array of dates with a cache and box the result Parameters ---------- arg : integer, float, string, datetime, list, tuple, 1-d array, Series cache_array : Series Cache of converted, unique dates box : boolean True boxes result as an Index-like, False returns an ndarray errors : string 'ignore' plus box=True will convert result to Index name : string, default None Name for a DatetimeIndex Returns ------- result : datetime of converted dates Returns: - Index-like if box=True - ndarray if box=False """ from pandas import Series, DatetimeIndex, Index result = Series(arg).map(cache_array) if box: if errors == 'ignore': return Index(result, name=name) else: return DatetimeIndex(result, name=name) return result.values
def _convert_and_box_cache(arg, cache_array, box, errors, name=None): """ Convert array of dates with a cache and box the result Parameters ---------- arg : integer, float, string, datetime, list, tuple, 1-d array, Series cache_array : Series Cache of converted, unique dates box : boolean True boxes result as an Index-like, False returns an ndarray errors : string 'ignore' plus box=True will convert result to Index name : string, default None Name for a DatetimeIndex Returns ------- result : datetime of converted dates Returns: - Index-like if box=True - ndarray if box=False """ from pandas import Series, DatetimeIndex, Index result = Series(arg).map(cache_array) if box: if errors == 'ignore': return Index(result, name=name) else: return DatetimeIndex(result, name=name) return result.values
[ "Convert", "array", "of", "dates", "with", "a", "cache", "and", "box", "the", "result" ]
pandas-dev/pandas
python
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/tools/datetimes.py#L63-L94
[ "def", "_convert_and_box_cache", "(", "arg", ",", "cache_array", ",", "box", ",", "errors", ",", "name", "=", "None", ")", ":", "from", "pandas", "import", "Series", ",", "DatetimeIndex", ",", "Index", "result", "=", "Series", "(", "arg", ")", ".", "map", "(", "cache_array", ")", "if", "box", ":", "if", "errors", "==", "'ignore'", ":", "return", "Index", "(", "result", ",", "name", "=", "name", ")", "else", ":", "return", "DatetimeIndex", "(", "result", ",", "name", "=", "name", ")", "return", "result", ".", "values" ]
9feb3ad92cc0397a04b665803a49299ee7aa1037