diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 688f0226dcdba..b4b8d151151bd 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -369,6 +369,9 @@ def _constructor_expanddim(self): from pandas.core.panel import Panel return Panel + # ---------------------------------------------------------------------- + # Constructors + def __init__(self, data=None, index=None, columns=None, dtype=None, copy=False): if data is None: @@ -575,6 +578,8 @@ def _get_axes(N, K, index=index, columns=columns): return create_block_manager_from_blocks([values], [columns, index]) + # ---------------------------------------------------------------------- + @property def axes(self): """ @@ -643,6 +648,9 @@ def _is_homogeneous_type(self): else: return not self._data.is_mixed_type + # ---------------------------------------------------------------------- + # Rendering Methods + def _repr_fits_vertical_(self): """ Check length against max_rows. @@ -770,6 +778,57 @@ def _repr_html_(self): else: return None + @Substitution(header='Write out the column names. If a list of strings ' + 'is given, it is assumed to be aliases for the ' + 'column names') + @Substitution(shared_params=fmt.common_docstring, + returns=fmt.return_docstring) + def to_string(self, buf=None, columns=None, col_space=None, header=True, + index=True, na_rep='NaN', formatters=None, float_format=None, + sparsify=None, index_names=True, justify=None, + max_rows=None, max_cols=None, show_dimensions=False, + decimal='.', line_width=None): + """ + Render a DataFrame to a console-friendly tabular output. + %(shared_params)s + line_width : int, optional + Width to wrap a line in characters. + %(returns)s + See Also + -------- + to_html : Convert DataFrame to HTML. + + Examples + -------- + >>> d = {'col1': [1, 2, 3], 'col2': [4, 5, 6]} + >>> df = pd.DataFrame(d) + >>> print(df.to_string()) + col1 col2 + 0 1 4 + 1 2 5 + 2 3 6 + """ + + formatter = fmt.DataFrameFormatter(self, buf=buf, columns=columns, + col_space=col_space, na_rep=na_rep, + formatters=formatters, + float_format=float_format, + sparsify=sparsify, justify=justify, + index_names=index_names, + header=header, index=index, + max_rows=max_rows, + max_cols=max_cols, + show_dimensions=show_dimensions, + decimal=decimal, + line_width=line_width) + formatter.to_string() + + if buf is None: + result = formatter.buf.getvalue() + return result + + # ---------------------------------------------------------------------- + @property def style(self): """ @@ -2051,55 +2110,6 @@ def to_parquet(self, fname, engine='auto', compression='snappy', compression=compression, index=index, partition_cols=partition_cols, **kwargs) - @Substitution(header='Write out the column names. If a list of strings ' - 'is given, it is assumed to be aliases for the ' - 'column names') - @Substitution(shared_params=fmt.common_docstring, - returns=fmt.return_docstring) - def to_string(self, buf=None, columns=None, col_space=None, header=True, - index=True, na_rep='NaN', formatters=None, float_format=None, - sparsify=None, index_names=True, justify=None, - max_rows=None, max_cols=None, show_dimensions=False, - decimal='.', line_width=None): - """ - Render a DataFrame to a console-friendly tabular output. - %(shared_params)s - line_width : int, optional - Width to wrap a line in characters. - %(returns)s - See Also - -------- - to_html : Convert DataFrame to HTML. - - Examples - -------- - >>> d = {'col1': [1, 2, 3], 'col2': [4, 5, 6]} - >>> df = pd.DataFrame(d) - >>> print(df.to_string()) - col1 col2 - 0 1 4 - 1 2 5 - 2 3 6 - """ - - formatter = fmt.DataFrameFormatter(self, buf=buf, columns=columns, - col_space=col_space, na_rep=na_rep, - formatters=formatters, - float_format=float_format, - sparsify=sparsify, justify=justify, - index_names=index_names, - header=header, index=index, - max_rows=max_rows, - max_cols=max_cols, - show_dimensions=show_dimensions, - decimal=decimal, - line_width=line_width) - formatter.to_string() - - if buf is None: - result = formatter.buf.getvalue() - return result - @Substitution(header='Whether to print column labels, default True') @Substitution(shared_params=fmt.common_docstring, returns=fmt.return_docstring) @@ -2158,6 +2168,8 @@ def to_html(self, buf=None, columns=None, col_space=None, header=True, if buf is None: return formatter.buf.getvalue() + # ---------------------------------------------------------------------- + def info(self, verbose=None, buf=None, max_cols=None, memory_usage=None, null_counts=None): """ diff --git a/pandas/core/generic.py b/pandas/core/generic.py index e8402e7eefd90..f43b93f200db3 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -111,6 +111,9 @@ class NDFrame(PandasObject, SelectionMixin): _metadata = [] _is_copy = None + # ---------------------------------------------------------------------- + # Constructors + def __init__(self, data, axes=None, copy=False, dtype=None, fastpath=False): @@ -128,6 +131,25 @@ def __init__(self, data, axes=None, copy=False, dtype=None, object.__setattr__(self, '_data', data) object.__setattr__(self, '_item_cache', {}) + def _init_mgr(self, mgr, axes=None, dtype=None, copy=False): + """ passed a manager and a axes dict """ + for a, axe in axes.items(): + if axe is not None: + mgr = mgr.reindex_axis(axe, + axis=self._get_block_manager_axis(a), + copy=False) + + # make a copy if explicitly requested + if copy: + mgr = mgr.copy() + if dtype is not None: + # avoid further copies if we can + if len(mgr.blocks) > 1 or mgr.blocks[0].values.dtype != dtype: + mgr = mgr.astype(dtype=dtype) + return mgr + + # ---------------------------------------------------------------------- + @property def is_copy(self): warnings.warn("Attribute 'is_copy' is deprecated and will be removed " @@ -140,17 +162,6 @@ def is_copy(self, msg): "in a future version.", FutureWarning, stacklevel=2) self._is_copy = msg - def _repr_data_resource_(self): - """ - Not a real Jupyter special repr method, but we use the same - naming convention. - """ - if config.get_option("display.html.table_schema"): - data = self.head(config.get_option('display.max_rows')) - payload = json.loads(data.to_json(orient='table'), - object_pairs_hook=collections.OrderedDict) - return payload - def _validate_dtype(self, dtype): """ validate the passed dtype """ @@ -165,23 +176,6 @@ def _validate_dtype(self, dtype): return dtype - def _init_mgr(self, mgr, axes=None, dtype=None, copy=False): - """ passed a manager and a axes dict """ - for a, axe in axes.items(): - if axe is not None: - mgr = mgr.reindex_axis(axe, - axis=self._get_block_manager_axis(a), - copy=False) - - # make a copy if explicitly requested - if copy: - mgr = mgr.copy() - if dtype is not None: - # avoid further copies if we can - if len(mgr.blocks) > 1 or mgr.blocks[0].values.dtype != dtype: - mgr = mgr.astype(dtype=dtype) - return mgr - # ---------------------------------------------------------------------- # Construction @@ -192,20 +186,6 @@ def _constructor(self): """ raise AbstractMethodError(self) - def __unicode__(self): - # unicode representation based upon iterating over self - # (since, by definition, `PandasContainers` are iterable) - prepr = '[%s]' % ','.join(map(pprint_thing, self)) - return '%s(%s)' % (self.__class__.__name__, prepr) - - def _dir_additions(self): - """ add the string-like attributes from the info_axis. - If info_axis is a MultiIndex, it's first level values are used. - """ - additions = {c for c in self._info_axis.unique(level=0)[:100] - if isinstance(c, string_types) and isidentifier(c)} - return super(NDFrame, self)._dir_additions().union(additions) - @property def _constructor_sliced(self): """Used when a manipulation result has one lower dimension(s) as the @@ -1338,48 +1318,12 @@ def _set_axis_name(self, name, axis=0, inplace=False): return renamed # ---------------------------------------------------------------------- - # Comparisons + # Comparison Methods def _indexed_same(self, other): return all(self._get_axis(a).equals(other._get_axis(a)) for a in self._AXIS_ORDERS) - def __neg__(self): - values = com.values_from_object(self) - if is_bool_dtype(values): - arr = operator.inv(values) - elif (is_numeric_dtype(values) or is_timedelta64_dtype(values) - or is_object_dtype(values)): - arr = operator.neg(values) - else: - raise TypeError("Unary negative expects numeric dtype, not {}" - .format(values.dtype)) - return self.__array_wrap__(arr) - - def __pos__(self): - values = com.values_from_object(self) - if (is_bool_dtype(values) or is_period_arraylike(values)): - arr = values - elif (is_numeric_dtype(values) or is_timedelta64_dtype(values) - or is_object_dtype(values)): - arr = operator.pos(values) - else: - raise TypeError("Unary plus expects numeric dtype, not {}" - .format(values.dtype)) - return self.__array_wrap__(arr) - - def __invert__(self): - try: - arr = operator.inv(com.values_from_object(self)) - return self.__array_wrap__(arr) - except Exception: - - # inv fails with 0 len - if not np.prod(self.shape): - return self - - raise - def equals(self, other): """ Test whether two objects contain the same elements. @@ -1466,6 +1410,74 @@ def equals(self, other): return False return self._data.equals(other._data) + # ------------------------------------------------------------------------- + # Unary Methods + + def __neg__(self): + values = com.values_from_object(self) + if is_bool_dtype(values): + arr = operator.inv(values) + elif (is_numeric_dtype(values) or is_timedelta64_dtype(values) + or is_object_dtype(values)): + arr = operator.neg(values) + else: + raise TypeError("Unary negative expects numeric dtype, not {}" + .format(values.dtype)) + return self.__array_wrap__(arr) + + def __pos__(self): + values = com.values_from_object(self) + if (is_bool_dtype(values) or is_period_arraylike(values)): + arr = values + elif (is_numeric_dtype(values) or is_timedelta64_dtype(values) + or is_object_dtype(values)): + arr = operator.pos(values) + else: + raise TypeError("Unary plus expects numeric dtype, not {}" + .format(values.dtype)) + return self.__array_wrap__(arr) + + def __invert__(self): + try: + arr = operator.inv(com.values_from_object(self)) + return self.__array_wrap__(arr) + except Exception: + + # inv fails with 0 len + if not np.prod(self.shape): + return self + + raise + + def __nonzero__(self): + raise ValueError("The truth value of a {0} is ambiguous. " + "Use a.empty, a.bool(), a.item(), a.any() or a.all()." + .format(self.__class__.__name__)) + + __bool__ = __nonzero__ + + def bool(self): + """Return the bool of a single element PandasObject. + + This must be a boolean scalar value, either True or False. Raise a + ValueError if the PandasObject does not have exactly 1 element, or that + element is not boolean + """ + v = self.squeeze() + if isinstance(v, (bool, np.bool_)): + return bool(v) + elif is_scalar(v): + raise ValueError("bool cannot act on a non-boolean single element " + "{0}".format(self.__class__.__name__)) + + self.__nonzero__() + + def __abs__(self): + return self.abs() + + def __round__(self, decimals=0): + return self.round(decimals) + # ------------------------------------------------------------------------- # Label or Level Combination Helpers # @@ -1858,35 +1870,6 @@ def empty(self): """ return any(len(self._get_axis(a)) == 0 for a in self._AXIS_ORDERS) - def __nonzero__(self): - raise ValueError("The truth value of a {0} is ambiguous. " - "Use a.empty, a.bool(), a.item(), a.any() or a.all()." - .format(self.__class__.__name__)) - - __bool__ = __nonzero__ - - def bool(self): - """Return the bool of a single element PandasObject. - - This must be a boolean scalar value, either True or False. Raise a - ValueError if the PandasObject does not have exactly 1 element, or that - element is not boolean - """ - v = self.squeeze() - if isinstance(v, (bool, np.bool_)): - return bool(v) - elif is_scalar(v): - raise ValueError("bool cannot act on a non-boolean single element " - "{0}".format(self.__class__.__name__)) - - self.__nonzero__() - - def __abs__(self): - return self.abs() - - def __round__(self, decimals=0): - return self.round(decimals) - # ---------------------------------------------------------------------- # Array Interface @@ -1962,7 +1945,13 @@ def __setstate__(self, state): self._item_cache = {} # ---------------------------------------------------------------------- - # IO + # Rendering Methods + + def __unicode__(self): + # unicode representation based upon iterating over self + # (since, by definition, `PandasContainers` are iterable) + prepr = '[%s]' % ','.join(map(pprint_thing, self)) + return '%s(%s)' % (self.__class__.__name__, prepr) def _repr_latex_(self): """ @@ -1974,6 +1963,17 @@ def _repr_latex_(self): else: return None + def _repr_data_resource_(self): + """ + Not a real Jupyter special repr method, but we use the same + naming convention. + """ + if config.get_option("display.html.table_schema"): + data = self.head(config.get_option('display.max_rows')) + payload = json.loads(data.to_json(orient='table'), + object_pairs_hook=collections.OrderedDict) + return payload + # ---------------------------------------------------------------------- # I/O Methods @@ -2079,6 +2079,25 @@ def _repr_latex_(self): >>> df1.to_excel('output1.xlsx', engine='xlsxwriter') # doctest: +SKIP """ + @Appender(_shared_docs["to_excel"] % dict(klass="object")) + def to_excel(self, excel_writer, sheet_name="Sheet1", na_rep="", + float_format=None, columns=None, header=True, index=True, + index_label=None, startrow=0, startcol=0, engine=None, + merge_cells=True, encoding=None, inf_rep="inf", verbose=True, + freeze_panes=None): + df = self if isinstance(self, ABCDataFrame) else self.to_frame() + + from pandas.io.formats.excel import ExcelFormatter + formatter = ExcelFormatter(df, na_rep=na_rep, cols=columns, + header=header, + float_format=float_format, index=index, + index_label=index_label, + merge_cells=merge_cells, + inf_rep=inf_rep) + formatter.write(excel_writer, sheet_name=sheet_name, startrow=startrow, + startcol=startcol, freeze_panes=freeze_panes, + engine=engine) + def to_json(self, path_or_buf=None, orient=None, date_format=None, double_precision=10, force_ascii=True, date_unit='ms', default_handler=None, lines=False, compression='infer', @@ -2769,57 +2788,199 @@ def to_latex(self, buf=None, columns=None, col_space=None, header=True, Returns ------- - str or None - If buf is None, returns the resulting LateX format as a + str or None + If buf is None, returns the resulting LateX format as a + string. Otherwise returns None. + + See Also + -------- + DataFrame.to_string : Render a DataFrame to a console-friendly + tabular output. + DataFrame.to_html : Render a DataFrame as an HTML table. + + Examples + -------- + >>> df = pd.DataFrame({'name': ['Raphael', 'Donatello'], + ... 'mask': ['red', 'purple'], + ... 'weapon': ['sai', 'bo staff']}) + >>> df.to_latex(index=False) # doctest: +NORMALIZE_WHITESPACE + '\\begin{tabular}{lll}\n\\toprule\n name & mask & weapon + \\\\\n\\midrule\n Raphael & red & sai \\\\\n Donatello & + purple & bo staff \\\\\n\\bottomrule\n\\end{tabular}\n' + """ + # Get defaults from the pandas config + if self.ndim == 1: + self = self.to_frame() + if longtable is None: + longtable = config.get_option("display.latex.longtable") + if escape is None: + escape = config.get_option("display.latex.escape") + if multicolumn is None: + multicolumn = config.get_option("display.latex.multicolumn") + if multicolumn_format is None: + multicolumn_format = config.get_option( + "display.latex.multicolumn_format") + if multirow is None: + multirow = config.get_option("display.latex.multirow") + + formatter = DataFrameFormatter(self, buf=buf, columns=columns, + col_space=col_space, na_rep=na_rep, + header=header, index=index, + formatters=formatters, + float_format=float_format, + bold_rows=bold_rows, + sparsify=sparsify, + index_names=index_names, + escape=escape, decimal=decimal) + formatter.to_latex(column_format=column_format, longtable=longtable, + encoding=encoding, multicolumn=multicolumn, + multicolumn_format=multicolumn_format, + multirow=multirow) + + if buf is None: + return formatter.buf.getvalue() + + def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None, + columns=None, header=True, index=True, index_label=None, + mode='w', encoding=None, compression='infer', quoting=None, + quotechar='"', line_terminator=None, chunksize=None, + tupleize_cols=None, date_format=None, doublequote=True, + escapechar=None, decimal='.'): + r""" + Write object to a comma-separated values (csv) file. + + .. versionchanged:: 0.24.0 + The order of arguments for Series was changed. + + Parameters + ---------- + path_or_buf : str or file handle, default None + File path or object, if None is provided the result is returned as + a string. + + .. versionchanged:: 0.24.0 + + Was previously named "path" for Series. + + sep : str, default ',' + String of length 1. Field delimiter for the output file. + na_rep : str, default '' + Missing data representation. + float_format : str, default None + Format string for floating point numbers. + columns : sequence, optional + Columns to write. + header : bool or list of str, default True + Write out the column names. If a list of strings is given it is + assumed to be aliases for the column names. + + .. versionchanged:: 0.24.0 + + Previously defaulted to False for Series. + + index : bool, default True + Write row names (index). + index_label : str or sequence, or False, default None + Column label for index column(s) if desired. If None is given, and + `header` and `index` are True, then the index names are used. A + sequence should be given if the object uses MultiIndex. If + False do not print fields for index names. Use index_label=False + for easier importing in R. + mode : str + Python write mode, default 'w'. + encoding : str, optional + A string representing the encoding to use in the output file, + defaults to 'ascii' on Python 2 and 'utf-8' on Python 3. + compression : str, default 'infer' + Compression mode among the following possible values: {'infer', + 'gzip', 'bz2', 'zip', 'xz', None}. If 'infer' and `path_or_buf` + is path-like, then detect compression from the following + extensions: '.gz', '.bz2', '.zip' or '.xz'. (otherwise no + compression). + + .. versionchanged:: 0.24.0 + + 'infer' option added and set to default. + + quoting : optional constant from csv module + Defaults to csv.QUOTE_MINIMAL. If you have set a `float_format` + then floats are converted to strings and thus csv.QUOTE_NONNUMERIC + will treat them as non-numeric. + quotechar : str, default '\"' + String of length 1. Character used to quote fields. + line_terminator : string, optional + The newline character or character sequence to use in the output + file. Defaults to `os.linesep`, which depends on the OS in which + this method is called ('\n' for linux, '\r\n' for Windows, i.e.). + + .. versionchanged:: 0.24.0 + chunksize : int or None + Rows to write at a time. + tupleize_cols : bool, default False + Write MultiIndex columns as a list of tuples (if True) or in + the new, expanded format, where each MultiIndex column is a row + in the CSV (if False). + + .. deprecated:: 0.21.0 + This argument will be removed and will always write each row + of the multi-index as a separate row in the CSV file. + date_format : str, default None + Format string for datetime objects. + doublequote : bool, default True + Control quoting of `quotechar` inside a field. + escapechar : str, default None + String of length 1. Character used to escape `sep` and `quotechar` + when appropriate. + decimal : str, default '.' + Character recognized as decimal separator. E.g. use ',' for + European data. + + Returns + ------- + None or str + If path_or_buf is None, returns the resulting csv format as a string. Otherwise returns None. See Also -------- - DataFrame.to_string : Render a DataFrame to a console-friendly - tabular output. - DataFrame.to_html : Render a DataFrame as an HTML table. + read_csv : Load a CSV file into a DataFrame. + to_excel : Load an Excel file into a DataFrame. Examples -------- >>> df = pd.DataFrame({'name': ['Raphael', 'Donatello'], ... 'mask': ['red', 'purple'], ... 'weapon': ['sai', 'bo staff']}) - >>> df.to_latex(index=False) # doctest: +NORMALIZE_WHITESPACE - '\\begin{tabular}{lll}\n\\toprule\n name & mask & weapon - \\\\\n\\midrule\n Raphael & red & sai \\\\\n Donatello & - purple & bo staff \\\\\n\\bottomrule\n\\end{tabular}\n' + >>> df.to_csv(index=False) + 'name,mask,weapon\nRaphael,red,sai\nDonatello,purple,bo staff\n' """ - # Get defaults from the pandas config - if self.ndim == 1: - self = self.to_frame() - if longtable is None: - longtable = config.get_option("display.latex.longtable") - if escape is None: - escape = config.get_option("display.latex.escape") - if multicolumn is None: - multicolumn = config.get_option("display.latex.multicolumn") - if multicolumn_format is None: - multicolumn_format = config.get_option( - "display.latex.multicolumn_format") - if multirow is None: - multirow = config.get_option("display.latex.multirow") - formatter = DataFrameFormatter(self, buf=buf, columns=columns, - col_space=col_space, na_rep=na_rep, - header=header, index=index, - formatters=formatters, - float_format=float_format, - bold_rows=bold_rows, - sparsify=sparsify, - index_names=index_names, - escape=escape, decimal=decimal) - formatter.to_latex(column_format=column_format, longtable=longtable, - encoding=encoding, multicolumn=multicolumn, - multicolumn_format=multicolumn_format, - multirow=multirow) + df = self if isinstance(self, ABCDataFrame) else self.to_frame() - if buf is None: - return formatter.buf.getvalue() + if tupleize_cols is not None: + warnings.warn("The 'tupleize_cols' parameter is deprecated and " + "will be removed in a future version", + FutureWarning, stacklevel=2) + else: + tupleize_cols = False + + from pandas.io.formats.csvs import CSVFormatter + formatter = CSVFormatter(df, path_or_buf, + line_terminator=line_terminator, sep=sep, + encoding=encoding, + compression=compression, quoting=quoting, + na_rep=na_rep, float_format=float_format, + cols=columns, header=header, index=index, + index_label=index_label, mode=mode, + chunksize=chunksize, quotechar=quotechar, + tupleize_cols=tupleize_cols, + date_format=date_format, + doublequote=doublequote, + escapechar=escapechar, decimal=decimal) + formatter.save() + + if path_or_buf is None: + return formatter.path_or_buf.getvalue() # ---------------------------------------------------------------------- # Fancy Indexing @@ -4857,6 +5018,14 @@ def __setattr__(self, name, value): stacklevel=2) object.__setattr__(self, name, value) + def _dir_additions(self): + """ add the string-like attributes from the info_axis. + If info_axis is a MultiIndex, it's first level values are used. + """ + additions = {c for c in self._info_axis.unique(level=0)[:100] + if isinstance(c, string_types) and isidentifier(c)} + return super(NDFrame, self)._dir_additions().union(additions) + # ---------------------------------------------------------------------- # Getting and setting elements @@ -9909,167 +10078,6 @@ def first_valid_index(self): def last_valid_index(self): return self._find_valid_index('last') - def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None, - columns=None, header=True, index=True, index_label=None, - mode='w', encoding=None, compression='infer', quoting=None, - quotechar='"', line_terminator=None, chunksize=None, - tupleize_cols=None, date_format=None, doublequote=True, - escapechar=None, decimal='.'): - r""" - Write object to a comma-separated values (csv) file. - - .. versionchanged:: 0.24.0 - The order of arguments for Series was changed. - - Parameters - ---------- - path_or_buf : str or file handle, default None - File path or object, if None is provided the result is returned as - a string. - - .. versionchanged:: 0.24.0 - - Was previously named "path" for Series. - - sep : str, default ',' - String of length 1. Field delimiter for the output file. - na_rep : str, default '' - Missing data representation. - float_format : str, default None - Format string for floating point numbers. - columns : sequence, optional - Columns to write. - header : bool or list of str, default True - Write out the column names. If a list of strings is given it is - assumed to be aliases for the column names. - - .. versionchanged:: 0.24.0 - - Previously defaulted to False for Series. - - index : bool, default True - Write row names (index). - index_label : str or sequence, or False, default None - Column label for index column(s) if desired. If None is given, and - `header` and `index` are True, then the index names are used. A - sequence should be given if the object uses MultiIndex. If - False do not print fields for index names. Use index_label=False - for easier importing in R. - mode : str - Python write mode, default 'w'. - encoding : str, optional - A string representing the encoding to use in the output file, - defaults to 'ascii' on Python 2 and 'utf-8' on Python 3. - compression : str, default 'infer' - Compression mode among the following possible values: {'infer', - 'gzip', 'bz2', 'zip', 'xz', None}. If 'infer' and `path_or_buf` - is path-like, then detect compression from the following - extensions: '.gz', '.bz2', '.zip' or '.xz'. (otherwise no - compression). - - .. versionchanged:: 0.24.0 - - 'infer' option added and set to default. - - quoting : optional constant from csv module - Defaults to csv.QUOTE_MINIMAL. If you have set a `float_format` - then floats are converted to strings and thus csv.QUOTE_NONNUMERIC - will treat them as non-numeric. - quotechar : str, default '\"' - String of length 1. Character used to quote fields. - line_terminator : string, optional - The newline character or character sequence to use in the output - file. Defaults to `os.linesep`, which depends on the OS in which - this method is called ('\n' for linux, '\r\n' for Windows, i.e.). - - .. versionchanged:: 0.24.0 - chunksize : int or None - Rows to write at a time. - tupleize_cols : bool, default False - Write MultiIndex columns as a list of tuples (if True) or in - the new, expanded format, where each MultiIndex column is a row - in the CSV (if False). - - .. deprecated:: 0.21.0 - This argument will be removed and will always write each row - of the multi-index as a separate row in the CSV file. - date_format : str, default None - Format string for datetime objects. - doublequote : bool, default True - Control quoting of `quotechar` inside a field. - escapechar : str, default None - String of length 1. Character used to escape `sep` and `quotechar` - when appropriate. - decimal : str, default '.' - Character recognized as decimal separator. E.g. use ',' for - European data. - - Returns - ------- - None or str - If path_or_buf is None, returns the resulting csv format as a - string. Otherwise returns None. - - See Also - -------- - read_csv : Load a CSV file into a DataFrame. - to_excel : Load an Excel file into a DataFrame. - - Examples - -------- - >>> df = pd.DataFrame({'name': ['Raphael', 'Donatello'], - ... 'mask': ['red', 'purple'], - ... 'weapon': ['sai', 'bo staff']}) - >>> df.to_csv(index=False) - 'name,mask,weapon\nRaphael,red,sai\nDonatello,purple,bo staff\n' - """ - - df = self if isinstance(self, ABCDataFrame) else self.to_frame() - - if tupleize_cols is not None: - warnings.warn("The 'tupleize_cols' parameter is deprecated and " - "will be removed in a future version", - FutureWarning, stacklevel=2) - else: - tupleize_cols = False - - from pandas.io.formats.csvs import CSVFormatter - formatter = CSVFormatter(df, path_or_buf, - line_terminator=line_terminator, sep=sep, - encoding=encoding, - compression=compression, quoting=quoting, - na_rep=na_rep, float_format=float_format, - cols=columns, header=header, index=index, - index_label=index_label, mode=mode, - chunksize=chunksize, quotechar=quotechar, - tupleize_cols=tupleize_cols, - date_format=date_format, - doublequote=doublequote, - escapechar=escapechar, decimal=decimal) - formatter.save() - - if path_or_buf is None: - return formatter.path_or_buf.getvalue() - - @Appender(_shared_docs["to_excel"] % dict(klass="object")) - def to_excel(self, excel_writer, sheet_name="Sheet1", na_rep="", - float_format=None, columns=None, header=True, index=True, - index_label=None, startrow=0, startcol=0, engine=None, - merge_cells=True, encoding=None, inf_rep="inf", verbose=True, - freeze_panes=None): - df = self if isinstance(self, ABCDataFrame) else self.to_frame() - - from pandas.io.formats.excel import ExcelFormatter - formatter = ExcelFormatter(df, na_rep=na_rep, cols=columns, - header=header, - float_format=float_format, index=index, - index_label=index_label, - merge_cells=merge_cells, - inf_rep=inf_rep) - formatter.write(excel_writer, sheet_name=sheet_name, startrow=startrow, - startcol=startcol, freeze_panes=freeze_panes, - engine=engine) - def _doc_parms(cls): """Return a tuple of the doc parms.""" diff --git a/pandas/core/series.py b/pandas/core/series.py index 4b8274a9e8333..c3bcd2d76c27a 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -149,6 +149,9 @@ class Series(base.IndexOpsMixin, generic.NDFrame): hasnans = property(base.IndexOpsMixin.hasnans.func, doc=base.IndexOpsMixin.hasnans.__doc__) + # ---------------------------------------------------------------------- + # Constructors + def __init__(self, data=None, index=None, dtype=None, name=None, copy=False, fastpath=False): @@ -328,6 +331,8 @@ def from_array(cls, arr, index=None, name=None, dtype=None, copy=False, return cls(arr, index=index, name=name, dtype=dtype, copy=copy, fastpath=fastpath) + # ---------------------------------------------------------------------- + @property def _constructor(self): return Series @@ -635,6 +640,9 @@ def view(self, dtype=None): return self._constructor(self._values.view(dtype), index=self.index).__finalize__(self) + # ---------------------------------------------------------------------- + # NDArray Compat + def __array__(self, result=None): """ The array interface, return my values. @@ -665,7 +673,9 @@ def __array_prepare__(self, result, context=None): op=context[0].__name__)) return result - # complex + # ---------------------------------------------------------------------- + # Unary Methods + @property def real(self): return self.values.real @@ -687,6 +697,8 @@ def imag(self, v): __long__ = _coerce_method(int) __int__ = _coerce_method(int) + # ---------------------------------------------------------------------- + def _unpickle_series_compat(self, state): if isinstance(state, dict): self._data = state['_data'] @@ -1224,6 +1236,9 @@ def reset_index(self, level=None, drop=False, name=None, inplace=False): df = self.to_frame(name) return df.reset_index(level=level, drop=drop) + # ---------------------------------------------------------------------- + # Rendering Methods + def __unicode__(self): """ Return a string representation for a particular DataFrame. @@ -1299,6 +1314,8 @@ def to_string(self, buf=None, na_rep='NaN', float_format=None, header=True, with open(buf, 'w') as f: f.write(result) + # ---------------------------------------------------------------------- + def iteritems(self): """ Lazily iterate over (index, value) tuples.