Skip to content

Commit 56b61f2

Browse files
honor categories keyword
1 parent 11d2691 commit 56b61f2

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

‎python/pyarrow/pandas_compat.py‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -781,10 +781,12 @@ def table_to_dataframe(
781781
table, index = _reconstruct_index(table, index_descriptors,
782782
all_columns, types_mapper)
783783
ext_columns_dtypes = _get_extension_dtypes(
784-
table, all_columns, types_mapper, options)
784+
table, all_columns, types_mapper, options, categories)
785785
else:
786786
index = _pandas_api.pd.RangeIndex(table.num_rows)
787-
ext_columns_dtypes = _get_extension_dtypes(table, [], types_mapper, options)
787+
ext_columns_dtypes = _get_extension_dtypes(
788+
table, [], types_mapper, options, categories
789+
)
788790

789791
_check_data_column_metadata_consistency(all_columns)
790792
columns = _deserialize_column_index(table, all_columns, column_indexes)
@@ -829,7 +831,9 @@ def table_to_dataframe(
829831
}
830832

831833

832-
def _get_extension_dtypes(table, columns_metadata, types_mapper=None, options=None):
834+
def _get_extension_dtypes(
835+
table, columns_metadata, types_mapper=None, options=None, categories=None
836+
):
833837
"""
834838
Based on the stored column pandas metadata and the extension types
835839
in the arrow schema, infer which columns should be converted to a
@@ -843,6 +847,8 @@ def _get_extension_dtypes(table, columns_metadata, types_mapper=None, options=No
843847
844848
"""
845849
strings_to_categorical = options["strings_to_categorical"]
850+
categories = categories or []
851+
846852
ext_columns = {}
847853

848854
# older pandas version that does not yet support extension dtypes
@@ -856,7 +862,7 @@ def _get_extension_dtypes(table, columns_metadata, types_mapper=None, options=No
856862
pa.types.is_string(field.type)
857863
or pa.types.is_large_string(field.type)
858864
or pa.types.is_string_view(field.type)
859-
):
865+
) and field.name not in categories:
860866
ext_columns[field.name] = _pandas_api.pd.StringDtype(na_value=np.nan)
861867

862868
# infer the extension columns from the pandas metadata
@@ -872,7 +878,7 @@ def _get_extension_dtypes(table, columns_metadata, types_mapper=None, options=No
872878
# that are certainly numpy dtypes
873879
pandas_dtype = _pandas_api.pandas_dtype(dtype)
874880
if isinstance(pandas_dtype, _pandas_api.extension_dtype):
875-
if strings_to_categorical and isinstance(
881+
if (strings_to_categorical or name in categories) and isinstance(
876882
pandas_dtype, _pandas_api.pd.StringDtype
877883
):
878884
continue

0 commit comments

Comments
 (0)