Skip to content

Commit 5445b42

Browse files
SNOW-1620315, SNOW-1748326: Add support for DataFrame/Series.to_string (#3340)
1 parent e93727f commit 5445b42

File tree

13 files changed

+240
-33
lines changed

13 files changed

+240
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
- Added support for dict values in `Series.str.get`, `Series.str.slice`, and `Series.str.__getitem__` (`Series.str[...]`).
3232
- Added support for `DataFrame.to_html`.
33+
- Added support for `DataFrame.to_string` and `Series.to_string`.
3334

3435
#### Improvements
3536

docs/source/modin/dataframe.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,6 @@ DataFrame
234234
:toctree: pandas_api/
235235

236236
DataFrame.to_csv
237-
DataFrame.to_html
237+
DataFrame.to_html
238+
DataFrame.to_string
239+

docs/source/modin/series.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,4 @@ Series
327327
:toctree: pandas_api/
328328

329329
Series.to_csv
330+
Series.to_string

docs/source/modin/supported/dataframe_supported.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ Methods
472472
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+
473473
| ``to_stata`` | N | | |
474474
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+
475-
| ``to_string`` | N | | |
475+
| ``to_string`` | Y | | |
476476
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+
477477
| ``to_timestamp`` | N | | |
478478
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+

docs/source/modin/supported/series_supported.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ Methods
451451
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+
452452
| ``to_sql`` | N | | |
453453
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+
454-
| ``to_string`` | N | | |
454+
| ``to_string`` | Y | | |
455455
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+
456456
| ``to_timestamp`` | N | | |
457457
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+

src/snowflake/snowpark/modin/plugin/docstrings/dataframe.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4339,6 +4339,101 @@ def to_records():
43394339
Convert ``DataFrame`` to a NumPy record array.
43404340
"""
43414341

4342+
def to_string():
4343+
"""
4344+
Render a DataFrame to a console-friendly tabular output.
4345+
4346+
Parameters
4347+
----------
4348+
buf : str, Path or StringIO-like, optional, default None
4349+
Buffer to write to. If None, the output is returned as a string.
4350+
4351+
columns : array-like, optional, default None
4352+
The subset of columns to write. Writes all columns by default.
4353+
4354+
col_space : int, list or dict of int, optional
4355+
The minimum width of each column. If a list of ints is given every integers corresponds with one column. If a dict is given, the key references the column, while the value defines the space to use..
4356+
4357+
header : bool or list of str, optional
4358+
Write out the column names. If a list of columns is given, it is assumed to be aliases for the column names.
4359+
4360+
index : bool, optional, default True
4361+
Whether to print index (row) labels.
4362+
4363+
na_rep : str, optional, default ‘NaN’
4364+
String representation of NaN to use.
4365+
4366+
formatters : list, tuple or dict of one-param. functions, optional
4367+
Formatter functions to apply to columns’ elements by position or name. The result of each function must be a unicode string. List/tuple must be of length equal to the number of columns.
4368+
4369+
float_format : one-parameter function, optional, default None
4370+
Formatter function to apply to columns’ elements if they are floats. This function must return a unicode string and will be applied only to the non-NaN elements, with NaN being handled by na_rep.
4371+
4372+
sparsify : bool, optional, default True
4373+
Set to False for a DataFrame with a hierarchical index to print every multiindex key at each row.
4374+
4375+
index_names : bool, optional, default True
4376+
Prints the names of the indexes.
4377+
4378+
justify : str, default None
4379+
How to justify the column labels. If None uses the option from the print configuration (controlled by set_option), ‘right’ out of the box. Valid values are
4380+
- left
4381+
- right
4382+
- center
4383+
- justify
4384+
- justify-all
4385+
- start
4386+
- end
4387+
- inherit
4388+
- match-parent
4389+
- initial
4390+
- unset.
4391+
4392+
max_rows : int, optional
4393+
Maximum number of rows to display in the console.
4394+
4395+
max_cols : int, optional
4396+
Maximum number of columns to display in the console.
4397+
4398+
show_dimensions : bool, default False
4399+
Display DataFrame dimensions (number of rows by number of columns).
4400+
4401+
decimal : str, default ‘.’
4402+
Character recognized as decimal separator, e.g. ‘,’ in Europe.
4403+
4404+
line_width : int, optional
4405+
Width to wrap a line in characters.
4406+
4407+
min_rows : int, optional
4408+
The number of rows to display in the console in a truncated repr (when number of rows is above max_rows).
4409+
4410+
max_colwidth : int, optional
4411+
Max width to truncate each column in characters. By default, no limit.
4412+
4413+
encoding : str, default “utf-8”
4414+
Set character encoding.
4415+
4416+
Returns
4417+
-------
4418+
str or None
4419+
If buf is None, returns the result as a string. Otherwise returns None.
4420+
4421+
See also
4422+
--------
4423+
to_html
4424+
Convert DataFrame to HTML.
4425+
4426+
Examples
4427+
--------
4428+
>>> d = {'col1': [1, 2, 3], 'col2': [4, 5, 6]}
4429+
>>> df = pd.DataFrame(d)
4430+
>>> print(df.to_string())
4431+
col1 col2
4432+
0 1 4
4433+
1 2 5
4434+
2 3 6
4435+
"""
4436+
43424437
def to_stata():
43434438
pass
43444439

src/snowflake/snowpark/modin/plugin/docstrings/series.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3603,6 +3603,49 @@ def to_period():
36033603
def to_string():
36043604
"""
36053605
Render a string representation of the Series.
3606+
3607+
Parameters
3608+
----------
3609+
buf : StringIO-like, optional
3610+
Buffer to write to.
3611+
3612+
na_rep : str, optional
3613+
String representation of NaN to use, default ‘NaN’.
3614+
3615+
float_format : one-parameter function, optional
3616+
Formatter function to apply to columns’ elements if they are floats, default None.
3617+
3618+
header : bool, default True
3619+
Add the Series header (index name).
3620+
3621+
index : bool, optional
3622+
Add index (row) labels, default True.
3623+
3624+
length : bool, default False
3625+
Add the Series length.
3626+
3627+
dtype : bool, default False
3628+
Add the Series dtype.
3629+
3630+
name : bool, default False
3631+
Add the Series name if not None.
3632+
3633+
max_rows : int, optional
3634+
Maximum number of rows to show before truncating. If None, show all.
3635+
3636+
min_rows : int, optional
3637+
The number of rows to display in a truncated repr (when number of rows is above max_rows).
3638+
3639+
Returns
3640+
-------
3641+
str or None
3642+
String representation of Series if buf=None, otherwise None.
3643+
3644+
Examples
3645+
--------
3646+
>>> ser = pd.Series([1, 2, 3]).to_string()
3647+
>>> ser
3648+
'0 1\\n1 2\\n2 3'
36063649
"""
36073650

36083651
def to_timestamp():

src/snowflake/snowpark/modin/plugin/extensions/base_overrides.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -411,32 +411,6 @@ def to_pickle(
411411
pass # pragma: no cover
412412

413413

414-
@register_base_not_implemented()
415-
def to_string(
416-
self,
417-
buf=None,
418-
columns=None,
419-
col_space=None,
420-
header=True,
421-
index=True,
422-
na_rep="NaN",
423-
formatters=None,
424-
float_format=None,
425-
sparsify=None,
426-
index_names=True,
427-
justify=None,
428-
max_rows=None,
429-
min_rows=None,
430-
max_cols=None,
431-
show_dimensions=False,
432-
decimal=".",
433-
line_width=None,
434-
max_colwidth=None,
435-
encoding=None,
436-
): # noqa: PR01, RT01, D200
437-
pass # pragma: no cover
438-
439-
440414
@register_base_not_implemented()
441415
def to_sql(
442416
self,

src/snowflake/snowpark/modin/plugin/extensions/dataframe_overrides.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,54 @@ def to_records(
342342
pass # pragma: no cover
343343

344344

345+
def to_string(
346+
self,
347+
buf=None,
348+
columns=None,
349+
col_space=None,
350+
header=True,
351+
index=True,
352+
na_rep="NaN",
353+
formatters=None,
354+
float_format=None,
355+
sparsify=None,
356+
index_names=True,
357+
justify=None,
358+
max_rows=None,
359+
min_rows=None,
360+
max_cols=None,
361+
show_dimensions=False,
362+
decimal=".",
363+
line_width=None,
364+
max_colwidth=None,
365+
encoding=None,
366+
): # noqa: PR01, RT01, D200
367+
WarningMessage.single_warning(
368+
"DataFrame.to_string materializes data to the local machine."
369+
)
370+
return self._to_pandas().to_string(
371+
buf=buf,
372+
columns=columns,
373+
col_space=col_space,
374+
header=header,
375+
index=index,
376+
na_rep=na_rep,
377+
formatters=formatters,
378+
float_format=float_format,
379+
sparsify=sparsify,
380+
index_names=index_names,
381+
justify=justify,
382+
max_rows=max_rows,
383+
min_rows=min_rows,
384+
max_cols=max_cols,
385+
show_dimensions=show_dimensions,
386+
decimal=decimal,
387+
line_width=line_width,
388+
max_colwidth=max_colwidth,
389+
encoding=encoding,
390+
)
391+
392+
345393
@register_dataframe_not_implemented()
346394
def to_stata(
347395
self,

src/snowflake/snowpark/modin/plugin/extensions/series_overrides.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ def to_period(self, freq=None, copy=True): # noqa: PR01, RT01, D200
335335
pass # pragma: no cover
336336

337337

338-
@register_series_not_implemented()
339338
def to_string(
340339
self,
341340
buf=None,
@@ -349,7 +348,21 @@ def to_string(
349348
max_rows=None,
350349
min_rows=None,
351350
): # noqa: PR01, RT01, D200
352-
pass # pragma: no cover
351+
WarningMessage.single_warning(
352+
"Series.to_string materializes data to the local machine."
353+
)
354+
return self._to_pandas().to_string(
355+
buf=buf,
356+
na_rep=na_rep,
357+
float_format=float_format,
358+
header=header,
359+
index=index,
360+
length=length,
361+
dtype=dtype,
362+
name=name,
363+
max_rows=max_rows,
364+
min_rows=min_rows,
365+
)
353366

354367

355368
@register_series_not_implemented()

0 commit comments

Comments
 (0)