Skip to content
Snippets Groups Projects
Commit 35e64262 authored by Rob Moss's avatar Rob Moss
Browse files

Remove pypfilt.summary.dtype_names_to_str

All dtype field names are already Unicode strings, and we no longer need
to check for byte strings and convert them to Unicode.
parent 85efc567
Branches
Tags
No related merge requests found
......@@ -63,8 +63,6 @@ The following column types are provided for convenience when defining custom
.. autofunction:: pypfilt.summary.dtype_value
.. autofunction:: pypfilt.summary.dtype_names_to_str
The following functions are provided for converting column types in structured
arrays.
......
......@@ -31,37 +31,6 @@ def dtype_value(value, name='value'):
return (name, value_dtype)
def dtype_names_to_str(dtypes, encoding='utf-8'):
"""
Ensure that dtype field names are native strings, as
`required <https://github.com/numpy/numpy/issues/2407>`__ by NumPy.
:param dtypes: A list of fields where each field is either a string, or a
tuple of length 2 or 3 (see the NumPy
`docs <http://docs.scipy.org/doc/numpy-1.8.0/reference/arrays.dtypes.html#index-9>`__
for details).
:param encoding: The encoding for converting Unicode strings to native
strings.
:return: A list of fields, where each field name is a native string
(``str`` type).
:raises ValueError: If a name cannot be converted to a native string.
"""
def str_name(value):
if isinstance(value, str):
return value
elif isinstance(value, bytes):
return value.decode(encoding)
else:
raise ValueError("Invalid column name '{!r}'".format(value))
# Inspect the first element to see if we have a list of strings or tuples.
if isinstance(dtypes[0], tuple):
return [(str_name(dtype[0]),) + dtype[1:] for dtype in dtypes]
else:
return [str_name(dtype) for dtype in dtypes]
def obs_types(params, obs_list, obs_reqd=False):
"""
Return a sorted list of ``(unit, period)`` tuples that define the unique
......@@ -692,8 +661,8 @@ class HDF5(object):
self.__tbl_dict[name] = table
# NOTE: provide the table name here so that the table can look for
# table-specific parameters.
self.__dtypes[name] = dtype_names_to_str(
table.dtype(self.__ctx, self.__all_obs, name))
self.__dtypes[name] = table.dtype(self.__ctx, self.__all_obs,
name)
def load_state(self, grp):
"""
......@@ -1328,8 +1297,7 @@ def obs_table(ctx, all_obs):
value = dtype_value(obs_val)
incomplete = ('incomplete', np.bool)
upper = dtype_value(obs_val, name='upper_bound')
dtype = dtype_names_to_str([unit, period, source, date, value, incomplete,
upper])
dtype = [unit, period, source, date, value, incomplete, upper]
obs_df = np.empty(len(all_obs), dtype=dtype)
for ix, o in enumerate(all_obs):
date_enc = ctx.component['time'].to_dtype(o['date'])
......@@ -1351,8 +1319,8 @@ def convert_cols(data, converters):
:returns: A new structured array.
"""
dt = data.dtype
col_names = dtype_names_to_str(dt.names)
conv_names = dtype_names_to_str([n for n in converters.keys()])
col_names = dt.names
conv_names = [n for n in converters.keys()]
conv_fns = {}
new_dtype = []
for col in col_names:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment