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

[GitHub] [arrow] SamuelMarks opened a new issue, #37322: [Python] pyarrow pyarrow.lib.ArrowTypeError, how do I construct with schema?

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

   ### Describe the usage question you have. Please include as many useful details as  possible.
   
   
   Attempt:
   
   ```py
   import pyarrow as pa
   
   # Explicit schema definition to try and resolve error
   schema = pa.schema(
       [
           pa.field("timestamp_col", pa.timestamp("ns", tz="UTC")),
           pa.field("json_col", pa.struct([("can", pa.string())])),
           pa.field("array_str_col", pa.list_(pa.string())),
           pa.field("array_bigint_col", pa.list_(pa.int64())),
           pa.field(
               "array_json_col",
               pa.list_(pa.struct([("foo", pa.string()), ("can", pa.string())])),
           ),
       ]
   )
   row = pa.Table.from_pydict(
       {
           "timestamp_col": datetime.now().isoformat(),
           "json_col": {"can": "haz"},
           "array_str_col": ["Flamingo", "Centipede"],
           "array_bigint_col": np.arange(6, dtype=np.int64),
           "array_json_col": [{"foo": "bar"}, {"can": "haz"}],
       },
       schema=schema,
   )
   ```
   (for more experiments—including Parquet and a `pandas` variant of the above mock—see https://github.com/offscale/cdd-python-gae/blob/704e8df/cdd_gae/tests/test_parquet_to_table.py#L259 )
   
   Error:
   ```py
       row = pa.Table.from_pydict(
             ^^^^^^^^^^^^^^^^^^^^^
     File "pyarrow/table.pxi", line 3849, in pyarrow.lib.Table.from_pydict
     File "pyarrow/table.pxi", line 5418, in pyarrow.lib._from_pydict
     File "pyarrow/array.pxi", line 357, in pyarrow.lib.asarray
     File "pyarrow/array.pxi", line 327, in pyarrow.lib.array
     File "pyarrow/array.pxi", line 39, in pyarrow.lib._sequence_to_array
     File "pyarrow/error.pxi", line 144, in pyarrow.lib.pyarrow_internal_check_status
     File "pyarrow/error.pxi", line 123, in pyarrow.lib.check_status
   pyarrow.lib.ArrowTypeError: object of type <class 'str'> cannot be converted to int
   ```
   
   ### Component(s)
   
   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


[GitHub] [arrow] SamuelMarks commented on issue #37322: [Python] pyarrow pyarrow.lib.ArrowTypeError, how do I construct with schema?

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

   Thanks, that got me further:
   ```py
       pq.write_table(row, parquet_filepath)
     File "lib/python3.11/site-packages/pyarrow/parquet/core.py", line 3106, in write_table
       writer.write_table(table, row_group_size=row_group_size)
     File "lib/python3.11/site-packages/pyarrow/parquet/core.py", line 1092, in write_table
       self.writer.write_table(table, row_group_size=row_group_size)
     File "pyarrow/_parquet.pyx", line 1777, in pyarrow._parquet.ParquetWriter.write_table
     File "pyarrow/error.pxi", line 100, in pyarrow.lib.check_status
   pyarrow.lib.ArrowInvalid: Casting from timestamp[ns, tz=UTC] to timestamp[us] would lose data: 1692809086
   ```
   
   Maybe it wants `ms`?


-- 
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


[GitHub] [arrow] js8544 commented on issue #37322: [Python] pyarrow pyarrow.lib.ArrowTypeError, how do I construct with schema?

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

   Try this:
   ```python
   schema = pa.schema(
       [
           pa.field("timestamp_col", pa.timestamp("ns", tz="UTC")),
           pa.field("json_col", pa.struct([("can", pa.string())])),
           pa.field("array_str_col", pa.list_(pa.string())),
           pa.field("array_bigint_col", pa.list_(pa.int64())),
           pa.field(
               "array_json_col",
               pa.list_(pa.struct([("foo", pa.string()), ("can", pa.string())])),
           ),
       ]
   )
   table = pa.Table.from_pydict(
       {
           "timestamp_col": [datetime.now().timestamp()],
           "json_col": [{"can": "haz"}],
           "array_str_col": [["Flamingo", "Centipede"]],
           "array_bigint_col": [np.arange(6, dtype=np.int64)],
           "array_json_col": [[{"foo": "bar"}, {"can": "haz"}]],
       },
       schema=schema,
   )
   ```
   
   Two things I changed to make it work:
   1. `datetime.now().isoformat()` returns a string but `pa.timestamp` expects an int.
   2. `pa.Table.from_pydict` expects the whole table, not a single row, so you'll need to pass in a list for each column, not a single element.


-- 
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


[GitHub] [arrow] js8544 commented on issue #37322: [Python] pyarrow pyarrow.lib.ArrowTypeError, how do I construct with schema?

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

   @mapleFU Do you mind taking a look at this Parquet issue? Does Parquet writer not support timestamp types with timezones?


-- 
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] pyarrow pyarrow.lib.ArrowTypeError, how do I construct with schema? [arrow]

Posted by "thisisnic (via GitHub)" <gi...@apache.org>.
thisisnic closed issue #37322: [Python] pyarrow pyarrow.lib.ArrowTypeError, how do I construct with schema?
URL: https://github.com/apache/arrow/issues/37322


-- 
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] pyarrow pyarrow.lib.ArrowTypeError, how do I construct with schema? [arrow]

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

   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 usage question has evolved into a feature request or docs update, please remove the 'Type: usage' label and add the 'Type: enhancement' label instead.


-- 
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