MCPcopy Create free account
hub / github.com/apache/arrow / _get_columns_to_convert_given_schema

Function _get_columns_to_convert_given_schema

python/pyarrow/pandas_compat.py:450–509  ·  view source on GitHub ↗

Specialized version of _get_columns_to_convert in case a Schema is specified. In that case, the Schema is used as the single point of truth for the table structure (types, which columns are included, order of columns, ...).

(df, schema, preserve_index)

Source from the content-addressed store, hash-verified

448
449
450def _get_columns_to_convert_given_schema(df, schema, preserve_index):
451 """
452 Specialized version of _get_columns_to_convert in case a Schema is
453 specified.
454 In that case, the Schema is used as the single point of truth for the
455 table structure (types, which columns are included, order of columns, ...).
456 """
457 column_names = []
458 columns_to_convert = []
459 convert_fields = []
460 index_descriptors = []
461 index_column_names = []
462 index_levels = []
463
464 for name in schema.names:
465 try:
466 col = df[name]
467 is_index = False
468 except KeyError:
469 try:
470 col = _get_index_level(df, name)
471 except (KeyError, IndexError):
472 # name not found as index level
473 raise KeyError(
474 f"name '{name}' present in the specified schema is not found "
475 "in the columns or index")
476 if preserve_index is False:
477 raise ValueError(
478 f"name '{name}' present in the specified schema corresponds "
479 "to the index, but 'preserve_index=False' was "
480 "specified")
481 elif (preserve_index is None and
482 isinstance(col, _pandas_api.pd.RangeIndex)):
483 raise ValueError(
484 f"name '{name}' is present in the schema, but it is a "
485 "RangeIndex which will not be converted as a column "
486 "in the Table, but saved as metadata-only not in "
487 "columns. Specify 'preserve_index=True' to force it "
488 "being added as a column, or remove it from the "
489 "specified schema")
490 is_index = True
491
492 if _pandas_api.is_sparse(col):
493 raise TypeError(
494 f"Sparse pandas data (column {name}) not supported.")
495
496 field = schema.field(name)
497 columns_to_convert.append(col)
498 convert_fields.append(field)
499 column_names.append(name)
500
501 if is_index:
502 index_column_names.append(name)
503 index_descriptors.append(name)
504 index_levels.append(col)
505
506 all_names = column_names + index_column_names
507

Callers 1

_get_columns_to_convertFunction · 0.85

Calls 5

_get_index_levelFunction · 0.85
KeyErrorFunction · 0.85
TypeErrorFunction · 0.50
fieldMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected