Code
stringlengths
103
85.9k
Summary
sequencelengths
0
94
Please provide a description of the function:def html(self): ret_code = self._sphinx_build('html') zip_fname = os.path.join(BUILD_PATH, 'html', 'pandas.zip') if os.path.exists(zip_fname): os.remove(zip_fname) if self.single_doc_html is not None: self._open_browser(self.single_doc_html) else: self._add_redirects() return ret_code
[ "\n Build HTML documentation.\n " ]
Please provide a description of the function:def latex(self, force=False): if sys.platform == 'win32': sys.stderr.write('latex build has not been tested on windows\n') else: ret_code = self._sphinx_build('latex') os.chdir(os.path.join(BUILD_PATH, 'latex')) if force: for i in range(3): self._run_os('pdflatex', '-interaction=nonstopmode', 'pandas.tex') raise SystemExit('You should check the file ' '"build/latex/pandas.pdf" for problems.') else: self._run_os('make') return ret_code
[ "\n Build PDF documentation.\n " ]
Please provide a description of the function:def clean(): shutil.rmtree(BUILD_PATH, ignore_errors=True) shutil.rmtree(os.path.join(SOURCE_PATH, 'reference', 'api'), ignore_errors=True)
[ "\n Clean documentation generated files.\n " ]
Please provide a description of the function:def zip_html(self): zip_fname = os.path.join(BUILD_PATH, 'html', 'pandas.zip') if os.path.exists(zip_fname): os.remove(zip_fname) dirname = os.path.join(BUILD_PATH, 'html') fnames = os.listdir(dirname) os.chdir(dirname) self._run_os('zip', zip_fname, '-r', '-q', *fnames)
[ "\n Compress HTML documentation into a zip file.\n " ]
Please provide a description of the function:def write_result(self, buf): # string representation of the columns if len(self.frame.columns) == 0 or len(self.frame.index) == 0: info_line = ('Empty {name}\nColumns: {col}\nIndex: {idx}' .format(name=type(self.frame).__name__, col=self.frame.columns, idx=self.frame.index)) strcols = [[info_line]] else: strcols = self.fmt._to_str_columns() def get_col_type(dtype): if issubclass(dtype.type, np.number): return 'r' else: return 'l' # reestablish the MultiIndex that has been joined by _to_str_column if self.fmt.index and isinstance(self.frame.index, ABCMultiIndex): out = self.frame.index.format( adjoin=False, sparsify=self.fmt.sparsify, names=self.fmt.has_index_names, na_rep=self.fmt.na_rep ) # index.format will sparsify repeated entries with empty strings # so pad these with some empty space def pad_empties(x): for pad in reversed(x): if pad: break return [x[0]] + [i if i else ' ' * len(pad) for i in x[1:]] out = (pad_empties(i) for i in out) # Add empty spaces for each column level clevels = self.frame.columns.nlevels out = [[' ' * len(i[-1])] * clevels + i for i in out] # Add the column names to the last index column cnames = self.frame.columns.names if any(cnames): new_names = [i if i else '{}' for i in cnames] out[self.frame.index.nlevels - 1][:clevels] = new_names # Get rid of old multiindex column and add new ones strcols = out + strcols[1:] column_format = self.column_format if column_format is None: dtypes = self.frame.dtypes._values column_format = ''.join(map(get_col_type, dtypes)) if self.fmt.index: index_format = 'l' * self.frame.index.nlevels column_format = index_format + column_format elif not isinstance(column_format, str): # pragma: no cover raise AssertionError('column_format must be str or unicode, ' 'not {typ}'.format(typ=type(column_format))) if not self.longtable: buf.write('\\begin{{tabular}}{{{fmt}}}\n' .format(fmt=column_format)) buf.write('\\toprule\n') else: buf.write('\\begin{{longtable}}{{{fmt}}}\n' .format(fmt=column_format)) buf.write('\\toprule\n') ilevels = self.frame.index.nlevels clevels = self.frame.columns.nlevels nlevels = clevels if self.fmt.has_index_names and self.fmt.show_index_names: nlevels += 1 strrows = list(zip(*strcols)) self.clinebuf = [] for i, row in enumerate(strrows): if i == nlevels and self.fmt.header: buf.write('\\midrule\n') # End of header if self.longtable: buf.write('\\endhead\n') buf.write('\\midrule\n') buf.write('\\multicolumn{{{n}}}{{r}}{{{{Continued on next ' 'page}}}} \\\\\n'.format(n=len(row))) buf.write('\\midrule\n') buf.write('\\endfoot\n\n') buf.write('\\bottomrule\n') buf.write('\\endlastfoot\n') if self.fmt.kwds.get('escape', True): # escape backslashes first crow = [(x.replace('\\', '\\textbackslash ') .replace('_', '\\_') .replace('%', '\\%').replace('$', '\\$') .replace('#', '\\#').replace('{', '\\{') .replace('}', '\\}').replace('~', '\\textasciitilde ') .replace('^', '\\textasciicircum ') .replace('&', '\\&') if (x and x != '{}') else '{}') for x in row] else: crow = [x if x else '{}' for x in row] if self.bold_rows and self.fmt.index: # bold row labels crow = ['\\textbf{{{x}}}'.format(x=x) if j < ilevels and x.strip() not in ['', '{}'] else x for j, x in enumerate(crow)] if i < clevels and self.fmt.header and self.multicolumn: # sum up columns to multicolumns crow = self._format_multicolumn(crow, ilevels) if (i >= nlevels and self.fmt.index and self.multirow and ilevels > 1): # sum up rows to multirows crow = self._format_multirow(crow, ilevels, i, strrows) buf.write(' & '.join(crow)) buf.write(' \\\\\n') if self.multirow and i < len(strrows) - 1: self._print_cline(buf, i, len(strcols)) if not self.longtable: buf.write('\\bottomrule\n') buf.write('\\end{tabular}\n') else: buf.write('\\end{longtable}\n')
[ "\n Render a DataFrame to a LaTeX tabular/longtable environment output.\n " ]
Please provide a description of the function:def _format_multicolumn(self, row, ilevels): r row2 = list(row[:ilevels]) ncol = 1 coltext = '' def append_col(): # write multicolumn if needed if ncol > 1: row2.append('\\multicolumn{{{ncol:d}}}{{{fmt:s}}}{{{txt:s}}}' .format(ncol=ncol, fmt=self.multicolumn_format, txt=coltext.strip())) # don't modify where not needed else: row2.append(coltext) for c in row[ilevels:]: # if next col has text, write the previous if c.strip(): if coltext: append_col() coltext = c ncol = 1 # if not, add it to the previous multicolumn else: ncol += 1 # write last column name if coltext: append_col() return row2
[ "\n Combine columns belonging to a group to a single multicolumn entry\n according to self.multicolumn_format\n\n e.g.:\n a & & & b & c &\n will become\n \\multicolumn{3}{l}{a} & b & \\multicolumn{2}{l}{c}\n " ]
Please provide a description of the function:def _format_multirow(self, row, ilevels, i, rows): r for j in range(ilevels): if row[j].strip(): nrow = 1 for r in rows[i + 1:]: if not r[j].strip(): nrow += 1 else: break if nrow > 1: # overwrite non-multirow entry row[j] = '\\multirow{{{nrow:d}}}{{*}}{{{row:s}}}'.format( nrow=nrow, row=row[j].strip()) # save when to end the current block with \cline self.clinebuf.append([i + nrow - 1, j + 1]) return row
[ "\n Check following rows, whether row should be a multirow\n\n e.g.: becomes:\n a & 0 & \\multirow{2}{*}{a} & 0 &\n & 1 & & 1 &\n b & 0 & \\cline{1-2}\n b & 0 &\n " ]
Please provide a description of the function:def _print_cline(self, buf, i, icol): for cl in self.clinebuf: if cl[0] == i: buf.write('\\cline{{{cl:d}-{icol:d}}}\n' .format(cl=cl[1], icol=icol)) # remove entries that have been written to buffer self.clinebuf = [x for x in self.clinebuf if x[0] != i]
[ "\n Print clines after multirow-blocks are finished\n " ]
Please provide a description of the function:def _validate_integer(name, val, min_val=0): msg = "'{name:s}' must be an integer >={min_val:d}".format(name=name, min_val=min_val) if val is not None: if is_float(val): if int(val) != val: raise ValueError(msg) val = int(val) elif not (is_integer(val) and val >= min_val): raise ValueError(msg) return val
[ "\n Checks whether the 'name' parameter for parsing is either\n an integer OR float that can SAFELY be cast to an integer\n without losing accuracy. Raises a ValueError if that is\n not the case.\n\n Parameters\n ----------\n name : string\n Parameter name (used for error reporting)\n val : int or float\n The value to check\n min_val : int\n Minimum allowed value (val < min_val will result in a ValueError)\n " ]
Please provide a description of the function:def _validate_names(names): if names is not None: if len(names) != len(set(names)): msg = ("Duplicate names specified. This " "will raise an error in the future.") warnings.warn(msg, UserWarning, stacklevel=3) return names
[ "\n Check if the `names` parameter contains duplicates.\n\n If duplicates are found, we issue a warning before returning.\n\n Parameters\n ----------\n names : array-like or None\n An array containing a list of the names used for the output DataFrame.\n\n Returns\n -------\n names : array-like or None\n The original `names` parameter.\n " ]
Please provide a description of the function:def _read(filepath_or_buffer: FilePathOrBuffer, kwds): encoding = kwds.get('encoding', None) if encoding is not None: encoding = re.sub('_', '-', encoding).lower() kwds['encoding'] = encoding compression = kwds.get('compression', 'infer') compression = _infer_compression(filepath_or_buffer, compression) # TODO: get_filepath_or_buffer could return # Union[FilePathOrBuffer, s3fs.S3File, gcsfs.GCSFile] # though mypy handling of conditional imports is difficult. # See https://github.com/python/mypy/issues/1297 fp_or_buf, _, compression, should_close = get_filepath_or_buffer( filepath_or_buffer, encoding, compression) kwds['compression'] = compression if kwds.get('date_parser', None) is not None: if isinstance(kwds['parse_dates'], bool): kwds['parse_dates'] = True # Extract some of the arguments (pass chunksize on). iterator = kwds.get('iterator', False) chunksize = _validate_integer('chunksize', kwds.get('chunksize', None), 1) nrows = kwds.get('nrows', None) # Check for duplicates in names. _validate_names(kwds.get("names", None)) # Create the parser. parser = TextFileReader(fp_or_buf, **kwds) if chunksize or iterator: return parser try: data = parser.read(nrows) finally: parser.close() if should_close: try: fp_or_buf.close() except ValueError: pass return data
[ "Generic reader of line files." ]
Please provide a description of the function:def read_fwf(filepath_or_buffer: FilePathOrBuffer, colspecs='infer', widths=None, infer_nrows=100, **kwds): r # Check input arguments. if colspecs is None and widths is None: raise ValueError("Must specify either colspecs or widths") elif colspecs not in (None, 'infer') and widths is not None: raise ValueError("You must specify only one of 'widths' and " "'colspecs'") # Compute 'colspecs' from 'widths', if specified. if widths is not None: colspecs, col = [], 0 for w in widths: colspecs.append((col, col + w)) col += w kwds['colspecs'] = colspecs kwds['infer_nrows'] = infer_nrows kwds['engine'] = 'python-fwf' return _read(filepath_or_buffer, kwds)
[ "\n Read a table of fixed-width formatted lines into DataFrame.\n\n Also supports optionally iterating or breaking of the file\n into chunks.\n\n Additional help can be found in the `online docs for IO Tools\n <http://pandas.pydata.org/pandas-docs/stable/io.html>`_.\n\n Parameters\n ----------\n filepath_or_buffer : str, path object, or file-like object\n Any valid string path is acceptable. The string could be a URL. Valid\n URL schemes include http, ftp, s3, and file. For file URLs, a host is\n expected. A local file could be: file://localhost/path/to/table.csv.\n\n If you want to pass in a path object, pandas accepts either\n ``pathlib.Path`` or ``py._path.local.LocalPath``.\n\n By file-like object, we refer to objects with a ``read()`` method,\n such as a file handler (e.g. via builtin ``open`` function)\n or ``StringIO``.\n colspecs : list of tuple (int, int) or 'infer'. optional\n A list of tuples giving the extents of the fixed-width\n fields of each line as half-open intervals (i.e., [from, to[ ).\n String value 'infer' can be used to instruct the parser to try\n detecting the column specifications from the first 100 rows of\n the data which are not being skipped via skiprows (default='infer').\n widths : list of int, optional\n A list of field widths which can be used instead of 'colspecs' if\n the intervals are contiguous.\n infer_nrows : int, default 100\n The number of rows to consider when letting the parser determine the\n `colspecs`.\n\n .. versionadded:: 0.24.0\n **kwds : optional\n Optional keyword arguments can be passed to ``TextFileReader``.\n\n Returns\n -------\n DataFrame or TextParser\n A comma-separated values (csv) file is returned as two-dimensional\n data structure with labeled axes.\n\n See Also\n --------\n to_csv : Write DataFrame to a comma-separated values (csv) file.\n read_csv : Read a comma-separated values (csv) file into DataFrame.\n\n Examples\n --------\n >>> pd.read_fwf('data.csv') # doctest: +SKIP\n " ]
Please provide a description of the function:def _is_potential_multi_index(columns): return (len(columns) and not isinstance(columns, MultiIndex) and all(isinstance(c, tuple) for c in columns))
[ "\n Check whether or not the `columns` parameter\n could be converted into a MultiIndex.\n\n Parameters\n ----------\n columns : array-like\n Object which may or may not be convertible into a MultiIndex\n\n Returns\n -------\n boolean : Whether or not columns could become a MultiIndex\n " ]
Please provide a description of the function:def _evaluate_usecols(usecols, names): if callable(usecols): return {i for i, name in enumerate(names) if usecols(name)} return usecols
[ "\n Check whether or not the 'usecols' parameter\n is a callable. If so, enumerates the 'names'\n parameter and returns a set of indices for\n each entry in 'names' that evaluates to True.\n If not a callable, returns 'usecols'.\n " ]
Please provide a description of the function:def _validate_usecols_names(usecols, names): missing = [c for c in usecols if c not in names] if len(missing) > 0: raise ValueError( "Usecols do not match columns, " "columns expected but not found: {missing}".format(missing=missing) ) return usecols
[ "\n Validates that all usecols are present in a given\n list of names. If not, raise a ValueError that\n shows what usecols are missing.\n\n Parameters\n ----------\n usecols : iterable of usecols\n The columns to validate are present in names.\n names : iterable of names\n The column names to check against.\n\n Returns\n -------\n usecols : iterable of usecols\n The `usecols` parameter if the validation succeeds.\n\n Raises\n ------\n ValueError : Columns were missing. Error message will list them.\n " ]
Please provide a description of the function:def _validate_usecols_arg(usecols): msg = ("'usecols' must either be list-like of all strings, all unicode, " "all integers or a callable.") if usecols is not None: if callable(usecols): return usecols, None if not is_list_like(usecols): # see gh-20529 # # Ensure it is iterable container but not string. raise ValueError(msg) usecols_dtype = lib.infer_dtype(usecols, skipna=False) if usecols_dtype not in ("empty", "integer", "string", "unicode"): raise ValueError(msg) usecols = set(usecols) return usecols, usecols_dtype return usecols, None
[ "\n Validate the 'usecols' parameter.\n\n Checks whether or not the 'usecols' parameter contains all integers\n (column selection by index), strings (column by name) or is a callable.\n Raises a ValueError if that is not the case.\n\n Parameters\n ----------\n usecols : list-like, callable, or None\n List of columns to use when parsing or a callable that can be used\n to filter a list of table columns.\n\n Returns\n -------\n usecols_tuple : tuple\n A tuple of (verified_usecols, usecols_dtype).\n\n 'verified_usecols' is either a set if an array-like is passed in or\n 'usecols' if a callable or None is passed in.\n\n 'usecols_dtype` is the inferred dtype of 'usecols' if an array-like\n is passed in or None if a callable or None is passed in.\n " ]
Please provide a description of the function:def _validate_parse_dates_arg(parse_dates): msg = ("Only booleans, lists, and " "dictionaries are accepted " "for the 'parse_dates' parameter") if parse_dates is not None: if is_scalar(parse_dates): if not lib.is_bool(parse_dates): raise TypeError(msg) elif not isinstance(parse_dates, (list, dict)): raise TypeError(msg) return parse_dates
[ "\n Check whether or not the 'parse_dates' parameter\n is a non-boolean scalar. Raises a ValueError if\n that is the case.\n " ]
Please provide a description of the function:def _stringify_na_values(na_values): result = [] for x in na_values: result.append(str(x)) result.append(x) try: v = float(x) # we are like 999 here if v == int(v): v = int(v) result.append("{value}.0".format(value=v)) result.append(str(v)) result.append(v) except (TypeError, ValueError, OverflowError): pass try: result.append(int(x)) except (TypeError, ValueError, OverflowError): pass return set(result)
[ " return a stringified and numeric for these values " ]
Please provide a description of the function:def _get_na_values(col, na_values, na_fvalues, keep_default_na): if isinstance(na_values, dict): if col in na_values: return na_values[col], na_fvalues[col] else: if keep_default_na: return _NA_VALUES, set() return set(), set() else: return na_values, na_fvalues
[ "\n Get the NaN values for a given column.\n\n Parameters\n ----------\n col : str\n The name of the column.\n na_values : array-like, dict\n The object listing the NaN values as strings.\n na_fvalues : array-like, dict\n The object listing the NaN values as floats.\n keep_default_na : bool\n If `na_values` is a dict, and the column is not mapped in the\n dictionary, whether to return the default NaN values or the empty set.\n\n Returns\n -------\n nan_tuple : A length-two tuple composed of\n\n 1) na_values : the string NaN values for that column.\n 2) na_fvalues : the float NaN values for that column.\n " ]
Please provide a description of the function:def _extract_multi_indexer_columns(self, header, index_names, col_names, passed_names=False): if len(header) < 2: return header[0], index_names, col_names, passed_names # the names are the tuples of the header that are not the index cols # 0 is the name of the index, assuming index_col is a list of column # numbers ic = self.index_col if ic is None: ic = [] if not isinstance(ic, (list, tuple, np.ndarray)): ic = [ic] sic = set(ic) # clean the index_names index_names = header.pop(-1) index_names, names, index_col = _clean_index_names(index_names, self.index_col, self.unnamed_cols) # extract the columns field_count = len(header[0]) def extract(r): return tuple(r[i] for i in range(field_count) if i not in sic) columns = lzip(*[extract(r) for r in header]) names = ic + columns # If we find unnamed columns all in a single # level, then our header was too long. for n in range(len(columns[0])): if all(compat.to_str(c[n]) in self.unnamed_cols for c in columns): raise ParserError( "Passed header=[{header}] are too many rows for this " "multi_index of columns" .format(header=','.join(str(x) for x in self.header)) ) # Clean the column names (if we have an index_col). if len(ic): col_names = [r[0] if (len(r[0]) and r[0] not in self.unnamed_cols) else None for r in header] else: col_names = [None] * len(header) passed_names = True return names, index_names, col_names, passed_names
[ " extract and return the names, index_names, col_names\n header is a list-of-lists returned from the parsers " ]
Please provide a description of the function:def _infer_types(self, values, na_values, try_num_bool=True): na_count = 0 if issubclass(values.dtype.type, (np.number, np.bool_)): mask = algorithms.isin(values, list(na_values)) na_count = mask.sum() if na_count > 0: if is_integer_dtype(values): values = values.astype(np.float64) np.putmask(values, mask, np.nan) return values, na_count if try_num_bool: try: result = lib.maybe_convert_numeric(values, na_values, False) na_count = isna(result).sum() except Exception: result = values if values.dtype == np.object_: na_count = parsers.sanitize_objects(result, na_values, False) else: result = values if values.dtype == np.object_: na_count = parsers.sanitize_objects(values, na_values, False) if result.dtype == np.object_ and try_num_bool: result = libops.maybe_convert_bool(np.asarray(values), true_values=self.true_values, false_values=self.false_values) return result, na_count
[ "\n Infer types of values, possibly casting\n\n Parameters\n ----------\n values : ndarray\n na_values : set\n try_num_bool : bool, default try\n try to cast values to numeric (first preference) or boolean\n\n Returns:\n --------\n converted : ndarray\n na_count : int\n " ]
Please provide a description of the function:def _cast_types(self, values, cast_type, column): if is_categorical_dtype(cast_type): known_cats = (isinstance(cast_type, CategoricalDtype) and cast_type.categories is not None) if not is_object_dtype(values) and not known_cats: # XXX this is for consistency with # c-parser which parses all categories # as strings values = astype_nansafe(values, str) cats = Index(values).unique().dropna() values = Categorical._from_inferred_categories( cats, cats.get_indexer(values), cast_type, true_values=self.true_values) # use the EA's implementation of casting elif is_extension_array_dtype(cast_type): # ensure cast_type is an actual dtype and not a string cast_type = pandas_dtype(cast_type) array_type = cast_type.construct_array_type() try: return array_type._from_sequence_of_strings(values, dtype=cast_type) except NotImplementedError: raise NotImplementedError( "Extension Array: {ea} must implement " "_from_sequence_of_strings in order " "to be used in parser methods".format(ea=array_type)) else: try: values = astype_nansafe(values, cast_type, copy=True, skipna=True) except ValueError: raise ValueError( "Unable to convert column {column} to type " "{cast_type}".format( column=column, cast_type=cast_type)) return values
[ "\n Cast values to specified type\n\n Parameters\n ----------\n values : ndarray\n cast_type : string or np.dtype\n dtype to cast values to\n column : string\n column name - used only for error reporting\n\n Returns\n -------\n converted : ndarray\n " ]
Please provide a description of the function:def _handle_usecols(self, columns, usecols_key): if self.usecols is not None: if callable(self.usecols): col_indices = _evaluate_usecols(self.usecols, usecols_key) elif any(isinstance(u, str) for u in self.usecols): if len(columns) > 1: raise ValueError("If using multiple headers, usecols must " "be integers.") col_indices = [] for col in self.usecols: if isinstance(col, str): try: col_indices.append(usecols_key.index(col)) except ValueError: _validate_usecols_names(self.usecols, usecols_key) else: col_indices.append(col) else: col_indices = self.usecols columns = [[n for i, n in enumerate(column) if i in col_indices] for column in columns] self._col_indices = col_indices return columns
[ "\n Sets self._col_indices\n\n usecols_key is used if there are string usecols.\n " ]
Please provide a description of the function:def _check_for_bom(self, first_row): # first_row will be a list, so we need to check # that that list is not empty before proceeding. if not first_row: return first_row # The first element of this row is the one that could have the # BOM that we want to remove. Check that the first element is a # string before proceeding. if not isinstance(first_row[0], str): return first_row # Check that the string is not empty, as that would # obviously not have a BOM at the start of it. if not first_row[0]: return first_row # Since the string is non-empty, check that it does # in fact begin with a BOM. first_elt = first_row[0][0] if first_elt != _BOM: return first_row first_row = first_row[0] if len(first_row) > 1 and first_row[1] == self.quotechar: start = 2 quote = first_row[1] end = first_row[2:].index(quote) + 2 # Extract the data between the quotation marks new_row = first_row[start:end] # Extract any remaining data after the second # quotation mark. if len(first_row) > end + 1: new_row += first_row[end + 1:] return [new_row] elif len(first_row) > 1: return [first_row[1:]] else: # First row is just the BOM, so we # return an empty string. return [""]
[ "\n Checks whether the file begins with the BOM character.\n If it does, remove it. In addition, if there is quoting\n in the field subsequent to the BOM, remove it as well\n because it technically takes place at the beginning of\n the name, not the middle of it.\n " ]
Please provide a description of the function:def _alert_malformed(self, msg, row_num): if self.error_bad_lines: raise ParserError(msg) elif self.warn_bad_lines: base = 'Skipping line {row_num}: '.format(row_num=row_num) sys.stderr.write(base + msg + '\n')
[ "\n Alert a user about a malformed row.\n\n If `self.error_bad_lines` is True, the alert will be `ParserError`.\n If `self.warn_bad_lines` is True, the alert will be printed out.\n\n Parameters\n ----------\n msg : The error message to display.\n row_num : The row number where the parsing error occurred.\n Because this row number is displayed, we 1-index,\n even though we 0-index internally.\n " ]
Please provide a description of the function:def _next_iter_line(self, row_num): try: return next(self.data) except csv.Error as e: if self.warn_bad_lines or self.error_bad_lines: msg = str(e) if 'NULL byte' in msg: msg = ('NULL byte detected. This byte ' 'cannot be processed in Python\'s ' 'native csv library at the moment, ' 'so please pass in engine=\'c\' instead') if self.skipfooter > 0: reason = ('Error could possibly be due to ' 'parsing errors in the skipped footer rows ' '(the skipfooter keyword is only applied ' 'after Python\'s csv library has parsed ' 'all rows).') msg += '. ' + reason self._alert_malformed(msg, row_num) return None
[ "\n Wrapper around iterating through `self.data` (CSV source).\n\n When a CSV error is raised, we check for specific\n error messages that allow us to customize the\n error message displayed to the user.\n\n Parameters\n ----------\n row_num : The row number of the line being parsed.\n " ]
Please provide a description of the function:def _remove_empty_lines(self, lines): ret = [] for l in lines: # Remove empty lines and lines with only one whitespace value if (len(l) > 1 or len(l) == 1 and (not isinstance(l[0], str) or l[0].strip())): ret.append(l) return ret
[ "\n Iterate through the lines and remove any that are\n either empty or contain only one whitespace value\n\n Parameters\n ----------\n lines : array-like\n The array of lines that we are to filter.\n\n Returns\n -------\n filtered_lines : array-like\n The same array of lines with the \"empty\" ones removed.\n " ]
Please provide a description of the function:def _get_index_name(self, columns): orig_names = list(columns) columns = list(columns) try: line = self._next_line() except StopIteration: line = None try: next_line = self._next_line() except StopIteration: next_line = None # implicitly index_col=0 b/c 1 fewer column names implicit_first_cols = 0 if line is not None: # leave it 0, #2442 # Case 1 if self.index_col is not False: implicit_first_cols = len(line) - self.num_original_columns # Case 0 if next_line is not None: if len(next_line) == len(line) + self.num_original_columns: # column and index names on diff rows self.index_col = lrange(len(line)) self.buf = self.buf[1:] for c in reversed(line): columns.insert(0, c) # Update list of original names to include all indices. orig_names = list(columns) self.num_original_columns = len(columns) return line, orig_names, columns if implicit_first_cols > 0: # Case 1 self._implicit_index = True if self.index_col is None: self.index_col = lrange(implicit_first_cols) index_name = None else: # Case 2 (index_name, columns_, self.index_col) = _clean_index_names(columns, self.index_col, self.unnamed_cols) return index_name, orig_names, columns
[ "\n Try several cases to get lines:\n\n 0) There are headers on row 0 and row 1 and their\n total summed lengths equals the length of the next line.\n Treat row 0 as columns and row 1 as indices\n 1) Look for implicit index: there are more columns\n on row 1 than row 0. If this is true, assume that row\n 1 lists index columns and row 0 lists normal columns.\n 2) Get index from the columns if it was listed.\n " ]
Please provide a description of the function:def get_rows(self, infer_nrows, skiprows=None): if skiprows is None: skiprows = set() buffer_rows = [] detect_rows = [] for i, row in enumerate(self.f): if i not in skiprows: detect_rows.append(row) buffer_rows.append(row) if len(detect_rows) >= infer_nrows: break self.buffer = iter(buffer_rows) return detect_rows
[ "\n Read rows from self.f, skipping as specified.\n\n We distinguish buffer_rows (the first <= infer_nrows\n lines) from the rows returned to detect_colspecs\n because it's simpler to leave the other locations\n with skiprows logic alone than to modify them to\n deal with the fact we skipped some rows here as\n well.\n\n Parameters\n ----------\n infer_nrows : int\n Number of rows to read from self.f, not counting\n rows that are skipped.\n skiprows: set, optional\n Indices of rows to skip.\n\n Returns\n -------\n detect_rows : list of str\n A list containing the rows to read.\n\n " ]
Please provide a description of the function:def linkcode_resolve(domain, info): if domain != 'py': return None modname = info['module'] fullname = info['fullname'] submod = sys.modules.get(modname) if submod is None: return None obj = submod for part in fullname.split('.'): try: obj = getattr(obj, part) except AttributeError: return None try: # inspect.unwrap() was added in Python version 3.4 if sys.version_info >= (3, 5): fn = inspect.getsourcefile(inspect.unwrap(obj)) else: fn = inspect.getsourcefile(obj) except TypeError: fn = None if not fn: return None try: source, lineno = inspect.getsourcelines(obj) except OSError: lineno = None if lineno: linespec = "#L{:d}-L{:d}".format(lineno, lineno + len(source) - 1) else: linespec = "" fn = os.path.relpath(fn, start=os.path.dirname(pandas.__file__)) if '+' in pandas.__version__: return ("http://github.com/pandas-dev/pandas/blob/master/pandas/" "{}{}".format(fn, linespec)) else: return ("http://github.com/pandas-dev/pandas/blob/" "v{}/pandas/{}{}".format(pandas.__version__, fn, linespec))
[ "\n Determine the URL corresponding to Python object\n " ]
Please provide a description of the function:def process_class_docstrings(app, what, name, obj, options, lines): if what == "class": joined = '\n'.join(lines) templates = [ , ] for template in templates: if template in joined: joined = joined.replace(template, '') lines[:] = joined.split('\n')
[ "\n For those classes for which we use ::\n\n :template: autosummary/class_without_autosummary.rst\n\n the documented attributes/methods have to be listed in the class\n docstring. However, if one of those lists is empty, we use 'None',\n which then generates warnings in sphinx / ugly html output.\n This \"autodoc-process-docstring\" event connector removes that part\n from the processed docstring.\n\n ", ".. rubric:: Attributes\n\n.. autosummary::\n :toctree:\n\n None\n", ".. rubric:: Methods\n\n.. autosummary::\n :toctree:\n\n None\n" ]
Please provide a description of the function:def pack(o, stream, **kwargs): packer = Packer(**kwargs) stream.write(packer.pack(o))
[ "\n Pack object `o` and write it to `stream`\n\n See :class:`Packer` for options.\n " ]
Please provide a description of the function:def get_mgr_concatenation_plan(mgr, indexers): # Calculate post-reindex shape , save for item axis which will be separate # for each block anyway. mgr_shape = list(mgr.shape) for ax, indexer in indexers.items(): mgr_shape[ax] = len(indexer) mgr_shape = tuple(mgr_shape) if 0 in indexers: ax0_indexer = indexers.pop(0) blknos = algos.take_1d(mgr._blknos, ax0_indexer, fill_value=-1) blklocs = algos.take_1d(mgr._blklocs, ax0_indexer, fill_value=-1) else: if mgr._is_single_block: blk = mgr.blocks[0] return [(blk.mgr_locs, JoinUnit(blk, mgr_shape, indexers))] ax0_indexer = None blknos = mgr._blknos blklocs = mgr._blklocs plan = [] for blkno, placements in libinternals.get_blkno_placements(blknos, mgr.nblocks, group=False): assert placements.is_slice_like join_unit_indexers = indexers.copy() shape = list(mgr_shape) shape[0] = len(placements) shape = tuple(shape) if blkno == -1: unit = JoinUnit(None, shape) else: blk = mgr.blocks[blkno] ax0_blk_indexer = blklocs[placements.indexer] unit_no_ax0_reindexing = (len(placements) == len(blk.mgr_locs) and # Fastpath detection of join unit not # needing to reindex its block: no ax0 # reindexing took place and block # placement was sequential before. ((ax0_indexer is None and blk.mgr_locs.is_slice_like and blk.mgr_locs.as_slice.step == 1) or # Slow-ish detection: all indexer locs # are sequential (and length match is # checked above). (np.diff(ax0_blk_indexer) == 1).all())) # Omit indexer if no item reindexing is required. if unit_no_ax0_reindexing: join_unit_indexers.pop(0, None) else: join_unit_indexers[0] = ax0_blk_indexer unit = JoinUnit(blk, shape, join_unit_indexers) plan.append((placements, unit)) return plan
[ "\n Construct concatenation plan for given block manager and indexers.\n\n Parameters\n ----------\n mgr : BlockManager\n indexers : dict of {axis: indexer}\n\n Returns\n -------\n plan : list of (BlockPlacement, JoinUnit) tuples\n\n " ]
Please provide a description of the function:def concatenate_join_units(join_units, concat_axis, copy): if concat_axis == 0 and len(join_units) > 1: # Concatenating join units along ax0 is handled in _merge_blocks. raise AssertionError("Concatenating join units along axis0") empty_dtype, upcasted_na = get_empty_dtype_and_na(join_units) to_concat = [ju.get_reindexed_values(empty_dtype=empty_dtype, upcasted_na=upcasted_na) for ju in join_units] if len(to_concat) == 1: # Only one block, nothing to concatenate. concat_values = to_concat[0] if copy: if isinstance(concat_values, np.ndarray): # non-reindexed (=not yet copied) arrays are made into a view # in JoinUnit.get_reindexed_values if concat_values.base is not None: concat_values = concat_values.copy() else: concat_values = concat_values.copy() else: concat_values = _concat._concat_compat(to_concat, axis=concat_axis) return concat_values
[ "\n Concatenate values from several join units along selected axis.\n " ]
Please provide a description of the function:def get_empty_dtype_and_na(join_units): if len(join_units) == 1: blk = join_units[0].block if blk is None: return np.float64, np.nan if is_uniform_reindex(join_units): # XXX: integrate property empty_dtype = join_units[0].block.dtype upcasted_na = join_units[0].block.fill_value return empty_dtype, upcasted_na has_none_blocks = False dtypes = [None] * len(join_units) for i, unit in enumerate(join_units): if unit.block is None: has_none_blocks = True else: dtypes[i] = unit.dtype upcast_classes = defaultdict(list) null_upcast_classes = defaultdict(list) for dtype, unit in zip(dtypes, join_units): if dtype is None: continue if is_categorical_dtype(dtype): upcast_cls = 'category' elif is_datetime64tz_dtype(dtype): upcast_cls = 'datetimetz' elif issubclass(dtype.type, np.bool_): upcast_cls = 'bool' elif issubclass(dtype.type, np.object_): upcast_cls = 'object' elif is_datetime64_dtype(dtype): upcast_cls = 'datetime' elif is_timedelta64_dtype(dtype): upcast_cls = 'timedelta' elif is_sparse(dtype): upcast_cls = dtype.subtype.name elif is_extension_array_dtype(dtype): upcast_cls = 'object' elif is_float_dtype(dtype) or is_numeric_dtype(dtype): upcast_cls = dtype.name else: upcast_cls = 'float' # Null blocks should not influence upcast class selection, unless there # are only null blocks, when same upcasting rules must be applied to # null upcast classes. if unit.is_na: null_upcast_classes[upcast_cls].append(dtype) else: upcast_classes[upcast_cls].append(dtype) if not upcast_classes: upcast_classes = null_upcast_classes # create the result if 'object' in upcast_classes: return np.dtype(np.object_), np.nan elif 'bool' in upcast_classes: if has_none_blocks: return np.dtype(np.object_), np.nan else: return np.dtype(np.bool_), None elif 'category' in upcast_classes: return np.dtype(np.object_), np.nan elif 'datetimetz' in upcast_classes: # GH-25014. We use NaT instead of iNaT, since this eventually # ends up in DatetimeArray.take, which does not allow iNaT. dtype = upcast_classes['datetimetz'] return dtype[0], tslibs.NaT elif 'datetime' in upcast_classes: return np.dtype('M8[ns]'), tslibs.iNaT elif 'timedelta' in upcast_classes: return np.dtype('m8[ns]'), tslibs.iNaT else: # pragma try: g = np.find_common_type(upcast_classes, []) except TypeError: # At least one is an ExtensionArray return np.dtype(np.object_), np.nan else: if is_float_dtype(g): return g, g.type(np.nan) elif is_numeric_dtype(g): if has_none_blocks: return np.float64, np.nan else: return g, None msg = "invalid dtype determination in get_concat_dtype" raise AssertionError(msg)
[ "\n Return dtype and N/A values to use when concatenating specified units.\n\n Returned N/A value may be None which means there was no casting involved.\n\n Returns\n -------\n dtype\n na\n " ]
Please provide a description of the function:def is_uniform_join_units(join_units): return ( # all blocks need to have the same type all(type(ju.block) is type(join_units[0].block) for ju in join_units) and # noqa # no blocks that would get missing values (can lead to type upcasts) # unless we're an extension dtype. all(not ju.is_na or ju.block.is_extension for ju in join_units) and # no blocks with indexers (as then the dimensions do not fit) all(not ju.indexers for ju in join_units) and # disregard Panels all(ju.block.ndim <= 2 for ju in join_units) and # only use this path when there is something to concatenate len(join_units) > 1)
[ "\n Check if the join units consist of blocks of uniform type that can\n be concatenated using Block.concat_same_type instead of the generic\n concatenate_join_units (which uses `_concat._concat_compat`).\n\n " ]
Please provide a description of the function:def trim_join_unit(join_unit, length): if 0 not in join_unit.indexers: extra_indexers = join_unit.indexers if join_unit.block is None: extra_block = None else: extra_block = join_unit.block.getitem_block(slice(length, None)) join_unit.block = join_unit.block.getitem_block(slice(length)) else: extra_block = join_unit.block extra_indexers = copy.copy(join_unit.indexers) extra_indexers[0] = extra_indexers[0][length:] join_unit.indexers[0] = join_unit.indexers[0][:length] extra_shape = (join_unit.shape[0] - length,) + join_unit.shape[1:] join_unit.shape = (length,) + join_unit.shape[1:] return JoinUnit(block=extra_block, indexers=extra_indexers, shape=extra_shape)
[ "\n Reduce join_unit's shape along item axis to length.\n\n Extra items that didn't fit are returned as a separate block.\n " ]
Please provide a description of the function:def combine_concat_plans(plans, concat_axis): if len(plans) == 1: for p in plans[0]: yield p[0], [p[1]] elif concat_axis == 0: offset = 0 for plan in plans: last_plc = None for plc, unit in plan: yield plc.add(offset), [unit] last_plc = plc if last_plc is not None: offset += last_plc.as_slice.stop else: num_ended = [0] def _next_or_none(seq): retval = next(seq, None) if retval is None: num_ended[0] += 1 return retval plans = list(map(iter, plans)) next_items = list(map(_next_or_none, plans)) while num_ended[0] != len(next_items): if num_ended[0] > 0: raise ValueError("Plan shapes are not aligned") placements, units = zip(*next_items) lengths = list(map(len, placements)) min_len, max_len = min(lengths), max(lengths) if min_len == max_len: yield placements[0], units next_items[:] = map(_next_or_none, plans) else: yielded_placement = None yielded_units = [None] * len(next_items) for i, (plc, unit) in enumerate(next_items): yielded_units[i] = unit if len(plc) > min_len: # trim_join_unit updates unit in place, so only # placement needs to be sliced to skip min_len. next_items[i] = (plc[min_len:], trim_join_unit(unit, min_len)) else: yielded_placement = plc next_items[i] = _next_or_none(plans[i]) yield yielded_placement, yielded_units
[ "\n Combine multiple concatenation plans into one.\n\n existing_plan is updated in-place.\n " ]
Please provide a description of the function:def use(self, key, value): old_value = self[key] try: self[key] = value yield self finally: self[key] = old_value
[ "\n Temporarily set a parameter value using the with statement.\n Aliasing allowed.\n " ]
Please provide a description of the function:def _stata_elapsed_date_to_datetime_vec(dates, fmt): MIN_YEAR, MAX_YEAR = Timestamp.min.year, Timestamp.max.year MAX_DAY_DELTA = (Timestamp.max - datetime.datetime(1960, 1, 1)).days MIN_DAY_DELTA = (Timestamp.min - datetime.datetime(1960, 1, 1)).days MIN_MS_DELTA = MIN_DAY_DELTA * 24 * 3600 * 1000 MAX_MS_DELTA = MAX_DAY_DELTA * 24 * 3600 * 1000 def convert_year_month_safe(year, month): if year.max() < MAX_YEAR and year.min() > MIN_YEAR: return to_datetime(100 * year + month, format='%Y%m') else: index = getattr(year, 'index', None) return Series( [datetime.datetime(y, m, 1) for y, m in zip(year, month)], index=index) def convert_year_days_safe(year, days): if year.max() < (MAX_YEAR - 1) and year.min() > MIN_YEAR: return (to_datetime(year, format='%Y') + to_timedelta(days, unit='d')) else: index = getattr(year, 'index', None) value = [datetime.datetime(y, 1, 1) + relativedelta(days=int(d)) for y, d in zip(year, days)] return Series(value, index=index) def convert_delta_safe(base, deltas, unit): index = getattr(deltas, 'index', None) if unit == 'd': if deltas.max() > MAX_DAY_DELTA or deltas.min() < MIN_DAY_DELTA: values = [base + relativedelta(days=int(d)) for d in deltas] return Series(values, index=index) elif unit == 'ms': if deltas.max() > MAX_MS_DELTA or deltas.min() < MIN_MS_DELTA: values = [base + relativedelta(microseconds=(int(d) * 1000)) for d in deltas] return Series(values, index=index) else: raise ValueError('format not understood') base = to_datetime(base) deltas = to_timedelta(deltas, unit=unit) return base + deltas # TODO: If/when pandas supports more than datetime64[ns], this should be # improved to use correct range, e.g. datetime[Y] for yearly bad_locs = np.isnan(dates) has_bad_values = False if bad_locs.any(): has_bad_values = True data_col = Series(dates) data_col[bad_locs] = 1.0 # Replace with NaT dates = dates.astype(np.int64) if fmt.startswith(("%tc", "tc")): # Delta ms relative to base base = stata_epoch ms = dates conv_dates = convert_delta_safe(base, ms, 'ms') elif fmt.startswith(("%tC", "tC")): warnings.warn("Encountered %tC format. Leaving in Stata " "Internal Format.") conv_dates = Series(dates, dtype=np.object) if has_bad_values: conv_dates[bad_locs] = NaT return conv_dates # Delta days relative to base elif fmt.startswith(("%td", "td", "%d", "d")): base = stata_epoch days = dates conv_dates = convert_delta_safe(base, days, 'd') # does not count leap days - 7 days is a week. # 52nd week may have more than 7 days elif fmt.startswith(("%tw", "tw")): year = stata_epoch.year + dates // 52 days = (dates % 52) * 7 conv_dates = convert_year_days_safe(year, days) elif fmt.startswith(("%tm", "tm")): # Delta months relative to base year = stata_epoch.year + dates // 12 month = (dates % 12) + 1 conv_dates = convert_year_month_safe(year, month) elif fmt.startswith(("%tq", "tq")): # Delta quarters relative to base year = stata_epoch.year + dates // 4 month = (dates % 4) * 3 + 1 conv_dates = convert_year_month_safe(year, month) elif fmt.startswith(("%th", "th")): # Delta half-years relative to base year = stata_epoch.year + dates // 2 month = (dates % 2) * 6 + 1 conv_dates = convert_year_month_safe(year, month) elif fmt.startswith(("%ty", "ty")): # Years -- not delta year = dates month = np.ones_like(dates) conv_dates = convert_year_month_safe(year, month) else: raise ValueError("Date fmt {fmt} not understood".format(fmt=fmt)) if has_bad_values: # Restore NaT for bad values conv_dates[bad_locs] = NaT return conv_dates
[ "\n Convert from SIF to datetime. http://www.stata.com/help.cgi?datetime\n\n Parameters\n ----------\n dates : Series\n The Stata Internal Format date to convert to datetime according to fmt\n fmt : str\n The format to convert to. Can be, tc, td, tw, tm, tq, th, ty\n Returns\n\n Returns\n -------\n converted : Series\n The converted dates\n\n Examples\n --------\n >>> dates = pd.Series([52])\n >>> _stata_elapsed_date_to_datetime_vec(dates , \"%tw\")\n 0 1961-01-01\n dtype: datetime64[ns]\n\n Notes\n -----\n datetime/c - tc\n milliseconds since 01jan1960 00:00:00.000, assuming 86,400 s/day\n datetime/C - tC - NOT IMPLEMENTED\n milliseconds since 01jan1960 00:00:00.000, adjusted for leap seconds\n date - td\n days since 01jan1960 (01jan1960 = 0)\n weekly date - tw\n weeks since 1960w1\n This assumes 52 weeks in a year, then adds 7 * remainder of the weeks.\n The datetime value is the start of the week in terms of days in the\n year, not ISO calendar weeks.\n monthly date - tm\n months since 1960m1\n quarterly date - tq\n quarters since 1960q1\n half-yearly date - th\n half-years since 1960h1 yearly\n date - ty\n years since 0000\n\n If you don't have pandas with datetime support, then you can't do\n milliseconds accurately.\n ", "\n Convert year and month to datetimes, using pandas vectorized versions\n when the date range falls within the range supported by pandas.\n Otherwise it falls back to a slower but more robust method\n using datetime.\n ", "\n Converts year (e.g. 1999) and days since the start of the year to a\n datetime or datetime64 Series\n ", "\n Convert base dates and deltas to datetimes, using pandas vectorized\n versions if the deltas satisfy restrictions required to be expressed\n as dates in pandas.\n " ]
Please provide a description of the function:def _datetime_to_stata_elapsed_vec(dates, fmt): index = dates.index NS_PER_DAY = 24 * 3600 * 1000 * 1000 * 1000 US_PER_DAY = NS_PER_DAY / 1000 def parse_dates_safe(dates, delta=False, year=False, days=False): d = {} if is_datetime64_dtype(dates.values): if delta: delta = dates - stata_epoch d['delta'] = delta.values.astype( np.int64) // 1000 # microseconds if days or year: dates = DatetimeIndex(dates) d['year'], d['month'] = dates.year, dates.month if days: days = (dates.astype(np.int64) - to_datetime(d['year'], format='%Y').astype(np.int64)) d['days'] = days // NS_PER_DAY elif infer_dtype(dates, skipna=False) == 'datetime': if delta: delta = dates.values - stata_epoch f = lambda x: \ US_PER_DAY * x.days + 1000000 * x.seconds + x.microseconds v = np.vectorize(f) d['delta'] = v(delta) if year: year_month = dates.apply(lambda x: 100 * x.year + x.month) d['year'] = year_month.values // 100 d['month'] = (year_month.values - d['year'] * 100) if days: f = lambda x: (x - datetime.datetime(x.year, 1, 1)).days v = np.vectorize(f) d['days'] = v(dates) else: raise ValueError('Columns containing dates must contain either ' 'datetime64, datetime.datetime or null values.') return DataFrame(d, index=index) bad_loc = isna(dates) index = dates.index if bad_loc.any(): dates = Series(dates) if is_datetime64_dtype(dates): dates[bad_loc] = to_datetime(stata_epoch) else: dates[bad_loc] = stata_epoch if fmt in ["%tc", "tc"]: d = parse_dates_safe(dates, delta=True) conv_dates = d.delta / 1000 elif fmt in ["%tC", "tC"]: warnings.warn("Stata Internal Format tC not supported.") conv_dates = dates elif fmt in ["%td", "td"]: d = parse_dates_safe(dates, delta=True) conv_dates = d.delta // US_PER_DAY elif fmt in ["%tw", "tw"]: d = parse_dates_safe(dates, year=True, days=True) conv_dates = (52 * (d.year - stata_epoch.year) + d.days // 7) elif fmt in ["%tm", "tm"]: d = parse_dates_safe(dates, year=True) conv_dates = (12 * (d.year - stata_epoch.year) + d.month - 1) elif fmt in ["%tq", "tq"]: d = parse_dates_safe(dates, year=True) conv_dates = 4 * (d.year - stata_epoch.year) + (d.month - 1) // 3 elif fmt in ["%th", "th"]: d = parse_dates_safe(dates, year=True) conv_dates = (2 * (d.year - stata_epoch.year) + (d.month > 6).astype(np.int)) elif fmt in ["%ty", "ty"]: d = parse_dates_safe(dates, year=True) conv_dates = d.year else: raise ValueError( "Format {fmt} is not a known Stata date format".format(fmt=fmt)) conv_dates = Series(conv_dates, dtype=np.float64) missing_value = struct.unpack('<d', b'\x00\x00\x00\x00\x00\x00\xe0\x7f')[0] conv_dates[bad_loc] = missing_value return Series(conv_dates, index=index)
[ "\n Convert from datetime to SIF. http://www.stata.com/help.cgi?datetime\n\n Parameters\n ----------\n dates : Series\n Series or array containing datetime.datetime or datetime64[ns] to\n convert to the Stata Internal Format given by fmt\n fmt : str\n The format to convert to. Can be, tc, td, tw, tm, tq, th, ty\n " ]
Please provide a description of the function:def _cast_to_stata_types(data): ws = '' # original, if small, if large conversion_data = ((np.bool, np.int8, np.int8), (np.uint8, np.int8, np.int16), (np.uint16, np.int16, np.int32), (np.uint32, np.int32, np.int64)) float32_max = struct.unpack('<f', b'\xff\xff\xff\x7e')[0] float64_max = struct.unpack('<d', b'\xff\xff\xff\xff\xff\xff\xdf\x7f')[0] for col in data: dtype = data[col].dtype # Cast from unsupported types to supported types for c_data in conversion_data: if dtype == c_data[0]: if data[col].max() <= np.iinfo(c_data[1]).max: dtype = c_data[1] else: dtype = c_data[2] if c_data[2] == np.float64: # Warn if necessary if data[col].max() >= 2 ** 53: ws = precision_loss_doc % ('uint64', 'float64') data[col] = data[col].astype(dtype) # Check values and upcast if necessary if dtype == np.int8: if data[col].max() > 100 or data[col].min() < -127: data[col] = data[col].astype(np.int16) elif dtype == np.int16: if data[col].max() > 32740 or data[col].min() < -32767: data[col] = data[col].astype(np.int32) elif dtype == np.int64: if (data[col].max() <= 2147483620 and data[col].min() >= -2147483647): data[col] = data[col].astype(np.int32) else: data[col] = data[col].astype(np.float64) if data[col].max() >= 2 ** 53 or data[col].min() <= -2 ** 53: ws = precision_loss_doc % ('int64', 'float64') elif dtype in (np.float32, np.float64): value = data[col].max() if np.isinf(value): raise ValueError('Column {col} has a maximum value of ' 'infinity which is outside the range ' 'supported by Stata.'.format(col=col)) if dtype == np.float32 and value > float32_max: data[col] = data[col].astype(np.float64) elif dtype == np.float64: if value > float64_max: raise ValueError('Column {col} has a maximum value ' '({val}) outside the range supported by ' 'Stata ({float64_max})' .format(col=col, val=value, float64_max=float64_max)) if ws: warnings.warn(ws, PossiblePrecisionLoss) return data
[ "Checks the dtypes of the columns of a pandas DataFrame for\n compatibility with the data types and ranges supported by Stata, and\n converts if necessary.\n\n Parameters\n ----------\n data : DataFrame\n The DataFrame to check and convert\n\n Notes\n -----\n Numeric columns in Stata must be one of int8, int16, int32, float32 or\n float64, with some additional value restrictions. int8 and int16 columns\n are checked for violations of the value restrictions and upcast if needed.\n int64 data is not usable in Stata, and so it is downcast to int32 whenever\n the value are in the int32 range, and sidecast to float64 when larger than\n this range. If the int64 values are outside of the range of those\n perfectly representable as float64 values, a warning is raised.\n\n bool columns are cast to int8. uint columns are converted to int of the\n same size if there is no loss in precision, otherwise are upcast to a\n larger type. uint64 is currently not supported since it is concerted to\n object in a DataFrame.\n " ]
Please provide a description of the function:def _dtype_to_stata_type(dtype, column): # TODO: expand to handle datetime to integer conversion if dtype.type == np.object_: # try to coerce it to the biggest string # not memory efficient, what else could we # do? itemsize = max_len_string_array(ensure_object(column.values)) return max(itemsize, 1) elif dtype == np.float64: return 255 elif dtype == np.float32: return 254 elif dtype == np.int32: return 253 elif dtype == np.int16: return 252 elif dtype == np.int8: return 251 else: # pragma : no cover raise NotImplementedError( "Data type {dtype} not supported.".format(dtype=dtype))
[ "\n Convert dtype types to stata types. Returns the byte of the given ordinal.\n See TYPE_MAP and comments for an explanation. This is also explained in\n the dta spec.\n 1 - 244 are strings of this length\n Pandas Stata\n 251 - for int8 byte\n 252 - for int16 int\n 253 - for int32 long\n 254 - for float32 float\n 255 - for double double\n\n If there are dates to convert, then dtype will already have the correct\n type inserted.\n " ]
Please provide a description of the function:def _dtype_to_default_stata_fmt(dtype, column, dta_version=114, force_strl=False): # TODO: Refactor to combine type with format # TODO: expand this to handle a default datetime format? if dta_version < 117: max_str_len = 244 else: max_str_len = 2045 if force_strl: return '%9s' if dtype.type == np.object_: inferred_dtype = infer_dtype(column, skipna=True) if not (inferred_dtype in ('string', 'unicode') or len(column) == 0): raise ValueError('Column `{col}` cannot be exported.\n\nOnly ' 'string-like object arrays containing all ' 'strings or a mix of strings and None can be ' 'exported. Object arrays containing only null ' 'values are prohibited. Other object types' 'cannot be exported and must first be converted ' 'to one of the supported ' 'types.'.format(col=column.name)) itemsize = max_len_string_array(ensure_object(column.values)) if itemsize > max_str_len: if dta_version >= 117: return '%9s' else: raise ValueError(excessive_string_length_error % column.name) return "%" + str(max(itemsize, 1)) + "s" elif dtype == np.float64: return "%10.0g" elif dtype == np.float32: return "%9.0g" elif dtype == np.int32: return "%12.0g" elif dtype == np.int8 or dtype == np.int16: return "%8.0g" else: # pragma : no cover raise NotImplementedError( "Data type {dtype} not supported.".format(dtype=dtype))
[ "\n Map numpy dtype to stata's default format for this type. Not terribly\n important since users can change this in Stata. Semantics are\n\n object -> \"%DDs\" where DD is the length of the string. If not a string,\n raise ValueError\n float64 -> \"%10.0g\"\n float32 -> \"%9.0g\"\n int64 -> \"%9.0g\"\n int32 -> \"%12.0g\"\n int16 -> \"%8.0g\"\n int8 -> \"%8.0g\"\n strl -> \"%9s\"\n " ]
Please provide a description of the function:def _pad_bytes_new(name, length): if isinstance(name, str): name = bytes(name, 'utf-8') return name + b'\x00' * (length - len(name))
[ "\n Takes a bytes instance and pads it with null bytes until it's length chars.\n " ]
Please provide a description of the function:def generate_value_label(self, byteorder, encoding): self._encoding = encoding bio = BytesIO() null_string = '\x00' null_byte = b'\x00' # len bio.write(struct.pack(byteorder + 'i', self.len)) # labname labname = self._encode(_pad_bytes(self.labname[:32], 33)) bio.write(labname) # padding - 3 bytes for i in range(3): bio.write(struct.pack('c', null_byte)) # value_label_table # n - int32 bio.write(struct.pack(byteorder + 'i', self.n)) # textlen - int32 bio.write(struct.pack(byteorder + 'i', self.text_len)) # off - int32 array (n elements) for offset in self.off: bio.write(struct.pack(byteorder + 'i', offset)) # val - int32 array (n elements) for value in self.val: bio.write(struct.pack(byteorder + 'i', value)) # txt - Text labels, null terminated for text in self.txt: bio.write(self._encode(text + null_string)) bio.seek(0) return bio.read()
[ "\n Parameters\n ----------\n byteorder : str\n Byte order of the output\n encoding : str\n File encoding\n\n Returns\n -------\n value_label : bytes\n Bytes containing the formatted value label\n " ]
Please provide a description of the function:def _setup_dtype(self): if self._dtype is not None: return self._dtype dtype = [] # Convert struct data types to numpy data type for i, typ in enumerate(self.typlist): if typ in self.NUMPY_TYPE_MAP: dtype.append(('s' + str(i), self.byteorder + self.NUMPY_TYPE_MAP[typ])) else: dtype.append(('s' + str(i), 'S' + str(typ))) dtype = np.dtype(dtype) self._dtype = dtype return self._dtype
[ "Map between numpy and state dtypes" ]
Please provide a description of the function:def _do_convert_categoricals(self, data, value_label_dict, lbllist, order_categoricals): value_labels = list(value_label_dict.keys()) cat_converted_data = [] for col, label in zip(data, lbllist): if label in value_labels: # Explicit call with ordered=True cat_data = Categorical(data[col], ordered=order_categoricals) categories = [] for category in cat_data.categories: if category in value_label_dict[label]: categories.append(value_label_dict[label][category]) else: categories.append(category) # Partially labeled try: cat_data.categories = categories except ValueError: vc = Series(categories).value_counts() repeats = list(vc.index[vc > 1]) repeats = '-' * 80 + '\n' + '\n'.join(repeats) # GH 25772 msg = raise ValueError(msg.format(col=col, repeats=repeats)) # TODO: is the next line needed above in the data(...) method? cat_data = Series(cat_data, index=data.index) cat_converted_data.append((col, cat_data)) else: cat_converted_data.append((col, data[col])) data = DataFrame.from_dict(OrderedDict(cat_converted_data)) return data
[ "\n Converts categorical columns to Categorical type.\n ", "\nValue labels for column {col} are not unique. These cannot be converted to\npandas categoricals.\n\nEither read the file with `convert_categoricals` set to False or use the\nlow level interface in `StataReader` to separately read the values and the\nvalue_labels.\n\nThe repeated labels are:\n{repeats}\n" ]
Please provide a description of the function:def _write(self, to_write): self._file.write(to_write.encode(self._encoding or self._default_encoding))
[ "\n Helper to call encode before writing to file for Python 3 compat.\n " ]
Please provide a description of the function:def _prepare_categoricals(self, data): is_cat = [is_categorical_dtype(data[col]) for col in data] self._is_col_cat = is_cat self._value_labels = [] if not any(is_cat): return data get_base_missing_value = StataMissingValue.get_base_missing_value data_formatted = [] for col, col_is_cat in zip(data, is_cat): if col_is_cat: self._value_labels.append(StataValueLabel(data[col])) dtype = data[col].cat.codes.dtype if dtype == np.int64: raise ValueError('It is not possible to export ' 'int64-based categorical data to Stata.') values = data[col].cat.codes.values.copy() # Upcast if needed so that correct missing values can be set if values.max() >= get_base_missing_value(dtype): if dtype == np.int8: dtype = np.int16 elif dtype == np.int16: dtype = np.int32 else: dtype = np.float64 values = np.array(values, dtype=dtype) # Replace missing values with Stata missing value for type values[values == -1] = get_base_missing_value(dtype) data_formatted.append((col, values)) else: data_formatted.append((col, data[col])) return DataFrame.from_dict(OrderedDict(data_formatted))
[ "Check for categorical columns, retain categorical information for\n Stata file and convert categorical data to int" ]
Please provide a description of the function:def _replace_nans(self, data): # return data for c in data: dtype = data[c].dtype if dtype in (np.float32, np.float64): if dtype == np.float32: replacement = self.MISSING_VALUES['f'] else: replacement = self.MISSING_VALUES['d'] data[c] = data[c].fillna(replacement) return data
[ "Checks floating point data columns for nans, and replaces these with\n the generic Stata for missing value (.)" ]
Please provide a description of the function:def _check_column_names(self, data): converted_names = {} columns = list(data.columns) original_columns = columns[:] duplicate_var_id = 0 for j, name in enumerate(columns): orig_name = name if not isinstance(name, str): name = str(name) for c in name: if ((c < 'A' or c > 'Z') and (c < 'a' or c > 'z') and (c < '0' or c > '9') and c != '_'): name = name.replace(c, '_') # Variable name must not be a reserved word if name in self.RESERVED_WORDS: name = '_' + name # Variable name may not start with a number if name[0] >= '0' and name[0] <= '9': name = '_' + name name = name[:min(len(name), 32)] if not name == orig_name: # check for duplicates while columns.count(name) > 0: # prepend ascending number to avoid duplicates name = '_' + str(duplicate_var_id) + name name = name[:min(len(name), 32)] duplicate_var_id += 1 converted_names[orig_name] = name columns[j] = name data.columns = columns # Check date conversion, and fix key if needed if self._convert_dates: for c, o in zip(columns, original_columns): if c != o: self._convert_dates[c] = self._convert_dates[o] del self._convert_dates[o] if converted_names: conversion_warning = [] for orig_name, name in converted_names.items(): # need to possibly encode the orig name if its unicode try: orig_name = orig_name.encode('utf-8') except (UnicodeDecodeError, AttributeError): pass msg = '{0} -> {1}'.format(orig_name, name) conversion_warning.append(msg) ws = invalid_name_doc.format('\n '.join(conversion_warning)) warnings.warn(ws, InvalidColumnName) self._converted_names = converted_names self._update_strl_names() return data
[ "\n Checks column names to ensure that they are valid Stata column names.\n This includes checks for:\n * Non-string names\n * Stata keywords\n * Variables that start with numbers\n * Variables with names that are too long\n\n When an illegal variable name is detected, it is converted, and if\n dates are exported, the variable name is propagated to the date\n conversion dictionary\n " ]
Please provide a description of the function:def _close(self): # Some file-like objects might not support flush try: self._file.flush() except AttributeError: pass if self._own_file: self._file.close()
[ "\n Close the file if it was created by the writer.\n\n If a buffer or file-like object was passed in, for example a GzipFile,\n then leave this file open for the caller to close. In either case,\n attempt to flush the file contents to ensure they are written to disk\n (if supported)\n " ]
Please provide a description of the function:def generate_table(self): gso_table = self._gso_table gso_df = self.df columns = list(gso_df.columns) selected = gso_df[self.columns] col_index = [(col, columns.index(col)) for col in self.columns] keys = np.empty(selected.shape, dtype=np.uint64) for o, (idx, row) in enumerate(selected.iterrows()): for j, (col, v) in enumerate(col_index): val = row[col] # Allow columns with mixed str and None (GH 23633) val = '' if val is None else val key = gso_table.get(val, None) if key is None: # Stata prefers human numbers key = (v + 1, o + 1) gso_table[val] = key keys[o, j] = self._convert_key(key) for i, col in enumerate(self.columns): gso_df[col] = keys[:, i] return gso_table, gso_df
[ "\n Generates the GSO lookup table for the DataFRame\n\n Returns\n -------\n gso_table : OrderedDict\n Ordered dictionary using the string found as keys\n and their lookup position (v,o) as values\n gso_df : DataFrame\n DataFrame where strl columns have been converted to\n (v,o) values\n\n Notes\n -----\n Modifies the DataFrame in-place.\n\n The DataFrame returned encodes the (v,o) values as uint64s. The\n encoding depends on teh dta version, and can be expressed as\n\n enc = v + o * 2 ** (o_size * 8)\n\n so that v is stored in the lower bits and o is in the upper\n bits. o_size is\n\n * 117: 4\n * 118: 6\n * 119: 5\n " ]
Please provide a description of the function:def generate_blob(self, gso_table): # Format information # Length includes null term # 117 # GSOvvvvooootllllxxxxxxxxxxxxxxx...x # 3 u4 u4 u1 u4 string + null term # # 118, 119 # GSOvvvvooooooootllllxxxxxxxxxxxxxxx...x # 3 u4 u8 u1 u4 string + null term bio = BytesIO() gso = bytes('GSO', 'ascii') gso_type = struct.pack(self._byteorder + 'B', 130) null = struct.pack(self._byteorder + 'B', 0) v_type = self._byteorder + self._gso_v_type o_type = self._byteorder + self._gso_o_type len_type = self._byteorder + 'I' for strl, vo in gso_table.items(): if vo == (0, 0): continue v, o = vo # GSO bio.write(gso) # vvvv bio.write(struct.pack(v_type, v)) # oooo / oooooooo bio.write(struct.pack(o_type, o)) # t bio.write(gso_type) # llll utf8_string = bytes(strl, 'utf-8') bio.write(struct.pack(len_type, len(utf8_string) + 1)) # xxx...xxx bio.write(utf8_string) bio.write(null) bio.seek(0) return bio.read()
[ "\n Generates the binary blob of GSOs that is written to the dta file.\n\n Parameters\n ----------\n gso_table : OrderedDict\n Ordered dictionary (str, vo)\n\n Returns\n -------\n gso : bytes\n Binary content of dta file to be placed between strl tags\n\n Notes\n -----\n Output format depends on dta version. 117 uses two uint32s to\n express v and o while 118+ uses a uint32 for v and a uint64 for o.\n " ]
Please provide a description of the function:def _tag(val, tag): if isinstance(val, str): val = bytes(val, 'utf-8') return (bytes('<' + tag + '>', 'utf-8') + val + bytes('</' + tag + '>', 'utf-8'))
[ "Surround val with <tag></tag>" ]
Please provide a description of the function:def _write_header(self, data_label=None, time_stamp=None): byteorder = self._byteorder self._file.write(bytes('<stata_dta>', 'utf-8')) bio = BytesIO() # ds_format - 117 bio.write(self._tag(bytes('117', 'utf-8'), 'release')) # byteorder bio.write(self._tag(byteorder == ">" and "MSF" or "LSF", 'byteorder')) # number of vars, 2 bytes assert self.nvar < 2 ** 16 bio.write(self._tag(struct.pack(byteorder + "H", self.nvar), 'K')) # number of obs, 4 bytes bio.write(self._tag(struct.pack(byteorder + "I", self.nobs), 'N')) # data label 81 bytes, char, null terminated label = data_label[:80] if data_label is not None else '' label_len = struct.pack(byteorder + "B", len(label)) label = label_len + bytes(label, 'utf-8') bio.write(self._tag(label, 'label')) # time stamp, 18 bytes, char, null terminated # format dd Mon yyyy hh:mm if time_stamp is None: time_stamp = datetime.datetime.now() elif not isinstance(time_stamp, datetime.datetime): raise ValueError("time_stamp should be datetime type") # Avoid locale-specific month conversion months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] month_lookup = {i + 1: month for i, month in enumerate(months)} ts = (time_stamp.strftime("%d ") + month_lookup[time_stamp.month] + time_stamp.strftime(" %Y %H:%M")) # '\x11' added due to inspection of Stata file ts = b'\x11' + bytes(ts, 'utf8') bio.write(self._tag(ts, 'timestamp')) bio.seek(0) self._file.write(self._tag(bio.read(), 'header'))
[ "Write the file header" ]
Please provide a description of the function:def _write_map(self): if self._map is None: self._map = OrderedDict((('stata_data', 0), ('map', self._file.tell()), ('variable_types', 0), ('varnames', 0), ('sortlist', 0), ('formats', 0), ('value_label_names', 0), ('variable_labels', 0), ('characteristics', 0), ('data', 0), ('strls', 0), ('value_labels', 0), ('stata_data_close', 0), ('end-of-file', 0))) # Move to start of map self._file.seek(self._map['map']) bio = BytesIO() for val in self._map.values(): bio.write(struct.pack(self._byteorder + 'Q', val)) bio.seek(0) self._file.write(self._tag(bio.read(), 'map'))
[ "Called twice during file write. The first populates the values in\n the map with 0s. The second call writes the final map locations when\n all blocks have been written." ]
Please provide a description of the function:def _update_strl_names(self): # Update convert_strl if names changed for orig, new in self._converted_names.items(): if orig in self._convert_strl: idx = self._convert_strl.index(orig) self._convert_strl[idx] = new
[ "Update column names for conversion to strl if they might have been\n changed to comply with Stata naming rules" ]
Please provide a description of the function:def _convert_strls(self, data): convert_cols = [ col for i, col in enumerate(data) if self.typlist[i] == 32768 or col in self._convert_strl] if convert_cols: ssw = StataStrLWriter(data, convert_cols) tab, new_data = ssw.generate_table() data = new_data self._strl_blob = ssw.generate_blob(tab) return data
[ "Convert columns to StrLs if either very large or in the\n convert_strl variable" ]
Please provide a description of the function:def register(explicit=True): # Renamed in pandas.plotting.__init__ global _WARN if explicit: _WARN = False pairs = get_pairs() for type_, cls in pairs: converter = cls() if type_ in units.registry: previous = units.registry[type_] _mpl_units[type_] = previous units.registry[type_] = converter
[ "\n Register Pandas Formatters and Converters with matplotlib\n\n This function modifies the global ``matplotlib.units.registry``\n dictionary. Pandas adds custom converters for\n\n * pd.Timestamp\n * pd.Period\n * np.datetime64\n * datetime.datetime\n * datetime.date\n * datetime.time\n\n See Also\n --------\n deregister_matplotlib_converter\n " ]
Please provide a description of the function:def deregister(): # Renamed in pandas.plotting.__init__ for type_, cls in get_pairs(): # We use type to catch our classes directly, no inheritance if type(units.registry.get(type_)) is cls: units.registry.pop(type_) # restore the old keys for unit, formatter in _mpl_units.items(): if type(formatter) not in {DatetimeConverter, PeriodConverter, TimeConverter}: # make it idempotent by excluding ours. units.registry[unit] = formatter
[ "\n Remove pandas' formatters and converters\n\n Removes the custom converters added by :func:`register`. This\n attempts to set the state of the registry back to the state before\n pandas registered its own units. Converters for pandas' own types like\n Timestamp and Period are removed completely. Converters for types\n pandas overwrites, like ``datetime.datetime``, are restored to their\n original value.\n\n See Also\n --------\n deregister_matplotlib_converters\n " ]
Please provide a description of the function:def _dt_to_float_ordinal(dt): if (isinstance(dt, (np.ndarray, Index, ABCSeries) ) and is_datetime64_ns_dtype(dt)): base = dates.epoch2num(dt.asi8 / 1.0E9) else: base = dates.date2num(dt) return base
[ "\n Convert :mod:`datetime` to the Gregorian date as UTC float days,\n preserving hours, minutes, seconds and microseconds. Return value\n is a :func:`float`.\n " ]
Please provide a description of the function:def _get_default_annual_spacing(nyears): if nyears < 11: (min_spacing, maj_spacing) = (1, 1) elif nyears < 20: (min_spacing, maj_spacing) = (1, 2) elif nyears < 50: (min_spacing, maj_spacing) = (1, 5) elif nyears < 100: (min_spacing, maj_spacing) = (5, 10) elif nyears < 200: (min_spacing, maj_spacing) = (5, 25) elif nyears < 600: (min_spacing, maj_spacing) = (10, 50) else: factor = nyears // 1000 + 1 (min_spacing, maj_spacing) = (factor * 20, factor * 100) return (min_spacing, maj_spacing)
[ "\n Returns a default spacing between consecutive ticks for annual data.\n " ]
Please provide a description of the function:def period_break(dates, period): current = getattr(dates, period) previous = getattr(dates - 1 * dates.freq, period) return np.nonzero(current - previous)[0]
[ "\n Returns the indices where the given period changes.\n\n Parameters\n ----------\n dates : PeriodIndex\n Array of intervals to monitor.\n period : string\n Name of the period to monitor.\n " ]
Please provide a description of the function:def has_level_label(label_flags, vmin): if label_flags.size == 0 or (label_flags.size == 1 and label_flags[0] == 0 and vmin % 1 > 0.0): return False else: return True
[ "\n Returns true if the ``label_flags`` indicate there is at least one label\n for this level.\n\n if the minimum view limit is not an exact integer, then the first tick\n label won't be shown, so we must adjust for that.\n " ]
Please provide a description of the function:def axisinfo(unit, axis): tz = unit majloc = PandasAutoDateLocator(tz=tz) majfmt = PandasAutoDateFormatter(majloc, tz=tz) datemin = pydt.date(2000, 1, 1) datemax = pydt.date(2010, 1, 1) return units.AxisInfo(majloc=majloc, majfmt=majfmt, label='', default_limits=(datemin, datemax))
[ "\n Return the :class:`~matplotlib.units.AxisInfo` for *unit*.\n\n *unit* is a tzinfo instance or None.\n The *axis* argument is required but not used.\n " ]
Please provide a description of the function:def get_locator(self, dmin, dmax): 'Pick the best locator based on a distance.' _check_implicitly_registered() delta = relativedelta(dmax, dmin) num_days = (delta.years * 12.0 + delta.months) * 31.0 + delta.days num_sec = (delta.hours * 60.0 + delta.minutes) * 60.0 + delta.seconds tot_sec = num_days * 86400. + num_sec if abs(tot_sec) < self.minticks: self._freq = -1 locator = MilliSecondLocator(self.tz) locator.set_axis(self.axis) locator.set_view_interval(*self.axis.get_view_interval()) locator.set_data_interval(*self.axis.get_data_interval()) return locator return dates.AutoDateLocator.get_locator(self, dmin, dmax)
[]
Please provide a description of the function:def autoscale(self): dmin, dmax = self.datalim_to_dt() if dmin > dmax: dmax, dmin = dmin, dmax # We need to cap at the endpoints of valid datetime # TODO(wesm): unused? # delta = relativedelta(dmax, dmin) # try: # start = dmin - delta # except ValueError: # start = _from_ordinal(1.0) # try: # stop = dmax + delta # except ValueError: # # The magic number! # stop = _from_ordinal(3652059.9999999) dmin, dmax = self.datalim_to_dt() vmin = dates.date2num(dmin) vmax = dates.date2num(dmax) return self.nonsingular(vmin, vmax)
[ "\n Set the view limits to include the data range.\n " ]
Please provide a description of the function:def _get_default_locs(self, vmin, vmax): "Returns the default locations of ticks." if self.plot_obj.date_axis_info is None: self.plot_obj.date_axis_info = self.finder(vmin, vmax, self.freq) locator = self.plot_obj.date_axis_info if self.isminor: return np.compress(locator['min'], locator['val']) return np.compress(locator['maj'], locator['val'])
[]
Please provide a description of the function:def autoscale(self): # requires matplotlib >= 0.98.0 (vmin, vmax) = self.axis.get_data_interval() locs = self._get_default_locs(vmin, vmax) (vmin, vmax) = locs[[0, -1]] if vmin == vmax: vmin -= 1 vmax += 1 return nonsingular(vmin, vmax)
[ "\n Sets the view limits to the nearest multiples of base that contain the\n data.\n " ]
Please provide a description of the function:def _set_default_format(self, vmin, vmax): "Returns the default ticks spacing." if self.plot_obj.date_axis_info is None: self.plot_obj.date_axis_info = self.finder(vmin, vmax, self.freq) info = self.plot_obj.date_axis_info if self.isminor: format = np.compress(info['min'] & np.logical_not(info['maj']), info) else: format = np.compress(info['maj'], info) self.formatdict = {x: f for (x, _, _, f) in format} return self.formatdict
[]
Please provide a description of the function:def set_locs(self, locs): 'Sets the locations of the ticks' # don't actually use the locs. This is just needed to work with # matplotlib. Force to use vmin, vmax _check_implicitly_registered() self.locs = locs (vmin, vmax) = vi = tuple(self.axis.get_view_interval()) if vi != self.plot_obj.view_interval: self.plot_obj.date_axis_info = None self.plot_obj.view_interval = vi if vmax < vmin: (vmin, vmax) = (vmax, vmin) self._set_default_format(vmin, vmax)
[]
Please provide a description of the function:def set_default_names(data): if com._all_not_none(*data.index.names): nms = data.index.names if len(nms) == 1 and data.index.name == 'index': warnings.warn("Index name of 'index' is not round-trippable") elif len(nms) > 1 and any(x.startswith('level_') for x in nms): warnings.warn("Index names beginning with 'level_' are not " "round-trippable") return data data = data.copy() if data.index.nlevels > 1: names = [name if name is not None else 'level_{}'.format(i) for i, name in enumerate(data.index.names)] data.index.names = names else: data.index.name = data.index.name or 'index' return data
[ "Sets index names to 'index' for regular, or 'level_x' for Multi" ]
Please provide a description of the function:def convert_json_field_to_pandas_type(field): typ = field['type'] if typ == 'string': return 'object' elif typ == 'integer': return 'int64' elif typ == 'number': return 'float64' elif typ == 'boolean': return 'bool' elif typ == 'duration': return 'timedelta64' elif typ == 'datetime': if field.get('tz'): return 'datetime64[ns, {tz}]'.format(tz=field['tz']) else: return 'datetime64[ns]' elif typ == 'any': if 'constraints' in field and 'ordered' in field: return CategoricalDtype(categories=field['constraints']['enum'], ordered=field['ordered']) else: return 'object' raise ValueError("Unsupported or invalid field type: {}".format(typ))
[ "\n Converts a JSON field descriptor into its corresponding NumPy / pandas type\n\n Parameters\n ----------\n field\n A JSON field descriptor\n\n Returns\n -------\n dtype\n\n Raises\n -----\n ValueError\n If the type of the provided field is unknown or currently unsupported\n\n Examples\n --------\n >>> convert_json_field_to_pandas_type({'name': 'an_int',\n 'type': 'integer'})\n 'int64'\n >>> convert_json_field_to_pandas_type({'name': 'a_categorical',\n 'type': 'any',\n 'contraints': {'enum': [\n 'a', 'b', 'c']},\n 'ordered': True})\n 'CategoricalDtype(categories=['a', 'b', 'c'], ordered=True)'\n >>> convert_json_field_to_pandas_type({'name': 'a_datetime',\n 'type': 'datetime'})\n 'datetime64[ns]'\n >>> convert_json_field_to_pandas_type({'name': 'a_datetime_with_tz',\n 'type': 'datetime',\n 'tz': 'US/Central'})\n 'datetime64[ns, US/Central]'\n " ]
Please provide a description of the function:def build_table_schema(data, index=True, primary_key=None, version=True): if index is True: data = set_default_names(data) schema = {} fields = [] if index: if data.index.nlevels > 1: for level in data.index.levels: fields.append(convert_pandas_type_to_json_field(level)) else: fields.append(convert_pandas_type_to_json_field(data.index)) if data.ndim > 1: for column, s in data.iteritems(): fields.append(convert_pandas_type_to_json_field(s)) else: fields.append(convert_pandas_type_to_json_field(data)) schema['fields'] = fields if index and data.index.is_unique and primary_key is None: if data.index.nlevels == 1: schema['primaryKey'] = [data.index.name] else: schema['primaryKey'] = data.index.names elif primary_key is not None: schema['primaryKey'] = primary_key if version: schema['pandas_version'] = '0.20.0' return schema
[ "\n Create a Table schema from ``data``.\n\n Parameters\n ----------\n data : Series, DataFrame\n index : bool, default True\n Whether to include ``data.index`` in the schema.\n primary_key : bool or None, default True\n column names to designate as the primary key.\n The default `None` will set `'primaryKey'` to the index\n level or levels if the index is unique.\n version : bool, default True\n Whether to include a field `pandas_version` with the version\n of pandas that generated the schema.\n\n Returns\n -------\n schema : dict\n\n Notes\n -----\n See `_as_json_table_type` for conversion types.\n Timedeltas as converted to ISO8601 duration format with\n 9 decimal places after the seconds field for nanosecond precision.\n\n Categoricals are converted to the `any` dtype, and use the `enum` field\n constraint to list the allowed values. The `ordered` attribute is included\n in an `ordered` field.\n\n Examples\n --------\n >>> df = pd.DataFrame(\n ... {'A': [1, 2, 3],\n ... 'B': ['a', 'b', 'c'],\n ... 'C': pd.date_range('2016-01-01', freq='d', periods=3),\n ... }, index=pd.Index(range(3), name='idx'))\n >>> build_table_schema(df)\n {'fields': [{'name': 'idx', 'type': 'integer'},\n {'name': 'A', 'type': 'integer'},\n {'name': 'B', 'type': 'string'},\n {'name': 'C', 'type': 'datetime'}],\n 'pandas_version': '0.20.0',\n 'primaryKey': ['idx']}\n " ]
Please provide a description of the function:def parse_table_schema(json, precise_float): table = loads(json, precise_float=precise_float) col_order = [field['name'] for field in table['schema']['fields']] df = DataFrame(table['data'], columns=col_order)[col_order] dtypes = {field['name']: convert_json_field_to_pandas_type(field) for field in table['schema']['fields']} # Cannot directly use as_type with timezone data on object; raise for now if any(str(x).startswith('datetime64[ns, ') for x in dtypes.values()): raise NotImplementedError('table="orient" can not yet read timezone ' 'data') # No ISO constructor for Timedelta as of yet, so need to raise if 'timedelta64' in dtypes.values(): raise NotImplementedError('table="orient" can not yet read ' 'ISO-formatted Timedelta data') df = df.astype(dtypes) if 'primaryKey' in table['schema']: df = df.set_index(table['schema']['primaryKey']) if len(df.index.names) == 1: if df.index.name == 'index': df.index.name = None else: df.index.names = [None if x.startswith('level_') else x for x in df.index.names] return df
[ "\n Builds a DataFrame from a given schema\n\n Parameters\n ----------\n json :\n A JSON table schema\n precise_float : boolean\n Flag controlling precision when decoding string to double values, as\n dictated by ``read_json``\n\n Returns\n -------\n df : DataFrame\n\n Raises\n ------\n NotImplementedError\n If the JSON table schema contains either timezone or timedelta data\n\n Notes\n -----\n Because :func:`DataFrame.to_json` uses the string 'index' to denote a\n name-less :class:`Index`, this function sets the name of the returned\n :class:`DataFrame` to ``None`` when said string is encountered with a\n normal :class:`Index`. For a :class:`MultiIndex`, the same limitation\n applies to any strings beginning with 'level_'. Therefore, an\n :class:`Index` name of 'index' and :class:`MultiIndex` names starting\n with 'level_' are not supported.\n\n See Also\n --------\n build_table_schema : Inverse function.\n pandas.read_json\n " ]
Please provide a description of the function:def get_op_result_name(left, right): # `left` is always a pd.Series when called from within ops if isinstance(right, (ABCSeries, pd.Index)): name = _maybe_match_name(left, right) else: name = left.name return name
[ "\n Find the appropriate name to pin to an operation result. This result\n should always be either an Index or a Series.\n\n Parameters\n ----------\n left : {Series, Index}\n right : object\n\n Returns\n -------\n name : object\n Usually a string\n " ]
Please provide a description of the function:def _maybe_match_name(a, b): a_has = hasattr(a, 'name') b_has = hasattr(b, 'name') if a_has and b_has: if a.name == b.name: return a.name else: # TODO: what if they both have np.nan for their names? return None elif a_has: return a.name elif b_has: return b.name return None
[ "\n Try to find a name to attach to the result of an operation between\n a and b. If only one of these has a `name` attribute, return that\n name. Otherwise return a consensus name if they match of None if\n they have different names.\n\n Parameters\n ----------\n a : object\n b : object\n\n Returns\n -------\n name : str or None\n\n See Also\n --------\n pandas.core.common.consensus_name_attr\n " ]
Please provide a description of the function:def maybe_upcast_for_op(obj): if type(obj) is datetime.timedelta: # GH#22390 cast up to Timedelta to rely on Timedelta # implementation; otherwise operation against numeric-dtype # raises TypeError return pd.Timedelta(obj) elif isinstance(obj, np.timedelta64) and not isna(obj): # In particular non-nanosecond timedelta64 needs to be cast to # nanoseconds, or else we get undesired behavior like # np.timedelta64(3, 'D') / 2 == np.timedelta64(1, 'D') # The isna check is to avoid casting timedelta64("NaT"), which would # return NaT and incorrectly be treated as a datetime-NaT. return pd.Timedelta(obj) elif isinstance(obj, np.ndarray) and is_timedelta64_dtype(obj): # GH#22390 Unfortunately we need to special-case right-hand # timedelta64 dtypes because numpy casts integer dtypes to # timedelta64 when operating with timedelta64 return pd.TimedeltaIndex(obj) return obj
[ "\n Cast non-pandas objects to pandas types to unify behavior of arithmetic\n and comparison operations.\n\n Parameters\n ----------\n obj: object\n\n Returns\n -------\n out : object\n\n Notes\n -----\n Be careful to call this *after* determining the `name` attribute to be\n attached to the result of the arithmetic operation.\n " ]
Please provide a description of the function:def make_invalid_op(name): def invalid_op(self, other=None): raise TypeError("cannot perform {name} with this index type: " "{typ}".format(name=name, typ=type(self).__name__)) invalid_op.__name__ = name return invalid_op
[ "\n Return a binary method that always raises a TypeError.\n\n Parameters\n ----------\n name : str\n\n Returns\n -------\n invalid_op : function\n " ]
Please provide a description of the function:def _gen_eval_kwargs(name): kwargs = {} # Series and Panel appear to only pass __add__, __radd__, ... # but DataFrame gets both these dunder names _and_ non-dunder names # add, radd, ... name = name.replace('__', '') if name.startswith('r'): if name not in ['radd', 'rand', 'ror', 'rxor']: # Exclude commutative operations kwargs['reversed'] = True if name in ['truediv', 'rtruediv']: kwargs['truediv'] = True if name in ['ne']: kwargs['masker'] = True return kwargs
[ "\n Find the keyword arguments to pass to numexpr for the given operation.\n\n Parameters\n ----------\n name : str\n\n Returns\n -------\n eval_kwargs : dict\n\n Examples\n --------\n >>> _gen_eval_kwargs(\"__add__\")\n {}\n\n >>> _gen_eval_kwargs(\"rtruediv\")\n {'reversed': True, 'truediv': True}\n " ]
Please provide a description of the function:def _gen_fill_zeros(name): name = name.strip('__') if 'div' in name: # truediv, floordiv, div, and reversed variants fill_value = np.inf elif 'mod' in name: # mod, rmod fill_value = np.nan else: fill_value = None return fill_value
[ "\n Find the appropriate fill value to use when filling in undefined values\n in the results of the given operation caused by operating on\n (generally dividing by) zero.\n\n Parameters\n ----------\n name : str\n\n Returns\n -------\n fill_value : {None, np.nan, np.inf}\n " ]
Please provide a description of the function:def _get_opstr(op, cls): # numexpr is available for non-sparse classes subtyp = getattr(cls, '_subtyp', '') use_numexpr = 'sparse' not in subtyp if not use_numexpr: # if we're not using numexpr, then don't pass a str_rep return None return {operator.add: '+', radd: '+', operator.mul: '*', rmul: '*', operator.sub: '-', rsub: '-', operator.truediv: '/', rtruediv: '/', operator.floordiv: '//', rfloordiv: '//', operator.mod: None, # TODO: Why None for mod but '%' for rmod? rmod: '%', operator.pow: '**', rpow: '**', operator.eq: '==', operator.ne: '!=', operator.le: '<=', operator.lt: '<', operator.ge: '>=', operator.gt: '>', operator.and_: '&', rand_: '&', operator.or_: '|', ror_: '|', operator.xor: '^', rxor: '^', divmod: None, rdivmod: None}[op]
[ "\n Find the operation string, if any, to pass to numexpr for this\n operation.\n\n Parameters\n ----------\n op : binary operator\n cls : class\n\n Returns\n -------\n op_str : string or None\n " ]
Please provide a description of the function:def _get_op_name(op, special): opname = op.__name__.strip('_') if special: opname = '__{opname}__'.format(opname=opname) return opname
[ "\n Find the name to attach to this method according to conventions\n for special and non-special methods.\n\n Parameters\n ----------\n op : binary operator\n special : bool\n\n Returns\n -------\n op_name : str\n " ]
Please provide a description of the function:def _make_flex_doc(op_name, typ): op_name = op_name.replace('__', '') op_desc = _op_descriptions[op_name] if op_desc['reversed']: equiv = 'other ' + op_desc['op'] + ' ' + typ else: equiv = typ + ' ' + op_desc['op'] + ' other' if typ == 'series': base_doc = _flex_doc_SERIES doc_no_examples = base_doc.format( desc=op_desc['desc'], op_name=op_name, equiv=equiv, reverse=op_desc['reverse'] ) if op_desc['series_examples']: doc = doc_no_examples + op_desc['series_examples'] else: doc = doc_no_examples elif typ == 'dataframe': base_doc = _flex_doc_FRAME doc = base_doc.format( desc=op_desc['desc'], op_name=op_name, equiv=equiv, reverse=op_desc['reverse'] ) elif typ == 'panel': base_doc = _flex_doc_PANEL doc = base_doc.format( desc=op_desc['desc'], op_name=op_name, equiv=equiv, reverse=op_desc['reverse'] ) else: raise AssertionError('Invalid typ argument.') return doc
[ "\n Make the appropriate substitutions for the given operation and class-typ\n into either _flex_doc_SERIES or _flex_doc_FRAME to return the docstring\n to attach to a generated method.\n\n Parameters\n ----------\n op_name : str {'__add__', '__sub__', ... '__eq__', '__ne__', ...}\n typ : str {series, 'dataframe']}\n\n Returns\n -------\n doc : str\n " ]
Please provide a description of the function:def fill_binop(left, right, fill_value): # TODO: can we make a no-copy implementation? if fill_value is not None: left_mask = isna(left) right_mask = isna(right) left = left.copy() right = right.copy() # one but not both mask = left_mask ^ right_mask left[left_mask & mask] = fill_value right[right_mask & mask] = fill_value return left, right
[ "\n If a non-None fill_value is given, replace null entries in left and right\n with this value, but only in positions where _one_ of left/right is null,\n not both.\n\n Parameters\n ----------\n left : array-like\n right : array-like\n fill_value : object\n\n Returns\n -------\n left : array-like\n right : array-like\n\n Notes\n -----\n Makes copies if fill_value is not None\n " ]
Please provide a description of the function:def mask_cmp_op(x, y, op, allowed_types): # TODO: Can we make the allowed_types arg unnecessary? xrav = x.ravel() result = np.empty(x.size, dtype=bool) if isinstance(y, allowed_types): yrav = y.ravel() mask = notna(xrav) & notna(yrav) result[mask] = op(np.array(list(xrav[mask])), np.array(list(yrav[mask]))) else: mask = notna(xrav) result[mask] = op(np.array(list(xrav[mask])), y) if op == operator.ne: # pragma: no cover np.putmask(result, ~mask, True) else: np.putmask(result, ~mask, False) result = result.reshape(x.shape) return result
[ "\n Apply the function `op` to only non-null points in x and y.\n\n Parameters\n ----------\n x : array-like\n y : array-like\n op : binary operation\n allowed_types : class or tuple of classes\n\n Returns\n -------\n result : ndarray[bool]\n " ]
Please provide a description of the function:def masked_arith_op(x, y, op): # For Series `x` is 1D so ravel() is a no-op; calling it anyway makes # the logic valid for both Series and DataFrame ops. xrav = x.ravel() assert isinstance(x, (np.ndarray, ABCSeries)), type(x) if isinstance(y, (np.ndarray, ABCSeries, ABCIndexClass)): dtype = find_common_type([x.dtype, y.dtype]) result = np.empty(x.size, dtype=dtype) # PeriodIndex.ravel() returns int64 dtype, so we have # to work around that case. See GH#19956 yrav = y if is_period_dtype(y) else y.ravel() mask = notna(xrav) & notna(yrav) if yrav.shape != mask.shape: # FIXME: GH#5284, GH#5035, GH#19448 # Without specifically raising here we get mismatched # errors in Py3 (TypeError) vs Py2 (ValueError) # Note: Only = an issue in DataFrame case raise ValueError('Cannot broadcast operands together.') if mask.any(): with np.errstate(all='ignore'): result[mask] = op(xrav[mask], com.values_from_object(yrav[mask])) else: assert is_scalar(y), type(y) assert isinstance(x, np.ndarray), type(x) # mask is only meaningful for x result = np.empty(x.size, dtype=x.dtype) mask = notna(xrav) # 1 ** np.nan is 1. So we have to unmask those. if op == pow: mask = np.where(x == 1, False, mask) elif op == rpow: mask = np.where(y == 1, False, mask) if mask.any(): with np.errstate(all='ignore'): result[mask] = op(xrav[mask], y) result, changed = maybe_upcast_putmask(result, ~mask, np.nan) result = result.reshape(x.shape) # 2D compat return result
[ "\n If the given arithmetic operation fails, attempt it again on\n only the non-null elements of the input array(s).\n\n Parameters\n ----------\n x : np.ndarray\n y : np.ndarray, Series, Index\n op : binary operator\n " ]
Please provide a description of the function:def invalid_comparison(left, right, op): if op is operator.eq: res_values = np.zeros(left.shape, dtype=bool) elif op is operator.ne: res_values = np.ones(left.shape, dtype=bool) else: raise TypeError("Invalid comparison between dtype={dtype} and {typ}" .format(dtype=left.dtype, typ=type(right).__name__)) return res_values
[ "\n If a comparison has mismatched types and is not necessarily meaningful,\n follow python3 conventions by:\n\n - returning all-False for equality\n - returning all-True for inequality\n - raising TypeError otherwise\n\n Parameters\n ----------\n left : array-like\n right : scalar, array-like\n op : operator.{eq, ne, lt, le, gt}\n\n Raises\n ------\n TypeError : on inequality comparisons\n " ]
Please provide a description of the function:def should_series_dispatch(left, right, op): if left._is_mixed_type or right._is_mixed_type: return True if not len(left.columns) or not len(right.columns): # ensure obj.dtypes[0] exists for each obj return False ldtype = left.dtypes.iloc[0] rdtype = right.dtypes.iloc[0] if ((is_timedelta64_dtype(ldtype) and is_integer_dtype(rdtype)) or (is_timedelta64_dtype(rdtype) and is_integer_dtype(ldtype))): # numpy integer dtypes as timedelta64 dtypes in this scenario return True if is_datetime64_dtype(ldtype) and is_object_dtype(rdtype): # in particular case where right is an array of DateOffsets return True return False
[ "\n Identify cases where a DataFrame operation should dispatch to its\n Series counterpart.\n\n Parameters\n ----------\n left : DataFrame\n right : DataFrame\n op : binary operator\n\n Returns\n -------\n override : bool\n " ]
Please provide a description of the function:def dispatch_to_series(left, right, func, str_rep=None, axis=None): # Note: we use iloc to access columns for compat with cases # with non-unique columns. import pandas.core.computation.expressions as expressions right = lib.item_from_zerodim(right) if lib.is_scalar(right) or np.ndim(right) == 0: def column_op(a, b): return {i: func(a.iloc[:, i], b) for i in range(len(a.columns))} elif isinstance(right, ABCDataFrame): assert right._indexed_same(left) def column_op(a, b): return {i: func(a.iloc[:, i], b.iloc[:, i]) for i in range(len(a.columns))} elif isinstance(right, ABCSeries) and axis == "columns": # We only get here if called via left._combine_match_columns, # in which case we specifically want to operate row-by-row assert right.index.equals(left.columns) def column_op(a, b): return {i: func(a.iloc[:, i], b.iloc[i]) for i in range(len(a.columns))} elif isinstance(right, ABCSeries): assert right.index.equals(left.index) # Handle other cases later def column_op(a, b): return {i: func(a.iloc[:, i], b) for i in range(len(a.columns))} else: # Remaining cases have less-obvious dispatch rules raise NotImplementedError(right) new_data = expressions.evaluate(column_op, str_rep, left, right) result = left._constructor(new_data, index=left.index, copy=False) # Pin columns instead of passing to constructor for compat with # non-unique columns case result.columns = left.columns return result
[ "\n Evaluate the frame operation func(left, right) by evaluating\n column-by-column, dispatching to the Series implementation.\n\n Parameters\n ----------\n left : DataFrame\n right : scalar or DataFrame\n func : arithmetic or comparison operator\n str_rep : str or None, default None\n axis : {None, 0, 1, \"index\", \"columns\"}\n\n Returns\n -------\n DataFrame\n " ]
Please provide a description of the function:def dispatch_to_index_op(op, left, right, index_class): left_idx = index_class(left) # avoid accidentally allowing integer add/sub. For datetime64[tz] dtypes, # left_idx may inherit a freq from a cached DatetimeIndex. # See discussion in GH#19147. if getattr(left_idx, 'freq', None) is not None: left_idx = left_idx._shallow_copy(freq=None) try: result = op(left_idx, right) except NullFrequencyError: # DatetimeIndex and TimedeltaIndex with freq == None raise ValueError # on add/sub of integers (or int-like). We re-raise as a TypeError. raise TypeError('incompatible type for a datetime/timedelta ' 'operation [{name}]'.format(name=op.__name__)) return result
[ "\n Wrap Series left in the given index_class to delegate the operation op\n to the index implementation. DatetimeIndex and TimedeltaIndex perform\n type checking, timezone handling, overflow checks, etc.\n\n Parameters\n ----------\n op : binary operator (operator.add, operator.sub, ...)\n left : Series\n right : object\n index_class : DatetimeIndex or TimedeltaIndex\n\n Returns\n -------\n result : object, usually DatetimeIndex, TimedeltaIndex, or Series\n " ]
Please provide a description of the function:def dispatch_to_extension_op(op, left, right): # The op calls will raise TypeError if the op is not defined # on the ExtensionArray # unbox Series and Index to arrays if isinstance(left, (ABCSeries, ABCIndexClass)): new_left = left._values else: new_left = left if isinstance(right, (ABCSeries, ABCIndexClass)): new_right = right._values else: new_right = right res_values = op(new_left, new_right) res_name = get_op_result_name(left, right) if op.__name__ in ['divmod', 'rdivmod']: return _construct_divmod_result( left, res_values, left.index, res_name) return _construct_result(left, res_values, left.index, res_name)
[ "\n Assume that left or right is a Series backed by an ExtensionArray,\n apply the operator defined by op.\n " ]
Please provide a description of the function:def _get_method_wrappers(cls): if issubclass(cls, ABCSparseSeries): # Be sure to catch this before ABCSeries and ABCSparseArray, # as they will both come see SparseSeries as a subclass arith_flex = _flex_method_SERIES comp_flex = _flex_method_SERIES arith_special = _arith_method_SPARSE_SERIES comp_special = _arith_method_SPARSE_SERIES bool_special = _bool_method_SERIES # TODO: I don't think the functions defined by bool_method are tested elif issubclass(cls, ABCSeries): # Just Series; SparseSeries is caught above arith_flex = _flex_method_SERIES comp_flex = _flex_method_SERIES arith_special = _arith_method_SERIES comp_special = _comp_method_SERIES bool_special = _bool_method_SERIES elif issubclass(cls, ABCSparseArray): arith_flex = None comp_flex = None arith_special = _arith_method_SPARSE_ARRAY comp_special = _arith_method_SPARSE_ARRAY bool_special = _arith_method_SPARSE_ARRAY elif issubclass(cls, ABCPanel): arith_flex = _flex_method_PANEL comp_flex = _comp_method_PANEL arith_special = _arith_method_PANEL comp_special = _comp_method_PANEL bool_special = _arith_method_PANEL elif issubclass(cls, ABCDataFrame): # Same for DataFrame and SparseDataFrame arith_flex = _arith_method_FRAME comp_flex = _flex_comp_method_FRAME arith_special = _arith_method_FRAME comp_special = _comp_method_FRAME bool_special = _arith_method_FRAME return arith_flex, comp_flex, arith_special, comp_special, bool_special
[ "\n Find the appropriate operation-wrappers to use when defining flex/special\n arithmetic, boolean, and comparison operations with the given class.\n\n Parameters\n ----------\n cls : class\n\n Returns\n -------\n arith_flex : function or None\n comp_flex : function or None\n arith_special : function\n comp_special : function\n bool_special : function\n\n Notes\n -----\n None is only returned for SparseArray\n " ]
Please provide a description of the function:def add_special_arithmetic_methods(cls): _, _, arith_method, comp_method, bool_method = _get_method_wrappers(cls) new_methods = _create_methods(cls, arith_method, comp_method, bool_method, special=True) # inplace operators (I feel like these should get passed an `inplace=True` # or just be removed def _wrap_inplace_method(method): def f(self, other): result = method(self, other) # this makes sure that we are aligned like the input # we are updating inplace so we want to ignore is_copy self._update_inplace(result.reindex_like(self, copy=False)._data, verify_is_copy=False) return self f.__name__ = "__i{name}__".format(name=method.__name__.strip("__")) return f new_methods.update( dict(__iadd__=_wrap_inplace_method(new_methods["__add__"]), __isub__=_wrap_inplace_method(new_methods["__sub__"]), __imul__=_wrap_inplace_method(new_methods["__mul__"]), __itruediv__=_wrap_inplace_method(new_methods["__truediv__"]), __ifloordiv__=_wrap_inplace_method(new_methods["__floordiv__"]), __imod__=_wrap_inplace_method(new_methods["__mod__"]), __ipow__=_wrap_inplace_method(new_methods["__pow__"]))) new_methods.update( dict(__iand__=_wrap_inplace_method(new_methods["__and__"]), __ior__=_wrap_inplace_method(new_methods["__or__"]), __ixor__=_wrap_inplace_method(new_methods["__xor__"]))) add_methods(cls, new_methods=new_methods)
[ "\n Adds the full suite of special arithmetic methods (``__add__``,\n ``__sub__``, etc.) to the class.\n\n Parameters\n ----------\n cls : class\n special methods will be defined and pinned to this class\n ", "\n return an inplace wrapper for this method\n " ]
Please provide a description of the function:def add_flex_arithmetic_methods(cls): flex_arith_method, flex_comp_method, _, _, _ = _get_method_wrappers(cls) new_methods = _create_methods(cls, flex_arith_method, flex_comp_method, bool_method=None, special=False) new_methods.update(dict(multiply=new_methods['mul'], subtract=new_methods['sub'], divide=new_methods['div'])) # opt out of bool flex methods for now assert not any(kname in new_methods for kname in ('ror_', 'rxor', 'rand_')) add_methods(cls, new_methods=new_methods)
[ "\n Adds the full suite of flex arithmetic methods (``pow``, ``mul``, ``add``)\n to the class.\n\n Parameters\n ----------\n cls : class\n flex methods will be defined and pinned to this class\n " ]
Please provide a description of the function:def _align_method_SERIES(left, right, align_asobject=False): # ToDo: Different from _align_method_FRAME, list, tuple and ndarray # are not coerced here # because Series has inconsistencies described in #13637 if isinstance(right, ABCSeries): # avoid repeated alignment if not left.index.equals(right.index): if align_asobject: # to keep original value's dtype for bool ops left = left.astype(object) right = right.astype(object) left, right = left.align(right, copy=False) return left, right
[ " align lhs and rhs Series " ]
Please provide a description of the function:def _construct_result(left, result, index, name, dtype=None): out = left._constructor(result, index=index, dtype=dtype) out = out.__finalize__(left) out.name = name return out
[ "\n If the raw op result has a non-None name (e.g. it is an Index object) and\n the name argument is None, then passing name to the constructor will\n not be enough; we still need to override the name attribute.\n " ]
Please provide a description of the function:def _construct_divmod_result(left, result, index, name, dtype=None): return ( _construct_result(left, result[0], index=index, name=name, dtype=dtype), _construct_result(left, result[1], index=index, name=name, dtype=dtype), )
[ "divmod returns a tuple of like indexed series instead of a single series.\n " ]