tables_io.utils.array_utils

Array-related utility functions for tables_io

Functions

array_length(→ int)

Get the length of an array

force_to_pandables(arr[, check_nrow])

Forces a numpy.array into a format that pandas can handle

get_group_input_data_length(→ int)

Return the length of a HDF5 group

get_initialization_for_ODict(→ Mapping)

shape of arrays and dtypes in a dictionary.

print_dict_shape(in_dict)

Print the shape of arrays in a dictionary.

slice_dict(→ Mapping)

Create a new dict by taking a slice of of every array in a dict

check_keys(in_dicts)

Check that the keys in all the in_dicts match

concatenate_dicts(→ Mapping)

Create a new dict by concatenating each array in in_dicts

Module Contents

array_length(arr: numpy.typing.ArrayLike) int[source]

Get the length of an array

The works on scalars and arrays, so it is safe to use

For scalars it returns 0 For arrays it np.shape(arr)[0]

Parameters:

arr (array-like) – The input array

Returns:

length – The object length

Return type:

int

force_to_pandables(arr: numpy.typing.ArrayLike, check_nrow: int | None = None)[source]

Forces a numpy.array into a format that pandas can handle

Parameters:
  • arr (numpy.array) – The input array

  • check_nrow (int or None) – If not None, require that arr.shape[0] match this value

Returns:

out – Something that pandas can handle

Return type:

numpy.array or list of numpy.array

get_group_input_data_length(hg) int[source]

Return the length of a HDF5 group

Parameters:

hg (h5py.Group or h5py.File) – The input data group

Returns:

length – The length of the data

Return type:

int

Notes

For a multi-D array this return the length of the first axis and not the total size of the array.

Normally that is what you want to be iterating over.

The group is meant to represent a table, hence all child datasets should be the same length

get_initialization_for_ODict(in_dict: Mapping, nRow_out: int | None = None) Mapping[source]

shape of arrays and dtypes in a dictionary. This is useful for initialize hdf5 files

Parameters:
  • in_dict (dict) – The dictionary with the data

  • nRow_out (int) – The number of rows in the output

Returns:

out_dict

Return type:

dict (str, tuple(tuple, dtype))

print_dict_shape(in_dict: Mapping)[source]

Print the shape of arrays in a dictionary. This is useful for debugging astropy.Table creation.

Parameters:

in_dict (dict) – The dictionary to print

slice_dict(in_dict: Mapping, subslice: int | slice) Mapping[source]

Create a new dict by taking a slice of of every array in a dict

Parameters:
  • in_dict (dict) – The dictionary to extract from

  • subslice (int or slice) – Used to slice the arrays

Returns:

out_dict – The converted dictionary

Return type:

dict

check_keys(in_dicts: List[Mapping])[source]

Check that the keys in all the in_dicts match

Parameters:

in_dicts (list, (OrderedDict, (str, numpy.array))) – The dictionaries for which compare keys

Raises:

KeyError – if keys do not match

concatenate_dicts(in_dicts: List[Mapping]) Mapping[source]

Create a new dict by concatenating each array in in_dicts

Parameters:

in_dicts (list, (OrderedDict, (str, numpy.array))) – The dictionaries to stack

Returns:

out_dict – The stacked dictionary

Return type:

dict