diff options
Diffstat (limited to 'numpy/lib/_iotools.py')
-rw-r--r-- | numpy/lib/_iotools.py | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/numpy/lib/_iotools.py b/numpy/lib/_iotools.py index 27c1e76db..7921b4116 100644 --- a/numpy/lib/_iotools.py +++ b/numpy/lib/_iotools.py @@ -503,10 +503,25 @@ class StringConverter(object): (_defaulttype, _defaultfunc, _defaultfill) = zip(*_mapper) # @classmethod + def _getdtype(cls, val): + """Returns the dtype of the input variable.""" + return np.array(val).dtype + # + @classmethod def _getsubdtype(cls, val): """Returns the type of the dtype of the input variable.""" return np.array(val).dtype.type # + # This is a bit annoying. We want to return the "general" type in most cases + # (ie. "string" rather than "S10"), but we want to return the specific type + # for datetime64 (ie. "datetime64[us]" rather than "datetime64"). + @classmethod + def _dtypeortype(cls, dtype): + """Returns dtype for datetime64 and type of dtype otherwise.""" + if dtype.type == np.datetime64: + return dtype + return dtype.type + # @classmethod def upgrade_mapper(cls, func, default=None): """ @@ -561,12 +576,12 @@ class StringConverter(object): self.func = str2bool self._status = 0 self.default = default or False - ttype = np.bool + dtype = np.dtype('bool') else: # Is the input a np.dtype ? try: self.func = None - ttype = np.dtype(dtype_or_func).type + dtype = np.dtype(dtype_or_func) except TypeError: # dtype_or_func must be a function, then if not hasattr(dtype_or_func, '__call__'): @@ -581,11 +596,11 @@ class StringConverter(object): default = self.func(asbytes('0')) except ValueError: default = None - ttype = self._getsubdtype(default) + dtype = self._getdtype(default) # Set the status according to the dtype _status = -1 for (i, (deftype, func, default_def)) in enumerate(self._mapper): - if np.issubdtype(ttype, deftype): + if np.issubdtype(dtype.type, deftype): _status = i if default is None: self.default = default_def @@ -603,9 +618,9 @@ class StringConverter(object): # If the status is 1 (int), change the function to # something more robust. if self.func == self._mapper[1][1]: - if issubclass(ttype, np.uint64): + if issubclass(dtype.type, np.uint64): self.func = np.uint64 - elif issubclass(ttype, np.int64): + elif issubclass(dtype.type, np.int64): self.func = np.int64 else: self.func = lambda x : int(float(x)) @@ -618,7 +633,7 @@ class StringConverter(object): self.missing_values = set(list(missing_values) + [asbytes('')]) # self._callingfunction = self._strict_call - self.type = ttype + self.type = self._dtypeortype(dtype) self._checked = False self._initial_default = default # @@ -747,13 +762,13 @@ class StringConverter(object): # Don't reset the default to None if we can avoid it if default is not None: self.default = default - self.type = self._getsubdtype(default) + self.type = self._dtypeortype(self._getdtype(default)) else: try: tester = func(testing_value or asbytes('1')) except (TypeError, ValueError): tester = None - self.type = self._getsubdtype(tester) + self.type = self._dtypeortype(self._getdtype(tester)) # Add the missing values to the existing set if missing_values is not None: if _is_bytes_like(missing_values): |