Skip to content

Commit 5369767

Browse files
authored
DOC: Fixing EX01 - Added examples (#54242)
Examples interchange
1 parent db3efcb commit 5369767

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

ci/code_checks.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
8383
pandas.util.hash_array \
8484
pandas.util.hash_pandas_object \
8585
pandas_object \
86-
pandas.api.interchange.from_dataframe \
87-
pandas.DatetimeIndex.snap \
8886
pandas.api.indexers.BaseIndexer \
8987
pandas.api.indexers.VariableOffsetWindowIndexer \
9088
pandas.api.extensions.ExtensionDtype \
@@ -100,7 +98,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
10098
pandas.api.extensions.ExtensionArray._values_for_factorize \
10199
pandas.api.extensions.ExtensionArray.interpolate \
102100
pandas.api.extensions.ExtensionArray.ravel \
103-
pandas.DataFrame.__dataframe__
104101
RET=$(($RET + $?)) ; echo $MSG "DONE"
105102

106103
fi

pandas/core/frame.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,22 @@ def __dataframe__(
912912
913913
`nan_as_null` currently has no effect; once support for nullable extension
914914
dtypes is added, this value should be propagated to columns.
915+
916+
Examples
917+
--------
918+
>>> df_not_necessarily_pandas = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
919+
>>> interchange_object = df_not_necessarily_pandas.__dataframe__()
920+
>>> interchange_object.column_names()
921+
Index(['A', 'B'], dtype='object')
922+
>>> df_pandas = (pd.api.interchange.from_dataframe
923+
... (interchange_object.select_columns_by_name(['A'])))
924+
>>> df_pandas
925+
A
926+
0 1
927+
1 2
928+
929+
These methods (``column_names``, ``select_columns_by_name``) should work
930+
for any dataframe library which implements the interchange protocol.
915931
"""
916932

917933
from pandas.core.interchange.dataframe import PandasDataFrameXchg

pandas/core/indexes/datetimes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,17 @@ def snap(self, freq: Frequency = "S") -> DatetimeIndex:
487487
Returns
488488
-------
489489
DatetimeIndex
490+
491+
Examples
492+
--------
493+
>>> idx = pd.DatetimeIndex(['2023-01-01', '2023-01-02',
494+
... '2023-02-01', '2023-02-02'])
495+
>>> idx
496+
DatetimeIndex(['2023-01-01', '2023-01-02', '2023-02-01', '2023-02-02'],
497+
dtype='datetime64[ns]', freq=None)
498+
>>> idx.snap('MS')
499+
DatetimeIndex(['2023-01-01', '2023-01-01', '2023-02-01', '2023-02-01'],
500+
dtype='datetime64[ns]', freq=None)
490501
"""
491502
# Superdumb, punting on any optimizing
492503
freq = to_offset(freq)

pandas/core/interchange/from_dataframe.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ def from_dataframe(df, allow_copy: bool = True) -> pd.DataFrame:
4444
Returns
4545
-------
4646
pd.DataFrame
47+
48+
Examples
49+
--------
50+
>>> df_not_necessarily_pandas = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
51+
>>> interchange_object = df_not_necessarily_pandas.__dataframe__()
52+
>>> interchange_object.column_names()
53+
Index(['A', 'B'], dtype='object')
54+
>>> df_pandas = (pd.api.interchange.from_dataframe
55+
... (interchange_object.select_columns_by_name(['A'])))
56+
>>> df_pandas
57+
A
58+
0 1
59+
1 2
60+
61+
These methods (``column_names``, ``select_columns_by_name``) should work
62+
for any dataframe library which implements the interchange protocol.
4763
"""
4864
if isinstance(df, pd.DataFrame):
4965
return df

0 commit comments

Comments
 (0)