pandas icon indicating copy to clipboard operation
pandas copied to clipboard

BUG: series created from pyarrow array with date32 type in 1.5rc0 can be created but not displayed

Open a-reich opened this issue 2 years ago • 5 comments

Pandas version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] I have confirmed this bug exists on the latest version of pandas.

  • [ ] I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd, pyarrow as pa, numpy as np, datetime as dt
arrow_dt = pa.array([dt.date.fromisoformat('2020-01-01')], type=pa.date32())
ser = pd.Series(arrow_dt, dtype=pd.ArrowDtype(arrow_dt.type))
ser

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\asafs\Miniconda3\envs\test\lib\site-packages\pandas\core\series.py", line 1594, in __repr__
    return self.to_string(**repr_params)
  File "C:\Users\asafs\Miniconda3\envs\test\lib\site-packages\pandas\core\series.py", line 1687, in to_string
    result = formatter.to_string()
  File "C:\Users\asafs\Miniconda3\envs\test\lib\site-packages\pandas\io\formats\format.py", line 397, in to_string
    fmt_values = self._get_formatted_values()
  File "C:\Users\asafs\Miniconda3\envs\test\lib\site-packages\pandas\io\formats\format.py", line 381, in _get_formatted_values
    return format_array(
  File "C:\Users\asafs\Miniconda3\envs\test\lib\site-packages\pandas\io\formats\format.py", line 1328, in format_array
    return fmt_obj.get_result()
  File "C:\Users\asafs\Miniconda3\envs\test\lib\site-packages\pandas\io\formats\format.py", line 1359, in get_result
    fmt_values = self._format_strings()
  File "C:\Users\asafs\Miniconda3\envs\test\lib\site-packages\pandas\io\formats\format.py", line 1833, in _format_strings
    fmt_values = [formatter(x) for x in values]
  File "C:\Users\asafs\Miniconda3\envs\test\lib\site-packages\pandas\io\formats\format.py", line 1833, in <listcomp>
    fmt_values = [formatter(x) for x in values]
  File "C:\Users\asafs\Miniconda3\envs\test\lib\site-packages\pandas\io\formats\format.py", line 1802, in <lambda>
    return lambda x: _format_datetime64_dateonly(
  File "C:\Users\asafs\Miniconda3\envs\test\lib\site-packages\pandas\io\formats\format.py", line 1792, in _format_datetime64_dateonly
    return x._date_repr
AttributeError: 'datetime.date' object has no attribute '_date_repr'

Issue Description

I saw in the "what's new" for the new release the functionality for creating general Arrow-backed objects and wanted to try it out. So I created a simple test Arrow Array with date32 type and tried to make a series from it as described, but the series cannot be displayed without erroring. The ser value itself can be created and its array values accessed still.

This issue didn't occur with eg pa.int32 but I did not comprehensively test across Arrow data types.

And while I'm here, thank you to the team for all your work on the awesome Arrow integration/extension features!

Expected Behavior

ser can be displayed in an interactive REPL as usual and repr(ser) does not error.

Installed Versions

INSTALLED VERSIONS

commit : 224458ee25d92ccdf289d1ae2741d178df4f323e python : 3.10.1.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.19044 machine : AMD64 processor : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : English_United States.1252

pandas : 1.5.0rc0 numpy : 1.23.2 pytz : 2022.2.1 dateutil : 2.8.2 setuptools : 60.2.0 pip : 21.3.1 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : None IPython : 7.31.0 pandas_datareader: None bs4 : None bottleneck : None brotli : fastparquet : None fsspec : None gcsfs : None matplotlib : None numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : 8.0.0 pyreadstat : None pyxlsb : None s3fs : None scipy : None snappy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlwt : None zstandard : None tzdata : None

a-reich avatar Aug 25 '22 01:08 a-reich

Thanks for the report! I was able to replicate this issue as well.

mroeschke avatar Aug 25 '22 17:08 mroeschke

One point of note: the Series constructor wasn't necessarily intended to accept a pa.array and a pd.ArrowDtype, but I suppose this should be supported as well.

mroeschke avatar Aug 25 '22 17:08 mroeschke

the Series constructor wasn't necessarily intended to accept a pa.array and a pd.ArrowDtype

Gotcha - what’s the right way to make a pd object from arrow array then? Or is only the “non-arrow data -> pd object construction-> pandas manages arrow conversion” path supported?

a-reich avatar Aug 25 '22 18:08 a-reich

arrays.ArrowExtensionArray was also added to the public API and would probably be the most explicit way to create a pandas object from arrow array: https://pandas.pydata.org/pandas-docs/version/1.5/reference/api/pandas.arrays.ArrowExtensionArray.html

I'll make sure there's some documentation around using ArrowExtensionArray for this use case.

In [4]: pd.Series(pd.arrays.ArrowExtensionArray(pa.array([1, 2, 3])))
Out[4]:
0   1
1   2
2   3
dtype: int64[pyarrow]

mroeschke avatar Aug 25 '22 19:08 mroeschke

Noting that this was fixed by https://github.com/pandas-dev/pandas/pull/48489/files and should be fixed when 1.5 is released. Just needs a unit test

mroeschke avatar Sep 14 '22 19:09 mroeschke

Is the test needed as simple as just making an appropriate series and calling repr()? Basically, is this issue something a first-time contributor could do?

a-reich avatar Oct 21 '22 13:10 a-reich

Yeah @a-reich essentially this test needs to be added which hopefully should be a good first time contribution: https://github.com/pandas-dev/pandas/pull/48258/files#diff-290226273f5cd28655f0fa2a4c154cd9909edc6a1f5abee07fec8ab374a9133eR1755

mroeschke avatar Oct 21 '22 16:10 mroeschke

take

a-reich avatar Oct 23 '22 21:10 a-reich

looks like this works but just needs a test? removing from the 2.0 milestone then

MarcoGorelli avatar Mar 27 '23 11:03 MarcoGorelli