Skip to content

Add 2d constructor #208

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

Merged
merged 4 commits into from
Aug 1, 2023
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
53 changes: 53 additions & 0 deletions spec/API_specification/dataframe_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
"DataFrame",
"Column",
"column_from_sequence",
"column_from_1d_array",
"concat",
"dataframe_from_dict",
"dataframe_from_2d_array",
"is_null",
"null",
"Int64",
Expand Down Expand Up @@ -96,6 +98,57 @@ def dataframe_from_dict(data: Mapping[str, Column[Any]]) -> DataFrame:
"""
...


def column_from_1d_array(array: Any, *, name: str, dtype: Any) -> Column[Any]:
"""
Construct Column from 1D array.

See `dataframe_from_2d_array` for related 2D function.

Only Array-API-compliant 1D arrays are supported.
Cross-kind casting is undefined and may vary across implementations.
Downcasting is disallowed.

Parameters
----------
array : array
array-API compliant 1D array
name : str
Name to give columns.
dtype : DType
Dtype of column.

Returns
-------
Column
"""
...

def dataframe_from_2d_array(array: Any, *, names: Sequence[str], dtypes: Mapping[str, Any]) -> DataFrame:
"""
Construct DataFrame from 2D array.

See `column_from_1d_array` for related 1D function.

Only Array-API-compliant 2D arrays are supported.
Cross-kind casting is undefined and may vary across implementations.
Downcasting is disallowed.

Parameters
----------
array : array
array-API compliant 2D array
names : Sequence[str]
Names to give columns. Must be the same length as ``array.shape[1]``.
dtypes : Mapping[str, DType]
Dtype of each column. Must be the same length as ``array.shape[1]``.
Comment on lines +143 to +144
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What type of casting do we allow vs disallow here? I.E. if someone passes an array of type float32 and a mapping that uses bool dtypes here, is that something we expect to support?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No cross-kind casting? So, float32 to bool would be disallowed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we allow downcasting within kind or only upcasting? I.e. is float64 array --> float32 column allowed? Or only float32 array --> float32 column?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any array-api precedent to go off of? (haven't looked to carefully yet)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is. Here are the casting rules: https://data-apis.org/array-api/latest/API_specification/type_promotion.html#type-promotion. In summary:

  • cross-kind casting is undefined, given that not all libraries allow that
  • downcasting is disallow in general, and if you're sure that that is what you want, there is a single function (astype) to explicitly downcast

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks - OK to just go with that?


Returns
-------
DataFrame
"""
...

class null:
"""
A `null` object to represent missing data.
Expand Down
4 changes: 4 additions & 0 deletions spec/API_specification/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ of objects and functions in the top-level namespace. The latter are:
Float64
Float32
Bool
column_from_sequence
column_from_1d_array
dataframe_from_dict
dataframe_from_2d_array

The ``DataFrame``, ``Column`` and ``GroupBy`` objects have the following
methods and attributes:
Expand Down