-
Notifications
You must be signed in to change notification settings - Fork 63
feat: Add transpose support for small homogeneously typed DataFrames. #621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
28273d6
aa43f87
234da48
08d17db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,6 +93,88 @@ def values(self) -> np.ndarray: | |
| """ | ||
| raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE) | ||
|
|
||
| @property | ||
| def T(self) -> DataFrame: | ||
| """ | ||
| The transpose of the DataFrame. | ||
|
|
||
| All columns must be the same dtype (numerics can be coerced to a common supertype). | ||
|
|
||
| **Examples:** | ||
|
|
||
| >>> import bigframes.pandas as bpd | ||
| >>> bpd.options.display.progress_bar = None | ||
| >>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4]}) | ||
| >>> df | ||
| col1 col2 | ||
| 0 1 3 | ||
| 1 2 4 | ||
| <BLANKLINE> | ||
| [2 rows x 2 columns] | ||
|
|
||
| >>> df.T | ||
| 0 1 | ||
| col1 1 2 | ||
| col2 3 4 | ||
| <BLANKLINE> | ||
| [2 rows x 2 columns] | ||
|
|
||
| Returns: | ||
| DataFrame: The transposed DataFrame. | ||
| """ | ||
| raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE) | ||
|
|
||
| def transpose(self) -> DataFrame: | ||
| """ | ||
| Transpose index and columns. | ||
|
|
||
| Reflect the DataFrame over its main diagonal by writing rows as columns | ||
| and vice-versa. The property :attr:`.T` is an accessor to the method | ||
| :meth:`transpose`. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add something here about the dtypes requirements? (Columns must be coerceable to a common type) (I know it's below but seems important enough to be up top.)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
|
|
||
| All columns must be the same dtype (numerics can be coerced to a common supertype). | ||
|
|
||
| **Examples:** | ||
|
|
||
| **Square DataFrame with homogeneous dtype** | ||
|
|
||
| >>> import bigframes.pandas as bpd | ||
| >>> bpd.options.display.progress_bar = None | ||
|
|
||
| >>> d1 = {'col1': [1, 2], 'col2': [3, 4]} | ||
| >>> df1 = bpd.DataFrame(data=d1) | ||
| >>> df1 | ||
| col1 col2 | ||
| 0 1 3 | ||
| 1 2 4 | ||
| <BLANKLINE> | ||
| [2 rows x 2 columns] | ||
|
|
||
| >>> df1_transposed = df1.T # or df1.transpose() | ||
| >>> df1_transposed | ||
| 0 1 | ||
| col1 1 2 | ||
| col2 3 4 | ||
| <BLANKLINE> | ||
| [2 rows x 2 columns] | ||
|
|
||
| When the dtype is homogeneous in the original DataFrame, we get a | ||
| transposed DataFrame with the same dtype: | ||
|
|
||
| >>> df1.dtypes | ||
| col1 Int64 | ||
| col2 Int64 | ||
| dtype: object | ||
| >>> df1_transposed.dtypes | ||
| 0 Int64 | ||
| 1 Int64 | ||
| dtype: object | ||
|
|
||
| Returns: | ||
| DataFrame: The transposed DataFrame. | ||
| """ | ||
| raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE) | ||
|
|
||
| def info( | ||
| self, | ||
| verbose: bool | None = None, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's a cheap test, we could add a check that we raise TypeError for mixing strs and floats.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added