Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 126 additions & 1 deletion python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: Copyright (c) 2018-2026, NVIDIA CORPORATION.
# SPDX-FileCopyrightText: Copyright (c) 2018-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations
Expand Down Expand Up @@ -2642,6 +2642,131 @@ def to_dict(

return self.to_pandas().to_dict(orient=orient, into=into, index=index)

@_performance_tracking
def to_string(
self,
buf=None,
*,
columns=None,
col_space=None,
header=True,
index: bool = True,
na_rep: str = "NaN",
formatters=None,
float_format=None,
sparsify: bool | None = None,
index_names: bool = True,
justify: str | None = None,
max_rows: int | None = None,
max_cols: int | None = None,
show_dimensions: bool = False,
decimal: str = ".",
line_width: int | None = None,
min_rows: int | None = None,
max_colwidth: int | None = None,
encoding: str | None = None,
) -> str | None:
r"""
Render a DataFrame to a console-friendly tabular output.

cuDF uses pandas internals for string formatting, so this mirrors
:meth:`pandas.DataFrame.to_string` and accepts the same arguments.
Unlike ``repr``, the output is not truncated by the
``display.max_rows``/``display.max_columns`` options unless
``max_rows``/``max_cols`` are passed explicitly.

cuDF supports `null/None` as a value in any column type, which
is transparently supported during this output process.

Parameters
----------
buf : writable buffer, optional
File path or buffer to write to. If ``None`` (default), the
output is returned as a string.
columns : sequence, optional
The subset of columns to write. Writes all columns by default.
col_space : int, list or dict of int, optional
The minimum width of each column.
header : bool or list of str, default True
Whether to write the column names. If a list of strings is
given, it is assumed to be aliases for the column names.
index : bool, default True
Whether to print index (row) labels.
na_rep : str, default "NaN"
String representation of ``NaN``/null values.
formatters : list, tuple or dict of callables, optional
One-parameter formatter functions to apply to columns' elements
by position or name.
float_format : callable, optional
One-parameter formatter function applied to floating point
values.
sparsify : bool, optional
Set to ``False`` for a DataFrame with a hierarchical index to
print every multiindex key at each row. Defaults to ``True``.
index_names : bool, default True
Whether to print the names of the indexes.
justify : str, optional
How to justify the column labels (e.g. ``"left"``, ``"right"``,
``"center"``). Defaults to the pandas display option.
max_rows : int, optional
Maximum number of rows to display before truncating.
max_cols : int, optional
Maximum number of columns to display before truncating.
show_dimensions : bool, default False
Whether to display the DataFrame dimensions (number of rows
and columns).
decimal : str, default "."
Character recognized as the decimal separator, e.g. ``","`` in
Europe.
line_width : int, optional
Width, in characters, at which to wrap a line.
min_rows : int, optional
Number of rows to show in a truncated output (when the number
of rows exceeds ``max_rows``).
max_colwidth : int, optional
Maximum width, in characters, to truncate each column. By
default there is no limit.
encoding : str, optional
Character encoding to use when writing to ``buf``.

Returns
-------
str or None
The string representation of the DataFrame when ``buf`` is
``None``; otherwise the result is written to ``buf`` and
``None`` is returned.

Examples
--------
>>> import cudf
>>> df = cudf.DataFrame()
>>> df['key'] = [0, 1, 2]
>>> df['val'] = [float(i + 10) for i in range(3)]
>>> df.to_string()
' key val\n0 0 10.0\n1 1 11.0\n2 2 12.0'
"""
return self.to_pandas().to_string(
buf=buf,
columns=columns,
col_space=col_space,
header=header,
index=index,
na_rep=na_rep,
formatters=formatters,
float_format=float_format,
sparsify=sparsify,
index_names=index_names,
justify=justify,
max_rows=max_rows,
max_cols=max_cols,
show_dimensions=show_dimensions,
decimal=decimal,
line_width=line_width,
min_rows=min_rows,
max_colwidth=max_colwidth,
encoding=encoding,
)

@_performance_tracking
def scatter_by_map(
self,
Expand Down
23 changes: 0 additions & 23 deletions python/cudf/cudf/core/indexed_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,29 +698,6 @@ def to_hdf(self, path_or_buf, key, *args, **kwargs):

cudf.io.hdf.to_hdf(path_or_buf, key, self, *args, **kwargs)

@_performance_tracking
def to_string(self):
r"""
Convert to string

cuDF uses Pandas internals for efficient string formatting.
Set formatting options using pandas string formatting options and
cuDF objects will print identically to Pandas objects.

cuDF supports `null/None` as a value in any column type, which
is transparently supported during this output process.

Examples
--------
>>> import cudf
>>> df = cudf.DataFrame()
>>> df['key'] = [0, 1, 2]
>>> df['val'] = [float(i + 10) for i in range(3)]
>>> df.to_string()
' key val\n0 0 10.0\n1 1 11.0\n2 2 12.0'
"""
return str(self)

def copy(self, deep: bool = True) -> Self:
"""Make a copy of this object's indices and data.

Expand Down
82 changes: 82 additions & 0 deletions python/cudf/cudf/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,88 @@ def to_dict(self, into: type[dict] = dict) -> dict:
"""
return self.to_pandas().to_dict(into=into)

@_performance_tracking
def to_string(
self,
buf=None,
*,
na_rep: str = "NaN",
float_format=None,
header: bool = True,
index: bool = True,
length: bool = False,
dtype: bool = False,
name: bool = False,
max_rows: int | None = None,
min_rows: int | None = None,
) -> str | None:
r"""
Render a Series to a console-friendly string output.

cuDF uses pandas internals for string formatting, so this mirrors
:meth:`pandas.Series.to_string` and accepts the same arguments.
Unlike ``repr``, the output is not truncated by the
``display.max_rows`` option unless ``max_rows`` is passed
explicitly, and the ``dtype``/``name``/``length`` footer is
omitted unless requested.

cuDF supports `null/None` as a value in any column type, which
is transparently supported during this output process.

Parameters
----------
buf : writable buffer, optional
File path or buffer to write to. If ``None`` (default), the
output is returned as a string.
na_rep : str, default "NaN"
String representation of ``NaN``/null values.
float_format : callable, optional
One-parameter formatter function applied to floating point
values.
header : bool, default True
Whether to print the Series header (the index name).
index : bool, default True
Whether to print index (row) labels.
length : bool, default False
Whether to append the Series length to the output.
dtype : bool, default False
Whether to append the Series dtype to the output.
name : bool, default False
Whether to append the Series name to the output.
max_rows : int, optional
Maximum number of rows to show before truncating. If ``None``,
all rows are shown.
min_rows : int, optional
Number of rows to show in a truncated output (when the number
of rows exceeds ``max_rows``).

Returns
-------
str or None
The string representation of the Series when ``buf`` is
``None``; otherwise the result is written to ``buf`` and
``None`` is returned.

Examples
--------
>>> import cudf
>>> series = cudf.Series([1, 2, 3, 4])
>>> series.to_string()
'0 1\n1 2\n2 3\n3 4'
"""
Comment thread
coderabbitai[bot] marked this conversation as resolved.
return self.to_pandas().to_string(
buf=buf,
na_rep=na_rep,
float_format=float_format,
header=header,
index=index,
length=length,
dtype=dtype,
name=name,
max_rows=max_rows,
min_rows=min_rows,
)

@_performance_tracking
def reindex(
self,
Expand Down
16 changes: 0 additions & 16 deletions python/cudf/cudf/pandas/scripts/pandas-testing-plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3565,21 +3565,6 @@ def pytest_unconfigure(config):
"tests/io/formats/test_to_csv.py::TestToCSV::test_to_csv_multi_index": "TODO: Add a reason for failure",
"tests/io/formats/test_to_csv.py::TestToCSV::test_to_csv_na_rep": "TODO: Add a reason for failure",
"tests/io/formats/test_to_csv.py::TestToCSV::test_to_csv_with_single_column": "TODO: Add a reason for failure",
"tests/io/formats/test_to_string.py::TestDataFrameToString::test_to_string": "AssertionError: assert ' ...878547 199?!' == ' ... x 2 columns]'",
"tests/io/formats/test_to_string.py::TestDataFrameToString::test_to_string_truncate": "TODO: Add a reason for failure",
"tests/io/formats/test_to_string.py::TestSeriesToString::test_nullable_float_to_string[Float32]": "AssertionError: assert '0 0.0\n1...type: Float32' == '0 0.0\n1....0\n2 <NA>'",
"tests/io/formats/test_to_string.py::TestSeriesToString::test_nullable_float_to_string[Float64]": "AssertionError: assert '0 0.0\n1...type: Float64' == '0 0.0\n1....0\n2 <NA>'",
"tests/io/formats/test_to_string.py::TestSeriesToString::test_nullable_int_to_string[Int16]": "AssertionError: assert '0 0\n1...ndtype: Int16' == '0 0\n1... 1\n2 <NA>'",
"tests/io/formats/test_to_string.py::TestSeriesToString::test_nullable_int_to_string[Int32]": "AssertionError: assert '0 0\n1...ndtype: Int32' == '0 0\n1... 1\n2 <NA>'",
"tests/io/formats/test_to_string.py::TestSeriesToString::test_nullable_int_to_string[Int64]": "AssertionError: assert '0 0\n1...ndtype: Int64' == '0 0\n1... 1\n2 <NA>'",
"tests/io/formats/test_to_string.py::TestSeriesToString::test_nullable_int_to_string[Int8]": "AssertionError: assert '0 0\n1...\ndtype: Int8' == '0 0\n1... 1\n2 <NA>'",
"tests/io/formats/test_to_string.py::TestSeriesToString::test_nullable_int_to_string[UInt16]": "AssertionError: assert '0 0\n1...dtype: UInt16' == '0 0\n1... 1\n2 <NA>'",
"tests/io/formats/test_to_string.py::TestSeriesToString::test_nullable_int_to_string[UInt32]": "AssertionError: assert '0 0\n1...dtype: UInt32' == '0 0\n1... 1\n2 <NA>'",
"tests/io/formats/test_to_string.py::TestSeriesToString::test_nullable_int_to_string[UInt64]": "AssertionError: assert '0 0\n1...dtype: UInt64' == '0 0\n1... 1\n2 <NA>'",
"tests/io/formats/test_to_string.py::TestSeriesToString::test_nullable_int_to_string[UInt8]": "AssertionError: assert '0 0\n1...ndtype: UInt8' == '0 0\n1... 1\n2 <NA>'",
"tests/io/formats/test_to_string.py::TestSeriesToString::test_to_string_float_na_spacing": "AssertionError: assert '0 NaN\\...type: float64' == '0 NaN\\...\n4 NaN'",
"tests/io/formats/test_to_string.py::TestSeriesToString::test_to_string_mixed": "AssertionError: assert '0 foo\n1 ...z\ndtype: str' == '0 foo\n1 ...bar\n3 baz'",
"tests/io/formats/test_to_string.py::TestSeriesToString::test_to_string_timedelta64": "AssertionError: assert '0 1 days\n...medelta64[us]' == '0 1 days\n...s\n2 3 days'",
"tests/io/json/test_compression.py::test_chunksize_with_compression[bz2]": "TODO: Add a reason for failure",
"tests/io/json/test_compression.py::test_chunksize_with_compression[gzip]": "TODO: Add a reason for failure",
"tests/io/json/test_compression.py::test_chunksize_with_compression[tar]": "TODO: Add a reason for failure",
Expand Down Expand Up @@ -6666,7 +6651,6 @@ def pytest_unconfigure(config):
"tests/indexing/test_indexing.py::TestDatetimelikeCoercion::test_setitem_td64_string_values[setitem-key2-array1]": "Asserts private APIs",
"tests/indexing/test_indexing.py::TestDatetimelikeCoercion::test_setitem_td64_string_values[setitem-key2-list]": "Asserts private APIs",
"tests/indexing/test_indexing.py::TestMisc::test_no_reference_cycle": "Flaky xfails (TODO: Validate with pandas 3)",
"tests/io/formats/test_to_string.py::TestDataFrameToString::test_to_string_index_with_nan": "pandas xfails, but xpasses with cudf.pandas",
"tests/io/json/test_compression.py::test_with_s3_url[bz2]": "Requires moto server running on http://localhost:5000",
"tests/io/json/test_compression.py::test_with_s3_url[gzip]": "Requires moto server running on http://localhost:5000",
"tests/io/json/test_compression.py::test_with_s3_url[tar]": "Requires moto server running on http://localhost:5000",
Expand Down
Loading
Loading