You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@arrow.apache.org by "mmcdermott (via GitHub)" <gi...@apache.org> on 2024/08/09 12:37:21 UTC

[I] It would be nice if schemas could support optional columns [arrow]

mmcdermott opened a new issue, #43626:
URL: https://github.com/apache/arrow/issues/43626

   ### Describe the enhancement requested
   
   I tried to check existing issues and did not see this discussed anywhere previously, but if I missed something I apologize. I am involved with the [MEDS project](https://github.com/Medical-Event-Data-Standard/meds/blob/main/src/meds/schema.py) which uses parquet files as its storage format with apache pyarrow. We have a number of schemas where only a subset of columns are mandatory, but other columns may be either optional (e.g., if a column by that name is present, it must have a certain type), or some schemas can accept additional columns of arbitrary types. It would be nice if there were a way to handle this directly in the notion of a PyArrow schema, e.g., instead of something like this
   
   ```python
   label_schema = pa.schema(
       [
           ("patient_id", pa.int64()),
           ("prediction_time", pa.timestamp("us")), 
           ("boolean_value", pa.bool_()), # Optional
           ("integer_value", pa.int64()), # Optional
           ("float_value", pa.float64()), # Optional
           ("categorical_value", pa.string()), # Optional
       ]
   )
   ```
   
   Where we need to dynamically filter the columns to those present on the fly, we could have something like this:
   
   ```python
   label_schema = pa.schema(
       [
           ("patient_id", pa.int64()),
           ("prediction_time", pa.timestamp("us")), 
           ("boolean_value", pa.optional(pa.bool_())),
           ("integer_value", pa.optional(pa.int64())),
           ("float_value", pa.optional(pa.float64())),
           ("categorical_value", pa.optional(pa.string())),
       ]
   )
   ```
   
   And then when using something like `df.to_arrow().cast(label_schema)` the system naturally errors if mandatory columns are missing and casts optional columns that are present to their mandatory types but doesn't error if an optional column is missing.
   
   ### Component(s)
   
   Parquet, Python


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@arrow.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Python] It would be nice if schemas could support optional columns [arrow]

Posted by "mmcdermott (via GitHub)" <gi...@apache.org>.
mmcdermott commented on issue #43626:
URL: https://github.com/apache/arrow/issues/43626#issuecomment-2709210755

   In case it is helpful to any folks dealing with this issue, I built this which might be useful: https://github.com/mmcdermott/flexible_schema/tree/main
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Python] It would be nice if schemas could support optional columns [arrow]

Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on issue #43626:
URL: https://github.com/apache/arrow/issues/43626#issuecomment-2286493827

   The `Schema` object is very much tied to actual data (i.e. a RecordBatch or Table having a schema), and in that context pyarrow doesn't really support such notion of optional column (in an actual table, the columns are either present or are not).
   
   So I think adding that concept to just a Schema seems unlikely that we would want to do that. But, we can maybe look at ways to make your use case easier to do, as it is definitely a valid and logical thing to do.
   
   One idea could be to add an option to `Table.cast(schema)` to ignore columns in the target schema that are not present in the calling object (i.e. essentially take the calling table's set of columns as the ground truth for the columns of the result, and only use the passed `schema` to lookup the type for each column).
   
   Another idea could be to make it easier to select a subset of columns in the schema. For example, assume that a `Schema` had a `select` method to select a subset of the fields of the schema, one could do something like (in two steps):
   
   ```python
   arrow_table = df.to_arrow()
   arrow_table = arrow_table.cast(label_schema.select(arrow_table.schema.names)
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Python] It would be nice if schemas could support optional columns [arrow]

Posted by "ketozhang (via GitHub)" <gi...@apache.org>.
ketozhang commented on issue #43626:
URL: https://github.com/apache/arrow/issues/43626#issuecomment-2968225055

   I tripped up in this thinking that `nullable=True` is the same as optional columns. 
   
   ```python
   >>> import pyarrow as pa
   >>> 
   >>> schema = pa.schema(
   ...     [
   ...         pa.field("id", pa.int64()),
   ...         pa.field("nullable", pa.string(), nullable=True),
   ...     ]
   ... )
   >>> 
   >>> table = pa.Table.from_arrays(
   ...     [pa.array([1, 2, 3], type=pa.int64())], names=["id"]
   ... )
   >>> 
   >>> table.cast(schema)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     File "pyarrow/table.pxi", line 4717, in pyarrow.lib.Table.cast
   ValueError: Target schema's field names are not matching the table's field names: ['id'], ['id', 'nullable']
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Python] It would be nice if schemas could support optional columns [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed issue #43626: [Python] It would be nice if schemas could support optional columns
URL: https://github.com/apache/arrow/issues/43626


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Python] It would be nice if schemas could support optional columns [arrow]

Posted by "mmcdermott (via GitHub)" <gi...@apache.org>.
mmcdermott commented on issue #43626:
URL: https://github.com/apache/arrow/issues/43626#issuecomment-2286707680

   Either of those options sounds great to me and like they would be very
   useful generally, thanks!
   
   On Tue, Aug 13, 2024, 11:10 AM Joris Van den Bossche <
   ***@***.***> wrote:
   
   > The Schema object is very much tied to actual data (i.e. a RecordBatch or
   > Table having a schema), and in that context pyarrow doesn't really support
   > such notion of optional column (in an actual table, the columns are either
   > present or are not).
   >
   > So I think adding that concept to just a Schema seems unlikely that we
   > would want to do that. But, we can maybe look at ways to make your use case
   > easier to do, as it is definitely a valid and logical thing to do.
   >
   > One idea could be to add an option to Table.cast(schema) to ignore
   > columns in the target schema that are not present in the calling object
   > (i.e. essentially take the calling table's set of columns as the ground
   > truth for the columns of the result, and only use the passed schema to
   > lookup the type for each column).
   >
   > Another idea could be to make it easier to select a subset of columns in
   > the schema. For example, assume that a Schema had a select method to
   > select a subset of the fields of the schema, one could do something like
   > (in two steps):
   >
   > arrow_table = df.to_arrow()arrow_table = arrow_table.cast(label_schema.select(arrow_table.schema.names)
   >
   > —
   > Reply to this email directly, view it on GitHub
   > <https://github.com/apache/arrow/issues/43626#issuecomment-2286493827>,
   > or unsubscribe
   > <https://github.com/notifications/unsubscribe-auth/AADS5XYCFDVE6CBAQW4IPDTZRIOVZAVCNFSM6AAAAABMIMQQC2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOBWGQ4TGOBSG4>
   > .
   > You are receiving this because you authored the thread.Message ID:
   > ***@***.***>
   >
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Python] It would be nice if schemas could support optional columns [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on issue #43626:
URL: https://github.com/apache/arrow/issues/43626#issuecomment-4698432362

   This issue has been marked as stale because it has had no activity in the past 365 days. Please remove the stale label or comment below, or this issue will be closed in 14 days. If this improvement is still desired but has no current owner, please add the 'Status: needs champion' label.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org