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:455–514  ·  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

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

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