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

[GitHub] [arrow] NMAC427 commented on issue #36277: [Python] Can't convert datetime.date to pa.timestamp in pa.array

NMAC427 commented on issue #36277:
URL: https://github.com/apache/arrow/issues/36277#issuecomment-1606992527

   This can also be replicated with pandas:
   
   ```python
   pd.Series([dt.date(1970, 1, 1)], dtype=pd.ArrowDtype(pa.timestamp("ms")))
   
   ---------------------------------------------------------------------------
   ArrowTypeError                            Traceback (most recent call last)
   Cell In[14], line 1
   ----> 1 pd.Series([dt.date(1970, 1, 1)], dtype=pd.ArrowDtype(pa.timestamp("ms")))
   
   File ~/Library/Caches/pypoetry/virtualenvs/pydiverse-pipedag-JBY4b-V4-py3.11/lib/python3.11/site-packages/pandas/core/series.py:509, in Series.__init__(self, data, index, dtype, name, copy, fastpath)
       507         data = data.copy()
       508 else:
   --> 509     data = sanitize_array(data, index, dtype, copy)
       511     manager = get_option("mode.data_manager")
       512     if manager == "block":
   
   File ~/Library/Caches/pypoetry/virtualenvs/pydiverse-pipedag-JBY4b-V4-py3.11/lib/python3.11/site-packages/pandas/core/construction.py:559, in sanitize_array(data, index, dtype, copy, allow_2d)
       557     _sanitize_non_ordered(data)
       558     cls = dtype.construct_array_type()
   --> 559     subarr = cls._from_sequence(data, dtype=dtype, copy=copy)
       561 # GH#846
       562 elif isinstance(data, np.ndarray):
   
   File ~/Library/Caches/pypoetry/virtualenvs/pydiverse-pipedag-JBY4b-V4-py3.11/lib/python3.11/site-packages/pandas/core/arrays/arrow/array.py:270, in ArrowExtensionArray._from_sequence(cls, scalars, dtype, copy)
       268     scalars = deepcopy(scalars)
       269 try:
   --> 270     scalars = pa.array(scalars, type=pa_dtype, from_pandas=True)
       271 except pa.ArrowInvalid:
       272     # GH50430: let pyarrow infer type, then cast
       273     scalars = pa.array(scalars, from_pandas=True)
   
   File ~/Library/Caches/pypoetry/virtualenvs/pydiverse-pipedag-JBY4b-V4-py3.11/lib/python3.11/site-packages/pyarrow/array.pxi:327, in pyarrow.lib.array()
   
   File ~/Library/Caches/pypoetry/virtualenvs/pydiverse-pipedag-JBY4b-V4-py3.11/lib/python3.11/site-packages/pyarrow/array.pxi:39, in pyarrow.lib._sequence_to_array()
   
   File ~/Library/Caches/pypoetry/virtualenvs/pydiverse-pipedag-JBY4b-V4-py3.11/lib/python3.11/site-packages/pyarrow/error.pxi:144, in pyarrow.lib.pyarrow_internal_check_status()
   
   File ~/Library/Caches/pypoetry/virtualenvs/pydiverse-pipedag-JBY4b-V4-py3.11/lib/python3.11/site-packages/pyarrow/error.pxi:123, in pyarrow.lib.check_status()
   
   ArrowTypeError: object of type <class 'datetime.date'> cannot be converted to int
   ```
   
   However, initializing the Series as a `date32` and then casting to `timestamp` works:
   
   ```python
   pd.Series([dt.date(1970, 1, 1)], dtype=pd.ArrowDtype(pa.date32())).astype(pd.ArrowDtype(pa.timestamp("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