Skip to content

Commit 6fea8ef

Browse files
ashleyxuutswast
andauthored
docs: add runnable code samples for DataFrames I/O methods and property (#129)
* docs: add runnable code samples for DataFrames I/O methods and property * fix: expose `bigframes.pandas.reset_session` as a public API (#128) * fix: address the comment * Empty commit * fix: address comments for better visualization of the output * Empty commit --------- Co-authored-by: Tim Swast <[email protected]>
1 parent 95bff3f commit 6fea8ef

File tree

1 file changed

+164
-8
lines changed
  • third_party/bigframes_vendored/pandas/core

1 file changed

+164
-8
lines changed

third_party/bigframes_vendored/pandas/core/frame.py

+164-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,19 @@ class DataFrame(NDFrame):
3333

3434
@property
3535
def shape(self) -> tuple[int, int]:
36-
"""Return a tuple representing the dimensionality of the DataFrame."""
36+
"""
37+
Return a tuple representing the dimensionality of the DataFrame.
38+
39+
**Examples:**
40+
41+
>>> import bigframes.pandas as bpd
42+
>>> bpd.options.display.progress_bar = None
43+
44+
>>> df = bpd.DataFrame({'col1': [1, 2, 3],
45+
... 'col2': [4, 5, 6]})
46+
>>> df.shape
47+
(3, 2)
48+
"""
3749
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
3850

3951
@property
@@ -44,21 +56,31 @@ def axes(self) -> list:
4456
It has the row axis labels and column axis labels as the only members.
4557
They are returned in that order.
4658
47-
Examples
59+
**Examples:**
4860
49-
.. code-block::
61+
>>> import bigframes.pandas as bpd
62+
>>> bpd.options.display.progress_bar = None
5063
51-
df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
52-
df.axes
53-
[RangeIndex(start=0, stop=2, step=1), Index(['col1', 'col2'],
54-
dtype='object')]
64+
>>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
65+
>>> df.axes[1:]
66+
[Index(['col1', 'col2'], dtype='object')]
5567
"""
5668
return [self.index, self.columns]
5769

5870
@property
5971
def values(self) -> np.ndarray:
6072
"""Return the values of DataFrame in the form of a NumPy array.
6173
74+
**Examples:**
75+
76+
>>> import bigframes.pandas as bpd
77+
>>> bpd.options.display.progress_bar = None
78+
79+
>>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
80+
>>> df.values
81+
array([[1, 3],
82+
[2, 4]], dtype=object)
83+
6284
Args:
6385
dytype (default None):
6486
The dtype to pass to `numpy.asarray()`.
@@ -76,6 +98,16 @@ def to_numpy(self, dtype=None, copy=False, na_value=None, **kwargs) -> np.ndarra
7698
"""
7799
Convert the DataFrame to a NumPy array.
78100
101+
**Examples:**
102+
103+
>>> import bigframes.pandas as bpd
104+
>>> bpd.options.display.progress_bar = None
105+
106+
>>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
107+
>>> df.to_numpy()
108+
array([[1, 3],
109+
[2, 4]], dtype=object)
110+
79111
Args:
80112
dtype (None):
81113
The dtype to pass to `numpy.asarray()`.
@@ -101,6 +133,15 @@ def to_gbq(
101133
) -> None:
102134
"""Write a DataFrame to a BigQuery table.
103135
136+
**Examples:**
137+
138+
>>> import bigframes.pandas as bpd
139+
>>> bpd.options.display.progress_bar = None
140+
141+
>>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
142+
>>> # destination_table = PROJECT_ID + "." + DATASET_ID + "." + TABLE_NAME
143+
>>> df.to_gbq("bigframes-dev.birds.test-numbers", if_exists="replace")
144+
104145
Args:
105146
destination_table (str):
106147
Name of table to be written, in the form ``dataset.tablename``
@@ -137,6 +178,15 @@ def to_parquet(
137178
This function writes the dataframe as a `parquet file
138179
<https://parquet.apache.org/>`_ to Cloud Storage.
139180
181+
**Examples:**
182+
183+
>>> import bigframes.pandas as bpd
184+
>>> bpd.options.display.progress_bar = None
185+
186+
>>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
187+
>>> gcs_bucket = "gs://bigframes-dev-testing/sample_parquet*.parquet"
188+
>>> df.to_parquet(path=gcs_bucket)
189+
140190
Args:
141191
path (str):
142192
Destination URI(s) of Cloud Storage files(s) to store the extracted dataframe
@@ -171,6 +221,35 @@ def to_dict(
171221
The type of the key-value pairs can be customized with the parameters
172222
(see below).
173223
224+
**Examples:**
225+
226+
>>> import bigframes.pandas as bpd
227+
>>> bpd.options.display.progress_bar = None
228+
229+
>>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
230+
>>> df.to_dict()
231+
{'col1': {0: 1, 1: 2}, 'col2': {0: 3, 1: 4}}
232+
233+
You can specify the return orientation.
234+
235+
>>> df.to_dict('series')
236+
{'col1': 0 1
237+
1 2
238+
Name: col1, dtype: Int64,
239+
'col2': 0 3
240+
1 4
241+
Name: col2, dtype: Int64}
242+
243+
>>> df.to_dict('split')
244+
{'index': [0, 1], 'columns': ['col1', 'col2'], 'data': [[1, 3], [2, 4]]}
245+
246+
>>> df.to_dict("tight")
247+
{'index': [0, 1],
248+
'columns': ['col1', 'col2'],
249+
'data': [[1, 3], [2, 4]],
250+
'index_names': [None],
251+
'column_names': [None]}
252+
174253
Args:
175254
orient (str {'dict', 'list', 'series', 'split', 'tight', 'records', 'index'}):
176255
Determines the type of the values of the dictionary.
@@ -213,6 +292,15 @@ def to_excel(self, excel_writer, sheet_name: str = "Sheet1", **kwargs) -> None:
213292
Note that creating an `ExcelWriter` object with a file name that already
214293
exists will result in the contents of the existing file being erased.
215294
295+
**Examples:**
296+
297+
>>> import bigframes.pandas as bpd
298+
>>> import tempfile
299+
>>> bpd.options.display.progress_bar = None
300+
301+
>>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
302+
>>> df.to_excel(tempfile.TemporaryFile())
303+
216304
Args:
217305
excel_writer (path-like, file-like, or ExcelWriter object):
218306
File path or existing ExcelWriter.
@@ -231,6 +319,23 @@ def to_latex(
231319
into a main LaTeX document or read from an external file
232320
with ``\input{{table.tex}}``.
233321
322+
**Examples:**
323+
324+
>>> import bigframes.pandas as bpd
325+
>>> bpd.options.display.progress_bar = None
326+
327+
>>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
328+
>>> print(df.to_latex())
329+
\begin{tabular}{lrr}
330+
\toprule
331+
& col1 & col2 \\
332+
\midrule
333+
0 & 1 & 3 \\
334+
1 & 2 & 4 \\
335+
\bottomrule
336+
\end{tabular}
337+
<BLANKLINE>
338+
234339
Args:
235340
buf (str, Path or StringIO-like, optional, default None):
236341
Buffer to write to. If None, the output is returned as a string.
@@ -253,6 +358,16 @@ def to_records(
253358
Index will be included as the first field of the record array if
254359
requested.
255360
361+
**Examples:**
362+
363+
>>> import bigframes.pandas as bpd
364+
>>> bpd.options.display.progress_bar = None
365+
366+
>>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
367+
>>> df.to_records()
368+
rec.array([(0, 1, 3), (1, 2, 4)],
369+
dtype=[('index', 'O'), ('col1', 'O'), ('col2', 'O')])
370+
256371
Args:
257372
index (bool, default True):
258373
Include index in resulting record array, stored in 'index'
@@ -298,6 +413,17 @@ def to_string(
298413
):
299414
"""Render a DataFrame to a console-friendly tabular output.
300415
416+
**Examples:**
417+
418+
>>> import bigframes.pandas as bpd
419+
>>> bpd.options.display.progress_bar = None
420+
421+
>>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
422+
>>> print(df.to_string())
423+
col1 col2
424+
0 1 3
425+
1 2 4
426+
301427
Args:
302428
buf (str, Path or StringIO-like, optional, default None):
303429
Buffer to write to. If None, the output is returned as a string.
@@ -363,6 +489,18 @@ def to_markdown(
363489
):
364490
"""Print DataFrame in Markdown-friendly format.
365491
492+
**Examples:**
493+
494+
>>> import bigframes.pandas as bpd
495+
>>> bpd.options.display.progress_bar = None
496+
497+
>>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
498+
>>> print(df.to_markdown())
499+
| | col1 | col2 |
500+
|---:|-------:|-------:|
501+
| 0 | 1 | 3 |
502+
| 1 | 2 | 4 |
503+
366504
Args:
367505
buf (str, Path or StringIO-like, optional, default None):
368506
Buffer to write to. If None, the output is returned as a string.
@@ -371,7 +509,7 @@ def to_markdown(
371509
index (bool, optional, default True):
372510
Add index (row) labels.
373511
**kwargs
374-
These parameters will be passed to `tabulate <https://pypi.org/project/tabulate>`_.
512+
These parameters will be passed to `tabulate <https://pypi.org/project/tabulate>`_.
375513
376514
Returns:
377515
DataFrame in Markdown-friendly format.
@@ -381,6 +519,15 @@ def to_markdown(
381519
def to_pickle(self, path, **kwargs) -> None:
382520
"""Pickle (serialize) object to file.
383521
522+
**Examples:**
523+
524+
>>> import bigframes.pandas as bpd
525+
>>> bpd.options.display.progress_bar = None
526+
527+
>>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
528+
>>> gcs_bucket = "gs://bigframes-dev-testing/sample_pickle_gcs.pkl"
529+
>>> df.to_pickle(path=gcs_bucket)
530+
384531
Args:
385532
path (str):
386533
File path where the pickled object will be stored.
@@ -391,6 +538,15 @@ def to_orc(self, path=None, **kwargs) -> bytes | None:
391538
"""
392539
Write a DataFrame to the ORC format.
393540
541+
**Examples:**
542+
543+
>>> import bigframes.pandas as bpd
544+
>>> bpd.options.display.progress_bar = None
545+
546+
>>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
547+
>>> import tempfile
548+
>>> df.to_orc(tempfile.TemporaryFile())
549+
394550
Args:
395551
path (str, file-like object or None, default None):
396552
If a string, it will be used as Root Directory path

0 commit comments

Comments
 (0)