Skip to content

Table.from_pandas sets empty string columns to null type #2110

Closed

Description

@ja-michel

GitHub Issues for Apache Arrow

The issues seems to be that pyarrow.Table.from_pandas will set string (object) columns to null type if the dataframe is empty.

df = pd.DataFrame({'a':[],'b':[],'c':[]}, dtype=object)  
df['b'] = df['b'].astype(np.int32)
df['c'] = pd.to_datetime(df['c'])
df.dtypes

>> a            object
>> b             int32
>> c    datetime64[ns]
>> dtype: object

The pyarrow schema is then of null type. Other types (numeric and datetimes) seem to work as expected.

table = pa.Table.from_pandas(tdf, preserve_index=False)
table.schema

>> a: null
>> b: int32
>> c: timestamp[ns]
>> metadata
>> --------
>> {b'pandas': b'{"index_columns": [], "column_indexes": [], "columns": [{"name":'
>>             b' "a", "field_name": "a", "pandas_type": "empty", "numpy_type": "'
>>             b'object", "metadata": null}, {"name": "b", "field_name": "b", "pa'
>>             b'ndas_type": "int32", "numpy_type": "int32", "metadata": null}, {'
>>             b'"name": "c", "field_name": "c", "pandas_type": "datetime", "nump'
>>             b'y_type": "datetime64[ns]", "metadata": null}], "pandas_version":'
>>            b' "0.23.0"}'}

You can then modify that particular field to be a pyarrow.string() type.

t2 = pa.string()
fields = [pa.field('a', t2)]
s=pa.schema(fields)
table = pa.Table.from_pandas(tdf, schema=s, preserve_index=False)
table.schema

>> a: string
>> b: int32
>> c: timestamp[ns]
>> metadata
>> --------
>> {b'pandas': b'{"index_columns": [], "column_indexes": [], "columns": [{"name":'
>>            b' "a", "field_name": "a", "pandas_type": "unicode", "numpy_type":'
>>            b' "object", "metadata": null}, {"name": "b", "field_name": "b", "'
>>            b'pandas_type": "int32", "numpy_type": "int32", "metadata": null},'
>>            b' {"name": "c", "field_name": "c", "pandas_type": "datetime", "nu'
>>            b'mpy_type": "datetime64[ns]", "metadata": null}], "pandas_version'
>>            b'": "0.23.0"}'}

This seems to affect only empty dataframes.

Activity

  1. xhochy commented on Jun 7, 2018

    @xhochy
    Member

    This is sadly a known limitation of the type inferrence from empty DataFrames. These columns in Pandas DataFrames are from type object, not str. This column could also contain datetime.date or list objects when filled. Thus we are unable to guess the correct dtype from the existing Pandas DataFrame. null is the most common type we can guess for all of them.

    I'm closing this as we cannot fix this as long as Pandas DataFrames don't carry more schema information than the NumPy dtype.

    Feel free to ask again if I could not clarify why this is impossible to fix from the Arrow side.

  2. ja-michel commented on Jun 7, 2018

    @ja-michel
    Author
  3. wesm commented on Jun 7, 2018

    @wesm
    Member

    Perhaps we should add documentation how a user can supply column types for such columns to skip the inference.

  4. ja-michel commented on Jun 7, 2018

    @ja-michel
    Author
  5. xhochy commented on Jun 8, 2018

    @xhochy
    Member

    @arturomichel I added some documentation: #2116

  6. gsakkis commented on Aug 14, 2018

    @gsakkis
    Contributor

    This looks related to dask/dask#3454; is it something to be fixed on the dask or pyarrow side? I can open a new issue if it's the latter.

  7. ak-gupta commented on Sep 8, 2018

    @ak-gupta

    @xhochy I'm still having issues with pyarrow. I have no issues writing the parquet files but when I try to read them back using Dask I get an error saying that the schema is different between the common metadata and the other partitions. When I individually read in each parquet partition, however, the resulting pyarrow table has the correct schema (I am specifying a schema when I write the files, but the string vs null column type is being ignored).

  8. wesm commented on Sep 8, 2018

    @wesm
    Member

    @ak-gupta this isn't the best place for this discussion. Can you open a JIRA issue for Arrow and preferably post a reproducible example? Thanks

  9. ikucan commented on Apr 6, 2020

    @ikucan

    Sorry, was this ever resolved. I have come across this issue

  10. wesm commented on Apr 6, 2020

    @wesm
    Member

    If you're having the issue with pyarrow 0.16.0 please open a JIRA issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions